From 92c277608778f955108e5a9ff6e231473c2cd039 Mon Sep 17 00:00:00 2001 From: jsonz1993 Date: Thu, 14 Apr 2022 13:12:04 +0800 Subject: [PATCH] fix(cascader): filterable visible --- src/cascader/utils/helper.ts | 8 ++++++++ src/cascader/utils/inputContent.ts | 12 +++++------- 2 files changed, 13 insertions(+), 7 deletions(-) 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); } /**