diff --git a/src/cascader/utils/helper.ts b/src/cascader/utils/helper.ts index 336748c53e..dacf04b4f4 100644 --- a/src/cascader/utils/helper.ts +++ b/src/cascader/utils/helper.ts @@ -35,6 +35,14 @@ export const getTreeValue = (value: CascaderContextType['value']) => { return treeValue; }; +/** + * 判断node是否在页面上 + * @param node Node + */ +export function isInPage(node: Node) { + return node === document.body ? false : document.body.contains(node); +} + export default { getFullPathLabel, }; diff --git a/src/cascader/utils/inputContent.ts b/src/cascader/utils/inputContent.ts index f695f18ba4..af2e96ec63 100644 --- a/src/cascader/utils/inputContent.ts +++ b/src/cascader/utils/inputContent.ts @@ -1,6 +1,7 @@ import isFunction from 'lodash/isFunction'; import isEmpty from 'lodash/isEmpty'; import { TreeNode, CascaderContextType, TreeNodeValue, CascaderProps } from '../interface'; +import { isInPage } from './helper'; /** * icon Class @@ -147,13 +148,10 @@ export function outerClickListenerEffect( event: MouseEvent | TouchEvent, ) { const { visible, setVisible } = cascaderContext; - if (!ref || ref.contains(event.target as Node)) { - return; - } - - if (visible) { - setVisible(false); - } + const target = event.target as Node; + // 可能点击的dom在判断的时候已经被移除了 + if (!visible || !ref || ref.contains(target) || !isInPage(target)) return; + setVisible(false); } /**