feat(Cascader): add role, aria and tabindex for a11y (#9886)

* feat(Cascader): add role, aria and tabindex for a11y

* fix: typing

* fix: snapshot
This commit is contained in:
neverland 2021-11-17 11:51:56 +08:00 committed by GitHub
parent 2e67881ca7
commit fe7f5e28d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 24 deletions

View File

@ -26,9 +26,8 @@ export default defineComponent({
} }
}; };
const onClickSubtitle = (event: MouseEvent) => { const onClickSubtitle = (event: MouseEvent) =>
emit('click-subtitle', event); emit('click-subtitle', event);
};
const renderSubtitle = () => { const renderSubtitle = () => {
if (props.showSubtitle) { if (props.showSubtitle) {

View File

@ -205,8 +205,10 @@ export default defineComponent({
selectedOption: CascaderOption | null, selectedOption: CascaderOption | null,
tabIndex: number tabIndex: number
) => { ) => {
const selected = const { disabled } = option;
selectedOption && option[valueKey] === selectedOption[valueKey]; const selected = !!(
selectedOption && option[valueKey] === selectedOption[valueKey]
);
const color = option.color || (selected ? props.activeColor : undefined); const color = option.color || (selected ? props.activeColor : undefined);
const Text = slots.option ? ( const Text = slots.option ? (
@ -217,14 +219,12 @@ export default defineComponent({
return ( return (
<li <li
class={[ role="menuitemradio"
bem('option', { class={[bem('option', { selected, disabled }), option.className]}
selected,
disabled: option.disabled,
}),
option.className,
]}
style={{ color }} style={{ color }}
tabindex={disabled ? undefined : selected ? 0 : -1}
aria-checked={selected}
aria-disabled={disabled || undefined}
onClick={() => onSelect(option, tabIndex)} onClick={() => onSelect(option, tabIndex)}
> >
{Text} {Text}
@ -240,7 +240,7 @@ export default defineComponent({
selectedOption: CascaderOption | null, selectedOption: CascaderOption | null,
tabIndex: number tabIndex: number
) => ( ) => (
<ul class={bem('options')}> <ul role="menu" class={bem('options')}>
{options.map((option) => {options.map((option) =>
renderOption(option, selectedOption, tabIndex) renderOption(option, selectedOption, tabIndex)
)} )}

View File

@ -6,7 +6,11 @@ exports[`should change close icon when using close-icon prop 1`] = `
`; `;
exports[`should render option slot correctly 1`] = ` exports[`should render option slot correctly 1`] = `
<li class="van-cascader__option"> <li role="menuitemradio"
class="van-cascader__option"
tabindex="-1"
aria-checked="false"
>
Custom Option foo Custom Option foo
</li> </li>
`; `;
@ -14,15 +18,25 @@ exports[`should render option slot correctly 1`] = `
exports[`should render options-top、options-bottom slots correctly 1`] = ` exports[`should render options-top、options-bottom slots correctly 1`] = `
<div class="van-tab__panel"> <div class="van-tab__panel">
Top, tab index: 0 Top, tab index: 0
<ul class="van-cascader__options"> <ul role="menu"
<li class="van-cascader__option van-cascader__option--selected"> class="van-cascader__options"
>
<li role="menuitemradio"
class="van-cascader__option van-cascader__option--selected"
tabindex="0"
aria-checked="true"
>
<span> <span>
Zhejiang Zhejiang
</span> </span>
<i class="van-badge__wrapper van-icon van-icon-success van-cascader__selected-icon"> <i class="van-badge__wrapper van-icon van-icon-success van-cascader__selected-icon">
</i> </i>
</li> </li>
<li class="van-cascader__option"> <li role="menuitemradio"
class="van-cascader__option"
tabindex="-1"
aria-checked="false"
>
<span> <span>
Jiangsu Jiangsu
</span> </span>
@ -39,7 +53,11 @@ exports[`should render title slot correctly 1`] = `
`; `;
exports[`should select correct option when value changed 1`] = ` exports[`should select correct option when value changed 1`] = `
<li class="van-cascader__option van-cascader__option--selected"> <li role="menuitemradio"
class="van-cascader__option van-cascader__option--selected"
tabindex="0"
aria-checked="true"
>
<span> <span>
Wenzhou Wenzhou
</span> </span>
@ -102,13 +120,23 @@ exports[`should update tabs when previous tab is clicked 1`] = `
style="width: 100px;" style="width: 100px;"
> >
<div class="van-tab__panel"> <div class="van-tab__panel">
<ul class="van-cascader__options"> <ul role="menu"
<li class="van-cascader__option"> class="van-cascader__options"
>
<li role="menuitemradio"
class="van-cascader__option"
tabindex="-1"
aria-checked="false"
>
<span> <span>
Zhejiang Zhejiang
</span> </span>
</li> </li>
<li class="van-cascader__option van-cascader__option--selected"> <li role="menuitemradio"
class="van-cascader__option van-cascader__option--selected"
tabindex="0"
aria-checked="true"
>
<span> <span>
Jiangsu Jiangsu
</span> </span>
@ -127,18 +155,32 @@ exports[`should update tabs when previous tab is clicked 1`] = `
style="width: 100px;" style="width: 100px;"
> >
<div class="van-tab__panel"> <div class="van-tab__panel">
<ul class="van-cascader__options"> <ul role="menu"
<li class="van-cascader__option"> class="van-cascader__options"
>
<li role="menuitemradio"
class="van-cascader__option"
tabindex="-1"
aria-checked="false"
>
<span> <span>
Nanjing Nanjing
</span> </span>
</li> </li>
<li class="van-cascader__option"> <li role="menuitemradio"
class="van-cascader__option"
tabindex="-1"
aria-checked="false"
>
<span> <span>
Wuxi Wuxi
</span> </span>
</li> </li>
<li class="van-cascader__option"> <li role="menuitemradio"
class="van-cascader__option"
tabindex="-1"
aria-checked="false"
>
<span> <span>
Xuzhou Xuzhou
</span> </span>