@flatbiz/antd
Version:
121 lines (117 loc) • 4.97 kB
JavaScript
/*! @flatjs/forge MIT @flatbiz/antd */
import { _ as _objectWithoutProperties, a as _slicedToArray, b as _objectSpread2 } from './_rollupPluginBabelHelpers-BYm17lo8.js';
import { hooks } from '@wove/react/hooks';
import { isObject } from '@dimjs/lang/is-object';
import { useRef, useState, useEffect } from 'react';
import { Select, message } from 'antd';
import { isUndefinedOrNull, toArray } from '@flatbiz/utils';
import { TreeModal } from './tree-modal/index.js';
import { jsx } from 'react/jsx-runtime';
var _excluded = ["placeholder", "maxTagCount", "labelInValue", "maxCount", "overMaxCountMsg", "disabled"];
/**
* 树弹框选择器
* ```
* 1. 可通过 treeProps.checkableType 配置多选节点模式(包括返回所有节点、只返回叶子节点、叶子节点&父节点全选只返回父节点),默认只返回叶子节点
* 2. demo: https://fex.qa.tcshuke.com/docs/admin/main/selector/tree
* ```
*/
var TreeModalSelector = function TreeModalSelector(props) {
var _otherProps$fieldName, _otherProps$fieldName2;
var placeholder = props.placeholder,
maxTagCount = props.maxTagCount,
labelInValue = props.labelInValue,
maxCount = props.maxCount,
overMaxCountMsg = props.overMaxCountMsg,
disabled = props.disabled,
otherProps = _objectWithoutProperties(props, _excluded);
var placeholderFt = isUndefinedOrNull(placeholder) ? '请选择' : placeholder;
var dataSourceMapRef = useRef({});
var labelKey = ((_otherProps$fieldName = otherProps.fieldNames) === null || _otherProps$fieldName === void 0 ? void 0 : _otherProps$fieldName.label) || 'label';
var valueKey = ((_otherProps$fieldName2 = otherProps.fieldNames) === null || _otherProps$fieldName2 === void 0 ? void 0 : _otherProps$fieldName2.value) || 'value';
var _useState = useState(),
_useState2 = _slicedToArray(_useState, 2),
showValues = _useState2[0],
setShowValues = _useState2[1];
useEffect(function () {
var _toArray;
var dataList = ((_toArray = toArray(props.value)) === null || _toArray === void 0 ? void 0 : _toArray.map(function (item) {
var _dataSourceMapRef$cur;
var objItems = isObject(item) ? item : {
label: item,
value: item
};
return {
value: objItems.value,
label: ((_dataSourceMapRef$cur = dataSourceMapRef.current[objItems.value]) === null || _dataSourceMapRef$cur === void 0 ? void 0 : _dataSourceMapRef$cur[labelKey]) || objItems.value
};
})) || [];
setShowValues(dataList);
}, [labelKey, props.value]);
var onInnerChange = function onInnerChange(value) {
var valueFt = toArray(value).length ? value : undefined;
if (otherProps.isMultiple == false) {
var _props$onChange;
(_props$onChange = props.onChange) === null || _props$onChange === void 0 || _props$onChange.call(props, valueFt === null || valueFt === void 0 ? void 0 : valueFt[0]);
} else {
var _props$onChange2;
if (valueFt && maxCount !== undefined && valueFt.length > maxCount) {
void message.error(overMaxCountMsg ? overMaxCountMsg : "\u6700\u591A\u9009\u62E9".concat(maxCount, "\u9879"));
return;
}
(_props$onChange2 = props.onChange) === null || _props$onChange2 === void 0 || _props$onChange2.call(props, valueFt);
}
};
var onSelectChange = function onSelectChange(value) {
setShowValues(value);
if (labelInValue) {
onInnerChange(value);
return;
}
onInnerChange(value === null || value === void 0 ? void 0 : value.map(function (item) {
return item.value;
}));
};
var treeModalChange = function treeModalChange(value) {
setShowValues(value);
if (labelInValue) {
onInnerChange(value);
return;
}
onInnerChange(value === null || value === void 0 ? void 0 : value.map(function (item) {
return item.value;
}));
};
var onDataSourceChange = hooks.useCallbackRef(function (_treeDataList, mapData) {
dataSourceMapRef.current = mapData;
var result = showValues === null || showValues === void 0 ? void 0 : showValues.map(function (item) {
var target = mapData[item.value];
return target ? {
label: target[labelKey],
value: target[valueKey]
} : undefined;
}).filter(Boolean);
setShowValues(result);
});
return /*#__PURE__*/jsx(TreeModal, _objectSpread2(_objectSpread2({}, otherProps), {}, {
value: showValues,
forceRender: true,
onChange: treeModalChange,
onDataSourceChange: onDataSourceChange,
disabled: disabled,
children: /*#__PURE__*/jsx(Select, {
labelInValue: true,
style: {
width: '100%'
},
placeholder: placeholderFt,
maxTagCount: maxTagCount,
mode: otherProps.isMultiple ? 'multiple' : undefined,
open: false,
value: showValues,
onChange: onSelectChange,
disabled: disabled
})
}));
};
export { TreeModalSelector as T };
//# sourceMappingURL=selector-DhN_bftS.js.map