Skip to content

Commit

Permalink
convert to immediate prop on the Combobox itself
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Aug 31, 2023
1 parent 4577735 commit 6a36fba
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4699,11 +4699,11 @@ describe('Mouse interactions', () => {
)

it(
'should be possible to open the combobox by focusing the input with openOnFocus enabled',
'should be possible to open the combobox by focusing the input with immediate mode enabled',
suppressConsoleLogs(async () => {
render(
<Combobox value="test">
<Combobox.Input onChange={NOOP} openOnFocus />
<Combobox value="test" immediate>
<Combobox.Input onChange={NOOP} />
<Combobox.Button>Trigger</Combobox.Button>
<Combobox.Options>
<Combobox.Option value="a">Option A</Combobox.Option>
Expand Down Expand Up @@ -4739,7 +4739,7 @@ describe('Mouse interactions', () => {
)

it(
'should not be possible to open the combobox by focusing the input with openOnFocus disabled',
'should not be possible to open the combobox by focusing the input with immediate mode disabled',
suppressConsoleLogs(async () => {
render(
<Combobox value="test">
Expand Down Expand Up @@ -4772,11 +4772,11 @@ describe('Mouse interactions', () => {
)

it(
'should not be possible to open the combobox by focusing the input with openOnFocus enabled when button is disabled',
'should not be possible to open the combobox by focusing the input with immediate mode enabled when button is disabled',
suppressConsoleLogs(async () => {
render(
<Combobox value="test" disabled>
<Combobox.Input onChange={NOOP} openOnFocus />
<Combobox value="test" disabled immediate>
<Combobox.Input onChange={NOOP} />
<Combobox.Button>Trigger</Combobox.Button>
<Combobox.Options>
<Combobox.Option value="a">Option A</Combobox.Option>
Expand Down Expand Up @@ -4805,11 +4805,11 @@ describe('Mouse interactions', () => {
)

it(
'should be possible to close a combobox on click with openOnFocus enabled',
'should be possible to close a combobox on click with immediate mode enabled',
suppressConsoleLogs(async () => {
render(
<Combobox value="test">
<Combobox.Input onChange={NOOP} openOnFocus />
<Combobox value="test" immediate>
<Combobox.Input onChange={NOOP} />
<Combobox.Button>Trigger</Combobox.Button>
<Combobox.Options>
<Combobox.Option value="a">Option A</Combobox.Option>
Expand All @@ -4836,11 +4836,11 @@ describe('Mouse interactions', () => {
)

it(
'should be possible to close a focused combobox on click with openOnFocus enabled',
'should be possible to close a focused combobox on click with immediate mode enabled',
suppressConsoleLogs(async () => {
render(
<Combobox value="test">
<Combobox.Input onChange={NOOP} openOnFocus />
<Combobox value="test" immediate>
<Combobox.Input onChange={NOOP} />
<Combobox.Button>Trigger</Combobox.Button>
<Combobox.Options>
<Combobox.Option value="a">Option A</Combobox.Option>
Expand Down Expand Up @@ -5529,11 +5529,11 @@ describe('Mouse interactions', () => {
)

it(
'should be possible to click a combobox option, which closes the combobox with openOnFocus enabled',
'should be possible to click a combobox option, which closes the combobox with immediate mode enabled',
suppressConsoleLogs(async () => {
render(
<Combobox value="test">
<Combobox.Input onChange={NOOP} openOnFocus />
<Combobox value="test" immediate>
<Combobox.Input onChange={NOOP} />
<Combobox.Button>Trigger</Combobox.Button>
<Combobox.Options>
<Combobox.Option value="a">Option A</Combobox.Option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ let ComboboxDataContext = createContext<
mode: ValueMode
activeOptionIndex: number | null
nullable: boolean
immediate: boolean
compare(a: unknown, z: unknown): boolean
isSelected(value: unknown): boolean
__demoMode: boolean
Expand Down Expand Up @@ -395,6 +396,7 @@ export type ComboboxProps<
__demoMode?: boolean
form?: string
name?: string
immediate?: boolean
}

function ComboboxFn<TValue, TTag extends ElementType = typeof DEFAULT_COMBOBOX_TAG>(
Expand Down Expand Up @@ -429,6 +431,7 @@ function ComboboxFn<TValue, TTag extends ElementType = typeof DEFAULT_COMBOBOX_T
__demoMode = false,
nullable = false,
multiple = false,
immediate = false,
...theirProps
} = props
let [value = multiple ? [] : undefined, theirOnChange] = useControllable<any>(
Expand Down Expand Up @@ -479,6 +482,7 @@ function ComboboxFn<TValue, TTag extends ElementType = typeof DEFAULT_COMBOBOX_T
let data = useMemo<_Data>(
() => ({
...state,
immediate,
optionsPropsRef,
labelRef,
inputRef,
Expand Down Expand Up @@ -734,7 +738,6 @@ export type ComboboxInputProps<TTag extends ElementType, TType> = Props<
defaultValue?: TType
displayValue?(item: TType): string
onChange?(event: React.ChangeEvent<HTMLInputElement>): void
openOnFocus?: boolean
}
>

Expand All @@ -749,7 +752,6 @@ function InputFn<
id = `headlessui-combobox-input-${internalId}`,
onChange,
displayValue,
openOnFocus = false,
// @ts-ignore: We know this MAY NOT exist for a given tag but we only care when it _does_ exist.
type = 'text',
...theirProps
Expand Down Expand Up @@ -1080,7 +1082,7 @@ function InputFn<
if (data.optionsRef.current?.contains(event.relatedTarget as HTMLElement)) return
if (data.disabled) return

if (!openOnFocus) return
if (!data.immediate) return
if (data.comboboxState === ComboboxState.Open) return

actions.openCombobox()
Expand Down
32 changes: 16 additions & 16 deletions packages/@headlessui-vue/src/components/combobox/combobox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5004,12 +5004,12 @@ describe('Mouse interactions', () => {
)

it(
'should be possible to open the combobox by focusing the input with openOnFocus enabled',
'should be possible to open the combobox by focusing the input with immediate mode enabled',
suppressConsoleLogs(async () => {
renderTemplate({
template: html`
<Combobox v-model="value" as="div">
<ComboboxInput openOnFocus />
<Combobox v-model="value" as="div" immediate>
<ComboboxInput />
<ComboboxButton>Trigger</ComboboxButton>
<ComboboxOptions>
<ComboboxOption value="a">Option A</ComboboxOption>
Expand Down Expand Up @@ -5050,7 +5050,7 @@ describe('Mouse interactions', () => {
)

it(
'should not be possible to open the combobox by focusing the input with openOnFocus disabled',
'should not be possible to open the combobox by focusing the input with immediate mode disabled',
suppressConsoleLogs(async () => {
renderTemplate({
template: html`
Expand Down Expand Up @@ -5086,12 +5086,12 @@ describe('Mouse interactions', () => {
)

it(
'should not be possible to open the combobox by focusing the input with openOnFocus enabled when button is disabled',
'should not be possible to open the combobox by focusing the input with immediate mode enabled when button is disabled',
suppressConsoleLogs(async () => {
renderTemplate({
template: html`
<Combobox v-model="value" as="div" disabled>
<ComboboxInput openOnFocus />
<Combobox v-model="value" as="div" disabled immediate>
<ComboboxInput />
<ComboboxButton>Trigger</ComboboxButton>
<ComboboxOptions>
<ComboboxOption value="a">Option A</ComboboxOption>
Expand Down Expand Up @@ -5122,12 +5122,12 @@ describe('Mouse interactions', () => {
)

it(
'should be possible to close a combobox on click with openOnFocus enabled',
'should be possible to close a combobox on click with immediate mode enabled',
suppressConsoleLogs(async () => {
renderTemplate({
template: html`
<Combobox v-model="value">
<ComboboxInput openOnFocus />
<Combobox v-model="value" immediate>
<ComboboxInput />
<ComboboxButton>Trigger</ComboboxButton>
<ComboboxOptions>
<ComboboxOption value="a">Option A</ComboboxOption>
Expand Down Expand Up @@ -5156,12 +5156,12 @@ describe('Mouse interactions', () => {
)

it(
'should be possible to close a focused combobox on click with openOnFocus enabled',
'should be possible to close a focused combobox on click with immediate mode enabled',
suppressConsoleLogs(async () => {
renderTemplate({
template: html`
<Combobox v-model="value">
<ComboboxInput openOnFocus />
<Combobox v-model="value" immediate>
<ComboboxInput />
<ComboboxButton>Trigger</ComboboxButton>
<ComboboxOptions>
<ComboboxOption value="a">Option A</ComboboxOption>
Expand Down Expand Up @@ -5891,12 +5891,12 @@ describe('Mouse interactions', () => {
)

it(
'should be possible to click a combobox option, which closes the combobox with openOnFocus enabled',
'should be possible to click a combobox option, which closes the combobox with immediate mode enabled',
suppressConsoleLogs(async () => {
renderTemplate({
template: html`
<Combobox v-model="value">
<ComboboxInput openOnFocus />
<Combobox v-model="value" immediate>
<ComboboxInput />
<ComboboxButton>Trigger</ComboboxButton>
<ComboboxOptions>
<ComboboxOption value="alice">alice</ComboboxOption>
Expand Down
6 changes: 4 additions & 2 deletions packages/@headlessui-vue/src/components/combobox/combobox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export let Combobox = defineComponent({
name: { type: String, optional: true },
nullable: { type: Boolean, default: false },
multiple: { type: [Boolean], default: false },
immediate: { type: [Boolean], default: false },
},
inheritAttrs: false,
setup(props, { slots, attrs, emit }) {
Expand Down Expand Up @@ -223,6 +224,7 @@ export let Combobox = defineComponent({
},
defaultValue: computed(() => props.defaultValue),
nullable,
immediate: computed(() => props.immediate),
inputRef,
labelRef,
buttonRef,
Expand Down Expand Up @@ -536,6 +538,7 @@ export let Combobox = defineComponent({
'defaultValue',
'nullable',
'multiple',
'immediate',
'onUpdate:modelValue',
'by',
]),
Expand Down Expand Up @@ -700,7 +703,6 @@ export let ComboboxInput = defineComponent({
displayValue: { type: Function as PropType<(item: unknown) => string> },
defaultValue: { type: String, default: undefined },
id: { type: String, default: () => `headlessui-combobox-input-${useId()}` },
openOnFocus: { type: Boolean, default: false },
},
emits: {
change: (_value: Event & { target: HTMLInputElement }) => true,
Expand Down Expand Up @@ -1038,7 +1040,7 @@ export let ComboboxInput = defineComponent({
if (dom(api.optionsRef)?.contains(event.relatedTarget as HTMLElement)) return
if (api.disabled.value) return

if (!props.openOnFocus) return
if (!api.immediate.value) return
if (api.comboboxState.value === ComboboxStates.Open) return

api.openCombobox()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default function Home() {
setActivePerson(value)
setQuery('')
}}
immediate
as="div"
>
<Combobox.Label className="block text-sm font-medium leading-5 text-gray-700">
Expand All @@ -64,7 +65,6 @@ export default function Home() {
<Combobox.Input
onChange={(e) => setQuery(e.target.value)}
className="border-none px-3 py-1 outline-none"
openOnFocus
/>
<Combobox.Button as={Button}>
<span className="pointer-events-none flex items-center px-2">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default defineComponent({
Selected person: {{ activePerson?.name ?? 'Nobody yet' }}
</div>
<div class="space-y-1">
<Combobox v-model="activePerson" as="div">
<Combobox v-model="activePerson" as="div" immediate>
<ComboboxLabel class="block text-sm font-medium leading-5 text-gray-700">
Assigned to
</ComboboxLabel>
Expand All @@ -77,7 +77,6 @@ export default defineComponent({
<ComboboxInput
@change="query = $event.target.value"
:displayValue="(person) => person?.name ?? ''"
:open-on-focus="true"
class="border-none px-3 py-1 outline-none"
/>
<ComboboxButton
Expand Down

0 comments on commit 6a36fba

Please sign in to comment.