Skip to content

Commit

Permalink
Add onOpenChange function as prop to select (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
vineethasok authored Oct 9, 2023
1 parent 7952128 commit 630a1e6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/components/Select/MultiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface MultiSelectProps
onSelect?: (value: Array<string>) => void;
value?: Array<string>;
defaultOpen?: boolean;
onOpenChange?: (open: boolean) => void;
}

export const MultiSelect = ({
Expand All @@ -20,13 +21,24 @@ export const MultiSelect = ({
onSelect: onSelectProp,
options,
children,
onOpenChange: onOpenChangeProp,
...props
}: MultiSelectProps) => {
const [selectedValues, setSelectedValues] = useState<Array<string>>(
valueProp ?? defaultValue ?? []
);
const [open, setOpen] = useState(false);

const onOpenChange = useCallback(
(open: boolean) => {
setOpen(open);
if (typeof onOpenChangeProp === "function") {
onOpenChangeProp(open);
}
},
[onOpenChangeProp]
);

useEffect(() => {
setSelectedValues(valueProp ?? []);
}, [valueProp]);
Expand Down Expand Up @@ -67,7 +79,7 @@ export const MultiSelect = ({
onChange={onChange}
value={valueProp ?? selectedValues}
open={open}
onOpenChange={setOpen}
onOpenChange={onOpenChange}
onSelect={onSelect}
multiple
{...conditionalProps}
Expand Down
14 changes: 11 additions & 3 deletions src/components/Select/SingleSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface SelectProps
onSelect?: (value: string) => void;
value?: string;
placeholder?: string;
onOpenChange?: (open: boolean) => void;
}

export const Select = ({
Expand All @@ -19,16 +20,23 @@ export const Select = ({
onSelect: onSelectProp,
options,
children,
onOpenChange: onOpenChangeProp,
...props
}: SelectProps) => {
const [selectedValues, setSelectedValues] = useState<Array<string>>(
valueProp ? [valueProp] : defaultValue ? [defaultValue] : []
);
const [open, setOpen] = useState(false);

const onOpenChange = useCallback((open: boolean) => {
setOpen(open);
}, []);
const onOpenChange = useCallback(
(open: boolean) => {
setOpen(open);
if (typeof onOpenChangeProp === "function") {
onOpenChangeProp(open);
}
},
[onOpenChangeProp]
);

const onSelect = useCallback(
(value: string) => {
Expand Down

1 comment on commit 630a1e6

@vercel
Copy link

@vercel vercel bot commented on 630a1e6 Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

click-ui – ./

click-ui-clickhouse.vercel.app
click-ui.vercel.app
click-ui-git-main-clickhouse.vercel.app

Please sign in to comment.