Combobox: do not automatically add labelledby on input
most of the time, the input is already described by a label. So we had a label pointing to an input pointing to a label : an infinite loop. Moreover, we already use a label tag for user without assistive tech so we remove labelledby.
This commit is contained in:
parent
96163d4ec1
commit
adaa2545a5
2 changed files with 1 additions and 33 deletions
|
@ -18,7 +18,6 @@ import { findOrCreateContainerElement } from '@coldwired/react';
|
||||||
import * as s from 'superstruct';
|
import * as s from 'superstruct';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
useLabelledBy,
|
|
||||||
useDispatchChangeEvent,
|
useDispatchChangeEvent,
|
||||||
useMultiList,
|
useMultiList,
|
||||||
useSingleList,
|
useSingleList,
|
||||||
|
@ -94,7 +93,6 @@ export function SingleComboBox({
|
||||||
...maybeProps
|
...maybeProps
|
||||||
}: SingleComboBoxProps) {
|
}: SingleComboBoxProps) {
|
||||||
const {
|
const {
|
||||||
'aria-labelledby': ariaLabelledby,
|
|
||||||
items: defaultItems,
|
items: defaultItems,
|
||||||
selectedKey: defaultSelectedKey,
|
selectedKey: defaultSelectedKey,
|
||||||
emptyFilterKey,
|
emptyFilterKey,
|
||||||
|
@ -105,7 +103,6 @@ export function SingleComboBox({
|
||||||
...props
|
...props
|
||||||
} = useMemo(() => s.create(maybeProps, SingleComboBoxProps), [maybeProps]);
|
} = useMemo(() => s.create(maybeProps, SingleComboBoxProps), [maybeProps]);
|
||||||
|
|
||||||
const labelledby = useLabelledBy(props.id, ariaLabelledby);
|
|
||||||
const { ref, dispatch } = useDispatchChangeEvent();
|
const { ref, dispatch } = useDispatchChangeEvent();
|
||||||
|
|
||||||
const { selectedItem, onReset, ...comboBoxProps } = useSingleList({
|
const { selectedItem, onReset, ...comboBoxProps } = useSingleList({
|
||||||
|
@ -117,12 +114,7 @@ export function SingleComboBox({
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<ComboBox
|
<ComboBox menuTrigger="focus" {...comboBoxProps} {...props}>
|
||||||
aria-labelledby={labelledby}
|
|
||||||
menuTrigger="focus"
|
|
||||||
{...comboBoxProps}
|
|
||||||
{...props}
|
|
||||||
>
|
|
||||||
{(item) => <ComboBoxItem id={item.value}>{item.label}</ComboBoxItem>}
|
{(item) => <ComboBoxItem id={item.value}>{item.label}</ComboBoxItem>}
|
||||||
</ComboBox>
|
</ComboBox>
|
||||||
{children || name ? (
|
{children || name ? (
|
||||||
|
@ -147,7 +139,6 @@ export function SingleComboBox({
|
||||||
|
|
||||||
export function MultiComboBox(maybeProps: MultiComboBoxProps) {
|
export function MultiComboBox(maybeProps: MultiComboBoxProps) {
|
||||||
const {
|
const {
|
||||||
'aria-labelledby': ariaLabelledby,
|
|
||||||
items: defaultItems,
|
items: defaultItems,
|
||||||
selectedKeys: defaultSelectedKeys,
|
selectedKeys: defaultSelectedKeys,
|
||||||
name,
|
name,
|
||||||
|
@ -159,7 +150,6 @@ export function MultiComboBox(maybeProps: MultiComboBoxProps) {
|
||||||
...props
|
...props
|
||||||
} = useMemo(() => s.create(maybeProps, MultiComboBoxProps), [maybeProps]);
|
} = useMemo(() => s.create(maybeProps, MultiComboBoxProps), [maybeProps]);
|
||||||
|
|
||||||
const labelledby = useLabelledBy(props.id, ariaLabelledby);
|
|
||||||
const { ref, dispatch } = useDispatchChangeEvent();
|
const { ref, dispatch } = useDispatchChangeEvent();
|
||||||
const inputRef = useRef<HTMLInputElement>(null);
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
|
@ -202,7 +192,6 @@ export function MultiComboBox(maybeProps: MultiComboBoxProps) {
|
||||||
</TagGroup>
|
</TagGroup>
|
||||||
) : null}
|
) : null}
|
||||||
<ComboBox
|
<ComboBox
|
||||||
aria-labelledby={labelledby}
|
|
||||||
allowsCustomValue={allowsCustomValue}
|
allowsCustomValue={allowsCustomValue}
|
||||||
inputRef={inputRef}
|
inputRef={inputRef}
|
||||||
menuTrigger="focus"
|
menuTrigger="focus"
|
||||||
|
@ -246,7 +235,6 @@ export function RemoteComboBox({
|
||||||
...maybeProps
|
...maybeProps
|
||||||
}: RemoteComboBoxProps) {
|
}: RemoteComboBoxProps) {
|
||||||
const {
|
const {
|
||||||
'aria-labelledby': ariaLabelledby,
|
|
||||||
items: defaultItems,
|
items: defaultItems,
|
||||||
selectedKey: defaultSelectedKey,
|
selectedKey: defaultSelectedKey,
|
||||||
allowsCustomValue,
|
allowsCustomValue,
|
||||||
|
@ -261,7 +249,6 @@ export function RemoteComboBox({
|
||||||
...props
|
...props
|
||||||
} = useMemo(() => s.create(maybeProps, RemoteComboBoxProps), [maybeProps]);
|
} = useMemo(() => s.create(maybeProps, RemoteComboBoxProps), [maybeProps]);
|
||||||
|
|
||||||
const labelledby = useLabelledBy(props.id, ariaLabelledby);
|
|
||||||
const { ref, dispatch } = useDispatchChangeEvent();
|
const { ref, dispatch } = useDispatchChangeEvent();
|
||||||
|
|
||||||
const load = useMemo(
|
const load = useMemo(
|
||||||
|
@ -288,7 +275,6 @@ export function RemoteComboBox({
|
||||||
<ComboBox
|
<ComboBox
|
||||||
allowsEmptyCollection={comboBoxProps.inputValue.length > 0}
|
allowsEmptyCollection={comboBoxProps.inputValue.length > 0}
|
||||||
allowsCustomValue={allowsCustomValue}
|
allowsCustomValue={allowsCustomValue}
|
||||||
aria-labelledby={labelledby}
|
|
||||||
{...comboBoxProps}
|
{...comboBoxProps}
|
||||||
{...props}
|
{...props}
|
||||||
>
|
>
|
||||||
|
|
|
@ -510,24 +510,6 @@ export const createLoader: (
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export function useLabelledBy(id?: string, ariaLabelledby?: string) {
|
|
||||||
return useMemo(
|
|
||||||
() => (ariaLabelledby ? ariaLabelledby : findLabelledbyId(id)),
|
|
||||||
[id, ariaLabelledby]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function findLabelledbyId(id?: string) {
|
|
||||||
if (!id) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const label = document.querySelector(`[for="${id}"]`);
|
|
||||||
if (!label?.id) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return label.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useOnFormReset(onReset?: () => void) {
|
export function useOnFormReset(onReset?: () => void) {
|
||||||
const ref = useRef<HTMLInputElement>(null);
|
const ref = useRef<HTMLInputElement>(null);
|
||||||
const onResetListener = useEvent<EventListener>((event) => {
|
const onResetListener = useEvent<EventListener>((event) => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue