Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/components/select/components/select-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ export default defineComponent({

// 递归render options
const renderOptionsContent = (options: SelectOption[]) => {
let globalIndex = 0;
return (
<ul class={`${COMPONENT_NAME.value}__list`}>
{options.map((item: SelectOptionGroup & TdOptionProps & { slots: Slots } & { $index: number }, index) => {
{options.map((item: SelectOptionGroup & TdOptionProps & { slots: Slots } & { $index: number }) => {
if (item.children) {
return (
<OptionGroup label={item.group} divider={item.divider}>
Expand All @@ -74,6 +75,8 @@ export default defineComponent({
);
}

const currentIndex = globalIndex++;

const defaultOmit = ['index', '$index', 'className', 'tagName'];

const { value, label, disabled } = keys.value || {};
Expand All @@ -91,12 +94,12 @@ export default defineComponent({
scrollType: props.scroll?.type,
isVirtual: isVirtual.value,
bufferSize: props.scroll?.bufferSize,
key: `${item.$index || ''}_${index}_${item.value}`,
key: `${item.$index || ''}_${currentIndex}_${item.value}`,
}
: {
key: `${index}_${item.value}`,
key: `${currentIndex}_${item.value}`,
})}
index={index}
index={currentIndex}
multiple={props.multiple}
v-slots={item.slots}
onRowMounted={handleRowMounted}
Expand Down
13 changes: 6 additions & 7 deletions packages/components/select/hooks/useKeyboardControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { usePrefixClass } from '@tdesign/shared-hooks';

import { getNewMultipleValue } from '../utils';

import type { SelectOption, TdOptionProps, SelectValue } from '../type';
import type { TdOptionProps, SelectValue } from '../type';
import type { ChangeHandler } from '@tdesign/shared-hooks';
import type { PopupVisibleChangeContext } from '../../popup';

export type useKeyboardControlType = {
displayOptions: ComputedRef<SelectOption[]>;
optionsListLength: ComputedRef<number>;
optionsList: ComputedRef<TdOptionProps[]>;
innerPopupVisible: Ref<boolean>;
setInnerPopupVisible: ChangeHandler<boolean, [context: PopupVisibleChangeContext]>;
Expand All @@ -27,7 +27,7 @@ export type useKeyboardControlType = {

// 统一处理键盘控制的hooks
export function useKeyboardControl({
displayOptions,
optionsListLength,
optionsList,
innerPopupVisible,
setInnerPopupVisible,
Expand All @@ -48,15 +48,14 @@ export function useKeyboardControl({
const virtualFilteredOptions = ref([]); // 处理虚拟滚动下选项过滤通过键盘选择的问题
const classPrefix = usePrefixClass();
const handleKeyDown = (e: KeyboardEvent) => {
const optionsListLength = displayOptions.value.length;
let newIndex = hoverIndex.value;
switch (e.code) {
case 'ArrowUp':
e.preventDefault();
if (hoverIndex.value === -1) {
newIndex = 0;
} else if (hoverIndex.value === 0 || hoverIndex.value > displayOptions.value.length - 1) {
newIndex = optionsListLength - 1;
} else if (hoverIndex.value === 0 || hoverIndex.value > optionsListLength.value - 1) {
newIndex = optionsListLength.value - 1;
} else {
newIndex--;
}
Expand All @@ -68,7 +67,7 @@ export function useKeyboardControl({
case 'ArrowDown':
e.preventDefault();

if (hoverIndex.value === -1 || hoverIndex.value >= optionsListLength - 1) {
if (hoverIndex.value === -1 || hoverIndex.value >= optionsListLength.value - 1) {
newIndex = 0;
} else {
newIndex++;
Expand Down
14 changes: 14 additions & 0 deletions packages/components/select/hooks/useSelectOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ export const useSelectOptions = (
return res.length && checkAllOption ? [checkAllOption, ...res] : res;
});

// 获取总计的option数量,包括children项
const optionsListLength = computed(() => {
let total = 0;
options.value.forEach((option: SelectOptionGroup) => {
if (option?.children) {
total += option.children.length;
} else {
total++;
}
});
return total;
});

return {
options,
optionsMap,
Expand All @@ -180,5 +193,6 @@ export const useSelectOptions = (
displayOptions,
filterMethods,
searchDisplayOptions,
optionsListLength,
};
};
13 changes: 10 additions & 3 deletions packages/components/select/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,15 @@ export default defineComponent({
return orgValue.value;
});

const { optionsMap, optionsList, optionsCache, displayOptions, filterMethods, searchDisplayOptions } =
useSelectOptions(props, keys, innerInputValue, innerValue);
const {
optionsMap,
optionsList,
optionsCache,
displayOptions,
filterMethods,
searchDisplayOptions,
optionsListLength,
} = useSelectOptions(props, keys, innerInputValue, innerValue);

const setInnerValue: TdSelectProps['onChange'] = (newVal: SelectValue | SelectValue[], context) => {
if (isObjectType.value) {
Expand Down Expand Up @@ -319,7 +326,7 @@ export default defineComponent({
});

const { hoverIndex, virtualFilteredOptions, handleKeyDown, filteredOptions } = useKeyboardControl({
displayOptions,
optionsListLength,
optionsList,
innerPopupVisible,
setInnerPopupVisible,
Expand Down