Skip to content

Commit

Permalink
fix(common): lift compare values from select promises
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyperkid123 committed Mar 21, 2024
1 parent dc745bf commit 23a38f5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
11 changes: 4 additions & 7 deletions packages/common/src/use-select/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const init = ({ propsOptions, optionsTransformer }) => ({
...(optionsTransformer && { originalOptions: propsOptions }),
});

const reducer = (state, { type, payload, options = [], optionsTransformer }) => {
const reducer = (state, { type, payload, options = [], optionsTransformer, compareValues }) => {
switch (type) {
case 'updateOptions':
return {
Expand Down Expand Up @@ -42,14 +42,11 @@ const reducer = (state, { type, payload, options = [], optionsTransformer }) =>
options: optionsTransformer
? optionsTransformer([
...state.options,
...options.filter(({ value }) => !state.options.find((option) => payload.compareValues(option.value, value))),
...options.filter(({ value }) => !state.options.find((option) => compareValues(option.value, value))),

Check warning on line 45 in packages/common/src/use-select/reducer.js

View check run for this annotation

Codecov / codecov/patch

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

Added line #L45 was not covered by tests
])
: [...state.options, ...options.filter(({ value }) => !state.options.find((option) => payload.compareValues(option.value, value)))],
: [...state.options, ...options.filter(({ value }) => !state.options.find((option) => compareValues(option.value, value)))],
...(optionsTransformer && {
originalOptions: [
...state.options,
...options.filter(({ value }) => !state.options.find((option) => payload.compareValues(option.value, value))),
],
originalOptions: [...state.options, ...options.filter(({ value }) => !state.options.find((option) => compareValues(option.value, value)))],

Check warning on line 49 in packages/common/src/use-select/reducer.js

View check run for this annotation

Codecov / codecov/patch

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

Added line #L49 was not covered by tests
}),
};
default:
Expand Down
6 changes: 3 additions & 3 deletions packages/common/src/use-select/use-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const useSelect = ({
}) => {
const [propsOptions, setPropsCache] = useState(initialOptions);
const [state, originalDispatch] = useReducer(reducer, { optionsTransformer, propsOptions: initialOptions }, init);
const dispatch = (action) => originalDispatch({ ...action, optionsTransformer });
const dispatch = (action) => originalDispatch({ ...action, optionsTransformer, compareValues });

useEffect(() => {
if (!isEqual(initialOptions, propsOptions)) {
Expand Down Expand Up @@ -132,14 +132,14 @@ const useSelect = ({

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

loadOptions(inputValue)
.then((options) => {
if (isMounted.current) {
dispatch({
type: 'setPromises',
payload: { [inputValue]: false, compareValues },
payload: { [inputValue]: false },
options,
});
}
Expand Down

0 comments on commit 23a38f5

Please sign in to comment.