From fddc49f2c72c6c40c062a7ab5c468871f6c6f9a4 Mon Sep 17 00:00:00 2001 From: shaodahong Date: Wed, 11 Mar 2020 20:52:53 +0800 Subject: [PATCH 1/2] fix: null value display when mode is combobox --- src/Selector/SingleSelector.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Selector/SingleSelector.tsx b/src/Selector/SingleSelector.tsx index 22d30580d..ad33ae55e 100644 --- a/src/Selector/SingleSelector.tsx +++ b/src/Selector/SingleSelector.tsx @@ -34,9 +34,10 @@ const SingleSelector: React.FC = ({ const inputEditable = combobox || (showSearch && open); const item = values[0]; + const getDisplayValue = (value: React.ReactText): string => (value === null ? '' : String(value)); let inputValue: string = searchValue; if (combobox) { - inputValue = item ? String(item.value) : activeValue || searchValue; + inputValue = item ? getDisplayValue(item.value) : activeValue || searchValue; } const hasTextInput = !!inputValue; From c27c2aa7c5d0f7accf7daf5bc5377c50509eea9c Mon Sep 17 00:00:00 2001 From: shaodahong Date: Wed, 11 Mar 2020 21:13:46 +0800 Subject: [PATCH 2/2] add test suite --- tests/Combobox.test.tsx | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/tests/Combobox.test.tsx b/tests/Combobox.test.tsx index bf542c376..dd7982647 100644 --- a/tests/Combobox.test.tsx +++ b/tests/Combobox.test.tsx @@ -6,12 +6,7 @@ import Select, { Option, SelectProps } from '../src'; import focusTest from './shared/focusTest'; import keyDownTest from './shared/keyDownTest'; import openControlledTest from './shared/openControlledTest'; -import { - expectOpen, - toggleOpen, - selectItem, - injectRunAllTimers, -} from './utils/common'; +import { expectOpen, toggleOpen, selectItem, injectRunAllTimers } from './utils/common'; import allowClearTest from './shared/allowClearTest'; import throwOptionValue from './shared/throwOptionValue'; @@ -61,9 +56,7 @@ describe('Select.Combobox', () => { ); expect(wrapper.find('input').props().value).toBe(''); - expect(wrapper.find('.rc-select-selection-placeholder').text()).toEqual( - 'placeholder', - ); + expect(wrapper.find('.rc-select-selection-placeholder').text()).toEqual('placeholder'); wrapper.find('input').simulate('change', { target: { value: '1' } }); expect(wrapper.find('.rc-select-selection-placeholder').length).toBeFalsy(); expect(wrapper.find('input').props().value).toBe('1'); @@ -224,13 +217,7 @@ describe('Select.Combobox', () => { const handleChange = jest.fn(); const handleSelect = jest.fn(); const wrapper = mount( - , @@ -243,14 +230,8 @@ describe('Select.Combobox', () => { input.simulate('keyDown', { which: KeyCode.ENTER }); expect(wrapper.find('input').props().value).toEqual('One'); - expect(handleChange).toHaveBeenCalledWith( - 'One', - expect.objectContaining({ value: 'One' }), - ); - expect(handleSelect).toHaveBeenCalledWith( - 'One', - expect.objectContaining({ value: 'One' }), - ); + expect(handleChange).toHaveBeenCalledWith('One', expect.objectContaining({ value: 'One' })); + expect(handleSelect).toHaveBeenCalledWith('One', expect.objectContaining({ value: 'One' })); }); it("should hide clear icon when value is ''", () => { @@ -340,11 +321,7 @@ describe('Select.Combobox', () => { jest.useFakeTimers(); const onDropdownVisibleChange = jest.fn(); const wrapper = mount( - , @@ -399,4 +376,10 @@ describe('Select.Combobox', () => { wrapper.update(); expectOpen(wrapper, false); }); + + it('expect null value display empty string', () => { + const wrapper = mount(