UNPKG

antd

Version:

An enterprise-class UI design language and React components implementation

147 lines (146 loc) 6.64 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = ItemHolder; var _CheckCircleFilled = _interopRequireDefault(require("@ant-design/icons/CheckCircleFilled")); var _CloseCircleFilled = _interopRequireDefault(require("@ant-design/icons/CloseCircleFilled")); var _ExclamationCircleFilled = _interopRequireDefault(require("@ant-design/icons/ExclamationCircleFilled")); var _LoadingOutlined = _interopRequireDefault(require("@ant-design/icons/LoadingOutlined")); var _useLayoutEffect = _interopRequireDefault(require("rc-util/lib/hooks/useLayoutEffect")); var _classnames = _interopRequireDefault(require("classnames")); var React = _interopRequireWildcard(require("react")); var _omit = _interopRequireDefault(require("rc-util/lib/omit")); var _grid = require("../../grid"); var _FormItemLabel = _interopRequireDefault(require("../FormItemLabel")); var _FormItemInput = _interopRequireDefault(require("../FormItemInput")); var _context = require("../context"); var _useDebounce = _interopRequireDefault(require("../hooks/useDebounce")); var __rest = void 0 && (void 0).__rest || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; const iconMap = { success: _CheckCircleFilled.default, warning: _ExclamationCircleFilled.default, error: _CloseCircleFilled.default, validating: _LoadingOutlined.default }; function ItemHolder(props) { const { prefixCls, className, style, help, errors, warnings, validateStatus, meta, hasFeedback, hidden, children, fieldId, isRequired, onSubItemMetaChange } = props, restProps = __rest(props, ["prefixCls", "className", "style", "help", "errors", "warnings", "validateStatus", "meta", "hasFeedback", "hidden", "children", "fieldId", "isRequired", "onSubItemMetaChange"]); const itemPrefixCls = `${prefixCls}-item`; const { requiredMark } = React.useContext(_context.FormContext); // ======================== Margin ======================== const itemRef = React.useRef(null); const debounceErrors = (0, _useDebounce.default)(errors); const debounceWarnings = (0, _useDebounce.default)(warnings); const hasHelp = help !== undefined && help !== null; const hasError = !!(hasHelp || errors.length || warnings.length); const [marginBottom, setMarginBottom] = React.useState(null); (0, _useLayoutEffect.default)(() => { if (hasError && itemRef.current) { const itemStyle = getComputedStyle(itemRef.current); setMarginBottom(parseInt(itemStyle.marginBottom, 10)); } }, [hasError]); const onErrorVisibleChanged = nextVisible => { if (!nextVisible) { setMarginBottom(null); } }; // ======================== Status ======================== let mergedValidateStatus = ''; if (validateStatus !== undefined) { mergedValidateStatus = validateStatus; } else if (meta.validating) { mergedValidateStatus = 'validating'; } else if (debounceErrors.length) { mergedValidateStatus = 'error'; } else if (debounceWarnings.length) { mergedValidateStatus = 'warning'; } else if (meta.touched) { mergedValidateStatus = 'success'; } const formItemStatusContext = React.useMemo(() => { let feedbackIcon; if (hasFeedback) { const IconNode = mergedValidateStatus && iconMap[mergedValidateStatus]; feedbackIcon = IconNode ? /*#__PURE__*/React.createElement("span", { className: (0, _classnames.default)(`${itemPrefixCls}-feedback-icon`, `${itemPrefixCls}-feedback-icon-${mergedValidateStatus}`) }, /*#__PURE__*/React.createElement(IconNode, null)) : null; } return { status: mergedValidateStatus, hasFeedback, feedbackIcon, isFormItemInput: true }; }, [mergedValidateStatus, hasFeedback]); // ======================== Render ======================== const itemClassName = { [itemPrefixCls]: true, [`${itemPrefixCls}-with-help`]: hasHelp || debounceErrors.length || debounceWarnings.length, [`${className}`]: !!className, // Status [`${itemPrefixCls}-has-feedback`]: mergedValidateStatus && hasFeedback, [`${itemPrefixCls}-has-success`]: mergedValidateStatus === 'success', [`${itemPrefixCls}-has-warning`]: mergedValidateStatus === 'warning', [`${itemPrefixCls}-has-error`]: mergedValidateStatus === 'error', [`${itemPrefixCls}-is-validating`]: mergedValidateStatus === 'validating', [`${itemPrefixCls}-hidden`]: hidden }; return /*#__PURE__*/React.createElement("div", { className: (0, _classnames.default)(itemClassName), style: style, ref: itemRef }, /*#__PURE__*/React.createElement(_grid.Row, Object.assign({ className: `${itemPrefixCls}-row` }, (0, _omit.default)(restProps, ['_internalItemRender', 'colon', 'dependencies', 'extra', 'fieldKey', 'getValueFromEvent', 'getValueProps', 'htmlFor', 'id', 'initialValue', 'isListField', 'label', 'labelAlign', 'labelCol', 'labelWrap', 'messageVariables', 'name', 'normalize', 'noStyle', 'preserve', 'required', 'requiredMark', 'rules', 'shouldUpdate', 'trigger', 'tooltip', 'validateFirst', 'validateTrigger', 'valuePropName', 'wrapperCol'])), /*#__PURE__*/React.createElement(_FormItemLabel.default, Object.assign({ htmlFor: fieldId, required: isRequired, requiredMark: requiredMark }, props, { prefixCls: prefixCls })), /*#__PURE__*/React.createElement(_FormItemInput.default, Object.assign({}, props, meta, { errors: debounceErrors, warnings: debounceWarnings, prefixCls: prefixCls, status: mergedValidateStatus, help: help, marginBottom: marginBottom, onErrorVisibleChanged: onErrorVisibleChanged }), /*#__PURE__*/React.createElement(_context.NoStyleItemContext.Provider, { value: onSubItemMetaChange }, /*#__PURE__*/React.createElement(_context.FormItemInputContext.Provider, { value: formItemStatusContext }, children)))), !!marginBottom && /*#__PURE__*/React.createElement("div", { className: `${itemPrefixCls}-margin-offset`, style: { marginBottom: -marginBottom } })); }