UNPKG

tdesign-react

Version:
327 lines (319 loc) 16.1 kB
/** * tdesign v1.15.1 * (c) 2025 tdesign * @license MIT */ 'use strict'; var _typeof = require('./dep-667ac7af.js'); var defineProperty = require('./dep-0006fcfa.js'); var slicedToArray = require('./dep-8e4d656d.js'); var React = require('react'); var toConsumableArray = require('./dep-e4e1901e.js'); var select_base_OptionGroup = require('../select/base/OptionGroup.js'); var select_base_Option = require('../select/base/Option.js'); var get = require('./dep-0c8c9057.js'); var isPlainObject = require('./dep-a2cb9299.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var React__default = /*#__PURE__*/_interopDefaultLegacy(React); 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._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(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.get(child, (keys === null || keys === void 0 ? void 0 : keys.value) || "value")] = _objectSpread$1(_objectSpread$1({}, child), {}, { value: get.get(child, (keys === null || keys === void 0 ? void 0 : keys.value) || "value"), label: get.get(child, (keys === null || keys === void 0 ? void 0 : keys.label) || "label") }); }); } else { valueToOption[get.get(option, (keys === null || keys === void 0 ? void 0 : keys.value) || "value")] = _objectSpread$1(_objectSpread$1({}, option), {}, { value: get.get(option, (keys === null || keys === void 0 ? void 0 : keys.value) || "value"), label: get.get(option, (keys === null || keys === void 0 ? void 0 : keys.label) || "label") }); } }); return valueToOption; } if (isPlainObject.isPlainObject(children)) { if (children.type === select_base_Option["default"]) { setValueToOptionFormOptionDom(children, valueToOption, keys); return valueToOption; } if (children.type === select_base_OptionGroup["default"]) { 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 === select_base_Option["default"]) { setValueToOptionFormOptionDom(item, valueToOption, keys); } if (item.type === select_base_OptionGroup["default"]) { 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.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.isPlainObject(children)) { selectedLabel = children.props.label; if (children.type === select_base_OptionGroup["default"]) { var groupChildren = children.props.children; if (Array.isArray(groupChildren)) { groupChildren.some(function (item) { var selectedValue = isPlainObject.isPlainObject(value) ? get.get(value, "value") : value; if (isPlainObject.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 === select_base_OptionGroup["default"]) { var _groupChildren2 = item.props.children; if (Array.isArray(_groupChildren2)) { var isSelected = _groupChildren2.some(function (item2) { var selectedValue2 = isPlainObject.isPlainObject(value) ? get.get(value, "value") : value; if (isPlainObject.isPlainObject(item2.props) && item2.props.value === selectedValue2) { selectedLabel = item2.props.label || item2.props.children; return true; } return false; }); return isSelected; } } var selectedValue = isPlainObject.isPlainObject(value) ? get.get(value, "value") : value; if (isPlainObject.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.get(item, (keys === null || keys === void 0 ? void 0 : keys.label) || "label") || item.toString(), value: get.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._toConsumableArray(values); var isValueObj = valueType === "object"; if (selected) { currentValues = currentValues.filter(function (item) { if (isValueObj) { if (isPlainObject.isPlainObject(activeValue)) { return get.get(item, (keys === null || keys === void 0 ? void 0 : keys.value) || "value") !== get.get(activeValue, (keys === null || keys === void 0 ? void 0 : keys.value) || "value"); } return get.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._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 = React.useState({}), _useState2 = slicedToArray._slicedToArray(_useState, 2), valueToOption = _useState2[0], setValueToOption = _useState2[1]; var _useState3 = React.useState([]), _useState4 = slicedToArray._slicedToArray(_useState3, 2), currentOptions = _useState4[0], setCurrentOptions = _useState4[1]; var _useState5 = React.useState([]), _useState6 = slicedToArray._slicedToArray(_useState5, 2), tmpPropOptions = _useState6[0], setTmpPropOptions = _useState6[1]; var _useState7 = React.useState([]), _useState8 = slicedToArray._slicedToArray(_useState7, 2), selectedOptions = _useState8[0], setSelectedOptions = _useState8[1]; React.useEffect(function () { var transformedOptions = options; var arrayChildren = React__default["default"].Children.toArray(children); var optionChildren = arrayChildren.filter(function (v) { return v.type === select_base_Option["default"] || v.type === select_base_OptionGroup["default"]; }); 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__default["default"].isValidElement(v)) { if (v.type === select_base_OptionGroup["default"]) { 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.get(option, (keys === null || keys === void 0 ? void 0 : keys.value) || "value"), label: get.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]); React.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.get(option, valueKey) === item; }) || defineProperty._defineProperty(defineProperty._defineProperty({}, valueKey, item), labelKey, item); } if (_typeof._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 }; } exports.UseOptions = UseOptions; exports.getLabel = getLabel; exports.getMultipleTags = getMultipleTags; exports.getSelectValueArr = getSelectValueArr; exports.getSelectedOptions = getSelectedOptions; exports.getValueToOption = getValueToOption; exports.isSelectOptionGroup = isSelectOptionGroup; //# sourceMappingURL=dep-e83bd2ab.js.map