Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add aria-activedescendant in input #108

Merged
merged 2 commits into from
Apr 10, 2023
Merged
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
10 changes: 10 additions & 0 deletions cmdk/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type Context = {
group: (id: string) => () => void
filter: () => boolean
label: string
commandRef: React.RefObject<HTMLDivElement | null>
// Ids
listId: string
labelId: string
Expand Down Expand Up @@ -273,6 +274,7 @@ const Command = React.forwardRef<HTMLDivElement, CommandProps>((props, forwarded
return propsRef.current.shouldFilter
},
label: label || props['aria-label'],
commandRef: ref,
listId,
inputId,
labelId,
Expand Down Expand Up @@ -609,6 +611,7 @@ const Item = React.forwardRef<HTMLDivElement, ItemProps>((props, forwardedRef) =
<div
ref={mergeRefs([ref, forwardedRef])}
{...etc}
id={id}
cmdk-item=""
role="option"
aria-disabled={disabled || undefined}
Expand Down Expand Up @@ -687,8 +690,14 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, forwardedRe
const isControlled = props.value != null
const store = useStore()
const search = useCmdk((state) => state.search)
const value = useCmdk((state) => state.value)
const context = useCommand()

const selectedItemId = React.useMemo(() => {
const item = context.commandRef.current?.querySelector(`${ITEM_SELECTOR}[${VALUE_ATTR}="${value}"]`)
return item?.getAttribute('id')
}, [value, context.commandRef])

React.useEffect(() => {
if (props.value != null) {
store.setState('search', props.value)
Expand All @@ -708,6 +717,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, forwardedRe
aria-expanded={true}
aria-controls={context.listId}
aria-labelledby={context.labelId}
aria-activedescendant={selectedItemId}
id={context.inputId}
type="text"
value={isControlled ? props.value : search}
Expand Down