Skip to content

Commit 7c5e8f3

Browse files
committed
fix(pf3): update select for newewst common
1 parent 7c2d3cd commit 7c5e8f3

File tree

5 files changed

+1178
-1338
lines changed

5 files changed

+1178
-1338
lines changed

packages/common/src/select/index.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const Select = ({
4040
noOptionsMessage,
4141
value,
4242
onChange,
43+
loadOptionsChangeCounter,
4344
...props
4445
}) => {
4546
const [state, dispatch] = useReducer(reducer, {
@@ -53,12 +54,12 @@ const Select = ({
5354
dispatch({ type: 'startLoading' });
5455

5556
return loadOptions().then((data) => {
56-
if (Array.isArray(value)) {
57+
if (value && Array.isArray(value)) {
5758
const selectValue = value.filter((value) =>
5859
typeof value === 'object' ? data.find((option) => value.value === option.value) : data.find((option) => value === option.value)
5960
);
6061
onChange(selectValue.length === 0 ? undefined : selectValue);
61-
} else if (!data.find(({ value: internalValue }) => internalValue === value)) {
62+
} else if (value && !data.find(({ value: internalValue }) => internalValue === value)) {
6263
onChange(undefined);
6364
}
6465

@@ -84,11 +85,11 @@ const Select = ({
8485
if (loadOptionsStr && state.isMounted) {
8586
updateOptions();
8687
}
87-
}, [loadOptionsStr]);
88+
}, [loadOptionsStr, loadOptionsChangeCounter]);
8889

8990
useEffect(() => {
9091
if (state.isMounted) {
91-
if (!propsOptions.map(({ value }) => value).includes(value)) {
92+
if (value && !propsOptions.map(({ value }) => value).includes(value)) {
9293
onChange(undefined);
9394
}
9495

@@ -97,11 +98,20 @@ const Select = ({
9798
}, [propsOptions]);
9899

99100
if (state.isLoading) {
100-
return <ReactSelect isDisabled={true} placeholder={loadingMessage} options={state.options} {...loadingProps} />;
101+
return (
102+
<ReactSelect
103+
{...props}
104+
classNamePrefix={classNamePrefix}
105+
isDisabled={true}
106+
placeholder={loadingMessage}
107+
options={state.options}
108+
{...loadingProps}
109+
/>
110+
);
101111
}
102112

103113
const onInputChange = (inputValue) => {
104-
if (loadOptions && state.promises[inputValue] === undefined) {
114+
if (inputValue && loadOptions && state.promises[inputValue] === undefined && props.isSearchable) {
105115
dispatch({ type: 'setPromises', payload: { [inputValue]: true } });
106116

107117
loadOptions(inputValue)
@@ -133,6 +143,7 @@ const Select = ({
133143
'has-error': invalid
134144
})}
135145
{...props}
146+
isDisabled={props.isDisabled || props.isReadOnly}
136147
options={state.options}
137148
classNamePrefix={classNamePrefix}
138149
isMulti={isMulti}
@@ -157,6 +168,7 @@ Select.propTypes = {
157168
pluckSingleValue: PropTypes.bool,
158169
value: PropTypes.any,
159170
placeholder: PropTypes.string,
171+
loadOptionsChangeCounter: PropTypes.number,
160172
...input
161173
};
162174

packages/pf3-component-mapper/demo/demo-schemas/sandbox.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
/* eslint-disable camelcase */
22
import { componentTypes as components, validatorTypes as validators } from '@data-driven-forms/react-form-renderer';
33

4+
const asyncOptions = [
5+
{ label: 'async-option-1', value: 'async-option-1' },
6+
{ label: 'async-option-2', value: 'async-option-2' },
7+
{ label: 'async-option-3', value: 'async-option-3' },
8+
{ label: 'async option pepa 1', value: 'async-option-4' },
9+
{ label: 'async option pepa 2', value: 'async-option-5' }
10+
];
11+
12+
const baseOptions = asyncOptions.slice(0, 3);
13+
14+
const asyncLoadOptions = (searchValue) =>
15+
new Promise((resolve) =>
16+
setTimeout(() => {
17+
if (searchValue && searchValue.trim() !== '') {
18+
return resolve(asyncOptions.filter(({ label }) => label.toLocaleLowerCase().includes(searchValue.trim().toLocaleLowerCase())));
19+
}
20+
21+
return resolve(baseOptions);
22+
}, 2000)
23+
);
24+
425
const output = {
526
title: 'Testing dialog',
627
description: 'Description of testing Dialog',
@@ -39,18 +60,8 @@ const output = {
3960
name: 'select-1',
4061
label: 'Dropdown 1',
4162
visible: true,
42-
dataType: 'number',
43-
options: [
44-
{
45-
value: 1,
46-
label: 'foo'
47-
},
48-
{
49-
value: 2,
50-
label: 'bar'
51-
}
52-
],
53-
isMulti: true
63+
loadOptions: asyncLoadOptions,
64+
isSearchable: true
5465
},
5566
{
5667
title: 'Text boxes',

0 commit comments

Comments
 (0)