Skip to content

Commit 1111f68

Browse files
authored
fix: ant-design issue #37277 when empty value into the select can't u… (#819)
* fix: ant-design issue #37277 when empty value into the select can't update the value * fix: delete only
1 parent b3987f7 commit 1111f68

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

src/BaseSelect.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,12 @@ const BaseSelect = React.forwardRef((props: BaseSelectProps, ref: React.Ref<Base
693693
onInternalSearch('', false, false);
694694
};
695695

696-
if (!disabled && allowClear && (displayValues.length || mergedSearchValue)) {
696+
if (
697+
!disabled &&
698+
allowClear &&
699+
(displayValues.length || mergedSearchValue) &&
700+
!(mode === 'combobox' && mergedSearchValue === '')
701+
) {
697702
clearNode = (
698703
<TransBtn
699704
className={`${prefixCls}-clear`}

src/Select.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import OptGroup from './OptGroup';
4949
import Option from './Option';
5050
import OptionList from './OptionList';
5151
import SelectContext from './SelectContext';
52-
import { toArray } from './utils/commonUtil';
52+
import { toArray, hasValue } from './utils/commonUtil';
5353
import { fillFieldNames, flattenOptions, injectPropsWithOption } from './utils/valueUtil';
5454
import warningProps, { warningNullOptions } from './utils/warningPropsUtil';
5555

@@ -294,8 +294,8 @@ const Select = React.forwardRef(
294294
const rawLabeledValues = React.useMemo(() => {
295295
const values = convert2LabelValues(internalValue);
296296

297-
// combobox no need save value when it's empty
298-
if (mode === 'combobox' && !values[0]?.value) {
297+
// combobox no need save value when it's no value
298+
if (mode === 'combobox' && !hasValue(values[0]?.value)) {
299299
return [];
300300
}
301301

src/utils/commonUtil.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ export const isClient =
1010

1111
/** Is client side and not jsdom */
1212
export const isBrowserClient = process.env.NODE_ENV !== 'test' && isClient;
13+
14+
export function hasValue(value) {
15+
return value !== undefined && value !== null;
16+
}

tests/Combobox.test.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,45 @@ describe('Select.Combobox', () => {
382382
expect(wrapper.find('input').prop('value')).toBe('abab');
383383
});
384384

385+
it("when value change to '', searchValue will change to '' ", () => {
386+
class App extends React.Component {
387+
public state = {
388+
value: '2',
389+
};
390+
391+
public render() {
392+
return (
393+
<div>
394+
<button onClick={() => this.setState({ value: '' })}>value to 0</button>
395+
<Select
396+
value={this.state.value}
397+
open
398+
mode="combobox"
399+
onChange={(value) => this.setState({ value })}
400+
filterOption={(inputValue, option) => {
401+
if (!inputValue) {
402+
return true;
403+
}
404+
return (option.value as string).includes(inputValue);
405+
}}
406+
>
407+
{['1'].map((i) => (
408+
<Option value={i} key={i}>
409+
{i}
410+
</Option>
411+
))}
412+
</Select>
413+
</div>
414+
);
415+
}
416+
}
417+
418+
const wrapper = mount(<App />);
419+
expect(wrapper.find('.rc-select-item-option').length).toBe(0);
420+
wrapper.find('button').simulate('click');
421+
expect(wrapper.find('.rc-select-item-option').length).toBe(1);
422+
});
423+
385424
// [Legacy] `optionLabelProp` should not work on `combobox`
386425
// https://github.com/ant-design/ant-design/issues/10367
387426
// origin test: https://github.com/react-component/select/blob/e5fa4959336f6a423b6e30652b9047510bb6f78f/tests/Select.combobox.spec.tsx#L362

0 commit comments

Comments
 (0)