UNPKG

plasod

Version:

An enterprise-class UI design language and React components implementation

265 lines (264 loc) 11.2 kB
"use client"; var __rest = this && this.__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; }; import classNames from 'classnames'; import React, { forwardRef, useContext, useEffect, useRef, useState } from 'react'; import RcInput from 'rc-input'; import { composeRef } from "rc-util/es/ref"; import getAllowClear from '../_util/getAllowClear'; import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils'; import { devUseWarning } from '../_util/warning'; import { ConfigContext } from '../config-provider'; import DisabledContext from '../config-provider/DisabledContext'; import useCSSVarCls from '../config-provider/hooks/useCSSVarCls'; import useSize from '../config-provider/hooks/useSize'; import { FormItemInputContext, NoFormStyle } from '../form/context'; import useVariant from '../form/hooks/useVariants'; import { NoCompactStyle, useCompactItemContext } from '../space/Compact'; import useRemovePasswordTimeout from './hooks/useRemovePasswordTimeout'; import useStyle from './style'; import { hasPrefixSuffix } from './utils'; export function triggerFocus(element, option) { if (!element) { return; } element.focus(option); // Selection content const { cursor } = option || {}; if (cursor) { const len = element.value.length; switch (cursor) { case 'start': element.setSelectionRange(0, 0); break; case 'end': element.setSelectionRange(len, len); break; default: element.setSelectionRange(0, len); break; } } } const Input = /*#__PURE__*/forwardRef((props, ref) => { var _a; const { prefixCls: customizePrefixCls, bordered = true, status: customStatus, size: customSize, disabled: customDisabled, onBlur, onFocus, suffix, allowClear, addonAfter, addonBefore, className, style, styles, rootClassName, onChange, classNames: classes, doubleByte = false, defaultValue, value = defaultValue, maxLength, variant: customVariant, count } = props, rest = __rest(props, ["prefixCls", "bordered", "status", "size", "disabled", "onBlur", "onFocus", "suffix", "allowClear", "addonAfter", "addonBefore", "className", "style", "styles", "rootClassName", "onChange", "classNames", "doubleByte", "defaultValue", "value", "maxLength", "variant", "count"]); if (process.env.NODE_ENV !== 'production') { const { deprecated } = devUseWarning('Input'); deprecated(!('bordered' in props), 'bordered', 'variant'); } const { getPrefixCls, direction, input } = React.useContext(ConfigContext); const prefixCls = getPrefixCls('input', customizePrefixCls); const inputRef = useRef(null); // Style const rootCls = useCSSVarCls(prefixCls); const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls); // ===================== Compact Item ===================== const { compactSize, compactItemClassnames } = useCompactItemContext(prefixCls, direction); // ===================== Size ===================== const mergedSize = useSize(ctx => { var _a; return (_a = customSize !== null && customSize !== void 0 ? customSize : compactSize) !== null && _a !== void 0 ? _a : ctx; }); // ===================== Disabled ===================== const disabled = React.useContext(DisabledContext); const mergedDisabled = customDisabled !== null && customDisabled !== void 0 ? customDisabled : disabled; // ===================== Status ===================== const { status: contextStatus, hasFeedback, feedbackIcon } = useContext(FormItemInputContext); const mergedStatus = getMergedStatus(contextStatus, customStatus); // ===================== Focus warning ===================== const inputHasPrefixSuffix = hasPrefixSuffix(props) || !!hasFeedback; const prevHasPrefixSuffix = useRef(inputHasPrefixSuffix); /* eslint-disable react-hooks/rules-of-hooks */ if (process.env.NODE_ENV !== 'production') { const warning = devUseWarning('Input'); useEffect(() => { var _a; if (inputHasPrefixSuffix && !prevHasPrefixSuffix.current) { process.env.NODE_ENV !== "production" ? warning(document.activeElement === ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.input), 'usage', `When Input is focused, dynamic add or remove prefix / suffix will make it lose focus caused by dom structure change. Read more: https://ant.design/components/input/#FAQ`) : void 0; } prevHasPrefixSuffix.current = inputHasPrefixSuffix; }, [inputHasPrefixSuffix]); } /* eslint-enable */ // ===================== Remove Password value ===================== const removePasswordTimeout = useRemovePasswordTimeout(inputRef, true); const handleBlur = e => { removePasswordTimeout(); onBlur === null || onBlur === void 0 ? void 0 : onBlur(e); }; const handleFocus = e => { removePasswordTimeout(); onFocus === null || onFocus === void 0 ? void 0 : onFocus(e); }; // doubleByte maxLength const [insideValue, setInsideValue] = useState(value); const isComposition = useRef(false); useEffect(() => { setInsideValue(value); }, [value]); const countStringLength = function (inputString) { let countDoubleByte = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var _a; const doubleByteLength = ((_a = inputString.match(/[\u4e00-\u9fa5]/g)) === null || _a === void 0 ? void 0 : _a.length) || 0; const singleByteLength = inputString.length - doubleByteLength; const length = doubleByteLength * (countDoubleByte ? 2 : 1) + singleByteLength; return length; }; const cutStringByByte = (str, L) => { let result = ''; const strlen = str.length; // 字符串长度 const chrlen = str.replace(/[^0-\xff]/g, '**').length; // 字节长度 if (chrlen <= L) { return str; } for (let i = 0, j = 0; i < strlen; i++) { const chr = str.charAt(i); if (/[0-\xff]/.test(chr)) { j++; // ascii码为0-255,一个字符就是一个字节的长度 } else { j += 2; // ascii码为0-255以外,一个字符就是两个字节的长度 } if (j <= L) { // 当加上当前字符以后,如果总字节长度小于等于L,则将当前字符真实的+在result后 result += chr; } else { // 反之则说明result已经是不拆分字符的情况下最接近L的值了,直接返回 return result; } } }; const handleChange = e => { removePasswordTimeout(); if (isComposition.current) { setInsideValue(e.target.value); onChange === null || onChange === void 0 ? void 0 : onChange(e); } else if (maxLength && countStringLength(e.target.value, doubleByte) > maxLength) { let resValue = ''; if (doubleByte) { resValue = cutStringByByte(e.target.value, maxLength); } else { resValue = e.target.value.substring(0, maxLength); } setInsideValue(resValue); const simulateEvent = Object.assign({}, e); simulateEvent.target.value = resValue; onChange === null || onChange === void 0 ? void 0 : onChange(e); } else { setInsideValue(e.target.value); onChange === null || onChange === void 0 ? void 0 : onChange(e); } }; const handleComposition = ev => { if (ev.type === 'compositionend') { isComposition.current = false; handleChange(ev); } else { isComposition.current = true; } }; const suffixNode = (hasFeedback || suffix) && ( /*#__PURE__*/React.createElement(React.Fragment, null, suffix, hasFeedback && feedbackIcon)); const mergedAllowClear = getAllowClear(allowClear !== null && allowClear !== void 0 ? allowClear : input === null || input === void 0 ? void 0 : input.allowClear); const [variant, enableVariantCls] = useVariant(customVariant, bordered); return wrapCSSVar( /*#__PURE__*/React.createElement(RcInput, Object.assign({ ref: composeRef(ref, inputRef), prefixCls: prefixCls, autoComplete: input === null || input === void 0 ? void 0 : input.autoComplete }, rest, { count: count, maxLength: maxLength, disabled: mergedDisabled, onBlur: handleBlur, onFocus: handleFocus, style: Object.assign(Object.assign({}, input === null || input === void 0 ? void 0 : input.style), style), styles: Object.assign(Object.assign({}, input === null || input === void 0 ? void 0 : input.styles), styles), suffix: suffixNode, allowClear: mergedAllowClear, className: classNames(className, rootClassName, (count === null || count === void 0 ? void 0 : count.noError) ? `${prefixCls}-noError` : null, cssVarCls, rootCls, compactItemClassnames, input === null || input === void 0 ? void 0 : input.className), onChange: handleChange, // @ts-ignore onCompositionStart: handleComposition, // @ts-ignore onCompositionEnd: handleComposition, value: insideValue, addonAfter: addonAfter && ( /*#__PURE__*/React.createElement(NoCompactStyle, null, /*#__PURE__*/React.createElement(NoFormStyle, { override: true, status: true }, addonAfter))), addonBefore: addonBefore && ( /*#__PURE__*/React.createElement(NoCompactStyle, null, /*#__PURE__*/React.createElement(NoFormStyle, { override: true, status: true }, addonBefore))), classNames: Object.assign(Object.assign(Object.assign({}, classes), input === null || input === void 0 ? void 0 : input.classNames), { input: classNames({ [`${prefixCls}-sm`]: mergedSize === 'small', [`${prefixCls}-lg`]: mergedSize === 'large', [`${prefixCls}-rtl`]: direction === 'rtl' }, classes === null || classes === void 0 ? void 0 : classes.input, (_a = input === null || input === void 0 ? void 0 : input.classNames) === null || _a === void 0 ? void 0 : _a.input, hashId), variant: classNames({ [`${prefixCls}-${variant}`]: enableVariantCls }, getStatusClassNames(prefixCls, mergedStatus)), affixWrapper: classNames({ [`${prefixCls}-affix-wrapper-sm`]: mergedSize === 'small', [`${prefixCls}-affix-wrapper-lg`]: mergedSize === 'large', [`${prefixCls}-affix-wrapper-rtl`]: direction === 'rtl' }, hashId), wrapper: classNames({ [`${prefixCls}-group-rtl`]: direction === 'rtl' }, hashId), groupWrapper: classNames({ [`${prefixCls}-group-wrapper-sm`]: mergedSize === 'small', [`${prefixCls}-group-wrapper-lg`]: mergedSize === 'large', [`${prefixCls}-group-wrapper-rtl`]: direction === 'rtl', [`${prefixCls}-group-wrapper-${variant}`]: enableVariantCls }, getStatusClassNames(`${prefixCls}-group-wrapper`, mergedStatus, hasFeedback), hashId) }) }))); }); export default Input;