You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
InternshipProject/node_modules/rc-select/lib/hooks/useCache.js

57 lines
1.9 KiB

"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var React = _interopRequireWildcard(require("react"));
/**
* Cache `value` related LabeledValue & options.
*/
var _default = function _default(labeledValues, valueOptions) {
var cacheRef = React.useRef({
values: new Map(),
options: new Map()
});
var filledLabeledValues = React.useMemo(function () {
var _cacheRef$current = cacheRef.current,
prevValueCache = _cacheRef$current.values,
prevOptionCache = _cacheRef$current.options; // Fill label by cache
var patchedValues = labeledValues.map(function (item) {
if (item.label === undefined) {
var _prevValueCache$get;
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, item), {}, {
label: (_prevValueCache$get = prevValueCache.get(item.value)) === null || _prevValueCache$get === void 0 ? void 0 : _prevValueCache$get.label
});
}
return item;
}); // Refresh cache
var valueCache = new Map();
var optionCache = new Map();
patchedValues.forEach(function (item) {
valueCache.set(item.value, item);
optionCache.set(item.value, valueOptions.get(item.value) || prevOptionCache.get(item.value));
});
cacheRef.current.values = valueCache;
cacheRef.current.options = optionCache;
return patchedValues;
}, [labeledValues, valueOptions]);
var getOption = React.useCallback(function (val) {
return valueOptions.get(val) || cacheRef.current.options.get(val);
}, [valueOptions]);
return [filledLabeledValues, getOption];
};
exports.default = _default;