-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
index.tsx
64 lines (55 loc) · 1.46 KB
/
index.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React from "react";
import classNames from "classnames";
import { TitleFieldProps } from "@rjsf/utils";
import { withConfigConsumer } from "antd/lib/config-provider/context";
// Add in the `prefixCls` element needed by the `withConfigConsumer` HOC
export type AntdTitleFieldProps = TitleFieldProps & {
prefixCls: string;
formContext: object;
};
const TitleField = ({
id,
prefixCls,
required,
registry,
formContext: formContext1,
title,
}: AntdTitleFieldProps) => {
const { formContext } = registry;
const { colon = true } = { ...formContext1, ...formContext };
let labelChildren = title;
if (colon && typeof title === "string" && title.trim() !== "") {
labelChildren = title.replace(/[::]\s*$/, "");
}
const labelClassName = classNames({
[`${prefixCls}-item-required`]: required,
[`${prefixCls}-item-no-colon`]: !colon,
});
const handleLabelClick = () => {
if (!id) {
return;
}
const control: HTMLLabelElement | null = document.querySelector(
`[id="${id}"]`
);
if (control && control.focus) {
control.focus();
}
};
return title ? (
<label
className={labelClassName}
htmlFor={id}
onClick={handleLabelClick}
title={typeof title === "string" ? title : ""}
>
{labelChildren}
</label>
) : null;
};
TitleField.defaultProps = {
formContext: {},
};
export default withConfigConsumer<AntdTitleFieldProps>({ prefixCls: "form" })(
TitleField
);