Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/pf3-component-mapper/demo/demo-schemas/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ const output = {
title: 'Dropdown',
dataType: 'string',
component: components.SELECT_COMPONENT,
initialValue: [ '1' ],
isSearchable: true,
options: [
{
Expand Down
11 changes: 9 additions & 2 deletions packages/pf3-component-mapper/src/form-fields/select/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ class Select extends Component {
loadingMessage,
simpleValue,
options: _options, // catch options from props, if they are undefined (bcs they would overwrite the state)
pluckSingleValue,
...rest
} = this.props;

const { options, isLoading } = this.state;
const { value, ...inputRest } = input;

if (isLoading) {
return <ReactSelect
Expand All @@ -100,10 +102,12 @@ class Select extends Component {
/>;
}

const selectValue = pluckSingleValue ? rest.multi ? value : Array.isArray(value) && value[0] ? value[0] : value : value;

return <ReactSelect
className={ `final-form-select ${invalid ? 'has-error' : ''}` }
styles={ customStyles }
{ ...input }
{ ...inputRest }
options={ options }
placeholder={ placeholder || 'Please choose' }
isMulti={ rest.multi }
Expand All @@ -121,7 +125,7 @@ class Select extends Component {
? option.map(item => item.value)
: option ? option.value : undefined)
: input.onChange(option) }
value={ simpleValue ? options.filter(({ value }) => rest.multi ? input.value.includes(value) : value === input.value) : input.value }
value={ simpleValue ? options.filter(({ value }) => rest.multi ? input.value.includes(value) : isEqual(value, selectValue)) : selectValue }
{ ...rest }
/>;
}
Expand All @@ -135,6 +139,7 @@ Select.propTypes = {
value: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.array,
]),
})),
invalid: PropTypes.oneOfType([
Expand All @@ -156,6 +161,7 @@ Select.propTypes = {
isDisabled: PropTypes.bool,
isReadOnly: PropTypes.bool,
loadingMessage: PropTypes.string,
pluckSingleValue: PropTypes.bool,
};

Select.defaultProps = {
Expand All @@ -164,6 +170,7 @@ Select.defaultProps = {
},
loadingMessage: 'Loading...',
simpleValue: true,
pluckSingleValue: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could bring some incompatibility issue, if someone uses an actual array as a value for the select.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep i though of that. Check the rest of the code and tests 😉

Copy link
Contributor

@rvsia rvsia Oct 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that if someone has options like this right now:

[
  { value:  ['apple', 'pineapple'], label: 'sweet'},
  { value: ['chocolate','kitkat'], label: 'best'},
] 

Then he should have to set this prop to false and that (he will need to change the schema) will break the backwards compatibility. Am I right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example you posted would not work at this point because we were using === to select the value from options instead if isEqual. So nobody could use it successfully

};

export default Select;
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ exports[`<SelectField /> should mount Async correctly 1`] = `
},
]
}
pluckSingleValue={true}
simpleValue={true}
>
<StateManager
Expand Down
19 changes: 19 additions & 0 deletions packages/pf3-component-mapper/src/tests/form-fields/select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SelectPF3 from '../../form-fields/select/index';
import { mount } from 'enzyme';
import Select from 'react-select';
import isEqual from 'lodash/isEqual';
import ReactSelect, { components } from 'react-select';

describe('<SelectField />', () => {
let initialProps;
Expand Down Expand Up @@ -179,5 +180,23 @@ describe('<SelectField />', () => {
});
});
});

it('should pick correct value for single select when passed array', () => {
const wrapper = mount(<SelectField { ...initialProps } input={{ ...initialProps.input, value: [ 2 ]}}/>);
expect(wrapper.find(ReactSelect).instance().state.value).toEqual([{ value: 2, label: 'option 2' }]);
});

it('should pick correct array value for single select when passed array', () => {
let wrapper = mount(<SelectField { ...initialProps } input={{ ...initialProps.input, value: [ 2 ]}} pluckSingleValue={ false }/>);
expect(wrapper.find(ReactSelect).instance().state.value).toEqual([]);

wrapper = mount(<SelectField
{ ...initialProps }
options={ [ ...initialProps.options, { label: 'array options', value: [ 2 ]}] }
input={{ ...initialProps.input, value: [ 2 ]}}
pluckSingleValue={ false }
/>);
expect(wrapper.find(ReactSelect).instance().state.value).toEqual([{ label: 'array options', value: [ 2 ]}]);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### Select

There are some additional props for PF3 variant to match PF3 requirements.

|prop name|type|default value|description|
|---------|----|-------------|-----------|
|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.|

### Async Select

PF3 Select allows to load the options asynchronously.
Expand Down