Skip to content

Commit a5c4a95

Browse files
authored
Merge pull request #180 from data-driven-forms/allow-array-value-select
Allow array value select
2 parents a48389e + 32438b0 commit a5c4a95

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ const output = {
261261
title: 'Dropdown',
262262
dataType: 'string',
263263
component: components.SELECT_COMPONENT,
264+
initialValue: [ '1' ],
264265
isSearchable: true,
265266
options: [
266267
{

packages/pf3-component-mapper/src/form-fields/select/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ class Select extends Component {
8686
loadingMessage,
8787
simpleValue,
8888
options: _options, // catch options from props, if they are undefined (bcs they would overwrite the state)
89+
pluckSingleValue,
8990
...rest
9091
} = this.props;
9192

9293
const { options, isLoading } = this.state;
94+
const { value, ...inputRest } = input;
9395

9496
if (isLoading) {
9597
return <ReactSelect
@@ -100,10 +102,12 @@ class Select extends Component {
100102
/>;
101103
}
102104

105+
const selectValue = pluckSingleValue ? rest.multi ? value : Array.isArray(value) && value[0] ? value[0] : value : value;
106+
103107
return <ReactSelect
104108
className={ `final-form-select ${invalid ? 'has-error' : ''}` }
105109
styles={ customStyles }
106-
{ ...input }
110+
{ ...inputRest }
107111
options={ options }
108112
placeholder={ placeholder || 'Please choose' }
109113
isMulti={ rest.multi }
@@ -121,7 +125,7 @@ class Select extends Component {
121125
? option.map(item => item.value)
122126
: option ? option.value : undefined)
123127
: input.onChange(option) }
124-
value={ simpleValue ? options.filter(({ value }) => rest.multi ? input.value.includes(value) : value === input.value) : input.value }
128+
value={ simpleValue ? options.filter(({ value }) => rest.multi ? input.value.includes(value) : isEqual(value, selectValue)) : selectValue }
125129
{ ...rest }
126130
/>;
127131
}
@@ -135,6 +139,7 @@ Select.propTypes = {
135139
value: PropTypes.oneOfType([
136140
PropTypes.string,
137141
PropTypes.number,
142+
PropTypes.array,
138143
]),
139144
})),
140145
invalid: PropTypes.oneOfType([
@@ -156,6 +161,7 @@ Select.propTypes = {
156161
isDisabled: PropTypes.bool,
157162
isReadOnly: PropTypes.bool,
158163
loadingMessage: PropTypes.string,
164+
pluckSingleValue: PropTypes.bool,
159165
};
160166

161167
Select.defaultProps = {
@@ -164,6 +170,7 @@ Select.defaultProps = {
164170
},
165171
loadingMessage: 'Loading...',
166172
simpleValue: true,
173+
pluckSingleValue: true,
167174
};
168175

169176
export default Select;

packages/pf3-component-mapper/src/tests/form-fields/__snapshots__/select.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ exports[`<SelectField /> should mount Async correctly 1`] = `
114114
},
115115
]
116116
}
117+
pluckSingleValue={true}
117118
simpleValue={true}
118119
>
119120
<StateManager

packages/pf3-component-mapper/src/tests/form-fields/select.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import SelectPF3 from '../../form-fields/select/index';
55
import { mount } from 'enzyme';
66
import Select from 'react-select';
77
import isEqual from 'lodash/isEqual';
8+
import ReactSelect, { components } from 'react-select';
89

910
describe('<SelectField />', () => {
1011
let initialProps;
@@ -179,5 +180,23 @@ describe('<SelectField />', () => {
179180
});
180181
});
181182
});
183+
184+
it('should pick correct value for single select when passed array', () => {
185+
const wrapper = mount(<SelectField { ...initialProps } input={{ ...initialProps.input, value: [ 2 ]}}/>);
186+
expect(wrapper.find(ReactSelect).instance().state.value).toEqual([{ value: 2, label: 'option 2' }]);
187+
});
188+
189+
it('should pick correct array value for single select when passed array', () => {
190+
let wrapper = mount(<SelectField { ...initialProps } input={{ ...initialProps.input, value: [ 2 ]}} pluckSingleValue={ false }/>);
191+
expect(wrapper.find(ReactSelect).instance().state.value).toEqual([]);
192+
193+
wrapper = mount(<SelectField
194+
{ ...initialProps }
195+
options={ [ ...initialProps.options, { label: 'array options', value: [ 2 ]}] }
196+
input={{ ...initialProps.input, value: [ 2 ]}}
197+
pluckSingleValue={ false }
198+
/>);
199+
expect(wrapper.find(ReactSelect).instance().state.value).toEqual([{ label: 'array options', value: [ 2 ]}]);
200+
});
182201
});
183202
});

packages/react-renderer-demo/src/docs-components/pf3-select.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
### Select
2+
3+
There are some additional props for PF3 variant to match PF3 requirements.
4+
5+
|prop name|type|default value|description|
6+
|---------|----|-------------|-----------|
7+
|pluckSingleValue|`bool`|`true`|If a value is an array, component will pick its first item and set it as a new value. This will override the value in state from `[2, 4, 5]` to `2` for example.|
8+
19
### Async Select
210

311
PF3 Select allows to load the options asynchronously.

0 commit comments

Comments
 (0)