Skip to content

Commit 8598057

Browse files
committed
feat(common): allow to flat options
1 parent 1f431a3 commit 8598057

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

packages/common/src/select/reducer.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
export const flatOptions = (options) => options.flatMap((option) => (option.options ? [{ group: option.label }, ...option.options] : [option]));
2+
13
const reducer = (state, { type, payload, options = [] }) => {
24
switch (type) {
35
case 'updateOptions':
46
return {
57
...state,
6-
options: payload,
8+
options: state.useFlatOptions ? flatOptions(payload) : payload,
79
isLoading: false,
810
promises: {},
911
};
@@ -15,7 +17,7 @@ const reducer = (state, { type, payload, options = [] }) => {
1517
case 'setOptions':
1618
return {
1719
...state,
18-
options: payload,
20+
options: state.useFlatOptions ? flatOptions(payload) : payload,
1921
};
2022
case 'initialLoaded':
2123
return {
@@ -29,7 +31,9 @@ const reducer = (state, { type, payload, options = [] }) => {
2931
...state.promises,
3032
...payload,
3133
},
32-
options: [...state.options, ...options.filter(({ value }) => !state.options.find((option) => option.value === value))],
34+
options: state.useFlatOptions
35+
? flatOptions([...state.options, ...options.filter(({ value }) => !state.options.find((option) => option.value === value))])
36+
: [...state.options, ...options.filter(({ value }) => !state.options.find((option) => option.value === value))],
3337
};
3438
default:
3539
return state;

packages/common/src/select/select.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface SelectProps {
2828
isSearchable?: boolean;
2929
SelectComponent?: React.ComponentType;
3030
noValueUpdates?: boolean;
31+
useFlatOptions?: boolean;
3132
}
3233

3334
declare const Select: React.ComponentType<SelectProps>;

packages/common/src/select/select.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import PropTypes from 'prop-types';
55
import clsx from 'clsx';
66
import isEqual from 'lodash/isEqual';
77
import fnToString from '../utils/fn-to-string';
8-
import reducer from './reducer';
8+
import reducer, { flatOptions } from './reducer';
99
import useIsMounted from '../hooks/use-is-mounted';
1010

1111
const getSelectValue = (stateValue, simpleValue, isMulti, allOptions) => {
@@ -73,11 +73,13 @@ const Select = ({
7373
loadOptionsChangeCounter,
7474
SelectComponent,
7575
noValueUpdates,
76+
useFlatOptions,
7677
...props
7778
}) => {
7879
const [state, dispatch] = useReducer(reducer, {
7980
isLoading: false,
80-
options: propsOptions,
81+
options: useFlatOptions ? flatOptions(propsOptions) : propsOptions,
82+
useFlatOptions,
8183
promises: {},
8284
isInitialLoaded: false,
8385
});
@@ -220,6 +222,7 @@ Select.propTypes = {
220222
isSearchable: PropTypes.bool,
221223
SelectComponent: PropTypes.elementType.isRequired,
222224
noValueUpdates: PropTypes.bool,
225+
useFlatOptions: PropTypes.bool,
223226
};
224227

225228
Select.defaultProps = {

0 commit comments

Comments
 (0)