Skip to content

Commit 11a285f

Browse files
committed
Fixed select title for simple value variant.
1 parent 4152142 commit 11a285f

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

packages/common/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"dependencies": {
4949
"@babel/plugin-proposal-class-properties": "^7.1.0",
5050
"clsx": "^1.0.4",
51+
"lodash": "^4.17.15",
5152
"react-select": "^3.0.8"
5253
},
5354
"peerDependencies": {

packages/common/src/select/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import React, { Component } from 'react';
22
import ReactSelect from 'react-select';
33
import PropTypes from 'prop-types';
44
import clsx from 'clsx';
5+
import isEqual from 'lodash/isEqual';
56

67
const getSelectValue = (stateValue, simpleValue, isMulti, allOptions) => simpleValue
78
? allOptions.filter(({ value }) => isMulti
89
? stateValue.includes(value)
9-
: value === stateValue)
10+
: isEqual(value, stateValue))
1011
: stateValue;
1112

1213
const handleSelectChange = (option, simpleValue, isMulti, onChange) => {
@@ -20,9 +21,11 @@ const handleSelectChange = (option, simpleValue, isMulti, onChange) => {
2021

2122
class Select extends Component {
2223
render() {
23-
const { input, invalid, classNamePrefix, options, simpleValue, isMulti, ...props } = this.props;
24+
const { input, invalid, classNamePrefix, options, simpleValue, isMulti, pluckSingleValue, ...props } = this.props;
2425
const { value, onChange, ...inputProps } = input;
2526

27+
const selectValue = pluckSingleValue ? isMulti ? value : Array.isArray(value) && value[0] ? value[0] : value : value;
28+
2629
return (
2730
<ReactSelect
2831
className={ clsx(classNamePrefix, {
@@ -33,7 +36,7 @@ class Select extends Component {
3336
classNamePrefix={ classNamePrefix }
3437
options={ options }
3538
isMulti={ isMulti }
36-
value={ getSelectValue(value, simpleValue, isMulti, options) }
39+
value={ getSelectValue(selectValue, simpleValue, isMulti, options) }
3740
onChange={ option => handleSelectChange(option, simpleValue, isMulti, onChange) }
3841
/>
3942
);
@@ -47,12 +50,14 @@ Select.propTypes = {
4750
invalid: PropTypes.bool,
4851
simpleValue: PropTypes.bool,
4952
isMulti: PropTypes.bool,
53+
pluckSingleValue: PropTypes.bool
5054
};
5155

5256
Select.defaultProps = {
5357
options: [],
5458
invalid: false,
5559
simpleValue: true,
60+
pluckSingleValue: true,
5661
};
5762

5863
const DataDrivenSelect = ({ multi, ...props }) => {

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ Select.defaultProps = {
187187

188188
export default Select;
189189

190-
const getDropdownText = (value, placeholder) => {
190+
const getDropdownText = (value, placeholder, options) => {
191191
if (Array.isArray(value)) {
192192
if (value.length === 0) {
193193
return [ placeholder, true ];
@@ -197,7 +197,7 @@ const getDropdownText = (value, placeholder) => {
197197
return [ value.map(({ label }) => label).join(', '), false ];
198198
}
199199

200-
return [ value.join(', '), false ];
200+
return [ value.map(item => options.find(({ value }) => value === item).label).join(', '), false ];
201201
}
202202

203203
if (typeof value === 'object') {
@@ -208,7 +208,7 @@ const getDropdownText = (value, placeholder) => {
208208
return [ placeholder, true ];
209209
}
210210

211-
return [ value, false ];
211+
return [ options.find(option => option.value === value).label, false ];
212212
};
213213

214214
class SearchInput extends Component {
@@ -217,12 +217,7 @@ class SearchInput extends Component {
217217
this.inputRef.current.focus();
218218
}
219219

220-
componentDidUpdate(prevProps, prevState) {
221-
console.log(prevProps, this.props);
222-
}
223-
224220
shouldComponentUpdate(nextProps) {
225-
226221
return nextProps.value !== this.props.value;
227222
}
228223

@@ -277,7 +272,7 @@ export class P3Select extends Component {
277272
render () {
278273
const { input, ...props } = this.props;
279274
const { isOpen } = this.state;
280-
const [ title, isPlaceholder ] = getDropdownText(input.value, props.placeholder);
275+
const [ title, isPlaceholder ] = getDropdownText(input.value, props.placeholder, props.options);
281276
if (props.isSearchable) {
282277
const searchableInput = {
283278
...input,

0 commit comments

Comments
 (0)