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: combobox onclose state can be tracked #2395

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 23 additions & 2 deletions next-docs/public/examples/combobox/Default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ const Example = () => {
const filteredPeople0 = filter(query0, people);
const filteredPeople1 = filter(query1, people);

const onClose0 = (value: undefined) => {
const v = value as unknown;
console.log('State on close:');
for (const [key, val] of Object.entries(v as Object)) {
console.log(`${key}: ${val}`);
}
}

const onClose1 = (value: undefined) => {
const v = value as unknown;
if (v === null) {
console.log('State on close: nothing is selected');
} else {
console.log('State on close:');
for (const [key, val] of Object.entries(v as Object)) {
console.log(`${key}: ${val}`);
}
}
}


return (
<div className="flex flex-col lg:flex-row lg:justify-center items-center w-full gap-4">
<Combobox
Expand All @@ -45,7 +66,7 @@ const Example = () => {
>
{({ open }) => (
<>
<Combobox.Trigger open={open} className="min-w-[18.75rem]">
<Combobox.Trigger open={open} onClose={onClose0} className="min-w-[18.75rem]">
<Combobox.Input
open={open}
placeholder={'Choose a name...'}
Expand Down Expand Up @@ -90,7 +111,7 @@ const Example = () => {
>
{({ open }) => (
<>
<Combobox.Trigger open={open} className="min-w-[18.75rem]">
<Combobox.Trigger open={open} onClose={onClose1} className="min-w-[18.75rem]">
<Combobox.Input
open={open}
placeholder={'Choose a name...'}
Expand Down
15 changes: 13 additions & 2 deletions workspaces/core/src/combobox/Combobox.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef } from 'react';
import React, { forwardRef, useEffect, useState } from 'react';
import {
Combobox as HeadlessCombobox,
Transition as HeadlessTransition,
Expand Down Expand Up @@ -109,12 +109,23 @@ const Trigger = forwardRef<HTMLDivElement, WithChildren<SelectProps>>(({
children,
className,
innerLabel,
open,
onClose,
},
ref
) => {
const { size, input, popper, disabled, isError } =
const { value, size, input, popper, disabled, isError } =
useComboboxContext('Combobox.Trigger');

const [isOpened, setIsOpened] = useState(open);

useEffect(() => {
if (open !== isOpened) {
!open && onClose && onClose(value as undefined);
setIsOpened(open);
}
}, [open, value]);

return (
<div
tabIndex={-1}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type SelectProps = {
className?: string;
multiple?: boolean;
counter?: number;
onClose?: (value: undefined) => void;
};

export default SelectProps
Loading