UNPKG

tdesign-react

Version:
314 lines (309 loc) 15.1 kB
/** * tdesign v1.15.1 * (c) 2025 tdesign * @license MIT */ import { _ as _typeof } from './dep-eca3a3de.js'; import { _ as _defineProperty } from './dep-cb0a3966.js'; import { _ as _slicedToArray } from './dep-48805ab8.js'; import React, { useState, useEffect } from 'react'; import { get, isPlainObject } from 'lodash-es'; import { _ as _toConsumableArray } from './dep-87d110df.js'; import OptionGroup from '../select/base/OptionGroup.js'; import Option from '../select/base/Option.js'; function ownKeys$1(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$1(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$1(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$1(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function setValueToOptionFormOptionDom(dom, valueToOption, keys) { var _dom$props = dom.props, value = _dom$props.value, label = _dom$props.label, children = _dom$props.children; valueToOption[value] = _objectSpread$1(_objectSpread$1({}, dom.props), {}, _defineProperty(_defineProperty({}, (keys === null || keys === void 0 ? void 0 : keys.value) || "value", value), (keys === null || keys === void 0 ? void 0 : keys.label) || "label", label || children || value)); } var getValueToOption = function getValueToOption(children, options, keys) { var valueToOption = {}; if (Array.isArray(options)) { options.forEach(function (option) { if (isSelectOptionGroup(option)) { var _option$children; (_option$children = option.children) === null || _option$children === void 0 || _option$children.forEach(function (child) { valueToOption[get(child, (keys === null || keys === void 0 ? void 0 : keys.value) || "value")] = _objectSpread$1(_objectSpread$1({}, child), {}, { value: get(child, (keys === null || keys === void 0 ? void 0 : keys.value) || "value"), label: get(child, (keys === null || keys === void 0 ? void 0 : keys.label) || "label") }); }); } else { valueToOption[get(option, (keys === null || keys === void 0 ? void 0 : keys.value) || "value")] = _objectSpread$1(_objectSpread$1({}, option), {}, { value: get(option, (keys === null || keys === void 0 ? void 0 : keys.value) || "value"), label: get(option, (keys === null || keys === void 0 ? void 0 : keys.label) || "label") }); } }); return valueToOption; } if (isPlainObject(children)) { if (children.type === Option) { setValueToOptionFormOptionDom(children, valueToOption, keys); return valueToOption; } if (children.type === OptionGroup) { var groupChildren = children.props.children; if (Array.isArray(groupChildren)) { groupChildren.forEach(function (item) { setValueToOptionFormOptionDom(item, valueToOption, keys); }); return valueToOption; } } } if (Array.isArray(children)) { var _handlerElement = function handlerElement(item) { if (item.type === Option) { setValueToOptionFormOptionDom(item, valueToOption, keys); } if (item.type === OptionGroup) { var _groupChildren = item.props.children; if (Array.isArray(_groupChildren)) { _groupChildren.forEach(function (groupItem) { setValueToOptionFormOptionDom(groupItem, valueToOption, keys); }); } } if (Array.isArray(item)) { item.forEach(function (child) { _handlerElement(child); }); } }; children.forEach(function (item) { return _handlerElement(item); }); } return valueToOption; }; var getLabel = function getLabel(children, value, options, keys) { var selectedLabel = ""; if (Array.isArray(options)) { options.some(function (option) { if ([get(value, (keys === null || keys === void 0 ? void 0 : keys.value) || "value"), value].includes(option.value)) { selectedLabel = option.label; return true; } return false; }); return selectedLabel; } if (isPlainObject(children)) { selectedLabel = children.props.label; if (children.type === OptionGroup) { var groupChildren = children.props.children; if (Array.isArray(groupChildren)) { groupChildren.some(function (item) { var selectedValue = isPlainObject(value) ? get(value, "value") : value; if (isPlainObject(item.props) && item.props.value === selectedValue) { selectedLabel = item.props.label || item.props.children; return true; } return false; }); } } } if (Array.isArray(children)) { children.some(function (item) { if (item.type === OptionGroup) { var _groupChildren2 = item.props.children; if (Array.isArray(_groupChildren2)) { var isSelected = _groupChildren2.some(function (item2) { var selectedValue2 = isPlainObject(value) ? get(value, "value") : value; if (isPlainObject(item2.props) && item2.props.value === selectedValue2) { selectedLabel = item2.props.label || item2.props.children; return true; } return false; }); return isSelected; } } var selectedValue = isPlainObject(value) ? get(value, "value") : value; if (isPlainObject(item.props) && item.props.value === selectedValue) { selectedLabel = item.props.label || item.props.children; return true; } return false; }); } return selectedLabel; }; var getMultipleTags = function getMultipleTags(values, keys) { var tags = values.map(function (item) { return { label: get(item, (keys === null || keys === void 0 ? void 0 : keys.label) || "label") || item.toString(), value: get(item, (keys === null || keys === void 0 ? void 0 : keys.value) || "value") || item }; }); return tags; }; var getSelectValueArr = function getSelectValueArr(values, activeValue, selected, valueType, keys, objVal) { values = Array.isArray(values) ? values : []; if (Array.isArray(values)) { var currentValues = _toConsumableArray(values); var isValueObj = valueType === "object"; if (selected) { currentValues = currentValues.filter(function (item) { if (isValueObj) { if (isPlainObject(activeValue)) { return get(item, (keys === null || keys === void 0 ? void 0 : keys.value) || "value") !== get(activeValue, (keys === null || keys === void 0 ? void 0 : keys.value) || "value"); } return get(item, (keys === null || keys === void 0 ? void 0 : keys.value) || "value") !== activeValue; } return item !== activeValue; }); } else { var item = isValueObj ? objVal : activeValue; currentValues.push(item); } return currentValues; } }; var getSelectedOptions = function getSelectedOptions(value, multiple, valueType, keys, valueToOption, selectedValue) { var isObjectType = valueType === "object"; var currentSelectedOptions = []; var currentOption; var allSelectedValue; var tmpPropOptions = Object.values(valueToOption); if (multiple) { var _tmpPropOptions$filte, _currentSelectedOptio, _currentSelectedOptio2; currentSelectedOptions = isObjectType ? value : tmpPropOptions === null || tmpPropOptions === void 0 || (_tmpPropOptions$filte = tmpPropOptions.filter) === null || _tmpPropOptions$filte === void 0 ? void 0 : _tmpPropOptions$filte.call(tmpPropOptions, function (v) { var _value$includes; return (_value$includes = value.includes) === null || _value$includes === void 0 ? void 0 : _value$includes.call(value, v[(keys === null || keys === void 0 ? void 0 : keys.value) || "value"]); }); allSelectedValue = isObjectType ? currentSelectedOptions : (_currentSelectedOptio = currentSelectedOptions) === null || _currentSelectedOptio === void 0 ? void 0 : _currentSelectedOptio.map(function (v) { return v[(keys === null || keys === void 0 ? void 0 : keys.value) || "value"]; }); currentOption = isObjectType ? value.find(function (v) { return v[(keys === null || keys === void 0 ? void 0 : keys.value) || "value"] === selectedValue; }) : (_currentSelectedOptio2 = currentSelectedOptions) === null || _currentSelectedOptio2 === void 0 ? void 0 : _currentSelectedOptio2.find(function (option) { return option[(keys === null || keys === void 0 ? void 0 : keys.value) || "value"] === selectedValue; }); } else { var _tmpPropOptions$filte2, _currentSelectedOptio3; currentSelectedOptions = isObjectType ? [value] : (tmpPropOptions === null || tmpPropOptions === void 0 || (_tmpPropOptions$filte2 = tmpPropOptions.filter) === null || _tmpPropOptions$filte2 === void 0 ? void 0 : _tmpPropOptions$filte2.call(tmpPropOptions, function (v) { return value === v[(keys === null || keys === void 0 ? void 0 : keys.value) || "value"]; })) || []; allSelectedValue = currentSelectedOptions; currentOption = isObjectType ? value : (_currentSelectedOptio3 = currentSelectedOptions) === null || _currentSelectedOptio3 === void 0 ? void 0 : _currentSelectedOptio3.find(function (option) { return option[(keys === null || keys === void 0 ? void 0 : keys.value) || "value"] === selectedValue; }); } return { currentSelectedOptions: currentSelectedOptions, currentOption: currentOption, allSelectedValue: allSelectedValue }; }; 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; } function isSelectOptionGroup(option) { return !!option && "group" in option && "children" in option; } function UseOptions(keys, options, children, valueType, value, reserveKeyword) { var _useState = useState({}), _useState2 = _slicedToArray(_useState, 2), valueToOption = _useState2[0], setValueToOption = _useState2[1]; var _useState3 = useState([]), _useState4 = _slicedToArray(_useState3, 2), currentOptions = _useState4[0], setCurrentOptions = _useState4[1]; var _useState5 = useState([]), _useState6 = _slicedToArray(_useState5, 2), tmpPropOptions = _useState6[0], setTmpPropOptions = _useState6[1]; var _useState7 = useState([]), _useState8 = _slicedToArray(_useState7, 2), selectedOptions = _useState8[0], setSelectedOptions = _useState8[1]; useEffect(function () { var transformedOptions = options; var arrayChildren = React.Children.toArray(children); var optionChildren = arrayChildren.filter(function (v) { return v.type === Option || v.type === OptionGroup; }); var isChildrenFilterable = arrayChildren.length > 0 && optionChildren.length === arrayChildren.length; if (reserveKeyword && currentOptions.length && isChildrenFilterable) return; if (isChildrenFilterable) { var _handlerOptionElement = function handlerOptionElement(v) { if (/*#__PURE__*/React.isValidElement(v)) { if (v.type === OptionGroup) { var _v$props$children; return _objectSpread(_objectSpread({}, v.props), {}, { group: v.props.label, children: (_v$props$children = v.props.children) === null || _v$props$children === void 0 ? void 0 : _v$props$children.map(function (v2) { return _handlerOptionElement(v2); }) }); } return _objectSpread(_objectSpread({}, v.props), {}, { label: v.props.label || v.props.children }); } return { label: v }; }; transformedOptions = arrayChildren === null || arrayChildren === void 0 ? void 0 : arrayChildren.map(function (v) { return _handlerOptionElement(v); }); } if (keys) { var _transformedOptions; transformedOptions = (_transformedOptions = transformedOptions) === null || _transformedOptions === void 0 ? void 0 : _transformedOptions.map(function (option) { return _objectSpread(_objectSpread({}, option), {}, { value: get(option, (keys === null || keys === void 0 ? void 0 : keys.value) || "value"), label: get(option, (keys === null || keys === void 0 ? void 0 : keys.label) || "label") }); }); } setCurrentOptions(transformedOptions); setTmpPropOptions(transformedOptions); setValueToOption(getValueToOption(children, options, keys) || {}); }, [options, keys, children, reserveKeyword]); useEffect(function () { var valueKey = (keys === null || keys === void 0 ? void 0 : keys.value) || "value"; var labelKey = (keys === null || keys === void 0 ? void 0 : keys.label) || "label"; setSelectedOptions(function (oldSelectedOptions) { var createOptionFromValue = function createOptionFromValue(item) { if (valueType === "value") { return valueToOption[item] || oldSelectedOptions.find(function (option) { return get(option, valueKey) === item; }) || _defineProperty(_defineProperty({}, valueKey, item), labelKey, item); } if (_typeof(item) === "object" && item !== null) { return item; } return []; }; if (Array.isArray(value)) { return value.map(createOptionFromValue); } if (value !== void 0 && value !== null) { var option = createOptionFromValue(value); return option ? [option] : []; } return []; }); }, [value, keys, valueType, valueToOption, setSelectedOptions]); return { currentOptions: currentOptions, setCurrentOptions: setCurrentOptions, tmpPropOptions: tmpPropOptions, setTmpPropOptions: setTmpPropOptions, valueToOption: valueToOption, setValueToOption: setValueToOption, selectedOptions: selectedOptions, setSelectedOptions: setSelectedOptions }; } export { UseOptions as U, getSelectedOptions as a, getValueToOption as b, getLabel as c, getMultipleTags as d, getSelectValueArr as g, isSelectOptionGroup as i }; //# sourceMappingURL=dep-72472aa7.js.map