useEntities.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import * as React from 'react';
  3. import { convertDataToEntities } from "rc-tree/es/utils/treeUtil";
  4. import { VALUE_SPLIT } from "../utils/commonUtil";
  5. /** Lazy parse options data into conduct-able info to avoid perf issue in single mode */
  6. export default (function (options, fieldNames) {
  7. var cacheRef = React.useRef({
  8. options: [],
  9. info: {
  10. keyEntities: {},
  11. pathKeyEntities: {}
  12. }
  13. });
  14. var getEntities = React.useCallback(function () {
  15. if (cacheRef.current.options !== options) {
  16. cacheRef.current.options = options;
  17. cacheRef.current.info = convertDataToEntities(options, {
  18. fieldNames: fieldNames,
  19. initWrapper: function initWrapper(wrapper) {
  20. return _objectSpread(_objectSpread({}, wrapper), {}, {
  21. pathKeyEntities: {}
  22. });
  23. },
  24. processEntity: function processEntity(entity, wrapper) {
  25. var pathKey = entity.nodes.map(function (node) {
  26. return node[fieldNames.value];
  27. }).join(VALUE_SPLIT);
  28. wrapper.pathKeyEntities[pathKey] = entity;
  29. // Overwrite origin key.
  30. // this is very hack but we need let conduct logic work with connect path
  31. entity.key = pathKey;
  32. }
  33. });
  34. }
  35. return cacheRef.current.info.pathKeyEntities;
  36. }, [fieldNames, options]);
  37. return getEntities;
  38. });