antd
Version:
An enterprise-class UI design language and React components implementation
50 lines (44 loc) • 2.15 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import * as React from 'react';
import classNames from 'classnames';
import Col from '../grid/col';
import { FormContext } from './context';
var FormItemLabel = function FormItemLabel(_ref) {
var prefixCls = _ref.prefixCls,
label = _ref.label,
htmlFor = _ref.htmlFor,
labelCol = _ref.labelCol,
labelAlign = _ref.labelAlign,
colon = _ref.colon,
required = _ref.required;
if (!label) return null;
return /*#__PURE__*/React.createElement(FormContext.Consumer, {
key: "label"
}, function (_ref2) {
var _classNames;
var vertical = _ref2.vertical,
contextLabelAlign = _ref2.labelAlign,
contextLabelCol = _ref2.labelCol,
contextColon = _ref2.colon;
var mergedLabelCol = labelCol || contextLabelCol || {};
var mergedLabelAlign = labelAlign || contextLabelAlign;
var labelClsBasic = "".concat(prefixCls, "-item-label");
var labelColClassName = classNames(labelClsBasic, mergedLabelAlign === 'left' && "".concat(labelClsBasic, "-left"), mergedLabelCol.className);
var labelChildren = label; // Keep label is original where there should have no colon
var computedColon = colon === true || contextColon !== false && colon !== false;
var haveColon = computedColon && !vertical; // Remove duplicated user input colon
if (haveColon && typeof label === 'string' && label.trim() !== '') {
labelChildren = label.replace(/[:|:]\s*$/, '');
}
var labelClassName = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-item-required"), required), _defineProperty(_classNames, "".concat(prefixCls, "-item-no-colon"), !computedColon), _classNames));
return /*#__PURE__*/React.createElement(Col, _extends({}, mergedLabelCol, {
className: labelColClassName
}), /*#__PURE__*/React.createElement("label", {
htmlFor: htmlFor,
className: labelClassName,
title: typeof label === 'string' ? label : ''
}, labelChildren));
});
};
export default FormItemLabel;