From 1506e14ce8459a3c3e5eef317e2048d9103ca6a6 Mon Sep 17 00:00:00 2001 From: Uyarn Date: Thu, 15 Aug 2024 02:00:46 +0800 Subject: [PATCH] fix(cascader): fix bug when clicking close btn --- src/cascader/core/effect.ts | 47 +++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/cascader/core/effect.ts b/src/cascader/core/effect.ts index b1bbf43e8f..911e0f971f 100644 --- a/src/cascader/core/effect.ts +++ b/src/cascader/core/effect.ts @@ -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'; /** @@ -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 }); + } } }