Skip to content

Commit 168ac7d

Browse files
authored
Merge pull request #298 from data-driven-forms/fix-pf4-async-multi
fix(pf4): Multi async select initial value change.
2 parents a07fe4a + 56dfdde commit 168ac7d

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

packages/pf4-component-mapper/demo/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class App extends React.Component {
4141
</Toolbar>
4242
<FormRenderer
4343
onSubmit={console.log}
44+
initialValues={{
45+
'async-drop-down': 'async-option-2'
46+
}}
4447
formFieldsMapper={{
4548
...formFieldsMapper,
4649
summary: Summary

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ export class Select extends React.Component {
5353
this.setState({ isLoading: true });
5454

5555
return this.props.loadOptions().then(data => {
56-
if (!data.map(({ value }) => value).includes(this.props.value)) {
56+
if (Array.isArray(this.props.value)) {
57+
const selectValue = this.props.value.filter(value => typeof value === 'object'
58+
? data.find((option) => value.value === option.value)
59+
: data.find((option) => value === option.value));
60+
this.props.onChange(selectValue.length === 0 ? undefined : selectValue);
61+
} else if (!data.find(({ value }) => value === this.props.value)) {
5762
this.props.onChange(undefined);
5863
}
5964

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

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ describe('<Select />', () => {
138138
});
139139
});
140140

141-
it('should load Async options correctly', (done) => {
141+
it('should load single select Async options correctly', (done) => {
142142
const asyncLoading = jest.fn().mockReturnValue(Promise.resolve([{ label: 'label' }]));
143143

144144
const wrapper = mount(<Select { ...initialProps } options={ undefined } loadOptions={ asyncLoading }/>);
@@ -150,6 +150,75 @@ describe('<Select />', () => {
150150
});
151151
});
152152

153+
it('should load multi select Async options correctly and set initial value to undefined', (done) => {
154+
const asyncLoading = jest.fn().mockReturnValue(Promise.resolve([{ label: 'label', value: '123' }]));
155+
const onChange = jest.fn();
156+
const wrapper = mount(
157+
<Select
158+
{ ...initialProps }
159+
value={ [ 'does not exists in options' ] }
160+
isMulti
161+
options={ undefined }
162+
loadOptions={ asyncLoading }
163+
onChange={ onChange }
164+
simpleValue
165+
/>
166+
);
167+
168+
setImmediate(() => {
169+
wrapper.update();
170+
expect(wrapper.find(Select).first().instance().state.allOptions).toEqual([{ label: 'label', value: '123' }]);
171+
expect(onChange).toHaveBeenCalledWith(undefined);
172+
done();
173+
});
174+
});
175+
176+
it('should load multi select Async options correctly and set initial value to ["123"]', (done) => {
177+
const asyncLoading = jest.fn().mockReturnValue(Promise.resolve([{ label: 'label', value: '123' }]));
178+
const onChange = jest.fn();
179+
const wrapper = mount(
180+
<Select
181+
{ ...initialProps }
182+
value={ [ '123', 'Not in options' ] }
183+
isMulti
184+
options={ undefined }
185+
loadOptions={ asyncLoading }
186+
onChange={ onChange }
187+
simpleValue
188+
/>
189+
);
190+
191+
setImmediate(() => {
192+
wrapper.update();
193+
expect(wrapper.find(Select).first().instance().state.allOptions).toEqual([{ label: 'label', value: '123' }]);
194+
expect(onChange).toHaveBeenCalledWith([ '123' ]);
195+
done();
196+
});
197+
});
198+
199+
it('should load multi select Async options correctly and set initial value to ["123"] if initial value is an object', (done) => {
200+
const asyncLoading = jest.fn().mockReturnValue(Promise.resolve([{ label: 'label', value: '123' }]));
201+
const onChange = jest.fn();
202+
const wrapper = mount(
203+
<Select
204+
{ ...initialProps }
205+
value={ [{ value: '123', label: 'label' }, 'Not in options' ] }
206+
isMulti
207+
options={ undefined }
208+
loadOptions={ asyncLoading }
209+
onChange={ onChange }
210+
simpleValue
211+
/>
212+
);
213+
214+
setImmediate(() => {
215+
wrapper.update();
216+
expect(wrapper.find(Select).first().instance().state.allOptions).toEqual([{ label: 'label', value: '123' }]);
217+
expect(onChange).toHaveBeenCalledWith([{ label: 'label', value: '123' }]);
218+
done();
219+
});
220+
});
221+
153222
it('should load Async options after filtering', (done) => {
154223
const asyncLoading = jest.fn().mockReturnValue(Promise.resolve([{ label: 'label' }]));
155224

0 commit comments

Comments
 (0)