fosrl.pangolin/src/components/StrategySelect.tsx

72 lines
2.4 KiB
TypeScript
Raw Normal View History

"use client";
import { cn } from "@app/lib/cn";
import { RadioGroup, RadioGroupItem } from "./ui/radio-group";
2025-03-16 15:20:19 -04:00
import { useState } from "react";
interface StrategyOption<TValue extends string> {
id: TValue;
title: string;
description: string;
disabled?: boolean;
}
interface StrategySelectProps<TValue extends string> {
options: ReadonlyArray<StrategyOption<TValue>>;
defaultValue?: TValue;
onChange?: (value: TValue) => void;
2025-03-16 15:20:19 -04:00
cols?: number;
}
export function StrategySelect<TValue extends string>({
options,
defaultValue,
2025-03-16 15:20:19 -04:00
onChange,
cols
}: StrategySelectProps<TValue>) {
const [selected, setSelected] = useState<TValue | undefined>(defaultValue);
2025-03-16 15:20:19 -04:00
return (
<RadioGroup
defaultValue={defaultValue}
onValueChange={(value: string) => {
const typedValue = value as TValue;
setSelected(typedValue);
onChange?.(typedValue);
2025-03-16 15:20:19 -04:00
}}
className={`grid md:grid-cols-${cols ? cols : 1} gap-4`}
>
{options.map((option: StrategyOption<TValue>) => (
<label
key={option.id}
htmlFor={option.id}
2025-03-16 15:20:19 -04:00
data-state={
selected === option.id ? "checked" : "unchecked"
}
className={cn(
2025-04-12 19:50:30 -04:00
"relative flex rounded-lg border p-4 transition-colors cursor-pointer",
2025-03-16 15:20:19 -04:00
option.disabled
? "border-input text-muted-foreground cursor-not-allowed opacity-50"
: selected === option.id
? "border-primary bg-primary/10 text-primary"
: "border-input hover:bg-accent"
)}
>
<RadioGroupItem
value={option.id}
id={option.id}
2025-03-16 15:20:19 -04:00
disabled={option.disabled}
className="absolute left-4 top-5 h-4 w-4 border-primary text-primary"
/>
<div className="pl-7">
<div className="font-medium">{option.title}</div>
<div className="text-sm text-muted-foreground">
{option.description}
</div>
</div>
</label>
))}
</RadioGroup>
);
}