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): fix bug when clicking close button #4478

Merged
merged 1 commit into from
Aug 16, 2024
Merged
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: 27 additions & 20 deletions src/cascader/core/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import isNumber from 'lodash/isNumber';
import isFunction from 'lodash/isFunction';
import isArray from 'lodash/isArray';
import cloneDeep from 'lodash/cloneDeep';
import { TreeNode, CascaderContextType, TdCascaderProps, TreeNodeValue, TreeNodeModel } from '../interface';

import type { TreeNode, CascaderContextType, TdCascaderProps, TreeNodeValue, TreeNodeModel } from '../interface';
import { getFullPathLabel, getTreeValue, isEmptyValues } from './helper';

/**
Expand Down Expand Up @@ -128,27 +129,33 @@ export function handleRemoveTagEffect(
onRemove: TdCascaderProps['onRemove'],
) {
const { disabled, setValue, value, valueType, treeStore } = cascaderContext;

if (disabled) return;
const newValue = cloneDeep(value) as [];
const res = newValue.splice(index, 1);
const node = treeStore.getNodes(res[0])[0];

const checked = node.setChecked(!node.isChecked());
// 处理不同数据类型
const resValue =
valueType === 'single'
? checked
: checked.map((val) =>
treeStore
.getNode(val)
.getPath()
.map((item) => item.value),
);

setValue(resValue, 'uncheck', node.getModel());
if (isFunction(onRemove)) {
onRemove({ value: checked, node: node as any });
// index equal to undefined means to click clear button
if (index !== undefined) {
const newValue = cloneDeep(value) as [];
const res = newValue.splice(index, 1);
const node = treeStore.getNodes(res[0])[0];

const checked = node.setChecked(!node.isChecked());
// 处理不同数据类型
const resValue =
valueType === 'single'
? checked
: checked.map((val) =>
treeStore
.getNode(val)
.getPath()
.map((item) => item.value),
);
setValue(resValue, 'uncheck', node.getModel());
if (isFunction(onRemove)) {
onRemove({ value: checked, node: node as any });
}
} else {
if (isFunction(onRemove)) {
onRemove({ value, node: undefined });
}
}
}

Expand Down
Loading