1 | 'use strict';
|
2 |
|
3 | var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
4 | var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
5 | var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
|
6 | var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
|
7 | var React = require('react');
|
8 | var index = require('./index-665c4ed8.cjs.prod.js');
|
9 |
|
10 | var _excluded = ["defaultOptions", "cacheOptions", "loadOptions", "options", "isLoading", "onInputChange", "filterOption"];
|
11 | function useAsync(_ref) {
|
12 | var _ref$defaultOptions = _ref.defaultOptions,
|
13 | propsDefaultOptions = _ref$defaultOptions === void 0 ? false : _ref$defaultOptions,
|
14 | _ref$cacheOptions = _ref.cacheOptions,
|
15 | cacheOptions = _ref$cacheOptions === void 0 ? false : _ref$cacheOptions,
|
16 | propsLoadOptions = _ref.loadOptions;
|
17 | _ref.options;
|
18 | var _ref$isLoading = _ref.isLoading,
|
19 | propsIsLoading = _ref$isLoading === void 0 ? false : _ref$isLoading,
|
20 | propsOnInputChange = _ref.onInputChange,
|
21 | _ref$filterOption = _ref.filterOption,
|
22 | filterOption = _ref$filterOption === void 0 ? null : _ref$filterOption,
|
23 | restSelectProps = _objectWithoutProperties(_ref, _excluded);
|
24 | var propsInputValue = restSelectProps.inputValue;
|
25 | var lastRequest = React.useRef(undefined);
|
26 | var mounted = React.useRef(false);
|
27 | var _useState = React.useState(Array.isArray(propsDefaultOptions) ? propsDefaultOptions : undefined),
|
28 | _useState2 = _slicedToArray(_useState, 2),
|
29 | defaultOptions = _useState2[0],
|
30 | setDefaultOptions = _useState2[1];
|
31 | var _useState3 = React.useState(typeof propsInputValue !== 'undefined' ? propsInputValue : ''),
|
32 | _useState4 = _slicedToArray(_useState3, 2),
|
33 | stateInputValue = _useState4[0],
|
34 | setStateInputValue = _useState4[1];
|
35 | var _useState5 = React.useState(propsDefaultOptions === true),
|
36 | _useState6 = _slicedToArray(_useState5, 2),
|
37 | isLoading = _useState6[0],
|
38 | setIsLoading = _useState6[1];
|
39 | var _useState7 = React.useState(undefined),
|
40 | _useState8 = _slicedToArray(_useState7, 2),
|
41 | loadedInputValue = _useState8[0],
|
42 | setLoadedInputValue = _useState8[1];
|
43 | var _useState9 = React.useState([]),
|
44 | _useState10 = _slicedToArray(_useState9, 2),
|
45 | loadedOptions = _useState10[0],
|
46 | setLoadedOptions = _useState10[1];
|
47 | var _useState11 = React.useState(false),
|
48 | _useState12 = _slicedToArray(_useState11, 2),
|
49 | passEmptyOptions = _useState12[0],
|
50 | setPassEmptyOptions = _useState12[1];
|
51 | var _useState13 = React.useState({}),
|
52 | _useState14 = _slicedToArray(_useState13, 2),
|
53 | optionsCache = _useState14[0],
|
54 | setOptionsCache = _useState14[1];
|
55 | var _useState15 = React.useState(undefined),
|
56 | _useState16 = _slicedToArray(_useState15, 2),
|
57 | prevDefaultOptions = _useState16[0],
|
58 | setPrevDefaultOptions = _useState16[1];
|
59 | var _useState17 = React.useState(undefined),
|
60 | _useState18 = _slicedToArray(_useState17, 2),
|
61 | prevCacheOptions = _useState18[0],
|
62 | setPrevCacheOptions = _useState18[1];
|
63 | if (cacheOptions !== prevCacheOptions) {
|
64 | setOptionsCache({});
|
65 | setPrevCacheOptions(cacheOptions);
|
66 | }
|
67 | if (propsDefaultOptions !== prevDefaultOptions) {
|
68 | setDefaultOptions(Array.isArray(propsDefaultOptions) ? propsDefaultOptions : undefined);
|
69 | setPrevDefaultOptions(propsDefaultOptions);
|
70 | }
|
71 | React.useEffect(function () {
|
72 | mounted.current = true;
|
73 | return function () {
|
74 | mounted.current = false;
|
75 | };
|
76 | }, []);
|
77 | var loadOptions = React.useCallback(function (inputValue, callback) {
|
78 | if (!propsLoadOptions) return callback();
|
79 | var loader = propsLoadOptions(inputValue, callback);
|
80 | if (loader && typeof loader.then === 'function') {
|
81 | loader.then(callback, function () {
|
82 | return callback();
|
83 | });
|
84 | }
|
85 | }, [propsLoadOptions]);
|
86 | React.useEffect(function () {
|
87 | if (propsDefaultOptions === true) {
|
88 | loadOptions(stateInputValue, function (options) {
|
89 | if (!mounted.current) return;
|
90 | setDefaultOptions(options || []);
|
91 | setIsLoading(!!lastRequest.current);
|
92 | });
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 | }, []);
|
98 | var onInputChange = React.useCallback(function (newValue, actionMeta) {
|
99 | var inputValue = index.handleInputChange(newValue, actionMeta, propsOnInputChange);
|
100 | if (!inputValue) {
|
101 | lastRequest.current = undefined;
|
102 | setStateInputValue('');
|
103 | setLoadedInputValue('');
|
104 | setLoadedOptions([]);
|
105 | setIsLoading(false);
|
106 | setPassEmptyOptions(false);
|
107 | return;
|
108 | }
|
109 | if (cacheOptions && optionsCache[inputValue]) {
|
110 | setStateInputValue(inputValue);
|
111 | setLoadedInputValue(inputValue);
|
112 | setLoadedOptions(optionsCache[inputValue]);
|
113 | setIsLoading(false);
|
114 | setPassEmptyOptions(false);
|
115 | } else {
|
116 | var request = lastRequest.current = {};
|
117 | setStateInputValue(inputValue);
|
118 | setIsLoading(true);
|
119 | setPassEmptyOptions(!loadedInputValue);
|
120 | loadOptions(inputValue, function (options) {
|
121 | if (!mounted) return;
|
122 | if (request !== lastRequest.current) return;
|
123 | lastRequest.current = undefined;
|
124 | setIsLoading(false);
|
125 | setLoadedInputValue(inputValue);
|
126 | setLoadedOptions(options || []);
|
127 | setPassEmptyOptions(false);
|
128 | setOptionsCache(options ? _objectSpread(_objectSpread({}, optionsCache), {}, _defineProperty({}, inputValue, options)) : optionsCache);
|
129 | });
|
130 | }
|
131 | }, [cacheOptions, loadOptions, loadedInputValue, optionsCache, propsOnInputChange]);
|
132 | var options = passEmptyOptions ? [] : stateInputValue && loadedInputValue ? loadedOptions : defaultOptions || [];
|
133 | return _objectSpread(_objectSpread({}, restSelectProps), {}, {
|
134 | options: options,
|
135 | isLoading: isLoading || propsIsLoading,
|
136 | onInputChange: onInputChange,
|
137 | filterOption: filterOption
|
138 | });
|
139 | }
|
140 |
|
141 | exports.useAsync = useAsync;
|