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

Fix missing aria-expanded for ComboboxInput component #1605

Merged
merged 3 commits into from
Jun 20, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
assertCombobox,
ComboboxMode,
assertNotActiveComboboxOption,
assertComboboxInput,
} from '../../test-utils/accessibility-assertions'
import { Transition } from '../transitions/transition'

Expand Down Expand Up @@ -296,8 +297,11 @@ describe('Rendering', () => {

render(<Example />)

assertComboboxInput({ state: ComboboxState.InvisibleUnmounted })

await click(getComboboxButton())

assertComboboxInput({ state: ComboboxState.Visible })
assertComboboxList({ state: ComboboxState.Visible })

await click(getComboboxOptions()[1])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,57 @@ export function assertCombobox(
}
}

export function assertComboboxInput(
options: {
attributes?: Record<string, string | null>
state: ComboboxState
},
input = getComboboxInput()
) {
try {
if (input === null) return expect(input).not.toBe(null)

// Ensure combobox input has these properties
expect(input).toHaveAttribute('id')

switch (options.state) {
case ComboboxState.Visible:
expect(input).toHaveAttribute('aria-controls')
expect(input).toHaveAttribute('aria-expanded', 'true')
break

case ComboboxState.InvisibleHidden:
expect(input).toHaveAttribute('aria-controls')
if (input.hasAttribute('disabled')) {
expect(input).not.toHaveAttribute('aria-expanded')
} else {
expect(input).toHaveAttribute('aria-expanded', 'false')
}
break

case ComboboxState.InvisibleUnmounted:
expect(input).not.toHaveAttribute('aria-controls')
if (input.hasAttribute('disabled')) {
expect(input).not.toHaveAttribute('aria-expanded')
} else {
expect(input).toHaveAttribute('aria-expanded', 'false')
}
break

default:
assertNever(options.state)
}

// Ensure combobox input has the following attributes
for (let attributeName in options.attributes) {
expect(input).toHaveAttribute(attributeName, options.attributes[attributeName])
}
} catch (err) {
if (err instanceof Error) Error.captureStackTrace(err, assertComboboxInput)
throw err
}
}

export function assertComboboxList(
options: {
attributes?: Record<string, string | null>
Expand Down
1 change: 1 addition & 0 deletions packages/@headlessui-vue/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Only render the `Dialog` on the client ([#1566](https://github.com/tailwindlabs/headlessui/pull/1566))
- Improve Combobox input cursor position ([#1574](https://github.com/tailwindlabs/headlessui/pull/1574))
- Fix scrolling issue in `Tab` component when using arrow keys ([#1584](https://github.com/tailwindlabs/headlessui/pull/1584))
- Fix missing `aria-expanded` for `ComboboxInput` component ([#1605](https://github.com/tailwindlabs/headlessui/pull/1605))

## [1.6.4] - 2022-05-29

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
assertCombobox,
ComboboxMode,
assertNotActiveComboboxOption,
assertComboboxInput,
} from '../../test-utils/accessibility-assertions'
import { html } from '../../test-utils/html'
import { useOpenClosedProvider, State, useOpenClosed } from '../../internal/open-closed'
Expand Down Expand Up @@ -332,8 +333,11 @@ describe('Rendering', () => {
// TODO: Rendering Example directly reveals a vue bug — I think it's been fixed for a while but I can't find the commit
renderTemplate(Example)

assertComboboxInput({ state: ComboboxState.InvisibleUnmounted })

await click(getComboboxButton())

assertComboboxInput({ state: ComboboxState.Visible })
assertComboboxList({ state: ComboboxState.Visible })

await click(getComboboxOptions()[1])
Expand Down
4 changes: 3 additions & 1 deletion packages/@headlessui-vue/src/components/combobox/combobox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,9 @@ export let ComboboxInput = defineComponent({
let slot = { open: api.comboboxState.value === ComboboxStates.Open }
let ourProps = {
'aria-controls': api.optionsRef.value?.id,
'aria-expanded': api.disabled ? undefined : api.comboboxState.value === ComboboxStates.Open,
'aria-expanded': api.disabled.value
? undefined
: api.comboboxState.value === ComboboxStates.Open,
'aria-activedescendant':
api.activeOptionIndex.value === null
? undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,57 @@ export function assertCombobox(
}
}

export function assertComboboxInput(
options: {
attributes?: Record<string, string | null>
state: ComboboxState
},
input = getComboboxInput()
) {
try {
if (input === null) return expect(input).not.toBe(null)

// Ensure combobox input has these properties
expect(input).toHaveAttribute('id')

switch (options.state) {
case ComboboxState.Visible:
expect(input).toHaveAttribute('aria-controls')
expect(input).toHaveAttribute('aria-expanded', 'true')
break

case ComboboxState.InvisibleHidden:
expect(input).toHaveAttribute('aria-controls')
if (input.hasAttribute('disabled')) {
expect(input).not.toHaveAttribute('aria-expanded')
} else {
expect(input).toHaveAttribute('aria-expanded', 'false')
}
break

case ComboboxState.InvisibleUnmounted:
expect(input).not.toHaveAttribute('aria-controls')
if (input.hasAttribute('disabled')) {
expect(input).not.toHaveAttribute('aria-expanded')
} else {
expect(input).toHaveAttribute('aria-expanded', 'false')
}
break

default:
assertNever(options.state)
}

// Ensure combobox input has the following attributes
for (let attributeName in options.attributes) {
expect(input).toHaveAttribute(attributeName, options.attributes[attributeName])
}
} catch (err) {
if (err instanceof Error) Error.captureStackTrace(err, assertComboboxInput)
throw err
}
}

export function assertComboboxList(
options: {
attributes?: Record<string, string | null>
Expand Down