Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [TreeSelect] Fixed the problem that showFilteredOnly does not ta… #1947

Merged
merged 2 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions cypress/e2e/treeSelect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,38 @@ describe('treeSelect', () => {
cy.wait(1000);
cy.get('.semi-checkbox').eq(0).get('.semi-checkbox-inner-checked').should("exist");
});

it('expanded controlled + showFilteredOnly', () => {
cy.visit('http://127.0.0.1:6006/iframe.html?id=treeselect--issue-1542');
cy.get('.semi-tree-select-selection').eq(0).trigger('click');
cy.get('.semi-tree-select-inputTrigger').eq(0).children(".semi-input").eq(0).type('b');
// showFilteredOnly,因此搜索后的选项应该只有 3 项
cy.get('.semi-tree-option').should('have.length', 3);
// 清空搜索框,由 state 中的 expandedKeys 决定展示展示项,应该只有两项
cy.get('.semi-tree-select-inputTrigger').eq(0).children(".semi-input").eq(0).clear();
cy.get('.semi-tree-option').should('have.length', 2);
cy.get('.semi-tree-select-inputTrigger').eq(0).children(".semi-input").eq(0).type('s');
cy.get('.semi-tree-option').should('have.length', 9);
// 搜索状态下,输入框有值,被筛选的选项点击展开按钮行为正常
cy.get('.semi-tree-option').eq(1).children('.semi-icon-tree_triangle_down').eq(0).trigger('click');
cy.get('.semi-tree-option').should('have.length', 6);
cy.get('body').click();
// 等待弹出层收起
cy.wait(500);
cy.get('.semi-tree-select-selection').eq(0).trigger('click');
// 等待弹出层展开
cy.wait(500);
cy.get('.semi-tree-option').should('have.length', 2);
cy.get('.semi-tree-select-inputTrigger').eq(0).children(".semi-input").eq(0).type('o');
cy.get('.semi-tree-option').should('have.length', 4);
cy.get('.semi-tree-option').eq(3).trigger('click');
cy.wait(1000);
cy.get('.semi-tree-select-selection').eq(0).trigger('click');
cy.wait(1000);
// 此时展开项目由选中项和原来的 state 中的 expandedKeys 决定
cy.get('.semi-tree-option').should('have.length', 4);
cy.get('.semi-icon-tree_triangle_down').eq(0).trigger('click');
cy.get('.semi-tree-option').should('have.length', 6);
});
});

20 changes: 12 additions & 8 deletions packages/semi-foundation/treeSelect/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export interface BasicTreeSelectInnerData extends Pick<BasicTreeInnerData,
'keyEntities'
| 'treeData'
| 'flattenNodes'
| 'cachedFlattenNodes'
| 'selectedKeys'
| 'checkedKeys'
| 'halfCheckedKeys'
Expand Down Expand Up @@ -346,9 +347,7 @@ export default class TreeSelectFoundation<P = Record<string, any>, S = Record<st
const isSearching = Boolean(inputValue);
const treeNodeProps: BasicTreeNodeProps = {
eventKey: key,
expanded: isSearching && !this._isExpandControlled()
? filteredExpandedKeys.has(key)
: expandedKeys.has(key),
expanded: isSearching ? filteredExpandedKeys.has(key) : expandedKeys.has(key),
selected: selectedKeys.includes(key),
checked: realChecked,
halfChecked: realHalfChecked,
Expand Down Expand Up @@ -444,6 +443,11 @@ export default class TreeSelectFoundation<P = Record<string, any>, S = Record<st
});
}

clearInputValue = () => {
const { inputValue } = this.getStates();
inputValue && this._adapter.updateInputValue('');
pointhalo marked this conversation as resolved.
Show resolved Hide resolved
}

