tdesign-react
Version:
TDesign Component for React
271 lines (267 loc) • 11.8 kB
JavaScript
/**
* tdesign v1.16.2
* (c) 2025 tdesign
* @license MIT
*/
import { _ as _defineProperty } from '../_chunks/dep-d67deb2c.js';
import { _ as _slicedToArray } from '../_chunks/dep-10d5731f.js';
import React, { forwardRef, useRef, useImperativeHandle, useMemo } from 'react';
import { CloseCircleFilledIcon } from 'tdesign-icons-react';
import classNames from 'classnames';
import { isFunction } from 'lodash-es';
import useConfig from '../hooks/useConfig.js';
import useControlled from '../hooks/useControlled.js';
import useDefaultProps from '../hooks/useDefaultProps.js';
import useDragSorter from '../hooks/useDragSorter.js';
import useGlobalIcon from '../hooks/useGlobalIcon.js';
import { Input } from '../input/index.js';
import { tagInputDefaultProps } from './defaultProps.js';
import useHover from './useHover.js';
import useTagList from './useTagList.js';
import useTagScroll from './useTagScroll.js';
import '../_chunks/dep-8abcbcbc.js';
import '../_chunks/dep-74a10cfb.js';
import '../config-provider/ConfigContext.js';
import '../locale/zh_CN.js';
import '../_chunks/dep-751cada9.js';
import 'dayjs';
import '../_chunks/dep-37c4dbb1.js';
import '../_util/noop.js';
import '../hooks/useEventCallback.js';
import '../input/Input.js';
import '../_chunks/dep-ed34fbd4.js';
import '../_util/forwardRefWithStatics.js';
import 'hoist-non-react-statics';
import '../_util/parseTNode.js';
import '../_chunks/dep-1144c9da.js';
import '../_chunks/dep-2ba7b13f.js';
import '../hooks/useLayoutEffect.js';
import '../_chunks/dep-ca8d3fa0.js';
import '../locale/LocalReceiver.js';
import '../config-provider/index.js';
import '../config-provider/ConfigProvider.js';
import '../config-provider/type.js';
import '../input/defaultProps.js';
import '../input/InputGroup.js';
import '../input/useLengthLimit.js';
import '../_chunks/dep-624e7b27.js';
import '../_chunks/dep-a3a3e527.js';
import '../input/style/index.js';
import '../input/type.js';
import '../tag/index.js';
import '../tag/Tag.js';
import '../_chunks/dep-5af45ced.js';
import '../tag/defaultProps.js';
import '../tag/CheckTag.js';
import '../_chunks/dep-9fcdea87.js';
import '../tag/CheckTagGroup.js';
import '../tag/style/index.js';
import '../tag/type.js';
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 TagInput = /*#__PURE__*/forwardRef(function (originalProps, ref) {
var props = useDefaultProps(originalProps, tagInputDefaultProps);
var _useConfig = useConfig(),
prefix = _useConfig.classPrefix;
var _useGlobalIcon = useGlobalIcon({
CloseCircleFilledIcon: CloseCircleFilledIcon
}),
CloseCircleFilledIcon$1 = _useGlobalIcon.CloseCircleFilledIcon;
var excessTagsDisplayType = props.excessTagsDisplayType,
autoWidth = props.autoWidth,
borderless = props.borderless,
disabled = props.disabled,
clearable = props.clearable,
placeholder = props.placeholder,
valueDisplay = props.valueDisplay,
label = props.label,
inputProps = props.inputProps,
size = props.size,
tips = props.tips,
status = props.status,
suffixIcon = props.suffixIcon,
suffix = props.suffix,
prefixIcon = props.prefixIcon,
maxRows = props.maxRows,
onClick = props.onClick,
onPaste = props.onPaste,
_onFocus = props.onFocus,
_onBlur = props.onBlur;
var readOnly = props.readOnly || props.readonly;
var _useControlled = useControlled(props, "inputValue", props.onInputChange),
_useControlled2 = _slicedToArray(_useControlled, 2),
tInputValue = _useControlled2[0],
setTInputValue = _useControlled2[1];
var _useHover = useHover(props),
isHover = _useHover.isHover,
addHover = _useHover.addHover,
cancelHover = _useHover.cancelHover;
var _useDragSorter = useDragSorter(_objectSpread(_objectSpread({}, props), {}, {
sortOnDraggable: props.dragSort,
onDragOverCheck: {
x: true,
targetClassNameRegExp: new RegExp("^".concat(prefix, "-tag"))
}
})),
getDragProps = _useDragSorter.getDragProps;
var isCompositionRef = useRef(false);
var _useTagScroll = useTagScroll(props),
scrollToRight = _useTagScroll.scrollToRight,
onWheel = _useTagScroll.onWheel,
scrollToRightOnEnter = _useTagScroll.scrollToRightOnEnter,
scrollToLeftOnLeave = _useTagScroll.scrollToLeftOnLeave,
tagInputRef = _useTagScroll.tagInputRef;
var _useTagList = useTagList(_objectSpread(_objectSpread({}, props), {}, {
getDragProps: getDragProps
})),
tagValue = _useTagList.tagValue,
_onClose = _useTagList.onClose,
onInnerEnter = _useTagList.onInnerEnter,
onInputBackspaceKeyUp = _useTagList.onInputBackspaceKeyUp,
clearAll = _useTagList.clearAll,
renderLabel = _useTagList.renderLabel,
onInputBackspaceKeyDown = _useTagList.onInputBackspaceKeyDown;
var NAME_CLASS = "".concat(prefix, "-tag-input");
var WITH_SUFFIX_ICON_CLASS = "".concat(prefix, "-tag-input__with-suffix-icon");
var CLEAR_CLASS = "".concat(prefix, "-tag-input__suffix-clear");
var BREAK_LINE_CLASS = "".concat(prefix, "-tag-input--break-line");
var tagInputPlaceholder = !(tagValue !== null && tagValue !== void 0 && tagValue.length) ? placeholder : "";
var showClearIcon = Boolean(!readOnly && !disabled && clearable && isHover && (tagValue === null || tagValue === void 0 ? void 0 : tagValue.length));
useImperativeHandle(ref, function () {
return _objectSpread({}, tagInputRef.current || {});
});
var onInputCompositionstart = function onInputCompositionstart(value, context) {
var _inputProps$onComposi;
isCompositionRef.current = true;
inputProps === null || inputProps === void 0 || (_inputProps$onComposi = inputProps.onCompositionstart) === null || _inputProps$onComposi === void 0 || _inputProps$onComposi.call(inputProps, value, context);
};
var onInputCompositionend = function onInputCompositionend(value, context) {
var _inputProps$onComposi2;
isCompositionRef.current = false;
inputProps === null || inputProps === void 0 || (_inputProps$onComposi2 = inputProps.onCompositionend) === null || _inputProps$onComposi2 === void 0 || _inputProps$onComposi2.call(inputProps, value, context);
};
var onInputEnter = function onInputEnter(value, context) {
setTInputValue("", {
e: context.e,
trigger: "enter"
});
!isCompositionRef.current && onInnerEnter(value, context);
scrollToRight();
};
var onInnerClick = function onInnerClick(context) {
if (!props.disabled && !props.readonly) {
var _tagInputRef$current, _tagInputRef$current$;
(_tagInputRef$current = tagInputRef.current) === null || _tagInputRef$current === void 0 || (_tagInputRef$current = _tagInputRef$current.inputElement) === null || _tagInputRef$current === void 0 || (_tagInputRef$current$ = _tagInputRef$current.focus) === null || _tagInputRef$current$ === void 0 || _tagInputRef$current$.call(_tagInputRef$current);
}
onClick === null || onClick === void 0 || onClick(context);
};
var onClearClick = function onClearClick(e) {
var _props$onClear;
clearAll({
e: e
});
setTInputValue("", {
e: e,
trigger: "clear"
});
(_props$onClear = props.onClear) === null || _props$onClear === void 0 || _props$onClear.call(props, {
e: e
});
};
var onKeydown = function onKeydown(value, context) {
var _inputProps$onKeydown;
onInputBackspaceKeyDown(value, context);
inputProps === null || inputProps === void 0 || (_inputProps$onKeydown = inputProps.onKeydown) === null || _inputProps$onKeydown === void 0 || _inputProps$onKeydown.call(inputProps, value, context);
};
var onKeyup = function onKeyup(value, context) {
var _inputProps$onKeyup;
onInputBackspaceKeyUp(value);
inputProps === null || inputProps === void 0 || (_inputProps$onKeyup = inputProps.onKeyup) === null || _inputProps$onKeyup === void 0 || _inputProps$onKeyup.call(inputProps, value, context);
};
var suffixIconNode = showClearIcon ? /* @__PURE__ */React.createElement(CloseCircleFilledIcon$1, {
className: CLEAR_CLASS,
onClick: onClearClick
}) : suffixIcon;
var displayNode = useMemo(function () {
return isFunction(valueDisplay) ? valueDisplay({
value: tagValue,
onClose: function onClose(index) {
return _onClose({
index: index
});
}
}) : valueDisplay;
}, [valueDisplay]);
var isEmpty = !(Array.isArray(tagValue) && tagValue.length);
var classes = [NAME_CLASS, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, BREAK_LINE_CLASS, excessTagsDisplayType === "break-line"), WITH_SUFFIX_ICON_CLASS, !!suffixIconNode), "".concat(prefix, "-is-empty"), isEmpty), "".concat(prefix, "-tag-input--with-tag"), !isEmpty), "".concat(prefix, "-tag-input--max-rows"), excessTagsDisplayType === "break-line" && maxRows), "".concat(prefix, "-tag-input--drag-sort"), props.dragSort && !disabled && !readOnly), props.className];
var maxRowsStyle = maxRows ? {
"--max-rows": maxRows,
"--tag-input-size": size
} : {};
return /* @__PURE__ */React.createElement(Input, _objectSpread(_objectSpread({
ref: tagInputRef,
value: tInputValue,
onChange: function onChange(val, context) {
setTInputValue(val, _objectSpread(_objectSpread({}, context), {}, {
trigger: "input"
}));
},
autoWidth: true,
onWheel: onWheel,
size: size,
borderless: borderless,
readOnly: readOnly,
disabled: disabled,
label: renderLabel({
displayNode: displayNode,
label: label
}),
className: classNames(classes),
style: _objectSpread(_objectSpread({}, props.style), maxRowsStyle),
tips: tips,
status: status,
placeholder: tagInputPlaceholder,
suffix: suffix,
prefixIcon: prefixIcon,
suffixIcon: suffixIconNode,
showInput: !(inputProps !== null && inputProps !== void 0 && inputProps.readonly) || !tagValue || !(tagValue !== null && tagValue !== void 0 && tagValue.length),
keepWrapperWidth: !autoWidth,
onPaste: onPaste,
onClick: onInnerClick,
onEnter: onInputEnter,
onMouseenter: function onMouseenter(context) {
addHover(context);
scrollToRightOnEnter();
},
onMouseleave: function onMouseleave(context) {
cancelHover(context);
scrollToLeftOnLeave();
},
onFocus: function onFocus(inputValue, context) {
_onFocus === null || _onFocus === void 0 || _onFocus(tagValue, {
e: context.e,
inputValue: inputValue
});
},
onBlur: function onBlur(inputValue, context) {
if (tInputValue) {
setTInputValue("", {
e: context.e,
trigger: "blur"
});
}
_onBlur === null || _onBlur === void 0 || _onBlur(tagValue, {
e: context.e,
inputValue: inputValue
});
},
onCompositionstart: onInputCompositionstart,
onCompositionend: onInputCompositionend
}, inputProps), {}, {
onKeydown: onKeydown,
onKeyup: onKeyup
}));
});
TagInput.displayName = "TagInput";
export { TagInput as default };
//# sourceMappingURL=TagInput.js.map