1 | "use client";
|
2 |
|
3 | var __rest = this && this.__rest || function (s, e) {
|
4 | var t = {};
|
5 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
|
6 | if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
7 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | import QuestionCircleOutlined from "@ant-design/icons/es/icons/QuestionCircleOutlined";
|
12 | import classNames from 'classnames';
|
13 | import * as React from 'react';
|
14 | import Col from '../grid/col';
|
15 | import defaultLocale from '../locale/en_US';
|
16 | import { useLocale } from '../locale';
|
17 | import Tooltip from '../tooltip';
|
18 | import { FormContext } from './context';
|
19 | function toTooltipProps(tooltip) {
|
20 | if (!tooltip) {
|
21 | return null;
|
22 | }
|
23 | if (typeof tooltip === 'object' && ! React.isValidElement(tooltip)) {
|
24 | return tooltip;
|
25 | }
|
26 | return {
|
27 | title: tooltip
|
28 | };
|
29 | }
|
30 | const FormItemLabel = _ref => {
|
31 | let {
|
32 | prefixCls,
|
33 | label,
|
34 | htmlFor,
|
35 | labelCol,
|
36 | labelAlign,
|
37 | colon,
|
38 | required,
|
39 | requiredMark,
|
40 | tooltip
|
41 | } = _ref;
|
42 | var _a;
|
43 | const [formLocale] = useLocale('Form');
|
44 | const {
|
45 | vertical,
|
46 | labelAlign: contextLabelAlign,
|
47 | labelCol: contextLabelCol,
|
48 | labelWrap,
|
49 | colon: contextColon
|
50 | } = React.useContext(FormContext);
|
51 | if (!label) {
|
52 | return null;
|
53 | }
|
54 | const mergedLabelCol = labelCol || contextLabelCol || {};
|
55 | const mergedLabelAlign = labelAlign || contextLabelAlign;
|
56 | const labelClsBasic = `${prefixCls}-item-label`;
|
57 | const labelColClassName = classNames(labelClsBasic, mergedLabelAlign === 'left' && `${labelClsBasic}-left`, mergedLabelCol.className, {
|
58 | [`${labelClsBasic}-wrap`]: !!labelWrap
|
59 | });
|
60 | let labelChildren = label;
|
61 |
|
62 | const computedColon = colon === true || contextColon !== false && colon !== false;
|
63 | const haveColon = computedColon && !vertical;
|
64 |
|
65 | if (haveColon && typeof label === 'string' && label.trim() !== '') {
|
66 | labelChildren = label.replace(/[:|:]\s*$/, '');
|
67 | }
|
68 |
|
69 | const tooltipProps = toTooltipProps(tooltip);
|
70 | if (tooltipProps) {
|
71 | const {
|
72 | icon = React.createElement(QuestionCircleOutlined, null)
|
73 | } = tooltipProps,
|
74 | restTooltipProps = __rest(tooltipProps, ["icon"]);
|
75 | const tooltipNode = React.createElement(Tooltip, Object.assign({}, restTooltipProps), React.cloneElement(icon, {
|
76 | className: `${prefixCls}-item-tooltip`,
|
77 | title: '',
|
78 | onClick: e => {
|
79 |
|
80 |
|
81 | e.preventDefault();
|
82 | },
|
83 | tabIndex: null
|
84 | }));
|
85 | labelChildren = React.createElement(React.Fragment, null, labelChildren, tooltipNode);
|
86 | }
|
87 |
|
88 | const isOptionalMark = requiredMark === 'optional';
|
89 | const isRenderMark = typeof requiredMark === 'function';
|
90 | if (isRenderMark) {
|
91 | labelChildren = requiredMark(labelChildren, {
|
92 | required: !!required
|
93 | });
|
94 | } else if (isOptionalMark && !required) {
|
95 | labelChildren = React.createElement(React.Fragment, null, labelChildren, React.createElement("span", {
|
96 | className: `${prefixCls}-item-optional`,
|
97 | title: ""
|
98 | }, (formLocale === null || formLocale === void 0 ? void 0 : formLocale.optional) || ((_a = defaultLocale.Form) === null || _a === void 0 ? void 0 : _a.optional)));
|
99 | }
|
100 | const labelClassName = classNames({
|
101 | [`${prefixCls}-item-required`]: required,
|
102 | [`${prefixCls}-item-required-mark-optional`]: isOptionalMark || isRenderMark,
|
103 | [`${prefixCls}-item-no-colon`]: !computedColon
|
104 | });
|
105 | return React.createElement(Col, Object.assign({}, mergedLabelCol, {
|
106 | className: labelColClassName
|
107 | }), React.createElement("label", {
|
108 | htmlFor: htmlFor,
|
109 | className: labelClassName,
|
110 | title: typeof label === 'string' ? label : ''
|
111 | }, labelChildren));
|
112 | };
|
113 | export default FormItemLabel; |
\ | No newline at end of file |