UNPKG

tdesign-react

Version:
414 lines (410 loc) 18.5 kB
/** * tdesign v1.15.1 * (c) 2025 tdesign * @license MIT */ import { _ as _defineProperty } from '../_chunks/dep-cb0a3966.js'; import { _ as _objectWithoutProperties } from '../_chunks/dep-6b660ef0.js'; import { _ as _slicedToArray } from '../_chunks/dep-48805ab8.js'; import React, { useRef, useState, useEffect, useImperativeHandle } from 'react'; import classNames from 'classnames'; import { BrowseIcon, BrowseOffIcon, CloseCircleFilledIcon } from 'tdesign-icons-react'; import { isFunction } from 'lodash-es'; import useIsomorphicLayoutEffect from '../hooks/useLayoutEffect.js'; import forwardRefWithStatics from '../_util/forwardRefWithStatics.js'; import useConfig from '../hooks/useConfig.js'; import useGlobalIcon from '../hooks/useGlobalIcon.js'; import InputGroup from './InputGroup.js'; import useControlled from '../hooks/useControlled.js'; import { useLocaleReceiver } from '../locale/LocalReceiver.js'; import { inputDefaultProps } from './defaultProps.js'; import parseTNode from '../_util/parseTNode.js'; import useLengthLimit from './useLengthLimit.js'; import useDefaultProps from '../hooks/useDefaultProps.js'; import '../_chunks/dep-eca3a3de.js'; import '../_chunks/dep-026a4c6b.js'; import '../_chunks/dep-3a09424a.js'; import 'hoist-non-react-statics'; import '../config-provider/ConfigContext.js'; import '../_chunks/dep-f97636ce.js'; import '../_chunks/dep-9dbbf468.js'; import 'dayjs'; import '../_util/noop.js'; import '../config-provider/ConfigProvider.js'; import '../_chunks/dep-b908e1fe.js'; import '../_chunks/dep-6af6bc60.js'; import '../_chunks/dep-87d110df.js'; var _excluded = ["type", "autoWidth", "borderless", "placeholder", "disabled", "status", "size", "className", "inputClass", "style", "prefixIcon", "suffixIcon", "clearable", "tips", "align", "maxlength", "maxcharacter", "showClearIconOnEmpty", "autofocus", "autocomplete", "readonly", "label", "suffix", "showInput", "keepWrapperWidth", "showLimitNumber", "allowInput", "allowInputOverMax", "name", "format", "onClick", "onClear", "onEnter", "onKeydown", "onKeyup", "onKeypress", "onFocus", "onBlur", "onPaste", "onMouseenter", "onMouseleave", "onWheel", "onCompositionstart", "onCompositionend", "onValidate", "onChange"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } var renderIcon = function renderIcon(classPrefix, type, icon) { var result = parseTNode(icon); var iconClassName = icon ? "".concat(classPrefix, "-input__").concat(type, "-icon") : ""; return result ? /* @__PURE__ */React.createElement("span", { className: "".concat(classPrefix, "-input__").concat(type, " ").concat(iconClassName) }, result) : null; }; var Input = forwardRefWithStatics(function (originalProps, ref) { var _classNames3; var _useLocaleReceiver = useLocaleReceiver("input"), _useLocaleReceiver2 = _slicedToArray(_useLocaleReceiver, 2), local = _useLocaleReceiver2[0], t = _useLocaleReceiver2[1]; var _useGlobalIcon = useGlobalIcon({ BrowseIcon: BrowseIcon, BrowseOffIcon: BrowseOffIcon, CloseCircleFilledIcon: CloseCircleFilledIcon }), BrowseIcon$1 = _useGlobalIcon.BrowseIcon, BrowseOffIcon$1 = _useGlobalIcon.BrowseOffIcon, CloseCircleFilledIcon$1 = _useGlobalIcon.CloseCircleFilledIcon; var props = useDefaultProps(originalProps, inputDefaultProps); var type = props.type, autoWidth = props.autoWidth, borderless = props.borderless, _props$placeholder = props.placeholder, placeholder = _props$placeholder === void 0 ? t(local.placeholder) : _props$placeholder, disabled = props.disabled, status = props.status, size = props.size, className = props.className, inputClass = props.inputClass, style = props.style, prefixIcon = props.prefixIcon, suffixIcon = props.suffixIcon, clearable = props.clearable, tips = props.tips, align = props.align, maxlength = props.maxlength, maxcharacter = props.maxcharacter, showClearIconOnEmpty = props.showClearIconOnEmpty, autofocus = props.autofocus, autocomplete = props.autocomplete, readonly = props.readonly, label = props.label, suffix = props.suffix, _props$showInput = props.showInput, showInput = _props$showInput === void 0 ? true : _props$showInput, keepWrapperWidth = props.keepWrapperWidth, showLimitNumber = props.showLimitNumber, allowInput = props.allowInput, allowInputOverMax = props.allowInputOverMax, name = props.name, format = props.format, _onClick = props.onClick, onClear = props.onClear, onEnter = props.onEnter, onKeydown = props.onKeydown, onKeyup = props.onKeyup, onKeypress = props.onKeypress, onFocus = props.onFocus, onBlur = props.onBlur, onPaste = props.onPaste, onMouseenter = props.onMouseenter, onMouseleave = props.onMouseleave, _onWheel = props.onWheel, onCompositionstart = props.onCompositionstart, onCompositionend = props.onCompositionend, onValidate = props.onValidate, onChangeFromProps = props.onChange, restProps = _objectWithoutProperties(props, _excluded); var _useControlled = useControlled(props, "value", onChangeFromProps), _useControlled2 = _slicedToArray(_useControlled, 2), value = _useControlled2[0], onChange = _useControlled2[1]; var _useLengthLimit = useLengthLimit({ value: value === void 0 ? void 0 : String(value), status: status, maxlength: maxlength, maxcharacter: maxcharacter, allowInputOverMax: allowInputOverMax, onValidate: onValidate }), limitNumber = _useLengthLimit.limitNumber, getValueByLimitNumber = _useLengthLimit.getValueByLimitNumber, tStatus = _useLengthLimit.tStatus; var _useConfig = useConfig(), classPrefix = _useConfig.classPrefix, inputConfig = _useConfig.input; var composingRef = useRef(false); var inputRef = useRef(null); var inputPreRef = useRef(null); var wrapperRef = useRef(null); var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), isHover = _useState2[0], toggleIsHover = _useState2[1]; var _useState3 = useState(false), _useState4 = _slicedToArray(_useState3, 2), isFocused = _useState4[0], toggleIsFocused = _useState4[1]; var _useState5 = useState(type), _useState6 = _slicedToArray(_useState5, 2), renderType = _useState6[0], setRenderType = _useState6[1]; var _useState7 = useState(""), _useState8 = _slicedToArray(_useState7, 2), composingValue = _useState8[0], setComposingValue = _useState8[1]; var isInnerInputReadonly = readonly || !allowInput; var isValueEnabled = value && !disabled; var alwaysShowClearIcon = (inputConfig === null || inputConfig === void 0 ? void 0 : inputConfig.clearTrigger) === "always"; var isShowClearIcon = (clearable && isValueEnabled || showClearIconOnEmpty) && isHover || isValueEnabled && alwaysShowClearIcon; var prefixIconContent = renderIcon(classPrefix, "prefix", parseTNode(prefixIcon)); var suffixIconNew = suffixIcon; if (isShowClearIcon) suffixIconNew = /* @__PURE__ */React.createElement(CloseCircleFilledIcon$1, { className: "".concat(classPrefix, "-input__suffix-clear"), onMouseDown: handleMouseDown, onClick: handleClear }); if (type === "password" && typeof suffixIcon === "undefined") { if (renderType === "password") { suffixIconNew = /* @__PURE__ */React.createElement(BrowseOffIcon$1, { className: "".concat(classPrefix, "-input__suffix-clear"), onClick: togglePasswordVisible }); } else if (renderType === "text") { suffixIconNew = /* @__PURE__ */React.createElement(BrowseIcon$1, { className: "".concat(classPrefix, "-input__suffix-clear"), onClick: togglePasswordVisible }); } } var suffixIconContent = renderIcon(classPrefix, "suffix", parseTNode(suffixIconNew)); var labelContent = isFunction(label) ? label() : label; var suffixContent = isFunction(suffix) ? suffix() : suffix; var limitNumberNode = limitNumber && showLimitNumber ? /* @__PURE__ */React.createElement("div", { className: classNames("".concat(classPrefix, "-input__limit-number"), _defineProperty({}, "".concat(classPrefix, "-is-disabled"), disabled)) }, limitNumber) : null; var updateInputWidth = function updateInputWidth() { if (!autoWidth || !inputRef.current) return; var offsetWidth = inputPreRef.current.offsetWidth; var _inputPreRef$current$ = inputPreRef.current.getBoundingClientRect(), width = _inputPreRef$current$.width; var calcWidth = width < offsetWidth ? offsetWidth + 1 : width; inputRef.current.style.width = "".concat(calcWidth, "px"); }; useIsomorphicLayoutEffect(function () { requestAnimationFrame(function () { updateInputWidth(); }); }, [autoWidth, value, placeholder, inputRef, composingValue]); useEffect(function () { var resizeObserver = null; if (typeof window.ResizeObserver === "undefined" || !inputRef.current) return; resizeObserver = new window.ResizeObserver(function () { updateInputWidth(); }); resizeObserver.observe(inputRef.current); return function () { var _resizeObserver$disco, _resizeObserver; (_resizeObserver$disco = (_resizeObserver = resizeObserver).disconnect) === null || _resizeObserver$disco === void 0 || _resizeObserver$disco.call(_resizeObserver); resizeObserver = null; }; }, [inputRef]); useEffect(function () { setRenderType(type); }, [type]); useEffect(function () { if (value) { var limitedValue = getValueByLimitNumber(value); if (limitedValue.length !== value.length && !allowInputOverMax) { onChange === null || onChange === void 0 || onChange(limitedValue, { trigger: "initial" }); } } }, []); var innerValue = composingRef.current ? composingValue : value !== null && value !== void 0 ? value : ""; var formatDisplayValue = format && !isFocused ? format(innerValue) : innerValue; var renderInput = /* @__PURE__ */React.createElement("input", { ref: inputRef, placeholder: placeholder, type: renderType, className: classNames("".concat(classPrefix, "-input__inner"), _defineProperty({}, "".concat(classPrefix, "-input--soft-hidden"), !showInput)), value: formatDisplayValue, readOnly: isInnerInputReadonly, disabled: disabled, autoComplete: autocomplete !== null && autocomplete !== void 0 ? autocomplete : local.autocomplete || void 0, autoFocus: autofocus, onChange: handleChange, onKeyDown: handleKeyDown, onKeyUp: handleKeyUp, onKeyPress: handleKeyPress, onCompositionStart: handleCompositionStart, onCompositionEnd: handleCompositionEnd, onFocus: handleFocus, onBlur: handleBlur, onPaste: handlePaste, name: name }); var renderInputNode = /* @__PURE__ */React.createElement("div", { className: classNames(inputClass, "".concat(classPrefix, "-input"), (_classNames3 = {}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_classNames3, "".concat(classPrefix, "-is-readonly"), readonly), "".concat(classPrefix, "-is-disabled"), disabled), "".concat(classPrefix, "-is-focused"), isFocused), "".concat(classPrefix, "-size-s"), size === "small"), "".concat(classPrefix, "-size-l"), size === "large"), "".concat(classPrefix, "-align-").concat(align), align), "".concat(classPrefix, "-is-").concat(tStatus), tStatus && tStatus !== "default"), "".concat(classPrefix, "-input--prefix"), prefixIcon || labelContent), "".concat(classPrefix, "-input--suffix"), suffixIconContent || suffixContent), "".concat(classPrefix, "-input--borderless"), borderless), _defineProperty(_classNames3, "".concat(classPrefix, "-input--focused"), isFocused))), onMouseEnter: handleMouseEnter, onMouseLeave: handleMouseLeave, onWheel: function onWheel(e) { return _onWheel === null || _onWheel === void 0 ? void 0 : _onWheel({ e: e }); }, onClick: function onClick(e) { var _inputRef$current; (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || _inputRef$current.focus(); _onClick === null || _onClick === void 0 || _onClick({ e: e }); } }, prefixIconContent, labelContent ? /* @__PURE__ */React.createElement("div", { className: "".concat(classPrefix, "-input__prefix") }, labelContent) : null, renderInput, autoWidth && /* @__PURE__ */React.createElement("span", { ref: inputPreRef, className: "".concat(classPrefix, "-input__input-pre") }, innerValue || placeholder), suffixContent || limitNumberNode ? /* @__PURE__ */React.createElement("div", { className: "".concat(classPrefix, "-input__suffix") }, suffixContent, limitNumberNode) : null, suffixIconContent); function togglePasswordVisible() { var _inputRef$current2; if (disabled) return; var inputEl = inputRef.current; var cursorPosition = ((_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 ? void 0 : _inputRef$current2.selectionStart) || 0; var toggleType = renderType === "password" ? "text" : "password"; setRenderType(toggleType); requestAnimationFrame(function () { inputEl === null || inputEl === void 0 || inputEl.setSelectionRange(cursorPosition, cursorPosition); }); } function handleChange(e) { var trigger = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "input"; var newStr = e.currentTarget.value; if (composingRef.current) { setComposingValue(newStr); } else { if (props.type !== "number") { newStr = getValueByLimitNumber(newStr); } setComposingValue(newStr); onChange(newStr, { e: e, trigger: trigger }); } } function handleMouseDown(e) { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); } function handleClear(e) { onChange === null || onChange === void 0 || onChange("", { e: e, trigger: "clear" }); onClear === null || onClear === void 0 || onClear({ e: e }); } function handleKeyDown(e) { var key = e.key, value2 = e.currentTarget.value; key === "Enter" && (onEnter === null || onEnter === void 0 ? void 0 : onEnter(value2, { e: e })); onKeydown === null || onKeydown === void 0 || onKeydown(value2, { e: e }); } function handleKeyUp(e) { var value2 = e.currentTarget.value; onKeyup === null || onKeyup === void 0 || onKeyup(value2, { e: e }); } function handleKeyPress(e) { var value2 = e.currentTarget.value; onKeypress === null || onKeypress === void 0 || onKeypress(value2, { e: e }); } function handleCompositionStart(e) { composingRef.current = true; var value2 = e.currentTarget.value; onCompositionstart === null || onCompositionstart === void 0 || onCompositionstart(value2, { e: e }); } function handleCompositionEnd(e) { var value2 = e.currentTarget.value; if (composingRef.current) { composingRef.current = false; handleChange(e); } onCompositionend === null || onCompositionend === void 0 || onCompositionend(value2, { e: e }); } function handleFocus(e) { var value2 = e.currentTarget.value; onFocus === null || onFocus === void 0 || onFocus(value2, { e: e }); if (isInnerInputReadonly) return; toggleIsFocused(true); } function handleBlur(e) { var value2 = e.currentTarget.value; onBlur === null || onBlur === void 0 || onBlur(value2, { e: e }); if (isInnerInputReadonly) return; toggleIsFocused(false); } function handlePaste(e) { var clipData = e.clipboardData; var pasteValue = clipData === null || clipData === void 0 ? void 0 : clipData.getData("text/plain"); onPaste === null || onPaste === void 0 || onPaste({ e: e, pasteValue: pasteValue }); } function handleMouseEnter(e) { !readonly && toggleIsHover(true); onMouseenter === null || onMouseenter === void 0 || onMouseenter({ e: e }); } function handleMouseLeave(e) { !readonly && toggleIsHover(false); onMouseleave === null || onMouseleave === void 0 || onMouseleave({ e: e }); } useImperativeHandle(ref, function () { return { currentElement: wrapperRef.current, inputElement: inputRef.current, focus: function focus() { var _inputRef$current3; return (_inputRef$current3 = inputRef.current) === null || _inputRef$current3 === void 0 ? void 0 : _inputRef$current3.focus(); }, blur: function blur() { var _inputRef$current4; return (_inputRef$current4 = inputRef.current) === null || _inputRef$current4 === void 0 ? void 0 : _inputRef$current4.blur(); }, select: function select() { var _inputRef$current5; return (_inputRef$current5 = inputRef.current) === null || _inputRef$current5 === void 0 ? void 0 : _inputRef$current5.select(); } }; }); return /* @__PURE__ */React.createElement("div", _objectSpread({ ref: wrapperRef, style: style, className: classNames("".concat(classPrefix, "-input__wrap"), className, _defineProperty({}, "".concat(classPrefix, "-input--auto-width"), autoWidth && !keepWrapperWidth)) }, restProps), renderInputNode, tips && /* @__PURE__ */React.createElement("div", { className: classNames("".concat(classPrefix, "-input__tips"), "".concat(classPrefix, "-input__tips--").concat(tStatus || "default")) }, tips)); }, { Group: InputGroup }); Input.displayName = "Input"; export { Input as default }; //# sourceMappingURL=Input.js.map