1 | 'use strict';
|
2 |
|
3 | var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
4 | var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
|
5 | var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
|
6 | var React = require('react');
|
7 | var index = require('./index-d1cb43f3.cjs.dev.js');
|
8 | var Select = require('./Select-d63eed7b.cjs.dev.js');
|
9 |
|
10 | var _excluded = ["allowCreateWhileLoading", "createOptionPosition", "formatCreateLabel", "isValidNewOption", "getNewOptionData", "onCreateOption", "options", "onChange"];
|
11 | var compareOption = function compareOption() {
|
12 | var inputValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
13 | var option = arguments.length > 1 ? arguments[1] : undefined;
|
14 | var accessors = arguments.length > 2 ? arguments[2] : undefined;
|
15 | var candidate = String(inputValue).toLowerCase();
|
16 | var optionValue = String(accessors.getOptionValue(option)).toLowerCase();
|
17 | var optionLabel = String(accessors.getOptionLabel(option)).toLowerCase();
|
18 | return optionValue === candidate || optionLabel === candidate;
|
19 | };
|
20 | var builtins = {
|
21 | formatCreateLabel: function formatCreateLabel(inputValue) {
|
22 | return "Create \"".concat(inputValue, "\"");
|
23 | },
|
24 | isValidNewOption: function isValidNewOption(inputValue, selectValue, selectOptions, accessors) {
|
25 | return !(!inputValue || selectValue.some(function (option) {
|
26 | return compareOption(inputValue, option, accessors);
|
27 | }) || selectOptions.some(function (option) {
|
28 | return compareOption(inputValue, option, accessors);
|
29 | }));
|
30 | },
|
31 | getNewOptionData: function getNewOptionData(inputValue, optionLabel) {
|
32 | return {
|
33 | label: optionLabel,
|
34 | value: inputValue,
|
35 | __isNew__: true
|
36 | };
|
37 | }
|
38 | };
|
39 | function useCreatable(_ref) {
|
40 | var _ref$allowCreateWhile = _ref.allowCreateWhileLoading,
|
41 | allowCreateWhileLoading = _ref$allowCreateWhile === void 0 ? false : _ref$allowCreateWhile,
|
42 | _ref$createOptionPosi = _ref.createOptionPosition,
|
43 | createOptionPosition = _ref$createOptionPosi === void 0 ? 'last' : _ref$createOptionPosi,
|
44 | _ref$formatCreateLabe = _ref.formatCreateLabel,
|
45 | formatCreateLabel = _ref$formatCreateLabe === void 0 ? builtins.formatCreateLabel : _ref$formatCreateLabe,
|
46 | _ref$isValidNewOption = _ref.isValidNewOption,
|
47 | isValidNewOption = _ref$isValidNewOption === void 0 ? builtins.isValidNewOption : _ref$isValidNewOption,
|
48 | _ref$getNewOptionData = _ref.getNewOptionData,
|
49 | getNewOptionData = _ref$getNewOptionData === void 0 ? builtins.getNewOptionData : _ref$getNewOptionData,
|
50 | onCreateOption = _ref.onCreateOption,
|
51 | _ref$options = _ref.options,
|
52 | propsOptions = _ref$options === void 0 ? [] : _ref$options,
|
53 | propsOnChange = _ref.onChange,
|
54 | restSelectProps = _objectWithoutProperties(_ref, _excluded);
|
55 | var _restSelectProps$getO = restSelectProps.getOptionValue,
|
56 | getOptionValue = _restSelectProps$getO === void 0 ? Select.getOptionValue : _restSelectProps$getO,
|
57 | _restSelectProps$getO2 = restSelectProps.getOptionLabel,
|
58 | getOptionLabel = _restSelectProps$getO2 === void 0 ? Select.getOptionLabel : _restSelectProps$getO2,
|
59 | inputValue = restSelectProps.inputValue,
|
60 | isLoading = restSelectProps.isLoading,
|
61 | isMulti = restSelectProps.isMulti,
|
62 | value = restSelectProps.value,
|
63 | name = restSelectProps.name;
|
64 | var newOption = React.useMemo(function () {
|
65 | return isValidNewOption(inputValue, index.cleanValue(value), propsOptions, {
|
66 | getOptionValue: getOptionValue,
|
67 | getOptionLabel: getOptionLabel
|
68 | }) ? getNewOptionData(inputValue, formatCreateLabel(inputValue)) : undefined;
|
69 | }, [formatCreateLabel, getNewOptionData, getOptionLabel, getOptionValue, inputValue, isValidNewOption, propsOptions, value]);
|
70 | var options = React.useMemo(function () {
|
71 | return (allowCreateWhileLoading || !isLoading) && newOption ? createOptionPosition === 'first' ? [newOption].concat(_toConsumableArray(propsOptions)) : [].concat(_toConsumableArray(propsOptions), [newOption]) : propsOptions;
|
72 | }, [allowCreateWhileLoading, createOptionPosition, isLoading, newOption, propsOptions]);
|
73 | var onChange = React.useCallback(function (newValue, actionMeta) {
|
74 | if (actionMeta.action !== 'select-option') {
|
75 | return propsOnChange(newValue, actionMeta);
|
76 | }
|
77 | var valueArray = Array.isArray(newValue) ? newValue : [newValue];
|
78 | if (valueArray[valueArray.length - 1] === newOption) {
|
79 | if (onCreateOption) onCreateOption(inputValue);else {
|
80 | var newOptionData = getNewOptionData(inputValue, inputValue);
|
81 | var newActionMeta = {
|
82 | action: 'create-option',
|
83 | name: name,
|
84 | option: newOptionData
|
85 | };
|
86 | propsOnChange(index.valueTernary(isMulti, [].concat(_toConsumableArray(index.cleanValue(value)), [newOptionData]), newOptionData), newActionMeta);
|
87 | }
|
88 | return;
|
89 | }
|
90 | propsOnChange(newValue, actionMeta);
|
91 | }, [getNewOptionData, inputValue, isMulti, name, newOption, onCreateOption, propsOnChange, value]);
|
92 | return _objectSpread(_objectSpread({}, restSelectProps), {}, {
|
93 | options: options,
|
94 | onChange: onChange
|
95 | });
|
96 | }
|
97 |
|
98 | exports.useCreatable = useCreatable;
|