medkit/src/kit/time-range-input.tsx

63 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { XIcon } from "lucide-react"
import { cn } from "@/lib/utils"
export interface TimeRangeValue {
from: string
to: string
}
/** Диапазон времени «с–по» в одну строку; подсвечивается, если «по» раньше «с». */
export function TimeRangeInput({
value,
onChange,
onRemove,
className,
}: {
value: TimeRangeValue
onChange: (value: TimeRangeValue) => void
onRemove?: () => void
className?: string
}) {
const invalid = Boolean(value.from && value.to && value.to <= value.from)
const input =
"h-7 w-[4.6rem] rounded-md bg-transparent px-1 font-mono text-xs tabular outline-none focus:bg-accent/60 [&::-webkit-calendar-picker-indicator]:hidden"
return (
<div
aria-invalid={invalid}
className={cn(
"group flex items-center rounded-lg border bg-card pr-0.5 transition focus-within:border-primary/50",
invalid && "border-destructive/60 bg-destructive/5",
className,
)}
>
<input
type="time"
aria-label="С"
value={value.from}
onChange={(e) => onChange({ ...value, from: e.target.value })}
className={input}
/>
<span className="text-xs text-muted-foreground">–</span>
<input
type="time"
aria-label="По"
value={value.to}
onChange={(e) => onChange({ ...value, to: e.target.value })}
className={input}
/>
{onRemove && (
<button
type="button"
aria-label="Удалить время"
onClick={onRemove}
className="grid size-5 place-items-center rounded text-muted-foreground opacity-0 transition group-hover:opacity-100 hover:bg-muted hover:text-destructive focus-visible:opacity-100"
>
<XIcon className="size-3" />
</button>
)}
</div>
)
}