Commit e00578c40a585a4a35f235c0228aebaf62cea1ba

Authored by Lan
Committed by GitHub
1 parent 6717fe65

feat(tree): 1. 添加自定义数据过滤判断方法 2. 添加搜索完成自动展开结果选项 3. 添加搜索完成自动选中结果选项 4. 树节点数据变化时强制搜索…

…(同步searchData避免展示错误) (#1132)
src/components/Tree/src/Tree.vue
@@ -211,16 +211,32 @@ @@ -211,16 +211,32 @@
211 searchState.startSearch = false; 211 searchState.startSearch = false;
212 return; 212 return;
213 } 213 }
  214 + const { filterFn, checkable, expandOnSearch, checkOnSearch } = unref(props);
214 searchState.startSearch = true; 215 searchState.startSearch = true;
215 - const { title: titleField } = unref(getReplaceFields); 216 + const { title: titleField, key: keyField } = unref(getReplaceFields);
216 217
  218 + const searchKeys: string[] = [];
217 searchState.searchData = filter( 219 searchState.searchData = filter(
218 unref(treeDataRef), 220 unref(treeDataRef),
219 (node) => { 221 (node) => {
220 - return node[titleField]?.includes(searchValue) ?? false; 222 + const result = filterFn
  223 + ? filterFn(searchValue, node, unref(getReplaceFields))
  224 + : node[titleField]?.includes(searchValue) ?? false;
  225 + if (result) {
  226 + searchKeys.push(node[keyField]);
  227 + }
  228 + return result;
221 }, 229 },
222 unref(getReplaceFields), 230 unref(getReplaceFields),
223 ); 231 );
  232 +
  233 + if (expandOnSearch && searchKeys.length > 0) {
  234 + setExpandedKeys(searchKeys);
  235 + }
  236 +
  237 + if (checkOnSearch && checkable && searchKeys.length > 0) {
  238 + setCheckedKeys(searchKeys);
  239 + }
224 } 240 }
225 241
226 function handleClickNode(key: string, children: TreeItem[]) { 242 function handleClickNode(key: string, children: TreeItem[]) {
@@ -239,6 +255,7 @@ @@ -239,6 +255,7 @@
239 255
240 watchEffect(() => { 256 watchEffect(() => {
241 treeDataRef.value = props.treeData as TreeItem[]; 257 treeDataRef.value = props.treeData as TreeItem[];
  258 + handleSearch(unref(searchText));
242 }); 259 });
243 260
244 onMounted(() => { 261 onMounted(() => {
src/components/Tree/src/props.ts
1 import type { PropType } from 'vue'; 1 import type { PropType } from 'vue';
2 -import type { ReplaceFields, ActionItem, Keys, CheckKeys, ContextMenuOptions } from './typing'; 2 +import type {
  3 + ReplaceFields,
  4 + ActionItem,
  5 + Keys,
  6 + CheckKeys,
  7 + ContextMenuOptions,
  8 + TreeItem,
  9 +} from './typing';
3 import type { ContextMenuItem } from '/@/hooks/web/useContextMenu'; 10 import type { ContextMenuItem } from '/@/hooks/web/useContextMenu';
4 import type { TreeDataItem } from 'ant-design-vue/es/tree/Tree'; 11 import type { TreeDataItem } from 'ant-design-vue/es/tree/Tree';
5 import { propTypes } from '/@/utils/propTypes'; 12 import { propTypes } from '/@/utils/propTypes';
@@ -66,6 +73,17 @@ export const basicProps = { @@ -66,6 +73,17 @@ export const basicProps = {
66 rightMenuList: { 73 rightMenuList: {
67 type: Array as PropType<ContextMenuItem[]>, 74 type: Array as PropType<ContextMenuItem[]>,
68 }, 75 },
  76 + // 自定义数据过滤判断方法(注: 不是整个过滤方法,而是内置过滤的判断方法,用于增强原本仅能通过title进行过滤的方式)
  77 + filterFn: {
  78 + type: Function as PropType<
  79 + (searchValue: any, node: TreeItem, replaceFields: ReplaceFields) => boolean
  80 + >,
  81 + default: null,
  82 + },
  83 + // 搜索完成时自动展开结果
  84 + expandOnSearch: propTypes.bool.def(false),
  85 + // 搜索完成自动选中所有结果,当且仅当 checkable===true 时生效
  86 + checkOnSearch: propTypes.bool.def(false),
69 }; 87 };
70 88
71 export const treeNodeProps = { 89 export const treeNodeProps = {