Skip to content

Commit

Permalink
fix(Select): should be able to not clear search value in multiple mode,
Browse files Browse the repository at this point in the history
  • Loading branch information
FairyYang authored and eternalsky committed Jul 1, 2024
1 parent 83269e0 commit ab017bc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
42 changes: 42 additions & 0 deletions components/select/__tests__/issue-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,46 @@ describe('issue in AutoComplete', () => {
cy.get('.next-select input').type('t');
cy.get('.next-menu-item').should('have.length', 1);
});

it('should be able to select multiple options while enable option filtering with keyword input in multi-select mode', () => {
const dataSource = [
{ value: '10001', label: 'Lucy King' },
{ value: '10002', label: 'Ami Lucky' },
{ value: '10003', label: 'Tom Cat' },
{ value: '10003', label: 'Serge Cat' },
];

cy.mount(
<Select
dataSource={dataSource}
filterLocal
hiddenSelected={false}
mode="multiple"
showSearch
style={{ width: '300px' }}
autoClearSearchValue={false}
/>
);
cy.get('.next-select input').type('l');
cy.get('.next-menu-item').should('have.length', 2);
cy.get('.next-menu-item').eq(1).click();
cy.get('.next-menu-item').should('have.length', 2);

// Tag mode 时,也应该支持
cy.mount(
<Select
dataSource={dataSource}
filterLocal
hiddenSelected={false}
mode="tag"
showSearch
style={{ width: '300px' }}
autoClearSearchValue={false}
/>
);
cy.get('.next-select input').type('l');
cy.get('.next-menu-item').should('have.length', 3);
cy.get('.next-menu-item').eq(1).click();
cy.get('.next-menu-item').should('have.length', 3);
});
});
11 changes: 9 additions & 2 deletions components/select/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Select extends Base<SelectProps, SelectState> {
locale: PropTypes.object,
popupAutoFocus: PropTypes.bool,
showDataSourceChildren: PropTypes.bool,
autoClearSearchValue: PropTypes.bool,
};

static defaultProps: SelectProps = {
Expand All @@ -119,6 +120,7 @@ class Select extends Base<SelectProps, SelectState> {
onMouseLeave: noop,
popupAutoFocus: false,
tagClosable: true,
autoClearSearchValue: true,
};

static displayName = 'Select';
Expand Down Expand Up @@ -393,7 +395,7 @@ class Select extends Base<SelectProps, SelectState> {
this.dataStore.getMapDS()
);

const { cacheValue, mode, hiddenSelected } = this.props;
const { cacheValue, mode, hiddenSelected, autoClearSearchValue } = this.props;

// cache those value maybe not exists in dataSource
if (cacheValue || mode === 'tag') {
Expand Down Expand Up @@ -421,7 +423,12 @@ class Select extends Base<SelectProps, SelectState> {
this.updateSelectAllYet(itemObj.value);

// 清空搜索
if (!('searchValue' in this.props) && this.state.searchValue && !keepSearchValue) {
if (
!('searchValue' in this.props) &&
this.state.searchValue &&
!keepSearchValue &&
!(mode && ['multiple', 'tag'].includes(mode) && !autoClearSearchValue)
) {
// 因为 SearchValue 被 clear 后会重新渲染 Menu,所以在 Overlay 检测 safeNode 的时候 e.target 可能会找不到导致弹窗关闭
setTimeout(() => {
this.handleSearchClear(triggerType);
Expand Down
7 changes: 7 additions & 0 deletions components/select/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,13 @@ export interface SelectProps
*/
showSearch?: boolean;

/**
* 是否在选中项后清空搜索框,只在 mode 为 multiple 或 tags 时有效
* @en Whether to clear the search box after selecting the selected item, only in mode multiple or tags is valid
* @defaultValue true
*/
autoClearSearchValue?: boolean;

/**
* 当搜索框值变化时回调
* @en Callback when the search box value changes
Expand Down

0 comments on commit ab017bc

Please sign in to comment.