new multi select tag input

This commit is contained in:
Fred KISSIE
2026-04-25 04:47:31 +02:00
parent 955aa41f53
commit 95cbaaae21
2 changed files with 58 additions and 55 deletions

View File

@@ -5,10 +5,7 @@ import { useMemo, useState } from "react";
import { useDebounce } from "use-debounce"; import { useDebounce } from "use-debounce";
import { useTranslations } from "next-intl"; import { useTranslations } from "next-intl";
import { import { MultiSelectInput } from "./multi-select/multi-select-input";
SuggestionsTagInput,
type SuggestionsTagInputProps
} from "./tags/suggestions-tag-input";
export type SelectedMachine = Pick< export type SelectedMachine = Pick<
ListClientsResponse["clients"][number], ListClientsResponse["clients"][number],
@@ -19,22 +16,12 @@ export type MachineSelectorProps = {
orgId: string; orgId: string;
selectedMachines?: SelectedMachine[]; selectedMachines?: SelectedMachine[];
onSelectMachines: (machine: SelectedMachine[]) => void; onSelectMachines: (machine: SelectedMachine[]) => void;
} & Omit< };
SuggestionsTagInputProps,
| "tags"
| "setTags"
| "suggestedOptions"
| "searchQuery"
| "onSearchQueryChange"
| "activeTagIndex"
| "setActiveTagIndex"
>;
export function MachinesSelector({ export function MachinesSelector({
orgId, orgId,
selectedMachines = [], selectedMachines = [],
onSelectMachines, onSelectMachines
...props
}: MachineSelectorProps) { }: MachineSelectorProps) {
const t = useTranslations(); const t = useTranslations();
const [machineSearchQuery, setMachineSearchQuery] = useState(""); const [machineSearchQuery, setMachineSearchQuery] = useState("");
@@ -60,42 +47,29 @@ export function MachinesSelector({
return allMachines; return allMachines;
}, [machines, selectedMachines, debouncedValue]); }, [machines, selectedMachines, debouncedValue]);
const [activeTagIndex, setActiveTagIndex] = useState<number | null>(null);
return ( return (
<SuggestionsTagInput <MultiSelectInput
{...props} buttonText={t("accessClientSelect")}
activeTagIndex={activeTagIndex} searchPlaceholder={t("search")}
setActiveTagIndex={setActiveTagIndex} emptyPlaceholder={t("machineNotFound")}
placeholder={t("accessClientSelect")} searchQuery={machineSearchQuery}
tags={selectedMachines.map((mc) => ({ onSearch={setMachineSearchQuery}
options={machinesShown.map((mc) => ({
id: mc.clientId.toString(), id: mc.clientId.toString(),
text: mc.name text: mc.name
}))} }))}
setTags={(newTags) => { value={selectedMachines.map((mc) => ({
const tags = id: mc.clientId.toString(),
typeof newTags === "function" text: mc.name
? newTags( }))}
selectedMachines.map((mc) => ({ onChange={(newValues) => {
id: mc.clientId.toString(),
text: mc.name
}))
)
: newTags;
onSelectMachines( onSelectMachines(
tags.map((tag) => ({ newValues.map((v) => ({
clientId: Number(tag.id), clientId: Number(v.id),
name: tag.text name: v.text
})) }))
); );
}} }}
searchQuery={machineSearchQuery}
onSearchQueryChange={setMachineSearchQuery}
suggestedOptions={machinesShown.map((mc) => ({
id: mc.clientId.toString(),
text: mc.name
}))}
allowDuplicates={false}
/> />
); );
} }

View File

@@ -3,14 +3,15 @@ import {
PopoverContent, PopoverContent,
PopoverTrigger PopoverTrigger
} from "@app/components/ui/popover"; } from "@app/components/ui/popover";
import { Button } from "@app/components/ui/button"; import { Button, buttonVariants } from "@app/components/ui/button";
import { cn } from "@app/lib/cn"; import { cn } from "@app/lib/cn";
import { ChevronDownIcon } from "lucide-react"; import { ChevronDownIcon, XIcon } from "lucide-react";
import { import {
type TagValue, type TagValue,
type MultiSelectTagsProps, type MultiSelectTagsProps,
MultiSelectTags MultiSelectTags
} from "./multi-select-tags"; } from "./multi-select-tags";
import { useState } from "react";
export interface MultiSelectInputProps< export interface MultiSelectInputProps<
T extends TagValue T extends TagValue
@@ -22,13 +23,20 @@ export function MultiSelectInput<T extends TagValue>({
buttonText, buttonText,
...props ...props
}: MultiSelectInputProps<T>) { }: MultiSelectInputProps<T>) {
const selectedValues = new Set(props.value.map((v) => v.id));
return ( return (
<Popover> <Popover>
<PopoverTrigger> <PopoverTrigger asChild>
<div <div
role="combobox"
className={cn( className={cn(
"justify-between w-full", buttonVariants({
"text-muted-foreground pl-1.5 cursor-text" variant: "outline"
}),
"justify-between w-full inline-flex",
"text-muted-foreground pl-1.5 cursor-text",
"hover:bg-transparent hover:text-muted-foreground"
)} )}
> >
<span <span
@@ -37,20 +45,41 @@ export function MultiSelectInput<T extends TagValue>({
"overflow-x-auto" "overflow-x-auto"
)} )}
> >
{/* {(field.value ?? []).map((client) => ( {props.value.map((option) => (
<span <span
key={client.clientId} key={option.id}
className={cn( className={cn(
"bg-muted-foreground/20 font-normal text-foreground rounded-sm", "bg-muted-foreground/20 font-normal text-foreground rounded-sm",
"py-1 px-1.5 text-xs" "py-1 pl-1.5 pr-0.5 text-xs inline-flex items-center gap-0.5"
)} )}
onClick={(e) => e.stopPropagation()}
> >
{client.name} {option.text}
<button
className="p-0.5 flex-none cursor-pointer"
onClick={(e) => {
e.stopPropagation();
let newValues = [];
if (selectedValues.has(option.id)) {
newValues = props.value.filter(
(v) => v.id !== option.id
);
} else {
newValues = [
...props.value,
option
];
}
props.onChange(newValues);
}}
>
<XIcon className="size-3.5" />
</button>
</span> </span>
))} */} ))}
<span className="pl-1 font-normal">{buttonText}</span> <span className="pl-1 font-normal">{buttonText}</span>
</span> </span>
<ChevronDownIcon className="ml-2 h-4 w-4 shrink-0 opacity-50" /> <ChevronDownIcon className="ml-2 h-4 w-4 shrink-0 text-muted-foreground" />
</div> </div>
</PopoverTrigger> </PopoverTrigger>
<PopoverContent className="p-0"> <PopoverContent className="p-0">