使用react-admin,我有一个这样的EditView:
export const LanguageEdit = props => (
<Edit title="Edit: Language" {...props}>
<SimpleForm>
<TextInput source="name" label="Name" />
<TextInput source="iso1" label="ISO-1" />
<TextInput source="iso2" label="ISO-2" />
</SimpleForm>
</Edit>
);我的应用程序将有几个编辑视图,每个视图在<SimpleForm>元素中都有不同的内容。然而,在不同的观点中,标题只会略有不同。
<Edit title="Edit: Language" {...props}>
<Edit title="Edit: City" {...props}>
<Edit title="Edit: Country" {...props}>
是否有方法将其定义为“模板”,然后在所有编辑视图中使用该模板?
模板
<Edit title="Edit: ${currentViewName}" {...props}>
<SimpleForm>
${somePlaceholder}
</SimpleForm>
</Edit>视图内容(伪代码)
currentViewName = "Country";
somePlaceholder => (
<TextInput source="name" label="Name" />
<TextInput source="iso1" label="ISO-1" />
<TextInput source="iso2" label="ISO-2" />
);
applyTemplate(currentViewName, somePlaceholder);发布于 2019-01-31 16:50:42
您可以将Edit组件包装如下:
const EditTemplate = ({ title, children, ...props }) => (
<Edit title={`Edit: ${title}`} {...props}>
<SimpleForm>
{children}
</SimpleForm>
</Edit>
)并将其用作普通编辑视图:
export const LanguageEdit = props => (
<EditTemplate title="Language" {...props}>
<TextInput source="name" label="Name" />
<TextInput source="iso1" label="ISO-1" />
<TextInput source="iso2" label="ISO-2" />
</EditTemplate>
);https://stackoverflow.com/questions/54462180
复制相似问题