[英]How to add label to custom field component in react-admin
So I have created a custom field for react admin which looks something like this:所以我为 React admin 创建了一个自定义字段,看起来像这样:
import * as React from "react";
import { useRecordContext } from "react-admin";
export const IsOnline = (props:{view:string}) => {
const record = useRecordContext();
if (record.is_online)
return <span className={props.view}>{("Online")}</span>;
return <span className={props.view}>{("Offline")}</span>;
};
export default IsOnline;
And using it like this:并像这样使用它:
import IsOnline from "./IsOnline.field";
<Datagrid hover={false} rowClick="edit">
(...)
<IsOnline view="list"/> // <-- Here I would normally add the "label" prop
</Datagrid>
(...)
The react-admin documentation claims that react-admin 文档声称
Tip: Note that the label property can be used on any field to customize the field label.
提示:请注意,可以在任何字段上使用 label 属性来自定义字段 label。
What am I missing then?那我错过了什么?
You need to use the <Labeled>
component as shown here:您需要使用
<Labeled>
组件,如下所示:
https://marmelab.com/react-admin/Fields.html#setting-a-field-label https://marmelab.com/react-admin/Fields.html#setting-a-field-label
https://marmelab.com/react-admin/Labeled.html https://marmelab.com/react-admin/Labeled.html
Ok so it turned out to be quite simple by adding the "label" property on the custom field:好的,事实证明通过在自定义字段上添加“标签”属性非常简单:
import * as React from "react";
import { useRecordContext } from "react-admin";
export const IsOnline = (props:{view:string, label:string}) => {
const record = useRecordContext();
if (record.is_online)
return <span className={props.view}>{("Online")}</span>;
return <span className={props.view}>{("Offline")}</span>;
};
export default IsOnline;
And in the datagrid:在数据网格中:
<IsOnline view="list" label="Status"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.