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(cascader): when dynamically loading can search for any node #4728

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
47 changes: 29 additions & 18 deletions src/cascader/core/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,20 @@ export function expendClickEffect(
node: TreeNode,
cascaderContext: CascaderContextType,
) {
const { checkStrictly, multiple, treeStore, setVisible, setValue, setTreeNodes, setExpend, value, max, valueType } =
cascaderContext;
const {
checkStrictly,
multiple,
treeStore,
setVisible,
setValue,
setTreeNodes,
setExpend,
value,
max,
valueType,
inputVal,
filter,
} = cascaderContext;

const isDisabled = node.disabled || (multiple && (value as TreeNodeValue[]).length >= max && max !== 0);

Expand All @@ -30,8 +42,9 @@ export function expendClickEffect(
const expanded = node.setExpanded(true);
treeStore.refreshNodes();
treeStore.replaceExpanded(expanded);
const nodes = treeStore.getNodes().filter((node: TreeNode) => node.visible);
setTreeNodes(nodes);

// 搜索状态下更新treeNodes
treeNodesEffect(inputVal, treeStore, setTreeNodes, filter);

// 多选条件下手动维护expend
if (multiple) {
Expand Down Expand Up @@ -174,21 +187,19 @@ export const treeNodesEffect = (
filter: CascaderContextType['filter'],
) => {
if (!treeStore) return;
let nodes = [];
if (inputVal) {
const filterMethods = (node: TreeNode) => {
if (!node.isLeaf()) return;
if (isFunction(filter)) {
return filter(`${inputVal}`, node as TreeNodeModel & TreeNode);
}
const fullPathLabel = getFullPathLabel(node, '');
return fullPathLabel.indexOf(`${inputVal}`) > -1;
};

nodes = treeStore.nodes.filter(filterMethods);
} else {
nodes = treeStore.getNodes().filter((node: TreeNode) => node.visible);
}
const filterMethods = (node: TreeNode) => {
if (isFunction(filter)) {
return filter(`${inputVal}`, node as TreeNodeModel & TreeNode);
}
const fullPathLabel = getFullPathLabel(node, '');
return fullPathLabel.includes(`${inputVal}`) && !Array.isArray(node.children) && node.visible;
};

const nodes = inputVal
? treeStore.nodes.filter((item) => filterMethods(item))
: treeStore.getNodes().filter((node) => node.visible);

setTreeNodes(nodes);
};

Expand Down
Loading