Skip to content
Open
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 @@ -344,6 +344,7 @@ let reducers: {
return { ...state, searchQuery: '' }
},
[ActionTypes.SelectOption](state) {
if (state.dataRef.current.disabled) return state
if (state.dataRef.current.mode === ValueMode.Single) {
// The moment you select a value in single value mode, we want to close
// the listbox and freeze the value to prevent UI flicker.
Expand Down
26 changes: 26 additions & 0 deletions packages/@headlessui-react/src/components/listbox/listbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4705,3 +4705,29 @@ describe('transitions', () => {
})
)
})

it('disables options when an open Listbox becomes disabled', async () => {
let onChange = jest.fn()
let ui = (disabled = false) => (
<Listbox value="apple" onChange={onChange} disabled={disabled}>
<Listbox.Button>Fruit</Listbox.Button>
<Listbox.Options>
<Listbox.Option value="apple">Apple</Listbox.Option>
<Listbox.Option value="orange">Orange</Listbox.Option>
</Listbox.Options>
</Listbox>
)
let { rerender } = render(ui())
await click(getListboxButton())
let orange = getByText('Orange')
rerender(ui(true))
expect(orange).toHaveAttribute('aria-disabled', 'true')
await rawClick(orange)
await press(Keys.Enter)
expect(onChange).not.toHaveBeenCalled()
expect(getListbox()).toBeInTheDocument()
rerender(ui())
expect(orange).not.toHaveAttribute('aria-disabled')
await click(orange)
expect(onChange).toHaveBeenCalledWith('orange')
})
Original file line number Diff line number Diff line change
Expand Up @@ -817,12 +817,13 @@ function OptionFn<
let internalId = useId()
let {
id = `headlessui-listbox-option-${internalId}`,
disabled = false,
disabled: disabledProp = false,
value,
...theirProps
} = props
let usedInSelectedOption = useContext(SelectedOptionContext) === true
let data = useData('Listbox.Option')
let disabled = disabledProp || data.disabled
let machine = useListboxMachineContext('Listbox.Option')

let active = useSlice(machine, (state) => machine.selectors.isActive(state, id))
Expand Down
30 changes: 30 additions & 0 deletions packages/@headlessui-vue/src/components/listbox/listbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,36 @@ describe('safeguards', () => {
})

describe('Rendering', () => {
it('disables options when an open listbox becomes disabled', async () => {
let disabled = ref(false)
let value = ref('a')
renderTemplate({
template: html`
<Listbox v-model="value" :disabled="disabled">
<ListboxButton>Trigger</ListboxButton>
<ListboxOptions>
<ListboxOption value="a">Option A</ListboxOption>
<ListboxOption value="b">Option B</ListboxOption>
</ListboxOptions>
</Listbox>
`,
setup: () => ({ disabled, value }),
})

await click(getListboxButton())
disabled.value = true
await nextTick()
expect(getByText('Option B')).toHaveAttribute('aria-disabled', 'true')
await click(getByText('Option B'))
expect(value.value).toBe('a')
expect(getListbox()).not.toBeNull()

disabled.value = false
await nextTick()
await click(getByText('Option B'))
expect(value.value).toBe('b')
})

describe('Listbox', () => {
it(
'should be possible to render a Listbox using a render prop',
Expand Down
18 changes: 9 additions & 9 deletions packages/@headlessui-vue/src/components/listbox/listbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ export let ListboxOption = defineComponent({
},
setup(props, { slots, attrs, expose }) {
let api = useListboxContext('ListboxOption')
let disabled = computed(() => props.disabled || api.disabled.value)
let internalOptionRef = ref<HTMLElement | null>(null)

expose({ el: internalOptionRef, $el: internalOptionRef })
Expand Down Expand Up @@ -737,7 +738,7 @@ export let ListboxOption = defineComponent({

let getTextValue = useTextValue(internalOptionRef)
let dataRef = computed<ListboxOptionData>(() => ({
disabled: props.disabled,
disabled: disabled.value,
value: props.value,
get textValue() {
return getTextValue()
Expand Down Expand Up @@ -776,7 +777,7 @@ export let ListboxOption = defineComponent({
})

function handleClick(event: MouseEvent) {
if (props.disabled) return event.preventDefault()
if (disabled.value) return event.preventDefault()
api.select(props.value)
if (api.mode.value === ValueMode.Single) {
api.closeListbox()
Expand All @@ -785,7 +786,7 @@ export let ListboxOption = defineComponent({
}

function handleFocus() {
if (props.disabled) return api.goToOption(Focus.Nothing)
if (disabled.value) return api.goToOption(Focus.Nothing)
api.goToOption(Focus.Specific, props.id)
}

Expand All @@ -797,28 +798,27 @@ export let ListboxOption = defineComponent({

function handleMove(evt: PointerEvent) {
if (!pointer.wasMoved(evt)) return
if (props.disabled) return
if (disabled.value) return
if (active.value) return
api.goToOption(Focus.Specific, props.id, ActivationTrigger.Pointer)
}

function handleLeave(evt: PointerEvent) {
if (!pointer.wasMoved(evt)) return
if (props.disabled) return
if (disabled.value) return
if (!active.value) return
api.goToOption(Focus.Nothing)
}

return () => {
let { disabled } = props
let slot = { active: active.value, selected: selected.value, disabled }
let slot = { active: active.value, selected: selected.value, disabled: disabled.value }
let { id, value: _value, disabled: _disabled, ...theirProps } = props
let ourProps = {
id,
ref: internalOptionRef,
role: 'option',
tabIndex: disabled === true ? undefined : -1,
'aria-disabled': disabled === true ? true : undefined,
tabIndex: disabled.value === true ? undefined : -1,
'aria-disabled': disabled.value === true ? true : undefined,
// According to the WAI-ARIA best practices, we should use aria-checked for
// multi-select,but Voice-Over disagrees. So we use aria-checked instead for
// both single and multi-select.
Expand Down