linkmore-design
Version:
🌈 🚀lm组件库。🚀
132 lines (126 loc) • 3.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
// 操作方法集合
const CoreOptions = ({
state,
dispatch,
props
}) => {
const {
checkboxConfig
} = state;
const {
dataSource,
cellKey,
checkboxChange,
selectionConfig
} = props;
const {
checkKeys,
checkValues,
checkMethod
} = checkboxConfig;
// 设置选中的值: 第一个参数是数据的键,第二个参数是选中与否, 第三个参数控制是否重置
// 未做key是否属于数据源校验,用于分页时保存之前的选中状态
const setCheckboxKeys = (keys, checked, reset) => {
const flag = typeof checked === 'boolean' ? checked : true;
let nKeys = keys;
let nValues = [];
if (reset) {
nValues = dataSource?.filter(v => nKeys.includes(v[cellKey])) || [];
dispatch({
type: 'changeCheckboxConfig',
checkboxConfig: {
checkKeys: nKeys,
checkValues: nValues
}
});
return;
}
if (flag) {
nKeys = Array.from(new Set([...checkKeys, ...keys]));
nValues = dataSource?.filter(v => nKeys.includes(v[cellKey])) || [];
} else {
nKeys = checkKeys.filter(v => !keys.includes(v));
nValues = checkValues.filter(v => !keys.includes(v[cellKey]));
}
dispatch({
type: 'changeCheckboxConfig',
checkboxConfig: {
checkKeys: nKeys,
checkValues: nValues
}
});
dispatch({
type: 'changeRadioConfig',
radioConfig: {
checkKey: '',
checkValue: ''
}
});
};
// 切换选中状态: 单个切换/批量切换
const toggleCheckboxKey = (k, options = {}) => {
const {
trigger = 'default',
cell,
cellIndex
} = options;
let nKeys = [k];
let isCheck = true;
let reset = false;
// 单个切换
if (!Array.isArray(k)) {
let isCanCheck = checkMethod; // 是否可选中
isCheck = !checkKeys?.some(v => v === k);
let record = cell || '';
// 检测选中前置阻挡事件
if (typeof checkMethod === 'function') {
record = record || dataSource?.find(v => v[cellKey] === k);
isCanCheck = checkMethod(record, state, trigger);
}
if (!isCanCheck) return;
// 触发复选框改变事件: 仅手动触发有效
if (typeof checkboxChange === 'function') {
record = record || dataSource?.find(v => v[cellKey] === k);
checkboxChange?.({
checked: isCheck,
cell: record,
cellIndex
});
}
}
// 多个切换 通过重置去除选中项保留切换项
if (Array.isArray(k)) {
const checkArr = checkKeys.filter(v => !k.includes(v)); // 选中的值
const notCheckArr = k.filter(v => !checkKeys.includes(v)); // 未选中的值
nKeys = [...notCheckArr, ...checkArr]; // 组合新值为选中状态
reset = true;
}
setCheckboxKeys(nKeys, isCheck, reset);
};
const cellClick = (cell, index) => {
if (!checkKeys.length) {
// 校验是否存在单选
const isRadio = Array.isArray(selectionConfig.type) ? selectionConfig.type.includes('radio') : selectionConfig.type === 'radio';
isRadio && dispatch({
type: 'changeRadioConfig',
radioConfig: {
checkKey: cell[cellKey],
checkValue: cell
}
});
}
props.cellClick?.(cell, index);
};
return {
setCheckboxKeys,
toggleCheckboxKey,
cellClick
};
};
var _default = CoreOptions;
exports.default = _default;