Skip to content

Commit 635d48e

Browse files
committed
fix(select-input): default selectInput to null
1 parent 8b8c708 commit 635d48e

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/components/inputs/select-input/select-input.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,23 @@ export class SelectInput extends React.Component {
7070
option => (has(option, 'value') ? option : option.options)
7171
);
7272

73+
const selectedOptions = optionsWithoutGroups.filter(
74+
option =>
75+
props.isMulti
76+
? props.value.includes(option.value)
77+
: has(option, 'value') && option.value === props.value
78+
);
79+
80+
// We default to null, `type ValueType = OptionType | OptionsType | null | void`
81+
/**
82+
* we default to null
83+
* > type ValueType = OptionType | OptionsType | null | void
84+
* https://react-select.com/props#prop-types
85+
*/
7386
return {
7487
selectedOptions: props.isMulti
75-
? optionsWithoutGroups.filter(option =>
76-
props.value.includes(option.value)
77-
)
78-
: optionsWithoutGroups.find(
79-
option => has(option, 'value') && option.value === props.value
80-
),
88+
? selectedOptions
89+
: selectedOptions[0] || null,
8190
};
8291
};
8392

src/components/inputs/select-input/select-input.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ describe('overwritten props', () => {
6161
});
6262
});
6363
});
64+
describe('when value is `undefined`', () => {
65+
beforeEach(() => {
66+
props = createTestProps({ value: undefined });
67+
wrapper = shallow(<SelectInput {...props} />);
68+
});
69+
it('should set `Select` value to null', () => {
70+
expect(wrapper.find(Select)).toHaveProp('value', null);
71+
});
72+
});
6473
});
6574
describe('when in multi-value mode', () => {
6675
let wrapper;

0 commit comments

Comments
 (0)