Skip to content

Commit 90f2774

Browse files
authored
Merge pull request #1449 from data-driven-forms/mui-searchable-async-select
Common select fixes
2 parents 271e466 + 23a38f5 commit 90f2774

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

packages/common/src/select/select.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const Select = ({
1212
simpleValue = true,
1313
isMulti,
1414
pluckSingleValue = true,
15-
options: propsOptions = [],
15+
options: propsOptions,
1616
loadOptions,
1717
loadingMessage,
1818
placeholder = 'Choose...',

packages/common/src/use-select/reducer.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const init = ({ propsOptions, optionsTransformer }) => ({
66
...(optionsTransformer && { originalOptions: propsOptions }),
77
});
88

9-
const reducer = (state, { type, payload, options = [], optionsTransformer }) => {
9+
const reducer = (state, { type, payload, options = [], optionsTransformer, compareValues }) => {
1010
switch (type) {
1111
case 'updateOptions':
1212
return {
@@ -42,14 +42,11 @@ const reducer = (state, { type, payload, options = [], optionsTransformer }) =>
4242
options: optionsTransformer
4343
? optionsTransformer([
4444
...state.options,
45-
...options.filter(({ value }) => !state.options.find((option) => payload.compareValues(option.value, value))),
45+
...options.filter(({ value }) => !state.options.find((option) => compareValues(option.value, value))),
4646
])
47-
: [...state.options, ...options.filter(({ value }) => !state.options.find((option) => payload.compareValues(option.value, value)))],
47+
: [...state.options, ...options.filter(({ value }) => !state.options.find((option) => compareValues(option.value, value)))],
4848
...(optionsTransformer && {
49-
originalOptions: [
50-
...state.options,
51-
...options.filter(({ value }) => !state.options.find((option) => payload.compareValues(option.value, value))),
52-
],
49+
originalOptions: [...state.options, ...options.filter(({ value }) => !state.options.find((option) => compareValues(option.value, value)))],
5350
}),
5451
};
5552
default:

packages/common/src/use-select/use-select.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useReducer } from 'react';
1+
import { useEffect, useReducer, useState } from 'react';
22

33
import isEqual from 'lodash/isEqual';
44

@@ -58,7 +58,7 @@ const handleSelectChange = (option, simpleValue, isMulti, onChange, allOptions,
5858
const useSelect = ({
5959
loadOptions,
6060
optionsTransformer,
61-
options: propsOptions,
61+
options: initialOptions = [],
6262
noValueUpdates,
6363
onChange,
6464
value,
@@ -69,8 +69,15 @@ const useSelect = ({
6969
simpleValue,
7070
compareValues,
7171
}) => {
72-
const [state, originalDispatch] = useReducer(reducer, { optionsTransformer, propsOptions }, init);
73-
const dispatch = (action) => originalDispatch({ ...action, optionsTransformer });
72+
const [propsOptions, setPropsCache] = useState(initialOptions);
73+
const [state, originalDispatch] = useReducer(reducer, { optionsTransformer, propsOptions: initialOptions }, init);
74+
const dispatch = (action) => originalDispatch({ ...action, optionsTransformer, compareValues });
75+
76+
useEffect(() => {
77+
if (!isEqual(initialOptions, propsOptions)) {
78+
setPropsCache(initialOptions);
79+
}
80+
}, [initialOptions]);
7481

7582
const isMounted = useIsMounted();
7683

@@ -114,7 +121,7 @@ const useSelect = ({
114121
}, [loadOptionsStr, loadOptionsChangeCounter]);
115122

116123
useEffect(() => {
117-
if (state.isInitialLoaded) {
124+
if (!isEqual(state.options, propsOptions) && state.isInitialLoaded) {
118125
if (!noValueUpdates && value && !propsOptions.map(({ value }) => value).includes(value)) {
119126
onChange(undefined);
120127
}
@@ -125,14 +132,14 @@ const useSelect = ({
125132

126133
const onInputChange = (inputValue) => {
127134
if (inputValue && loadOptions && state.promises[inputValue] === undefined && isSearchable) {
128-
dispatch({ type: 'setPromises', payload: { [inputValue]: true, compareValues } });
135+
dispatch({ type: 'setPromises', payload: { [inputValue]: true } });
129136

130137
loadOptions(inputValue)
131138
.then((options) => {
132139
if (isMounted.current) {
133140
dispatch({
134141
type: 'setPromises',
135-
payload: { [inputValue]: false, compareValues },
142+
payload: { [inputValue]: false },
136143
options,
137144
});
138145
}

0 commit comments

Comments
 (0)