Skip to content

Commit 4152142

Browse files
committed
Added handling of simple value to common select
1 parent 0fd05bd commit 4152142

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

packages/common/src/select/index.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,39 @@ import ReactSelect from 'react-select';
33
import PropTypes from 'prop-types';
44
import clsx from 'clsx';
55

6+
const getSelectValue = (stateValue, simpleValue, isMulti, allOptions) => simpleValue
7+
? allOptions.filter(({ value }) => isMulti
8+
? stateValue.includes(value)
9+
: value === stateValue)
10+
: stateValue;
11+
12+
const handleSelectChange = (option, simpleValue, isMulti, onChange) => {
13+
const sanitizedOption = !option && isMulti ? [] : option;
14+
return simpleValue
15+
? onChange(isMulti
16+
? sanitizedOption.map(item => item.value)
17+
: sanitizedOption ? sanitizedOption.value : undefined)
18+
: onChange(sanitizedOption);
19+
};
20+
621
class Select extends Component {
722
render() {
23+
const { input, invalid, classNamePrefix, options, simpleValue, isMulti, ...props } = this.props;
24+
const { value, onChange, ...inputProps } = input;
25+
826
return (
9-
<ReactSelect className={ clsx(this.props.classNamePrefix, {
10-
'has-error': this.props.invalid,
11-
}) } { ...this.props } {...this.props.input} />
27+
<ReactSelect
28+
className={ clsx(classNamePrefix, {
29+
'has-error': invalid,
30+
}) }
31+
{ ...props }
32+
{ ...inputProps }
33+
classNamePrefix={ classNamePrefix }
34+
options={ options }
35+
isMulti={ isMulti }
36+
value={ getSelectValue(value, simpleValue, isMulti, options) }
37+
onChange={ option => handleSelectChange(option, simpleValue, isMulti, onChange) }
38+
/>
1239
);
1340
}
1441
}
@@ -18,11 +45,14 @@ Select.propTypes = {
1845
onChange: PropTypes.func,
1946
classNamePrefix: PropTypes.string.isRequired,
2047
invalid: PropTypes.bool,
48+
simpleValue: PropTypes.bool,
49+
isMulti: PropTypes.bool,
2150
};
2251

2352
Select.defaultProps = {
2453
options: [],
2554
invalid: false,
55+
simpleValue: true,
2656
};
2757

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

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ import Switch from "../src/form-fields/switch-field";
1212

1313
const selectSchema = {
1414
fields: [{
15+
component: 'select-field',
16+
name: 'select-single',
17+
label: 'Select single',
18+
options: [{
19+
label: 'foo',
20+
value: 123
21+
}, {
22+
label: 'bar',
23+
value: 231
24+
}]
25+
}, {
1526
component: 'select-field',
1627
name: 'select-search',
1728
label: 'Select search',
@@ -62,16 +73,13 @@ const selectSchema = {
6273

6374
class App extends React.Component {
6475
render() {
65-
const initialValues = {
66-
date_control_1: new Date()
67-
}
6876
return (
6977
<div>
7078
<h1>Pf3 component mapper</h1>
7179
<Grid>
7280
<Row>
7381
<FormRenderer
74-
initialValues={initialValues}
82+
initialValues={{}}
7583
onSubmit={console.log}
7684
schemaType="default"
7785
formFieldsMapper={formFieldsMapper}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import isEqual from 'lodash/isEqual';
66
import './react-select.scss';
77

88
/**
9-
* New implementation of select of PF4
9+
* New implementation of select of PF3
1010
*/
1111

1212
import NewSelect from '@data-driven-forms/common/src/select';
13-
import './react-select.scss';
1413
import Option from './option';
1514
import DropdownIndicator from './dropdown-indicator';
1615
import ClearIndicator from './clear-indicator';
1716
import { DropdownButton, FormControl } from 'patternfly-react';
1817
import clsx from 'clsx';
18+
import './react-select.scss';
1919

2020
const fnToString = (fn = '') => fn.toString().replace(/\s+/g, ' ');
2121

@@ -325,21 +325,25 @@ export class P3Select extends Component {
325325
DropdownIndicator: null,
326326
IndicatorSeparator: null,
327327
Placeholder: () => null,
328-
Input: ({ selectProps, cx, isHidden, isDisabled, innerRef, getStyles, ...props }) => <SearchInput id={ this.props.input.name } { ...props } />,
328+
Input: ({ selectProps, cx, isHidden, isDisabled, innerRef, getStyles, ...props }) =>
329+
<SearchInput id={ this.props.input.name } { ...props } />,
329330
}} /> }
330331
</DropdownButton>
331332
</div>
332333
);
333334
}
334335

335336
return (
336-
<NewSelect { ...props }
337+
<NewSelect
338+
{ ...this.props }
339+
input={ input }
337340
className={ props.classNamePrefix }
338341
components={{
339342
ClearIndicator,
340343
Option,
341344
DropdownIndicator,
342-
}} />
345+
}}
346+
/>
343347
);
344348

345349
}

0 commit comments

Comments
 (0)