antd
Version:
An enterprise-class UI design language and React components implementation
52 lines (44 loc) • 2.56 kB
JavaScript
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
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;