-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathFieldTitle.tsx
46 lines (37 loc) · 1.09 KB
/
FieldTitle.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import * as React from 'react';
import { ReactElement, memo } from 'react';
import { useTranslateLabel } from '../i18n';
export interface FieldTitleProps {
isRequired?: boolean;
resource?: string;
source?: string;
label?: string | ReactElement | boolean;
}
export const FieldTitle = (props: FieldTitleProps) => {
const { source, label, resource, isRequired } = props;
const translateLabel = useTranslateLabel();
if (label === true) {
throw new Error(
'Label parameter must be a string, a ReactElement or false'
);
}
if (label === false || label === '') {
return null;
}
if (label && typeof label !== 'string') {
return label;
}
return (
<span>
{translateLabel({
label,
resource,
source,
})}
{isRequired && <span aria-hidden="true"> *</span>}
</span>
);
};
// What? TypeScript loses the displayName if we don't set it explicitly
FieldTitle.displayName = 'FieldTitle';
export default memo(FieldTitle);