useSelect.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  2. import { conductCheck } from "rc-tree/es/utils/conductUtil";
  3. import { toPathKey, toPathKeys } from "../utils/commonUtil";
  4. import { formatStrategyValues } from "../utils/treeUtil";
  5. export default function useSelect(multiple, triggerChange, checkedValues, halfCheckedValues, missingCheckedValues, getPathKeyEntities, getValueByKeyPath, showCheckedStrategy) {
  6. return function (valuePath) {
  7. if (!multiple) {
  8. triggerChange(valuePath);
  9. } else {
  10. // Prepare conduct required info
  11. var pathKey = toPathKey(valuePath);
  12. var checkedPathKeys = toPathKeys(checkedValues);
  13. var halfCheckedPathKeys = toPathKeys(halfCheckedValues);
  14. var existInChecked = checkedPathKeys.includes(pathKey);
  15. var existInMissing = missingCheckedValues.some(function (valueCells) {
  16. return toPathKey(valueCells) === pathKey;
  17. });
  18. // Do update
  19. var nextCheckedValues = checkedValues;
  20. var nextMissingValues = missingCheckedValues;
  21. if (existInMissing && !existInChecked) {
  22. // Missing value only do filter
  23. nextMissingValues = missingCheckedValues.filter(function (valueCells) {
  24. return toPathKey(valueCells) !== pathKey;
  25. });
  26. } else {
  27. // Update checked key first
  28. var nextRawCheckedKeys = existInChecked ? checkedPathKeys.filter(function (key) {
  29. return key !== pathKey;
  30. }) : [].concat(_toConsumableArray(checkedPathKeys), [pathKey]);
  31. var pathKeyEntities = getPathKeyEntities();
  32. // Conduction by selected or not
  33. var checkedKeys;
  34. if (existInChecked) {
  35. var _conductCheck = conductCheck(nextRawCheckedKeys, {
  36. checked: false,
  37. halfCheckedKeys: halfCheckedPathKeys
  38. }, pathKeyEntities);
  39. checkedKeys = _conductCheck.checkedKeys;
  40. } else {
  41. var _conductCheck2 = conductCheck(nextRawCheckedKeys, true, pathKeyEntities);
  42. checkedKeys = _conductCheck2.checkedKeys;
  43. }
  44. // Roll up to parent level keys
  45. var deDuplicatedKeys = formatStrategyValues(checkedKeys, getPathKeyEntities, showCheckedStrategy);
  46. nextCheckedValues = getValueByKeyPath(deDuplicatedKeys);
  47. }
  48. triggerChange([].concat(_toConsumableArray(nextMissingValues), _toConsumableArray(nextCheckedValues)));
  49. }
  50. };
  51. }