useOptions.js 847 B

12345678910111213141516171819202122
  1. import * as React from 'react';
  2. import useEntities from "./useEntities";
  3. export default function useOptions(mergedFieldNames, options) {
  4. var mergedOptions = React.useMemo(function () {
  5. return options || [];
  6. }, [options]);
  7. // Only used in multiple mode, this fn will not call in single mode
  8. var getPathKeyEntities = useEntities(mergedOptions, mergedFieldNames);
  9. /** Convert path key back to value format */
  10. var getValueByKeyPath = React.useCallback(function (pathKeys) {
  11. var keyPathEntities = getPathKeyEntities();
  12. return pathKeys.map(function (pathKey) {
  13. var nodes = keyPathEntities[pathKey].nodes;
  14. return nodes.map(function (node) {
  15. return node[mergedFieldNames.value];
  16. });
  17. });
  18. }, [getPathKeyEntities, mergedFieldNames]);
  19. return [mergedOptions, getPathKeyEntities, getValueByKeyPath];
  20. }