@ccs-design/rc-pro
Version:
ccs design basic react component
69 lines (60 loc) • 2.02 kB
JavaScript
import { isArray } from 'lodash';
import { useEffect, useRef } from 'react';
import { showWarning } from './utils';
/**
* table多选获取值
* @param options { value, valueFields, dataFields }
* @returns
*/
export default function useMultiSelection(_ref) {
var value = _ref.value,
_ref$valueFields = _ref.valueFields,
valueFields = _ref$valueFields === void 0 ? [] : _ref$valueFields,
_ref$dataFields = _ref.dataFields,
dataFields = _ref$dataFields === void 0 ? [] : _ref$dataFields;
var ref = useRef(null);
useEffect(function () {
ref.current = value;
}, []);
var onRowSelected = function onRowSelected(selectedKeys, selectedRows) {
if (!valueFields || !valueFields.length || !dataFields || !dataFields.length) return {};
if (dataFields.length !== valueFields.length) {
showWarning(true, 'dataFields 和 valueFields 长度不一致。');
return {};
}
var dataNames = [];
for (var i = 0; i < valueFields.length; i += 1) {
dataNames.push([]);
}
selectedKeys.forEach(function (k) {
var h = selectedRows.find(function (r) {
return r && r[dataFields[0]] === k;
});
if (h) {
dataFields.forEach(function (v, i) {
dataNames[i].push(h[v]);
});
} else {
var ids = ref.current[valueFields[0]];
var index = ids === null || ids === void 0 ? void 0 : ids.findIndex(function (d) {
return d === k;
});
if (index !== undefined) {
valueFields.forEach(function (v, i) {
var names = ref.current[v];
var name = isArray(names) ? names[index] : names === null || names === void 0 ? void 0 : names.split(',')[index];
dataNames[i].push(name);
});
}
}
});
var result = {};
valueFields.forEach(function (v, i) {
result[v] = i === 0 ? dataNames[i] : dataNames[i].toString();
});
return result;
};
return {
onRowSelected: onRowSelected
};
}