useDataEntities.js 1.3 KB

123456789101112131415161718192021222324252627282930
  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 warning from "rc-util/es/warning";
  5. import { isNil } from "../utils/valueUtil";
  6. export default (function (treeData, fieldNames) {
  7. return React.useMemo(function () {
  8. var collection = convertDataToEntities(treeData, {
  9. fieldNames: fieldNames,
  10. initWrapper: function initWrapper(wrapper) {
  11. return _objectSpread(_objectSpread({}, wrapper), {}, {
  12. valueEntities: new Map()
  13. });
  14. },
  15. processEntity: function processEntity(entity, wrapper) {
  16. var val = entity.node[fieldNames.value];
  17. // Check if exist same value
  18. if (process.env.NODE_ENV !== 'production') {
  19. var key = entity.node.key;
  20. warning(!isNil(val), 'TreeNode `value` is invalidate: undefined');
  21. warning(!wrapper.valueEntities.has(val), "Same `value` exist in the tree: ".concat(val));
  22. warning(!key || String(key) === String(val), "`key` or `value` with TreeNode must be the same or you can remove one of them. key: ".concat(key, ", value: ").concat(val, "."));
  23. }
  24. wrapper.valueEntities.set(val, entity);
  25. }
  26. });
  27. return collection;
  28. }, [treeData, fieldNames]);
  29. });