useSearchInput.ts
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import type { Menu as MenuType } from '/@/router/types';
import type { MenuState } from './types';
import type { Ref } from 'vue';
import { isString } from '/@/utils/is';
import { unref } from 'vue';
import { es6Unique } from '/@/utils';
import { getAllParentPath } from '/@/utils/helper/menuHelper';
interface UseSearchInputOptions {
menuState: MenuState;
flatMenusRef: Ref<MenuType[]>;
emit: EmitType;
handleMenuChange: Fn;
}
export function useSearchInput({
menuState,
flatMenusRef,
handleMenuChange,
emit,
}: UseSearchInputOptions) {
/**
* @description: 输入框搜索
*/
function handleInputChange(value?: string): void {
if (!isString(value)) {
value = (value as any).target.value;
}
if (!value) {
handleMenuChange && handleMenuChange();
}
menuState.searchValue = value || '';
if (!value) {
menuState.openKeys = [];
return;
}
const flatMenus = unref(flatMenusRef);
let openKeys: string[] = [];
for (const menu of flatMenus) {
const { name, path } = menu;
if (!name.includes(value)) {
continue;
}
openKeys = openKeys.concat(getAllParentPath(flatMenus, path));
}
openKeys = es6Unique(openKeys);
menuState.openKeys = openKeys;
}
// 搜索框点击
function handleInputClick(e: any): void {
emit('clickSearchInput', e);
}
return { handleInputChange, handleInputClick };
}