Skip to content

Commit 2aaf4c9

Browse files
authored
fix: null value display when mode is combobox (#462)
* fix: null value display when mode is combobox * add test suite
1 parent 20ecea3 commit 2aaf4c9

File tree

2 files changed

+14
-30
lines changed

2 files changed

+14
-30
lines changed

src/Selector/SingleSelector.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ const SingleSelector: React.FC<SelectorProps> = ({
3434
const inputEditable = combobox || (showSearch && open);
3535
const item = values[0];
3636

37+
const getDisplayValue = (value: React.ReactText): string => (value === null ? '' : String(value));
3738
let inputValue: string = searchValue;
3839
if (combobox) {
39-
inputValue = item ? String(item.value) : activeValue || searchValue;
40+
inputValue = item ? getDisplayValue(item.value) : activeValue || searchValue;
4041
}
4142

4243
const hasTextInput = !!inputValue;

tests/Combobox.test.tsx

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ import Select, { Option, SelectProps } from '../src';
66
import focusTest from './shared/focusTest';
77
import keyDownTest from './shared/keyDownTest';
88
import openControlledTest from './shared/openControlledTest';
9-
import {
10-
expectOpen,
11-
toggleOpen,
12-
selectItem,
13-
injectRunAllTimers,
14-
} from './utils/common';
9+
import { expectOpen, toggleOpen, selectItem, injectRunAllTimers } from './utils/common';
1510
import allowClearTest from './shared/allowClearTest';
1611
import throwOptionValue from './shared/throwOptionValue';
1712

@@ -61,9 +56,7 @@ describe('Select.Combobox', () => {
6156
);
6257

6358
expect(wrapper.find('input').props().value).toBe('');
64-
expect(wrapper.find('.rc-select-selection-placeholder').text()).toEqual(
65-
'placeholder',
66-
);
59+
expect(wrapper.find('.rc-select-selection-placeholder').text()).toEqual('placeholder');
6760
wrapper.find('input').simulate('change', { target: { value: '1' } });
6861
expect(wrapper.find('.rc-select-selection-placeholder').length).toBeFalsy();
6962
expect(wrapper.find('input').props().value).toBe('1');
@@ -224,13 +217,7 @@ describe('Select.Combobox', () => {
224217
const handleChange = jest.fn();
225218
const handleSelect = jest.fn();
226219
const wrapper = mount(
227-
<Select
228-
mode="combobox"
229-
backfill
230-
open
231-
onChange={handleChange}
232-
onSelect={handleSelect}
233-
>
220+
<Select mode="combobox" backfill open onChange={handleChange} onSelect={handleSelect}>
234221
<Option value="One">One</Option>
235222
<Option value="Two">Two</Option>
236223
</Select>,
@@ -243,14 +230,8 @@ describe('Select.Combobox', () => {
243230

244231
input.simulate('keyDown', { which: KeyCode.ENTER });
245232
expect(wrapper.find('input').props().value).toEqual('One');
246-
expect(handleChange).toHaveBeenCalledWith(
247-
'One',
248-
expect.objectContaining({ value: 'One' }),
249-
);
250-
expect(handleSelect).toHaveBeenCalledWith(
251-
'One',
252-
expect.objectContaining({ value: 'One' }),
253-
);
233+
expect(handleChange).toHaveBeenCalledWith('One', expect.objectContaining({ value: 'One' }));
234+
expect(handleSelect).toHaveBeenCalledWith('One', expect.objectContaining({ value: 'One' }));
254235
});
255236

256237
it("should hide clear icon when value is ''", () => {
@@ -340,11 +321,7 @@ describe('Select.Combobox', () => {
340321
jest.useFakeTimers();
341322
const onDropdownVisibleChange = jest.fn();
342323
const wrapper = mount(
343-
<Select
344-
mode="combobox"
345-
open
346-
onDropdownVisibleChange={onDropdownVisibleChange}
347-
>
324+
<Select mode="combobox" open onDropdownVisibleChange={onDropdownVisibleChange}>
348325
<Option value="One">One</Option>
349326
<Option value="Two">Two</Option>
350327
</Select>,
@@ -399,4 +376,10 @@ describe('Select.Combobox', () => {
399376
wrapper.update();
400377
expectOpen(wrapper, false);
401378
});
379+
380+
it('expect null value display empty string', () => {
381+
const wrapper = mount(<Select mode="combobox" value={null} />);
382+
383+
expect(wrapper.find('input').props().value).toBe('');
384+
});
402385
});

0 commit comments

Comments
 (0)