UNPKG

linkmore-design

Version:

🌈 🚀lm组件库。🚀

205 lines (200 loc) 10.5 kB
import _typeof from "@babel/runtime/helpers/esm/typeof"; import _extends from "@babel/runtime/helpers/esm/extends"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; var _excluded = ["prefixCls", "bordered", "showCount", "maxLength", "className", "style", "size", "disabled", "onCompositionStart", "onCompositionEnd", "onChange", "status"]; import classNames from 'classnames'; import RcTextArea from 'rc-textarea'; import useMergedState from 'rc-util/lib/hooks/useMergedState'; import omit from 'rc-util/lib/omit'; import * as React from 'react'; import { ConfigContext } from "../config-provider"; import DisabledContext from "../config-provider/DisabledContext"; import SizeContext from "../config-provider/SizeContext"; import { FormItemInputContext } from "../form/context"; import { getMergedStatus, getStatusClassNames } from "../_util/statusUtils"; import ClearableLabeledInput from "./ClearableLabeledInput"; import { fixControlledValue, resolveOnChange, triggerFocus } from "./Input"; function fixEmojiLength(value, maxLength) { return _toConsumableArray(value || '').slice(0, maxLength).join(''); } function setTriggerValue(isCursorInEnd, preValue, triggerValue, maxLength) { var newTriggerValue = triggerValue; if (isCursorInEnd) { // 光标在尾部,直接截断 newTriggerValue = fixEmojiLength(triggerValue, maxLength); } else if (_toConsumableArray(preValue || '').length < triggerValue.length && _toConsumableArray(triggerValue || '').length > maxLength) { // 光标在中间,如果最后的值超过最大值,则采用原先的值 newTriggerValue = preValue; } return newTriggerValue; } var TextArea = /*#__PURE__*/React.forwardRef(function (_ref, ref) { var _classNames; var customizePrefixCls = _ref.prefixCls, _ref$bordered = _ref.bordered, bordered = _ref$bordered === void 0 ? true : _ref$bordered, _ref$showCount = _ref.showCount, showCount = _ref$showCount === void 0 ? false : _ref$showCount, maxLength = _ref.maxLength, className = _ref.className, style = _ref.style, customizeSize = _ref.size, customDisabled = _ref.disabled, onCompositionStart = _ref.onCompositionStart, onCompositionEnd = _ref.onCompositionEnd, onChange = _ref.onChange, customStatus = _ref.status, props = _objectWithoutProperties(_ref, _excluded); var _React$useContext = React.useContext(ConfigContext), getPrefixCls = _React$useContext.getPrefixCls, direction = _React$useContext.direction; var size = React.useContext(SizeContext); // ===================== Disabled ===================== var disabled = React.useContext(DisabledContext); var mergedDisabled = customDisabled !== null && customDisabled !== void 0 ? customDisabled : disabled; var _React$useContext2 = React.useContext(FormItemInputContext), contextStatus = _React$useContext2.status, hasFeedback = _React$useContext2.hasFeedback, isFormItemInput = _React$useContext2.isFormItemInput, feedbackIcon = _React$useContext2.feedbackIcon; var mergedStatus = getMergedStatus(contextStatus, customStatus); var innerRef = React.useRef(null); var clearableInputRef = React.useRef(null); var _React$useState = React.useState(false), _React$useState2 = _slicedToArray(_React$useState, 2), compositing = _React$useState2[0], setCompositing = _React$useState2[1]; var oldCompositionValueRef = React.useRef(); var oldSelectionStartRef = React.useRef(0); var _useMergedState = useMergedState(props.defaultValue, { value: props.value }), _useMergedState2 = _slicedToArray(_useMergedState, 2), value = _useMergedState2[0], setValue = _useMergedState2[1]; var hidden = props.hidden; var handleSetValue = function handleSetValue(val, callback) { if (props.value === undefined) { setValue(val); callback === null || callback === void 0 ? void 0 : callback(); } }; // =========================== Value Update =========================== // Max length value var hasMaxLength = Number(maxLength) > 0; var onInternalCompositionStart = function onInternalCompositionStart(e) { setCompositing(true); // 拼音输入前保存一份旧值 oldCompositionValueRef.current = value; // 保存旧的光标位置 oldSelectionStartRef.current = e.currentTarget.selectionStart; onCompositionStart === null || onCompositionStart === void 0 ? void 0 : onCompositionStart(e); }; var onInternalCompositionEnd = function onInternalCompositionEnd(e) { setCompositing(false); var triggerValue = e.currentTarget.value; if (hasMaxLength) { var _oldCompositionValueR; var isCursorInEnd = oldSelectionStartRef.current >= maxLength + 1 || oldSelectionStartRef.current === ((_oldCompositionValueR = oldCompositionValueRef.current) === null || _oldCompositionValueR === void 0 ? void 0 : _oldCompositionValueR.length); triggerValue = setTriggerValue(isCursorInEnd, oldCompositionValueRef.current, triggerValue, maxLength); } // Patch composition onChange when value changed if (triggerValue !== value) { handleSetValue(triggerValue); resolveOnChange(e.currentTarget, e, onChange, triggerValue); } onCompositionEnd === null || onCompositionEnd === void 0 ? void 0 : onCompositionEnd(e); }; var handleChange = function handleChange(e) { var triggerValue = e.target.value; if (!compositing && hasMaxLength) { // 1. 复制粘贴超过maxlength的情况 2.未超过maxlength的情况 var isCursorInEnd = e.target.selectionStart >= maxLength + 1 || e.target.selectionStart === triggerValue.length || !e.target.selectionStart; triggerValue = setTriggerValue(isCursorInEnd, value, triggerValue, maxLength); } handleSetValue(triggerValue); resolveOnChange(e.currentTarget, e, onChange, triggerValue); }; // ============================== Reset =============================== var handleReset = function handleReset(e) { var _innerRef$current, _innerRef$current2, _innerRef$current2$re; handleSetValue(''); (_innerRef$current = innerRef.current) === null || _innerRef$current === void 0 ? void 0 : _innerRef$current.focus(); resolveOnChange((_innerRef$current2 = innerRef.current) === null || _innerRef$current2 === void 0 ? void 0 : (_innerRef$current2$re = _innerRef$current2.resizableTextArea) === null || _innerRef$current2$re === void 0 ? void 0 : _innerRef$current2$re.textArea, e, onChange); }; var prefixCls = getPrefixCls('input', customizePrefixCls); React.useImperativeHandle(ref, function () { var _innerRef$current3; return { resizableTextArea: (_innerRef$current3 = innerRef.current) === null || _innerRef$current3 === void 0 ? void 0 : _innerRef$current3.resizableTextArea, focus: function focus(option) { var _innerRef$current4, _innerRef$current4$re; triggerFocus((_innerRef$current4 = innerRef.current) === null || _innerRef$current4 === void 0 ? void 0 : (_innerRef$current4$re = _innerRef$current4.resizableTextArea) === null || _innerRef$current4$re === void 0 ? void 0 : _innerRef$current4$re.textArea, option); }, blur: function blur() { var _innerRef$current5; return (_innerRef$current5 = innerRef.current) === null || _innerRef$current5 === void 0 ? void 0 : _innerRef$current5.blur(); } }; }); var textArea = /*#__PURE__*/React.createElement(RcTextArea, _extends({}, omit(props, ['allowClear']), { disabled: mergedDisabled, className: classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-borderless"), !bordered), _defineProperty(_classNames, className, className && !showCount), _defineProperty(_classNames, "".concat(prefixCls, "-sm"), size === 'small' || customizeSize === 'small'), _defineProperty(_classNames, "".concat(prefixCls, "-lg"), size === 'large' || customizeSize === 'large'), _classNames), getStatusClassNames(prefixCls, mergedStatus)), style: showCount ? { resize: style === null || style === void 0 ? void 0 : style.resize } : style, prefixCls: prefixCls, onCompositionStart: onInternalCompositionStart, onChange: handleChange, onCompositionEnd: onInternalCompositionEnd, ref: innerRef })); var val = fixControlledValue(value); if (!compositing && hasMaxLength && (props.value === null || props.value === undefined)) { // fix #27612 将value转为数组进行截取,解决 '😂'.length === 2 等emoji表情导致的截取乱码的问题 val = fixEmojiLength(val, maxLength); } // TextArea var textareaNode = /*#__PURE__*/React.createElement(ClearableLabeledInput, _extends({ disabled: mergedDisabled }, props, { prefixCls: prefixCls, direction: direction, inputType: "text", value: val, element: textArea, handleReset: handleReset, ref: clearableInputRef, bordered: bordered, status: customStatus, style: showCount ? undefined : style })); // Only show text area wrapper when needed if (showCount || hasFeedback) { var _classNames2; var valueLength = _toConsumableArray(val).length; var dataCount = ''; if (_typeof(showCount) === 'object') { dataCount = showCount.formatter({ value: val, count: valueLength, maxLength: maxLength }); } else { dataCount = "".concat(valueLength).concat(hasMaxLength ? " / ".concat(maxLength) : ''); } return /*#__PURE__*/React.createElement("div", { hidden: hidden, className: classNames("".concat(prefixCls, "-textarea"), (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-textarea-rtl"), direction === 'rtl'), _defineProperty(_classNames2, "".concat(prefixCls, "-textarea-show-count"), showCount), _defineProperty(_classNames2, "".concat(prefixCls, "-textarea-in-form-item"), isFormItemInput), _classNames2), getStatusClassNames("".concat(prefixCls, "-textarea"), mergedStatus, hasFeedback), className), style: style, "data-count": dataCount }, textareaNode, hasFeedback && /*#__PURE__*/React.createElement("span", { className: "".concat(prefixCls, "-textarea-suffix") }, feedbackIcon)); } return textareaNode; }); export default TextArea;