Skip to content

Commit ff200e3

Browse files
authored
fix(editable-select):修复Eslint错误 (DevCloudFE#345)
* feat(editable-select): 新增单元测试 * fix(editable-select): update components status * fix: 修复eslint的错误和警告 * fix: 解决冲突 * fix: 解决冲突 * fix: 修复文档错误
1 parent c2f1f6e commit ff200e3

File tree

11 files changed

+564
-368
lines changed

11 files changed

+564
-368
lines changed

packages/devui-vue/devui/editable-select/src/composables/use-filter-options.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { OptionObjectItem } from '../editable-select-type';
44
const getFilterFunc = () => (val: string, option: OptionObjectItem) =>
55
option.label.toLocaleLowerCase().indexOf(val.toLocaleLowerCase()) > -1;
66

7-
export const userFilterOptions: (
7+
export const userFilterOptions = (
88
normalizeOptions: ComputedRef<OptionObjectItem[]>,
99
inputValue: Ref<string>,
10-
filteredOptions: boolean | ((val: string, option: OptionObjectItem) => boolean)
11-
) => ComputedRef<OptionObjectItem[]> = (normalizeOptions, inputValue, filterOption) =>
10+
filterOption: boolean | ((val: string, option: OptionObjectItem) => boolean) | undefined
11+
): ComputedRef<OptionObjectItem[]> =>
1212
computed(() => {
1313
const filteredOptions: OptionObjectItem[] = [];
1414

@@ -23,6 +23,5 @@ export const userFilterOptions: (
2323
filteredOptions.push(option);
2424
}
2525
});
26-
2726
return filteredOptions;
2827
});

