useSelect.js 2.7 KB

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