50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import { ChevronDown, X } from 'lucide-react'
|
|
import { cx } from './cx'
|
|
|
|
export function FilterChip({
|
|
label,
|
|
value,
|
|
options,
|
|
onChange,
|
|
}: {
|
|
label: string
|
|
value: string
|
|
options: Array<{ value: string; label: string }>
|
|
onChange: (value: string) => void
|
|
}) {
|
|
const selected = options.find(o => o.value === value)
|
|
const set = Boolean(value && selected)
|
|
return (
|
|
<label
|
|
className={cx(
|
|
'relative inline-flex items-center gap-1 h-7 px-2.5 rounded-md text-[12.5px] whitespace-nowrap cursor-pointer',
|
|
set ? 'bg-accent-tint-2 text-accent-700 shadow-chip-set' : 'bg-surface text-ink-2 shadow-chip',
|
|
)}
|
|
>
|
|
<span>{set ? `${label}: ${selected!.label}` : label}</span>
|
|
{!set && <ChevronDown size={12} strokeWidth={2.5} aria-hidden />}
|
|
<select
|
|
aria-label={label}
|
|
value={value}
|
|
onChange={e => onChange(e.target.value)}
|
|
className="absolute inset-0 w-full h-full opacity-0 cursor-pointer"
|
|
>
|
|
<option value="">{label}</option>
|
|
{options.map(o => (
|
|
<option key={o.value} value={o.value}>{o.label}</option>
|
|
))}
|
|
</select>
|
|
{set && (
|
|
<button
|
|
type="button"
|
|
aria-label={`Clear ${label}`}
|
|
onClick={() => onChange('')}
|
|
className="relative z-10 inline-grid place-items-center w-4 h-4 cursor-pointer"
|
|
>
|
|
<X size={12} strokeWidth={2.5} aria-hidden />
|
|
</button>
|
|
)}
|
|
</label>
|
|
)
|
|
}
|