Skip to content

Commit fac0ca3

Browse files
committed
fix(pf4): fix select lingering filter value
1 parent bb2c226 commit fac0ca3

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

packages/pf4-component-mapper/demo/demo-schemas/select-schema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const selectSchema = {
4343
name: 'simple-portal-select',
4444
label: 'Simple portal select',
4545
options,
46+
isSearchable: true,
4647
menuIsPortal: true
4748
},
4849
{

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ class App extends React.Component {
5757
onSubmit={console.log}
5858
initialValues={{
5959
'simple-select': 'Kay',
60-
'simple-async-select': 'Jenifer'
60+
'simple-async-select': 'Jenifer',
61+
'simple-searchable-async-select': 'Jenifer',
62+
'simple-portal-select': 'Jenifer'
6163
}}
6264
componentMapper={{
6365
...componentMapper,

packages/pf4-component-mapper/src/common/select/input.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,25 @@ import PropTypes from 'prop-types';
33

44
import './input.scss';
55

6+
const getInputString = (filter, value) => {
7+
if (typeof filter === 'string') {
8+
return filter;
9+
}
10+
11+
if (typeof value === 'string') {
12+
return value;
13+
}
14+
15+
if (Array.isArray(value) && typeof value[0] === 'string') {
16+
return value[0];
17+
}
18+
19+
return '';
20+
};
21+
622
const Input = ({ inputRef, isSearchable, isDisabled, getInputProps, value, ...props }) => {
723
const inputProps = getInputProps({ disabled: isDisabled });
24+
const initialInputValue = getInputString(inputProps.value, value);
825
return (
926
<input
1027
value=""
@@ -13,7 +30,7 @@ const Input = ({ inputRef, isSearchable, isDisabled, getInputProps, value, ...pr
1330
ref={inputRef}
1431
{...{
1532
...inputProps,
16-
value: inputProps.value || '',
33+
value: initialInputValue,
1734
onKeyDown: (event, ...args) => {
1835
event.stopPropagation();
1936
inputProps.onKeyDown(event, ...args);
@@ -29,7 +46,7 @@ Input.propTypes = {
2946
isSearchable: PropTypes.bool,
3047
isDisabled: PropTypes.bool,
3148
getInputProps: PropTypes.func.isRequired,
32-
value: PropTypes.string
49+
value: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)])
3350
};
3451

3552
export default Input;

packages/pf4-component-mapper/src/common/select/select.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ const getValue = (isMulti, option, value) => {
8181

8282
const stateReducer = (state, changes, isMulti) => {
8383
switch (changes.type) {
84+
case Downshift.stateChangeTypes.clickButton:
85+
return {
86+
...state,
87+
...changes,
88+
inputValue: undefined
89+
};
8490
case Downshift.stateChangeTypes.keyDownEnter:
8591
case Downshift.stateChangeTypes.clickItem:
8692
return {
@@ -108,6 +114,14 @@ const stateReducer = (state, changes, isMulti) => {
108114
};
109115
}
110116

117+
if (state.isOpen === true && changes.isOpen === false && changes.inputValue) {
118+
return {
119+
...state,
120+
...changes,
121+
inputValue: ''
122+
};
123+
}
124+
111125
return {
112126
...changes,
113127
inputValue: state.inputValue

packages/pf4-component-mapper/src/common/select/value-container.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const ValueContainer = ({ value, isMulti, placeholder, getInputProps, isSearchab
1313
}
1414

1515
if (!isMulti && isSearchable) {
16-
return <Input placeholder={placeholder} inputRef={inputRef} getInputProps={getInputProps} />;
16+
return <Input placeholder={placeholder} inputRef={inputRef} getInputProps={getInputProps} value={value} />;
1717
}
1818

1919
return <span className="pf-c-select__toggle-text">{value || placeholder}</span>;

0 commit comments

Comments
 (0)