packages/devui-vue/devui/editable-select/src/composables/use-input.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ import { SetupContext, Ref } from 'vue';
22
interface userInputReturnType {
33
handleInput: (event: Event) => void;
44
}
5-
export const useInput: (inputValue: Ref<string>, ctx: SetupContext) => userInputReturnType = (
6-
inputValue,
7-
ctx
8-
) => {
5+
export const useInput = (inputValue: Ref<string>, ctx: SetupContext): userInputReturnType => {
96
const onInputChange = (value: string) => {
107
ctx.emit('search', value);
118
};
@@ -17,6 +14,6 @@ export const useInput: (inputValue: Ref<string>, ctx: SetupContext) => userInput
1714
};
1815

1916
return {
20-
handleInput
17+
handleInput,
2118
};
2219
};
Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
1-
import { ComputedRef, nextTick, Ref } from 'vue';
1+
import { ref, nextTick, ComputedRef, Ref } from 'vue';
22
import { OptionObjectItem } from '../editable-select-type';
3-
43
interface useKeyboardSelectReturnType {
54
handleKeydown: (event: KeyboardEvent) => void;
5+
hoverIndex: Ref<number>;
6+
selectedIndex: Ref<number>;
67
}
7-
export const useKeyboardSelect: (
8-
dropdownRef: Ref<any>,
9-
disabled: string,
8+
export const useKeyboardSelect = (
9+
dropdownRef: Ref,
1010
visible: Ref<boolean>,
11-
hoverIndex: Ref<number>,
12-
selectedIndex: Ref<number>,
13-
options: ComputedRef<OptionObjectItem[]>,
14-
toggleMenu: () => void,
11+
inputValue: Ref<string>,
12+
filteredOptions: ComputedRef<OptionObjectItem[]>,
13+
optionDisabledKey: string,
14+
filterOption: boolean | ((val: string, option: OptionObjectItem) => boolean) | undefined,
15+
loading: Ref<boolean>,
16+
handleClick: (options: OptionObjectItem) => void,
1517
closeMenu: () => void,
16-
handleClick: (options: OptionObjectItem) => void
17-
) => useKeyboardSelectReturnType = (
18-
dropdownRef,
19-
disabled,
20-
visible,
21-
hoverIndex,
22-
selectedIndex,
23-
options,
24-
toggleMenu,
25-
closeMenu,
26-
handleClick
27-
) => {
18+
toggleMenu: () => void
19+
): useKeyboardSelectReturnType => {
20+
const hoverIndex = ref(0);
21+
const selectedIndex = ref(0);
2822
const updateHoveringIndex = (index: number) => {
2923
hoverIndex.value = index;
3024
};
@@ -43,68 +37,74 @@ export const useKeyboardSelect: (
4337
}
4438
});
4539
};
40+
const handleEscape = () => {
41+
if (inputValue.value) {
42+
inputValue.value = '';
43+
} else {
44+
closeMenu();
45+
}
46+
};
47+
48+
const handleEnter = () => {
49+
const len = filteredOptions.value.length;
50+
if (!visible.value) {
51+
toggleMenu();
52+
} else if (!len || len === 1) {
53+
closeMenu();
54+
} else if (len && len !== 1) {
55+
handleClick(filteredOptions.value[hoverIndex.value]);
56+
closeMenu();
57+
}
58+
};
59+
60+
const handleKeyboardNavigation = (direction: string): void => {
61+
const len = filteredOptions.value.length;
62+
if (!len || len === 1) {
63+
return;
64+
}
65+
if (!['ArrowDown', 'ArrowUp'].includes(direction)) {
66+
return;
67+
}
4668

47-
const onKeyboardNavigation = (direction: string, newIndex?: number) => {
48-
if (!newIndex) {
49-
newIndex = hoverIndex.value;
69+
if (filterOption === false && loading.value) {
70+
return;
5071
}
51-
if (!['ArrowDown', 'ArrowUp'].includes(direction)) {return;}
72+
let newIndex = 0;
73+
newIndex = hoverIndex.value;
74+
5275
if (direction === 'ArrowUp') {
53-
if (newIndex === 0) {
54-
newIndex = options.value.length - 1;
55-
scrollToItem(newIndex);
56-
updateHoveringIndex(newIndex);
57-
return;
76+
newIndex -= 1;
77+
if (newIndex === -1) {
78+
newIndex = len - 1;
5879
}
59-
newIndex = newIndex - 1;
6080
} else if (direction === 'ArrowDown') {
61-
if (newIndex === options.value.length - 1) {
81+
newIndex += 1;
82+
if (newIndex === len) {
6283
newIndex = 0;
63-
scrollToItem(newIndex);
64-
updateHoveringIndex(newIndex);
65-
return;
6684
}
67-
newIndex = newIndex + 1;
6885
}
69-
70-
const option = options.value[newIndex];
71-
if (option[disabled]) {
72-
return onKeyboardNavigation(direction, newIndex);
86+
hoverIndex.value = newIndex;
87+
const option = filteredOptions.value[newIndex];
88+
if (option[optionDisabledKey]) {
89+
return handleKeyboardNavigation(direction);
7390
}
74-
scrollToItem(newIndex);
7591
updateHoveringIndex(newIndex);
92+
scrollToItem(newIndex);
7693
};
7794

7895
const handleKeydown = (event: KeyboardEvent) => {
7996
const keyCode = event.key || event.code;
80-
81-
if (options.value.length === 0) {return;}
82-
83-
if (!visible.value) {
84-
return toggleMenu();
85-
}
86-
87-
const onKeydownEnter = () => {
88-
handleClick(options.value[hoverIndex.value]);
89-
closeMenu();
90-
};
91-
92-
const onKeydownEsc = () => {
93-
closeMenu();
94-
};
95-
9697
switch (keyCode) {
97-
case 'Enter':
98-
onKeydownEnter();
99-
break;
10098
case 'Escape':
101-
onKeydownEsc();
99+
event.preventDefault();
100+
handleEscape();
101+
break;
102+
case 'Enter':
103+
handleEnter();
102104
break;
103105
default:
104-
onKeyboardNavigation(keyCode);
106+
handleKeyboardNavigation(keyCode);
105107
}
106108
};
107-
return {
108-
handleKeydown
109-
};
109+
return { handleKeydown, hoverIndex, selectedIndex };
110110
};
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
import { Ref } from 'vue';
1+
import { Ref, SetupContext } from 'vue';
22
import { OptionObjectItem } from '../editable-select-type';
33

44
interface useLazyLoadReturenType {
55
loadMore: () => void;
66
}
7-
export const useLazyLoad: (
8-
dropdownRef: Ref,
7+
export const useLazyLoad = (
8+
dropdownRef: Ref<HTMLElement>,
99
inputValue: Ref<string>,
10-
filterOtion: boolean | ((val: string, option: OptionObjectItem) => boolean),
11-
load: (val: string) => void
12-
) => useLazyLoadReturenType = (dropdownRef, inputValue, filterOtion, load) => {
10+
filterOtion: boolean | ((val: string, option: OptionObjectItem) => boolean) | undefined,
11+
ctx: SetupContext
12+
): useLazyLoadReturenType => {
1313
const loadMore = () => {
14-
if (filterOtion !== false) {return;}
14+
const dropdownVal = dropdownRef.value;
1515

16-
if (
17-
dropdownRef.value.clientHeight + dropdownRef.value.scrollTop >=
18-
dropdownRef.value.scrollHeight
19-
) {
20-
load(inputValue.value);
16+
if (filterOtion !== false) {
17+
return;
2118
}
22-
};
2319

20+
if (dropdownVal.clientHeight + dropdownVal.scrollTop >= dropdownVal.scrollHeight) {
21+
ctx.emit('loadMore', inputValue.value);
22+
}
23+
};
2424
return { loadMore };
2525
};
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
export type OptionsType = Array<OptionType>;
2-
export type OptionType = string | number | OptionObjectItem;
31
export interface OptionObjectItem {
42
label: string;
53
value: string | number;
6-
[key: string]: any;
4+
[key: string]: unknown;
75
}
6+
export type OptionType = string | number | OptionObjectItem;
7+
8+
export type OptionsType = Array<OptionType>;
Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,36 @@
11
import type { PropType, ExtractPropTypes } from 'vue';
22
import { OptionObjectItem, OptionsType } from './editable-select-type';
33
export const editableSelectProps = {
4-
/* test: {
5-
type: Object as PropType<{ xxx: xxx }>
6-
} */
7-
appendToBody: {
8-
type: Boolean
9-
},
104
options: {
115
type: Array as PropType<OptionsType>,
12-
default: () => []
6+
default: () => [],
137
},
148
disabled: {
15-
type: Boolean
9+
type: Boolean,
1610
},
1711
loading: {
18-
type: Boolean
12+
type: Boolean,
1913
},
2014
optionDisabledKey: {
2115
type: String,
22-
default: ''
16+
default: '',
2317
},
2418
placeholder: {
2519
type: String,
26-
default: 'Search'
20+
default: 'Search',
2721
},
2822
modelValue: {
29-
type: String
23+
type: String,
3024
},
3125
width: {
32-
type: Number
26+
type: Number,
3327
},
3428
maxHeight: {
35-
type: Number
29+
type: Number,
3630
},
3731
filterOption: {
38-
type: [Function, Boolean] as PropType<
39-
boolean | ((input: string, option: OptionObjectItem) => boolean)
40-
>
32+
type: [Function, Boolean] as PropType<boolean | ((input: string, option: OptionObjectItem) => boolean)>,
4133
},
42-
loadMore: {
43-
type: Function as PropType<(val: string) => void>
44-
}
4534
} as const;
4635

4736
export type EditableSelectProps = ExtractPropTypes<typeof editableSelectProps>;

packages/devui-vue/devui/editable-select/src/editable-select.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190
}
191191

192192
.devui-popup-tips {
193-
color: $devui-text-weak; // TODO: Color-Question
193+
color: $devui-text-weak;
194194
padding: 4px 12px;
195195
}
196196
}

0 commit comments

Comments
 (0)