diff --git a/src/Select.jsx b/src/Select.jsx index 2e17fa7ae..ac3411046 100644 --- a/src/Select.jsx +++ b/src/Select.jsx @@ -34,7 +34,7 @@ function saveRef(name, component) { } function chaining(...fns) { - return function (...args) { + return function (...args) { // eslint-disable-line // eslint-disable-line for (let i = 0; i < fns.length; i++) { if (fns[i] && typeof fns[i] === 'function') { @@ -1104,13 +1104,41 @@ export default class Select extends React.Component { {innerNode} ); - }; + } + + renderClear() { + const { prefixCls, allowClear } = this.props; + const { value, inputValue } = this.state; + const clear = ( + + ); + if (!allowClear) { + return null; + } + if (isCombobox(this.props)) { + if (inputValue) { + return clear; + } + return null; + } + if (inputValue || value.length) { + return clear; + } + return null; + } render() { const props = this.props; const multiple = isMultipleOrTags(props); const state = this.state; - const { className, disabled, allowClear, prefixCls } = props; + const { className, disabled, prefixCls } = props; const ctrlNode = this.renderTopControlNode(); let extraSelectionProps = {}; const { open } = this.state; @@ -1131,23 +1159,6 @@ export default class Select extends React.Component { [`${prefixCls}-enabled`]: !disabled, [`${prefixCls}-allow-clear`]: !!props.allowClear, }; - const clearStyle = { - ...UNSELECTABLE_STYLE, - display: 'none', - }; - if (state.inputValue || state.value.length) { - clearStyle.display = 'block'; - } - const clear = ( - - ); return ( {ctrlNode} - {allowClear ? clear : null} + {this.renderClear()} {multiple || !props.showArrow ? null : ( { expect(handleChange).toBeCalledWith('Two'); expect(handleSelect).toBeCalledWith('Two', expect.anything()); }); + + it('should hide clear icon when inputValue is \'\'', () => { + const wrapper = mount( + + ); + + wrapper.find('input').simulate('change', { target: { value: '1' } }); + expect(wrapper.find('.rc-select-selection__clear').length).toBe(1); + wrapper.find('input').simulate('change', { target: { value: '' } }); + expect(wrapper.find('.rc-select-selection__clear').length).toBe(0); + }); }); diff --git a/tests/Select.spec.js b/tests/Select.spec.js index bdd6443ae..ced5347fd 100644 --- a/tests/Select.spec.js +++ b/tests/Select.spec.js @@ -101,7 +101,7 @@ describe('Select', () => { ); - expect(wrapper.find('.rc-select-selection__clear').props().style.display).toBe('none'); + expect(wrapper.find('.rc-select-selection__clear').length).toBe(0); }); it('should not response click event when select is disabled', () => { diff --git a/tests/__snapshots__/Select.combobox.spec.js.snap b/tests/__snapshots__/Select.combobox.spec.js.snap index 44ce20781..c58f20ace 100644 --- a/tests/__snapshots__/Select.combobox.spec.js.snap +++ b/tests/__snapshots__/Select.combobox.spec.js.snap @@ -38,11 +38,6 @@ exports[`Select.combobox allowClear renders correctly 1`] = ` -