Skip to content

Commit 04e124a

Browse files
committed
fix(common): change useFlatOptions to optionsTransformer
- for more generic usage
1 parent 676467e commit 04e124a

File tree

8 files changed

+63
-19
lines changed

8 files changed

+63
-19
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { ReactNode } from "react";
2+
3+
interface Option {
4+
label: string | ReactNode;
5+
value: any;
6+
selectAll?: boolean;
7+
selectNone?: boolean;
8+
}
9+
10+
interface ResultedOption {
11+
label?: string | ReactNode;
12+
value?: any;
13+
selectAll?: boolean;
14+
selectNone?: boolean;
15+
group?: string | ReactNode;
16+
divider?: boolean;
17+
}
18+
19+
20+
interface Options {
21+
label?: string | ReactNode;
22+
value?: any;
23+
divider?: boolean;
24+
selectAll?: boolean;
25+
selectNone?: boolean;
26+
options?: Option[];
27+
}
28+
29+
declare const flatOptions: (options: Options[]) => ResultedOption[];
30+
31+
export default flatOptions;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const flatOptions = (options) => options.flatMap((option) => (option.options ? [{ group: option.label }, ...option.options] : [option]));
2+
3+
export default flatOptions;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { default } from './select';
22
export * from './select';
33
export { default as parseInternalValue } from './parse-internal-value';
4+
export { default as flatOptions } from './flat-options';

packages/common/src/select/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { default } from './select';
22
export { default as parseInternalValue } from './parse-internal-value';
3+
export { default as flatOptions } from './flat-options';

packages/common/src/select/reducer.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
export const flatOptions = (options) => options.flatMap((option) => (option.options ? [{ group: option.label }, ...option.options] : [option]));
1+
export const init = ({ propsOptions, optionsTransformer }) => ({
2+
isLoading: false,
3+
options: optionsTransformer ? optionsTransformer(propsOptions) : propsOptions,
4+
promises: {},
5+
isInitialLoaded: false,
6+
});
27

3-
const reducer = (state, { type, payload, options = [] }) => {
8+
const reducer = (state, { type, payload, options = [], optionsTransformer }) => {
49
switch (type) {
510
case 'updateOptions':
611
return {
712
...state,
8-
options: state.useFlatOptions ? flatOptions(payload) : payload,
13+
options: optionsTransformer ? optionsTransformer(payload) : payload,
914
isLoading: false,
1015
promises: {},
1116
};
@@ -17,7 +22,7 @@ const reducer = (state, { type, payload, options = [] }) => {
1722
case 'setOptions':
1823
return {
1924
...state,
20-
options: state.useFlatOptions ? flatOptions(payload) : payload,
25+
options: optionsTransformer ? optionsTransformer(payload) : payload,
2126
};
2227
case 'initialLoaded':
2328
return {
@@ -31,8 +36,8 @@ const reducer = (state, { type, payload, options = [] }) => {
3136
...state.promises,
3237
...payload,
3338
},
34-
options: state.useFlatOptions
35-
? flatOptions([...state.options, ...options.filter(({ value }) => !state.options.find((option) => option.value === value))])
39+
options: optionsTransformer
40+
? optionsTransformer([...state.options, ...options.filter(({ value }) => !state.options.find((option) => option.value === value))])
3641
: [...state.options, ...options.filter(({ value }) => !state.options.find((option) => option.value === value))],
3742
};
3843
default:

packages/common/src/select/select.js

Lines changed: 5 additions & 10 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, { flatOptions } from './reducer';
8+
import reducer, { init } from './reducer';
99
import useIsMounted from '../hooks/use-is-mounted';
1010

1111
const getSelectValue = (stateValue, simpleValue, isMulti, allOptions) => {
@@ -73,16 +73,11 @@ const Select = ({
7373
loadOptionsChangeCounter,
7474
SelectComponent,
7575
noValueUpdates,
76-
useFlatOptions,
76+
optionsTransformer,
7777
...props
7878
}) => {
79-
const [state, dispatch] = useReducer(reducer, {
80-
isLoading: false,
81-
options: useFlatOptions ? flatOptions(propsOptions) : propsOptions,
82-
useFlatOptions,
83-
promises: {},
84-
isInitialLoaded: false,
85-
});
79+
const [state, originalDispatch] = useReducer(reducer, { optionsTransformer, propsOptions }, init);
80+
const dispatch = (action) => originalDispatch({ ...action, optionsTransformer });
8681

8782
const isMounted = useIsMounted();
8883

@@ -222,7 +217,7 @@ Select.propTypes = {
222217
isSearchable: PropTypes.bool,
223218
SelectComponent: PropTypes.elementType.isRequired,
224219
noValueUpdates: PropTypes.bool,
225-
useFlatOptions: PropTypes.bool,
220+
optionsTransformer: PropTypes.func,
226221
};
227222

228223
Select.defaultProps = {

packages/pf4-component-mapper/src/select/select.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const Select = (props) => {
1919
id={id || input.name}
2020
FormGroupProps={FormGroupProps}
2121
>
22-
<DataDrivenSelect {...input} {...rest} isDisabled={isDisabled || isReadOnly} useFlatOptions />
22+
<DataDrivenSelect {...input} {...rest} isDisabled={isDisabled || isReadOnly} />
2323
</FormGroup>
2424
);
2525
};

packages/pf4-component-mapper/src/select/select/select.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useRef, useState } from 'react';
22
import PropTypes from 'prop-types';
33

4-
import DataDrivenSelect from '@data-driven-forms/common/select';
4+
import DataDrivenSelect, { flatOptions } from '@data-driven-forms/common/select';
55
import parseInternalValue from '@data-driven-forms/common/select/parse-internal-value';
66
import Downshift from 'downshift';
77
import { CaretDownIcon, CloseIcon, CircleNotchIcon } from '@patternfly/react-icons';
@@ -262,7 +262,15 @@ InternalSelect.propTypes = {
262262
const Select = ({ menuIsPortal, ...props }) => {
263263
const menuPortalTarget = menuIsPortal ? document.body : undefined;
264264

265-
return <DataDrivenSelect SelectComponent={InternalSelect} menuPortalTarget={menuPortalTarget} menuIsPortal={menuIsPortal} {...props} />;
265+
return (
266+
<DataDrivenSelect
267+
SelectComponent={InternalSelect}
268+
menuPortalTarget={menuPortalTarget}
269+
menuIsPortal={menuIsPortal}
270+
{...props}
271+
optionsTransformer={flatOptions}
272+
/>
273+
);
266274
};
267275

268276
Select.propTypes = {

0 commit comments

Comments
 (0)