Skip to content

Commit 6078e62

Browse files
committed
fix(pf3): allow select null value
1 parent 852023e commit 6078e62

File tree

8 files changed

+1083
-976
lines changed

8 files changed

+1083
-976
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ const loadOptions = () => new Promise((res) => {
2323

2424
const selectSchema = {
2525
fields: [{
26+
component: 'select',
27+
name: 'falsey-values',
28+
label: 'Falsey values',
29+
options: [{
30+
label: 'Zero',
31+
value: 0
32+
}, {
33+
label: 'Minus one',
34+
value: -1
35+
}, {
36+
label: 'Empty',
37+
value: 'empty'
38+
}, {
39+
label: 'Null',
40+
value: null
41+
}]
42+
}, {
2643
component: 'select',
2744
name: 'async-single',
2845
label: 'Async single',

packages/pf3-component-mapper/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"clsx": "^1.0.4",
8080
"prop-types": "^15.7.2",
8181
"react-day-picker": "~7.3.2",
82-
"react-select": "^3.0.8"
82+
"react-select": "^3.1.0"
8383
},
8484
"resolutions": {
8585
"terser": "3.14.1"

packages/pf3-component-mapper/src/files/select.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ Select.propTypes = {
3636
description: PropTypes.node,
3737
placeholder: PropTypes.string,
3838
isDisabled: PropTypes.bool,
39-
inputAddon: PropTypes.shape({ fields: PropTypes.array })
39+
inputAddon: PropTypes.shape({ fields: PropTypes.array }),
40+
allowNull: PropTypes.bool
41+
};
42+
43+
Select.defaultProps = {
44+
allowNull: true
4045
};
4146

4247
export default Select;

packages/pf3-component-mapper/src/files/select/dropdown-indicator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ const DropdownIndicator = ({ selectProps: { isFetching, value } }) =>
66
isFetching ? (
77
<i
88
className={clsx('ddorg__pf3-component-mapper__select__dropdown-indicator fa fa-circle-o-notch spin', {
9-
placeholder: value.length === 0
9+
placeholder: value?.length === 0
1010
})}
1111
/>
1212
) : (
1313
<i
1414
className={clsx('ddorg__pf3-component-mapper__select__dropdown-indicator fa fa-angle-down', {
15-
placeholder: value.length === 0
15+
placeholder: value?.length === 0
1616
})}
1717
/>
1818
);

packages/pf3-component-mapper/src/files/select/select.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@ import Option from './option';
1313
import DropdownIndicator from './dropdown-indicator';
1414
import ClearIndicator from './clear-indicator';
1515

16+
const sanitizeNullValue = (value) => {
17+
if (value === null) {
18+
return '';
19+
}
20+
21+
if (Array.isArray(value)) {
22+
return value.map((item) => sanitizeNullValue(item));
23+
}
24+
25+
if (typeof value === 'object') {
26+
return {
27+
...value,
28+
value: value.value === null ? '' : value.value
29+
};
30+
}
31+
32+
return value;
33+
};
34+
1635
const getDropdownText = (value, placeholder, options) => {
1736
if (Array.isArray(value)) {
1837
if (value.length === 0) {
@@ -163,7 +182,7 @@ const Select = ({ input, loadOptions, ...props }) => {
163182
})}
164183
>
165184
<DataDrivenSelect
166-
SelectComponent={ReactSelect}
185+
SelectComponent={({ value, ...props }) => <ReactSelect value={sanitizeNullValue(value)} {...props} />}
167186
{...searchableInput}
168187
{...props}
169188
loadOptions={loadOptionsEnhanced}
@@ -196,7 +215,7 @@ const Select = ({ input, loadOptions, ...props }) => {
196215

197216
return (
198217
<DataDrivenSelect
199-
SelectComponent={ReactSelect}
218+
SelectComponent={({ value, ...props }) => <ReactSelect value={sanitizeNullValue(value)} {...props} />}
200219
{...props}
201220
{...input}
202221
loadOptionsChangeCounter={loadOptionsChangeCounter}

0 commit comments

Comments
 (0)