Skip to content
Closed
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
68 changes: 68 additions & 0 deletions components/dropdown/__tests__/dropdown-842.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { mount } from '@vue/test-utils';
import { h } from 'vue';
import Dropdown from '../dropdown.tsx';

const OPTIONS = [
{ value: '1', label: '删除' },
{ value: '2', label: '修改' },
{ value: '3', label: '添加' },
{ value: '4', label: '评论' },
{ value: '5', label: '收藏' },
];

const _mount = (props = {}) =>
mount(Dropdown, {
props,
slots: {
default: () => h('div', { class: 'test-trigger' }, '下拉菜单'),
},
attachTo: 'body',
});

describe('Dropdown keyboard navigation #842', () => {
test('ArrowDown moves to next item', async () => {
const wrapper = _mount({
options: OPTIONS,
trigger: 'click',
appendToContainer: false,
});
await wrapper.find('.test-trigger').trigger('click');
const menu = wrapper.find('.fes-dropdown-menu');
expect(menu.exists()).toBe(true);

await menu.trigger('keydown', { key: 'ArrowDown' });
expect(wrapper.find('.fes-dropdown-option.is-active').text()).toBe('删除');

await menu.trigger('keydown', { key: 'ArrowDown' });
expect(wrapper.find('.fes-dropdown-option.is-active').text()).toBe('修改');

await menu.trigger('keydown', { key: 'ArrowDown' });
expect(wrapper.find('.fes-dropdown-option.is-active').text()).toBe('添加');

await menu.trigger('keydown', { key: 'ArrowDown' });
expect(wrapper.find('.fes-dropdown-option.is-active').text()).toBe('评论');

await menu.trigger('keydown', { key: 'ArrowDown' });
expect(wrapper.find('.fes-dropdown-option.is-active').text()).toBe('收藏');

await menu.trigger('keydown', { key: 'ArrowDown' });
expect(wrapper.find('.fes-dropdown-option.is-active').text()).toBe('删除');
});

test('Enter triggers active item click', async () => {
const wrapper = _mount({
options: OPTIONS,
trigger: 'click',
appendToContainer: false,
});
await wrapper.find('.test-trigger').trigger('click');
const menu = wrapper.find('.fes-dropdown-menu');

await menu.trigger('keydown', { key: 'ArrowDown' });
expect(wrapper.find('.fes-dropdown-option.is-active').text()).toBe('删除');

await menu.trigger('keydown', { key: 'Enter' });
expect(wrapper.emitted()).toHaveProperty('click');
expect(wrapper.emitted().click[0]).toEqual(['1']);
});
});
151 changes: 105 additions & 46 deletions components/dropdown/dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { type DropdownValue, type DropdownOption as Option, dropdownProps } from

const prefixCls = getPrefixCls('dropdown');

const NAVIGATE_KEYS = ['ArrowDown', 'ArrowUp', 'Enter', 'Escape'] as const;
type NavigateKey = (typeof NAVIGATE_KEYS)[number];

export default defineComponent({
name: 'FDropdown',
props: dropdownProps,
Expand All @@ -23,10 +26,59 @@ export default defineComponent({
prop: 'visible',
});

const activeIndex = ref(-1);

const hasIcon = computed(() =>
props.options.some((option) => option.icon),
);

const getNextValidIndex = (
current: number,
direction: 1 | -1,
): number => {
const len = props.options.length;
let index = current + direction;
while (index !== current) {
if (index < 0) index = len - 1;
if (index >= len) index = 0;
if (!props.options[index]?.disabled) break;
index += direction;
}
return index === current ? -1 : index;
};

const handleKeydown = (event: KeyboardEvent) => {
if (!visible.value) return;
const key = event.key as NavigateKey;
if (!NAVIGATE_KEYS.includes(key)) return;
event.preventDefault();
if (key === 'Escape') {
updateVisible(false);
activeIndex.value = -1;
return;
}
if (key === 'ArrowDown') {
activeIndex.value = getNextValidIndex(
activeIndex.value,
1,
);
return;
}
if (key === 'ArrowUp') {
activeIndex.value = getNextValidIndex(
activeIndex.value,
-1,
);
return;
}
if (key === 'Enter' && activeIndex.value >= 0) {
const option = props.options[activeIndex.value];
if (option && !option.disabled) {
handleClick(option, event as unknown as Event);
}
}
};

const handleClick = (option: Option, event: Event) => {
event.stopPropagation();
if (option.disabled) {
Expand All @@ -46,54 +98,61 @@ export default defineComponent({
});

const renderOptions = () => (
<Scrollbar
onScroll={(event: Event) => {
emit('scroll', event);
}}
containerClass={[
`${prefixCls}-option-wrapper`,
hasIcon.value ? 'has-icon' : '',
]}
<div
class={`${prefixCls}-menu`}
tabindex={0}
onKeydown={handleKeydown as any}
>
{props.options.map((option) => {
const value = option[props.valueField] as Option['value'];
const label = option[props.labelField] as Option['label'];
const isSelected
= props.showSelectedOption
&& currentValue.value === value;
const optionClassList = [
`${prefixCls}-option`,
option.disabled && 'is-disabled',
isSelected && 'is-selected',
].filter(Boolean);
return (
<div
class={optionClassList}
onClick={(event: Event) => {
handleClick(option, event);
}}
>
{option.icon && (
<span class={`${prefixCls}-option-icon`}>
{option.icon?.()}
</span>
)}
<span class={`${prefixCls}-option-label`}>
{isFunction(label) ? label(option) : label}
</span>
{props.showSelectedOption && (
<span
class={`${prefixCls}-option-selected-wrapper`}
>
<CheckOutlined
class={`${prefixCls}-option-selected-icon`}
/>
<Scrollbar
onScroll={(event: Event) => {
emit('scroll', event);
}}
containerClass={[
`${prefixCls}-option-wrapper`,
hasIcon.value ? 'has-icon' : '',
]}
>
{props.options.map((option, index) => {
const value = option[props.valueField] as Option['value'];
const label = option[props.labelField] as Option['label'];
const isSelected
= props.showSelectedOption
&& currentValue.value === value;
const optionClassList = [
`${prefixCls}-option`,
option.disabled && 'is-disabled',
isSelected && 'is-selected',
activeIndex.value === index && 'is-active',
].filter(Boolean);
return (
<div
class={optionClassList}
onClick={(event: Event) => {
handleClick(option, event);
}}
>
{option.icon && (
<span class={`${prefixCls}-option-icon`}>
{option.icon?.()}
</span>
)}
<span class={`${prefixCls}-option-label`}>
{isFunction(label) ? label(option) : label}
</span>
)}
</div>
);
})}
</Scrollbar>
{props.showSelectedOption && (
<span
class={`${prefixCls}-option-selected-wrapper`}
>
<CheckOutlined
class={`${prefixCls}-option-selected-icon`}
/>
</span>
)}
</div>
);
})}
</Scrollbar>
</div>
);

return () => (
Expand Down
3 changes: 3 additions & 0 deletions components/dropdown/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
font-size: @font-size-head;
}
}
&.is-active {
background: @dropdown-option-hover-color;
}
&:hover {
background: @dropdown-option-hover-color;
transition: background-color @animation-duration-fast @ease-base-in;
Expand Down
Loading