implement custom value when custom is active on model selector
This commit is contained in:
parent
f4e1ca2ab7
commit
b3ae29db83
1 changed files with 145 additions and 102 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import { CheckIcon, ChevronsUpDownIcon } from "lucide-react";
|
import { CheckIcon, ChevronsUpDownIcon } from "lucide-react";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
import { Badge } from "@/components/ui/badge";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import {
|
import {
|
||||||
Command,
|
Command,
|
||||||
|
|
@ -24,6 +25,7 @@ export function ModelSelector({
|
||||||
placeholder = "Select model...",
|
placeholder = "Select model...",
|
||||||
searchPlaceholder = "Search model...",
|
searchPlaceholder = "Search model...",
|
||||||
noOptionsPlaceholder = "No models available",
|
noOptionsPlaceholder = "No models available",
|
||||||
|
custom = false,
|
||||||
}: {
|
}: {
|
||||||
options: {
|
options: {
|
||||||
value: string;
|
value: string;
|
||||||
|
|
@ -35,14 +37,17 @@ export function ModelSelector({
|
||||||
placeholder?: string;
|
placeholder?: string;
|
||||||
searchPlaceholder?: string;
|
searchPlaceholder?: string;
|
||||||
noOptionsPlaceholder?: string;
|
noOptionsPlaceholder?: string;
|
||||||
|
custom?: boolean;
|
||||||
onValueChange: (value: string) => void;
|
onValueChange: (value: string) => void;
|
||||||
}) {
|
}) {
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
|
const [searchValue, setSearchValue] = useState("");
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (value && !options.find((option) => option.value === value)) {
|
if (value && (!options.find((option) => option.value === value) && !custom)) {
|
||||||
onValueChange("");
|
onValueChange("");
|
||||||
}
|
}
|
||||||
}, [options, value, onValueChange]);
|
}, [options, value, custom, onValueChange]);
|
||||||
return (
|
return (
|
||||||
<Popover open={open} onOpenChange={setOpen}>
|
<Popover open={open} onOpenChange={setOpen}>
|
||||||
<PopoverTrigger asChild>
|
<PopoverTrigger asChild>
|
||||||
|
|
@ -57,13 +62,21 @@ export function ModelSelector({
|
||||||
{value ? (
|
{value ? (
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
{icon && <div className="w-4 h-4">{icon}</div>}
|
{icon && <div className="w-4 h-4">{icon}</div>}
|
||||||
{options.find((framework) => framework.value === value)?.label}
|
{options.find((framework) => framework.value === value)?.label ||
|
||||||
|
value}
|
||||||
{options.find((framework) => framework.value === value)
|
{options.find((framework) => framework.value === value)
|
||||||
?.default && (
|
?.default && (
|
||||||
<span className="text-xs text-foreground p-1 rounded-md bg-muted">
|
<span className="text-xs text-foreground p-1 rounded-md bg-muted">
|
||||||
Default
|
Default
|
||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
|
{custom &&
|
||||||
|
value &&
|
||||||
|
!options.find((framework) => framework.value === value) && (
|
||||||
|
<Badge variant="outline" className="text-xs">
|
||||||
|
CUSTOM
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
) : options.length === 0 ? (
|
) : options.length === 0 ? (
|
||||||
noOptionsPlaceholder
|
noOptionsPlaceholder
|
||||||
|
|
@ -75,7 +88,11 @@ export function ModelSelector({
|
||||||
</PopoverTrigger>
|
</PopoverTrigger>
|
||||||
<PopoverContent align="start" className="w-[400px] p-0">
|
<PopoverContent align="start" className="w-[400px] p-0">
|
||||||
<Command>
|
<Command>
|
||||||
<CommandInput placeholder={searchPlaceholder} />
|
<CommandInput
|
||||||
|
placeholder={searchPlaceholder}
|
||||||
|
value={searchValue}
|
||||||
|
onValueChange={setSearchValue}
|
||||||
|
/>
|
||||||
<CommandList>
|
<CommandList>
|
||||||
<CommandEmpty>{noOptionsPlaceholder}</CommandEmpty>
|
<CommandEmpty>{noOptionsPlaceholder}</CommandEmpty>
|
||||||
<CommandGroup>
|
<CommandGroup>
|
||||||
|
|
@ -106,6 +123,32 @@ export function ModelSelector({
|
||||||
</div>
|
</div>
|
||||||
</CommandItem>
|
</CommandItem>
|
||||||
))}
|
))}
|
||||||
|
{custom &&
|
||||||
|
searchValue &&
|
||||||
|
!options.find((option) => option.value === searchValue) && (
|
||||||
|
<CommandItem
|
||||||
|
value={searchValue}
|
||||||
|
onSelect={(currentValue) => {
|
||||||
|
if (currentValue !== value) {
|
||||||
|
onValueChange(currentValue);
|
||||||
|
}
|
||||||
|
setOpen(false);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<CheckIcon
|
||||||
|
className={cn(
|
||||||
|
"mr-2 h-4 w-4",
|
||||||
|
value === searchValue ? "opacity-100" : "opacity-0",
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
{searchValue}
|
||||||
|
<span className="text-xs text-foreground p-1 rounded-md bg-muted">
|
||||||
|
Custom
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</CommandItem>
|
||||||
|
)}
|
||||||
</CommandGroup>
|
</CommandGroup>
|
||||||
</CommandList>
|
</CommandList>
|
||||||
</Command>
|
</Command>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue