mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Improved the OrganizationCombobox.
This commit is contained in:
@@ -6,78 +6,77 @@ import {cn} from "@/lib/utils";
|
|||||||
import {Button} from "@/components/ui/button";
|
import {Button} from "@/components/ui/button";
|
||||||
import {Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList} from "@/components/ui/command";
|
import {Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList} from "@/components/ui/command";
|
||||||
import {Popover, PopoverContent, PopoverTrigger} from "@/components/ui/popover";
|
import {Popover, PopoverContent, PopoverTrigger} from "@/components/ui/popover";
|
||||||
import {FormControl} from "@/components/ui/form";
|
|
||||||
import {SidebarMenuButton} from "@/components/ui/sidebar";
|
import {SidebarMenuButton} from "@/components/ui/sidebar";
|
||||||
import {Separator} from "@/components/ui/separator";
|
import {Separator} from "@/components/ui/separator";
|
||||||
import {CreateOrganizationModal} from "@/components/wrappers/dashboard/organization/create-organisation-modal";
|
|
||||||
|
|
||||||
export type comboBoxProps = {
|
export type ComboBoxProps<T = string> = {
|
||||||
values: Array<{ value: string; label: string }>;
|
values: Array<{ value: T; label: string }>;
|
||||||
defaultValue?: string;
|
defaultValue?: T;
|
||||||
onValueChange?: any;
|
onValueChangeAction?: (value: T) => void;
|
||||||
searchField?: boolean;
|
searchField?: boolean;
|
||||||
sideBar?: boolean;
|
sideBar?: boolean;
|
||||||
reload?: () => void;
|
onAddItemAction?: () => void;
|
||||||
|
addItemLabel?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function ComboBox(props: comboBoxProps) {
|
export function ComboBox<T = string>(props: ComboBoxProps<T>) {
|
||||||
const {
|
const {
|
||||||
values: choices,
|
values: choices,
|
||||||
defaultValue: defaultChoice = "",
|
defaultValue,
|
||||||
onValueChange,
|
onValueChangeAction,
|
||||||
searchField = false,
|
searchField = false,
|
||||||
sideBar = false
|
sideBar = false,
|
||||||
|
onAddItemAction,
|
||||||
|
addItemLabel = "Add item",
|
||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
const [value, setValue] = useState<string>();
|
const [value, setValue] = useState<T | undefined>(defaultValue);
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setValue(defaultChoice);
|
setValue(defaultValue);
|
||||||
}, [defaultChoice]);
|
}, [defaultValue]);
|
||||||
|
|
||||||
const [open, setOpen] = useState(false);
|
|
||||||
const [openModal, setOpenModal] = useState(false);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<CreateOrganizationModal
|
|
||||||
open={openModal}
|
|
||||||
onSuccess={() => {
|
|
||||||
props.reload?.();
|
|
||||||
}}
|
|
||||||
onOpenChange={setOpenModal}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Popover open={open} onOpenChange={setOpen}>
|
<Popover open={open} onOpenChange={setOpen}>
|
||||||
<PopoverTrigger asChild>
|
<PopoverTrigger asChild>
|
||||||
{sideBar ? (
|
{sideBar ? (
|
||||||
<SidebarMenuButton>
|
<SidebarMenuButton>
|
||||||
{value ? choices.find((choice) => choice.value === value)?.label : "Select choice..."}
|
{value ? choices.find((c) => c.value === value)?.label : "Select choice..."}
|
||||||
<ChevronDown className="ml-auto"/>
|
<ChevronDown className="ml-auto"/>
|
||||||
</SidebarMenuButton>
|
</SidebarMenuButton>
|
||||||
) : (
|
) : (
|
||||||
<Button variant="outline" role="combobox" aria-expanded={open}
|
<Button variant="outline" role="combobox" aria-expanded={open}
|
||||||
className="w-full justify-between">
|
className="w-full justify-between">
|
||||||
{value ? choices.find((choice) => choice.value === value)?.label : "Select choice..."}
|
{value ? choices.find((c) => c.value === value)?.label : "Select choice..."}
|
||||||
<ChevronDown className="opacity-50"/>
|
<ChevronDown className="opacity-50"/>
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
</PopoverTrigger>
|
</PopoverTrigger>
|
||||||
<PopoverContent className="p-0 popover-content-width-full">
|
|
||||||
|
<PopoverContent
|
||||||
|
className="p-0"
|
||||||
|
align="start"
|
||||||
|
sideOffset={4}
|
||||||
|
style={{ width: 'var(--radix-popover-trigger-width)' }}
|
||||||
|
>
|
||||||
<Command>
|
<Command>
|
||||||
{searchField ? <CommandInput placeholder="Search choice..." className="h-9"/> : null}
|
{searchField && <CommandInput placeholder="Search choice..." className="h-9"/>}
|
||||||
<CommandList>
|
<CommandList>
|
||||||
<CommandEmpty>No choice found.</CommandEmpty>
|
<CommandEmpty>No choice found.</CommandEmpty>
|
||||||
<CommandGroup>
|
<CommandGroup>
|
||||||
{choices.map((choice) => (
|
{choices.map((choice) => (
|
||||||
<CommandItem
|
<CommandItem
|
||||||
key={choice.value}
|
key={String(choice.value)}
|
||||||
value={choice.value}
|
value={String(choice.value)}
|
||||||
onSelect={(currentValue) => {
|
onSelect={(currentValue) => {
|
||||||
setValue(currentValue);
|
const v = choices.find(c => String(c.value) === currentValue)?.value;
|
||||||
onValueChange(currentValue);
|
if (v !== undefined) {
|
||||||
setOpen(false);
|
setValue(v);
|
||||||
|
onValueChangeAction?.(v);
|
||||||
|
setOpen(false);
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{choice.label}
|
{choice.label}
|
||||||
@@ -88,66 +87,26 @@ export function ComboBox(props: comboBoxProps) {
|
|||||||
</CommandGroup>
|
</CommandGroup>
|
||||||
</CommandList>
|
</CommandList>
|
||||||
</Command>
|
</Command>
|
||||||
<Separator/>
|
{onAddItemAction && (
|
||||||
{sideBar ? (
|
<>
|
||||||
<SidebarMenuButton onClick={() => {
|
<Separator/>
|
||||||
setOpen(false)
|
{sideBar ? (
|
||||||
setOpenModal(true)
|
<SidebarMenuButton onClick={() => {
|
||||||
}
|
setOpen(false);
|
||||||
}>+ Create new organization</SidebarMenuButton>
|
onAddItemAction();
|
||||||
) : null}
|
}}>
|
||||||
|
+ {addItemLabel}
|
||||||
|
</SidebarMenuButton>
|
||||||
|
) : (
|
||||||
|
<Button variant="outline" className="w-full" onClick={onAddItemAction}>
|
||||||
|
+ {addItemLabel}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</PopoverContent>
|
</PopoverContent>
|
||||||
</Popover>
|
</Popover>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export type comboBoxFormItemProps = comboBoxProps & {
|
|
||||||
value: any;
|
|
||||||
name: any;
|
|
||||||
onChange: any;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function ComboBoxFormItem(props: comboBoxFormItemProps) {
|
|
||||||
const {values: choices, searchField = false, value, name, onChange} = props;
|
|
||||||
|
|
||||||
const [open, setOpen] = useState(false);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Popover open={open} onOpenChange={setOpen}>
|
|
||||||
<PopoverTrigger asChild>
|
|
||||||
<FormControl>
|
|
||||||
<Button variant="outline" role="combobox" aria-expanded={open}
|
|
||||||
className={cn("w-full justify-between", !value && "text-muted-foreground")}>
|
|
||||||
{value ? choices.find((choice) => choice.value === value)?.label : `Select ${name}`}
|
|
||||||
<ChevronDown className="opacity-50"/>
|
|
||||||
</Button>
|
|
||||||
</FormControl>
|
|
||||||
</PopoverTrigger>
|
|
||||||
<PopoverContent className="p-0 popover-content-width-full">
|
|
||||||
<Command>
|
|
||||||
{searchField ? <CommandInput placeholder="Search choice..." className="h-9"/> : null}
|
|
||||||
<CommandList>
|
|
||||||
<CommandEmpty>No {name} found.</CommandEmpty>
|
|
||||||
<CommandGroup>
|
|
||||||
{choices.map((choice) => (
|
|
||||||
<CommandItem
|
|
||||||
value={choice.label}
|
|
||||||
key={choice.value}
|
|
||||||
onSelect={() => {
|
|
||||||
onChange(choice.value);
|
|
||||||
setOpen(false);
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{choice.label}
|
|
||||||
<Check
|
|
||||||
className={cn("ml-auto", choice.value === value ? "opacity-100" : "opacity-0")}/>
|
|
||||||
</CommandItem>
|
|
||||||
))}
|
|
||||||
</CommandGroup>
|
|
||||||
</CommandList>
|
|
||||||
</Command>
|
|
||||||
</PopoverContent>
|
|
||||||
</Popover>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export function AppSidebar() {
|
|||||||
</SidebarContent>
|
</SidebarContent>
|
||||||
|
|
||||||
<SidebarMenu className="mb-2">
|
<SidebarMenu className="mb-2">
|
||||||
<SidebarMenuItem>
|
<SidebarMenuItem className="p-2">
|
||||||
<LoggedInButton/>
|
<LoggedInButton/>
|
||||||
</SidebarMenuItem>
|
</SidebarMenuItem>
|
||||||
</SidebarMenu>
|
</SidebarMenu>
|
||||||
|
|||||||
@@ -52,7 +52,10 @@ export function CreateOrganizationModal({open, onOpenChange, onSuccess}: createO
|
|||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
<Dialog open={open} onOpenChange={(state) => {
|
||||||
|
form.reset()
|
||||||
|
onOpenChange(state)
|
||||||
|
}}>
|
||||||
<DialogContent className="sm:max-w-[425px] w-full">
|
<DialogContent className="sm:max-w-[425px] w-full">
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle>Create a new organization</DialogTitle>
|
<DialogTitle>Create a new organization</DialogTitle>
|
||||||
|
|||||||
@@ -1,44 +1,50 @@
|
|||||||
"use client";
|
"use client"
|
||||||
|
|
||||||
import {ComboBox} from "@/components/wrappers/common/combobox";
|
import {ComboBox} from "@/components/wrappers/common/combobox";
|
||||||
import {useSidebar} from "@/components/ui/sidebar";
|
import {useSidebar} from "@/components/ui/sidebar";
|
||||||
import {useRouter} from "next/navigation";
|
import {useRouter} from "next/navigation";
|
||||||
import {authClient} from "@/lib/auth/auth-client";
|
import {authClient} from "@/lib/auth/auth-client";
|
||||||
|
import {CreateOrganizationModal} from "@/components/wrappers/dashboard/organization/create-organisation-modal";
|
||||||
|
import {useState} from "react";
|
||||||
|
|
||||||
export function OrganizationCombobox() {
|
export function OrganizationCombobox() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const {state} = useSidebar();
|
const {state} = useSidebar();
|
||||||
|
|
||||||
const {data: organizations, refetch} = authClient.useListOrganizations();
|
const {data: organizations, refetch} = authClient.useListOrganizations();
|
||||||
const {data: activeOrganization, refetch: refetchActiveOrga} = authClient.useActiveOrganization();
|
const {data: activeOrganization, refetch: refetchActiveOrga} = authClient.useActiveOrganization();
|
||||||
|
const [openModal, setOpenModal] = useState(false);
|
||||||
|
|
||||||
if (!organizations) return null;
|
if (!organizations) return null;
|
||||||
|
|
||||||
const values = organizations.map((organization) => {
|
const values = organizations.map(org => ({ value: org.slug, label: org.name }));
|
||||||
return {
|
|
||||||
value: organization.slug,
|
|
||||||
label: organization.name,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
const onValueChange = async (slug: string) => {
|
const onValueChange = async (slug: string) => {
|
||||||
|
await authClient.organization.setActive({ organizationSlug: slug });
|
||||||
await authClient.organization.setActive({
|
|
||||||
organizationSlug: slug,
|
|
||||||
});
|
|
||||||
router.refresh();
|
router.refresh();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleReset = () => {
|
const handleReload = () => {
|
||||||
refetch();
|
refetch();
|
||||||
refetchActiveOrga()
|
refetchActiveOrga();
|
||||||
router.refresh();
|
router.refresh();
|
||||||
}
|
};
|
||||||
|
|
||||||
return <>{state === "expanded" &&
|
return (
|
||||||
<ComboBox sideBar values={values} defaultValue={activeOrganization?.slug} onValueChange={onValueChange}
|
<>
|
||||||
reload={handleReset}/>}
|
<CreateOrganizationModal
|
||||||
</>;
|
open={openModal}
|
||||||
|
onSuccess={handleReload}
|
||||||
|
onOpenChange={setOpenModal}
|
||||||
|
/>
|
||||||
|
{state === "expanded" && (
|
||||||
|
<ComboBox
|
||||||
|
sideBar
|
||||||
|
values={values}
|
||||||
|
defaultValue={activeOrganization?.slug}
|
||||||
|
onValueChangeAction={onValueChange}
|
||||||
|
onAddItemAction={() => setOpenModal(true)}
|
||||||
|
addItemLabel="Create new organization"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user