UNPKG

4.17 kBJavaScriptView Raw
1"use client";
2
3var __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};
11import QuestionCircleOutlined from "@ant-design/icons/es/icons/QuestionCircleOutlined";
12import classNames from 'classnames';
13import * as React from 'react';
14import Col from '../grid/col';
15import defaultLocale from '../locale/en_US';
16import { useLocale } from '../locale';
17import Tooltip from '../tooltip';
18import { FormContext } from './context';
19function toTooltipProps(tooltip) {
20 if (!tooltip) {
21 return null;
22 }
23 if (typeof tooltip === 'object' && ! /*#__PURE__*/React.isValidElement(tooltip)) {
24 return tooltip;
25 }
26 return {
27 title: tooltip
28 };
29}
30const 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 // Keep label is original where there should have no colon
62 const computedColon = colon === true || contextColon !== false && colon !== false;
63 const haveColon = computedColon && !vertical;
64 // Remove duplicated user input colon
65 if (haveColon && typeof label === 'string' && label.trim() !== '') {
66 labelChildren = label.replace(/[:|:]\s*$/, '');
67 }
68 // Tooltip
69 const tooltipProps = toTooltipProps(tooltip);
70 if (tooltipProps) {
71 const {
72 icon = /*#__PURE__*/React.createElement(QuestionCircleOutlined, null)
73 } = tooltipProps,
74 restTooltipProps = __rest(tooltipProps, ["icon"]);
75 const tooltipNode = /*#__PURE__*/React.createElement(Tooltip, Object.assign({}, restTooltipProps), /*#__PURE__*/React.cloneElement(icon, {
76 className: `${prefixCls}-item-tooltip`,
77 title: '',
78 onClick: e => {
79 // Prevent label behavior in tooltip icon
80 // https://github.com/ant-design/ant-design/issues/46154
81 e.preventDefault();
82 },
83 tabIndex: null
84 }));
85 labelChildren = /*#__PURE__*/React.createElement(React.Fragment, null, labelChildren, tooltipNode);
86 }
87 // Required Mark
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 = /*#__PURE__*/React.createElement(React.Fragment, null, labelChildren, /*#__PURE__*/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 /*#__PURE__*/React.createElement(Col, Object.assign({}, mergedLabelCol, {
106 className: labelColClassName
107 }), /*#__PURE__*/React.createElement("label", {
108 htmlFor: htmlFor,
109 className: labelClassName,
110 title: typeof label === 'string' ? label : ''
111 }, labelChildren));
112};
113export default FormItemLabel;
\No newline at end of file