// Scenes that may trigger focus:
// 1、click selection
_notifyFocus(e: any) {
Expand Down Expand Up @@ -597,8 +601,8 @@ export default class TreeSelectFoundation<P = Record<string, any>, S = Record<st
const newFlattenNodes = flattenTreeData(treeData, newExpandedKeys, keyMaps);

this._adapter.updateState({
expandedKeys: isExpandControlled ? expandedKeys : newExpandedKeys,
flattenNodes: isExpandControlled ? flattenNodes : newFlattenNodes,
expandedKeys: newExpandedKeys,
flattenNodes: newFlattenNodes,
inputValue: '',
motionKeys: new Set([]),
filteredKeys: new Set([]),
Expand Down Expand Up @@ -637,8 +641,8 @@ export default class TreeSelectFoundation<P = Record<string, any>, S = Record<st
const newFilteredExpandedKeys = new Set(expandedOptsKeys);
this._adapter.notifySearch(sugInput, Array.from(newFilteredExpandedKeys));
this._adapter.updateState({
expandedKeys: this._isExpandControlled() ? expandedKeys : newExpandedKeys,
flattenNodes: this._isExpandControlled() ? flattenNodes : newFlattenNodes,
expandedKeys: newExpandedKeys,
flattenNodes: newFlattenNodes,
motionKeys: new Set([]),
filteredKeys: new Set(filteredOptsKeys),
filteredExpandedKeys: newFilteredExpandedKeys,
Expand Down Expand Up @@ -808,7 +812,7 @@ export default class TreeSelectFoundation<P = Record<string, any>, S = Record<st
}

const isExpandControlled = this._isExpandControlled();
if (isSearching && !isExpandControlled) {
if (isSearching) {
this.handleNodeExpandInSearch(e, treeNode);
return;
}
Expand Down
154 changes: 93 additions & 61 deletions packages/semi-ui/treeSelect/_story/treeSelect.stories.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useMemo, useRef, useCallback, useEffect } from 'react';
import { Icon, Input, Button, Form, Popover, Tag, Typography, CheckboxGroup, TagInput, Switch } from '../../index';
import { Icon, Input, Button, Form, Popover, Tag, Typography, CheckboxGroup, TagInput, Switch, Tree } from '../../index';
import TreeSelect from '../index';
import { flattenDeep, cloneDeep } from 'lodash';
import CustomTrigger from './CustomTrigger';
Expand Down Expand Up @@ -281,6 +281,67 @@ const specialTreeData = [
},
];

const treeDataEn = [
{
label: 'Asia',
value: 'Asia',
key: '0',
children: [
{
label: 'China',
value: 'China',
key: '0-0',
children: [
{
label: 'Beijing',
value: 'Beijing',
key: '0-0-0',
},
{
label: 'Shanghai',
value: 'Shanghai',
key: '0-0-1',
},
{
label: 'Chengdu',
value: 'Chengdu',
key: '0-0-2',
},
],
},
{
label: 'Japan',
value: 'Japan',
key: '0-1',
children: [
{
label: 'Osaka',
value: 'Osaka',
key: '0-1-0'
}
]
},
],
},
{
label: 'North America',
value: 'North America',
key: '1',
children: [
{
label: 'United States',
value: 'United States',
key: '1-0'
},
{
label: 'Canada',
value: 'Canada',
key: '1-1'
}
]
}
];

export const TreeSelectWrapper = () => (
<div>
<div>github issue 750 修改测试用例</div>
Expand Down Expand Up @@ -1382,66 +1443,7 @@ DisabledStrictly.story = {


export const CheckRelationDemo = () => {
const treeData = [
{
label: 'Asia',
value: 'Asia',
key: '0',
children: [
{
label: 'China',
value: 'China',
key: '0-0',
children: [
{
label: 'Beijing',
value: 'Beijing',
key: '0-0-0',
},
{
label: 'Shanghai',
value: 'Shanghai',
key: '0-0-1',
},
{
label: 'Chengdu',
value: 'Chengdu',
key: '0-0-2',
},
],
},
{
label: 'Japan',
value: 'Japan',
key: '0-1',
children: [
{
label: 'Osaka',
value: 'Osaka',
key: '0-1-0'
}
]
},
],
},
{
label: 'North America',
value: 'North America',
key: '1',
children: [
{
label: 'United States',
value: 'United States',
key: '1-0'
},
{
label: 'Canada',
value: 'Canada',
key: '1-1'
}
]
}
];
const treeData = treeDataEn;
const [value, setValue] = useState('China');
const [value2, setValue2] = useState();
const [value3, setValue3] = useState();
Expand Down Expand Up @@ -2653,3 +2655,33 @@ export const KeyMaps = () => {
</>
);
}

export const Issue1542 = () => {
const [expandedKeys, setExpandedKeys] = useState([]);
const treeData = treeDataEn;
const onExpand = useCallback((expandedKeys) => {
setExpandedKeys(expandedKeys);
}, [expandedKeys]);

const onSearch = useCallback((inputValue, filteredExpandedKeys) => {
const set = new Set([...filteredExpandedKeys, ...expandedKeys]);
setExpandedKeys(Array.from(set));
}, [setExpandedKeys]);

return (
<>
<TreeSelect
// multiple
style={{ width: 300 }}
dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
treeData={treeData}
filterTreeNode
searchPosition='trigger'
showFilteredOnly
expandedKeys={expandedKeys}
onExpand={onExpand}
onSearch={onSearch}
/>
</>
);
};
Loading
Loading