UNPKG

linkmore-design

Version:

🌈 🚀lm组件库。🚀

138 lines (133 loc) 4.88 kB
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; // 操作方法集合 var CoreOptions = function CoreOptions(_ref) { var state = _ref.state, dispatch = _ref.dispatch, props = _ref.props; var checkboxConfig = state.checkboxConfig; var dataSource = props.dataSource, cellKey = props.cellKey, checkboxChange = props.checkboxChange, selectionConfig = props.selectionConfig; var checkKeys = checkboxConfig.checkKeys, checkValues = checkboxConfig.checkValues, checkMethod = checkboxConfig.checkMethod; // 设置选中的值: 第一个参数是数据的键,第二个参数是选中与否, 第三个参数控制是否重置 // 未做key是否属于数据源校验,用于分页时保存之前的选中状态 var setCheckboxKeys = function setCheckboxKeys(keys, checked, reset) { var flag = typeof checked === 'boolean' ? checked : true; var nKeys = keys; var nValues = []; if (reset) { nValues = (dataSource === null || dataSource === void 0 ? void 0 : dataSource.filter(function (v) { return nKeys.includes(v[cellKey]); })) || []; dispatch({ type: 'changeCheckboxConfig', checkboxConfig: { checkKeys: nKeys, checkValues: nValues } }); return; } if (flag) { nKeys = Array.from(new Set([].concat(_toConsumableArray(checkKeys), _toConsumableArray(keys)))); nValues = (dataSource === null || dataSource === void 0 ? void 0 : dataSource.filter(function (v) { return nKeys.includes(v[cellKey]); })) || []; } else { nKeys = checkKeys.filter(function (v) { return !keys.includes(v); }); nValues = checkValues.filter(function (v) { return !keys.includes(v[cellKey]); }); } dispatch({ type: 'changeCheckboxConfig', checkboxConfig: { checkKeys: nKeys, checkValues: nValues } }); dispatch({ type: 'changeRadioConfig', radioConfig: { checkKey: '', checkValue: '' } }); }; // 切换选中状态: 单个切换/批量切换 var toggleCheckboxKey = function toggleCheckboxKey(k) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var _options$trigger = options.trigger, trigger = _options$trigger === void 0 ? 'default' : _options$trigger, cell = options.cell, cellIndex = options.cellIndex; var nKeys = [k]; var isCheck = true; var reset = false; // 单个切换 if (!Array.isArray(k)) { var isCanCheck = checkMethod; // 是否可选中 isCheck = !(checkKeys !== null && checkKeys !== void 0 && checkKeys.some(function (v) { return v === k; })); var record = cell || ''; // 检测选中前置阻挡事件 if (typeof checkMethod === 'function') { record = record || (dataSource === null || dataSource === void 0 ? void 0 : dataSource.find(function (v) { return v[cellKey] === k; })); isCanCheck = checkMethod(record, state, trigger); } if (!isCanCheck) return; // 触发复选框改变事件: 仅手动触发有效 if (typeof checkboxChange === 'function') { record = record || (dataSource === null || dataSource === void 0 ? void 0 : dataSource.find(function (v) { return v[cellKey] === k; })); checkboxChange === null || checkboxChange === void 0 ? void 0 : checkboxChange({ checked: isCheck, cell: record, cellIndex: cellIndex }); } } // 多个切换 通过重置去除选中项保留切换项 if (Array.isArray(k)) { var checkArr = checkKeys.filter(function (v) { return !k.includes(v); }); // 选中的值 var notCheckArr = k.filter(function (v) { return !checkKeys.includes(v); }); // 未选中的值 nKeys = [].concat(_toConsumableArray(notCheckArr), _toConsumableArray(checkArr)); // 组合新值为选中状态 reset = true; } setCheckboxKeys(nKeys, isCheck, reset); }; var cellClick = function cellClick(cell, index) { var _props$cellClick; if (!checkKeys.length) { // 校验是否存在单选 var isRadio = Array.isArray(selectionConfig.type) ? selectionConfig.type.includes('radio') : selectionConfig.type === 'radio'; isRadio && dispatch({ type: 'changeRadioConfig', radioConfig: { checkKey: cell[cellKey], checkValue: cell } }); } (_props$cellClick = props.cellClick) === null || _props$cellClick === void 0 ? void 0 : _props$cellClick.call(props, cell, index); }; return { setCheckboxKeys: setCheckboxKeys, toggleCheckboxKey: toggleCheckboxKey, cellClick: cellClick }; }; export default CoreOptions;