mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-06 11:36:28 +00:00
fb2d746fc0
Introduce a set of reusable UI primitives and integrate tooltip provider in the app layout. Adds: Combobox (with virtualized and non-virtualized variants), Command palette components (cmdk wrapper and related subcomponents), DatePicker and DateTimePicker (popover-based date/time selection with time pickers and Done footer), LazyImage (progressive image with blur/scale transition), and Tooltip (Radix wrapper and provider). Also imports TooltipProvider in app/layout.tsx and wraps the app content so tooltips are available globally.
134 lines
6.3 KiB
TypeScript
134 lines
6.3 KiB
TypeScript
import { Check, ChevronsUpDown } from "lucide-react"
|
|
|
|
import { cn } from "@/lib/utils"
|
|
import { Button } from "@/components/ui/button"
|
|
import {
|
|
Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList,
|
|
} from "@/components/ui/command"
|
|
import {
|
|
Popover, PopoverContent, PopoverTrigger,
|
|
} from "@/components/ui/popover"
|
|
import { Dispatch, SetStateAction, useDeferredValue, useMemo, useState } from "react"
|
|
import { VList } from "virtua"
|
|
|
|
type Props = {
|
|
title?: string
|
|
options: { value: any, label: any }[]
|
|
value: any
|
|
setValue: Dispatch<SetStateAction<any>>
|
|
onSelectItem?: (item: any) => any
|
|
className?: string
|
|
disabled?: boolean
|
|
};
|
|
|
|
export function Combobox({ value, setValue, onSelectItem, options, title, className, disabled }: Props) {
|
|
const [open, setOpen] = useState(false)
|
|
const [search, setSearch] = useState("")
|
|
|
|
// Deferred state for the heavy filtering
|
|
const deferredSearch = useDeferredValue(search)
|
|
|
|
const filteredOptions = useMemo(() => {
|
|
return options.filter((item) =>
|
|
item.label.toLowerCase().includes(deferredSearch.toLowerCase())
|
|
)
|
|
}, [deferredSearch, options])
|
|
|
|
return (
|
|
<Popover open={open} onOpenChange={setOpen}>
|
|
<PopoverTrigger asChild disabled={disabled}>
|
|
<Button variant="outline" role="combobox" aria-expanded={open} className={cn("justify-between", !value && "text-muted-foreground", className)}>
|
|
{value ? options.find((item) => item.value === value)?.label : `Pilih ${title || 'data'}...`}
|
|
<ChevronsUpDown className="opacity-50" />
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<PopoverContent className='w-(--radix-popover-trigger-width)'>
|
|
<Command shouldFilter={false}>
|
|
<CommandInput placeholder={`Cari ${title || 'data'}...`} value={search} onValueChange={(val) => setSearch(val)} />
|
|
<CommandList className="max-h-64 overflow-auto">
|
|
{/* MANUAL SEARCH */}
|
|
{filteredOptions.length === 0 ? (
|
|
<CommandEmpty>Data tidak ditemukan.</CommandEmpty>
|
|
) : (
|
|
<CommandGroup>
|
|
<VList style={{ height: Math.min(filteredOptions.length * 40, 240) }} // Dynamic height, max 240px (6 items)
|
|
>
|
|
{filteredOptions.map((item) => (
|
|
<CommandItem
|
|
key={item.value}
|
|
value={item.value}
|
|
onSelect={() => {
|
|
setValue(item.value === value ? "" : item.value);
|
|
if (onSelectItem) onSelectItem(item)
|
|
setOpen(false)
|
|
setSearch('')
|
|
}}
|
|
// className="h-10" // Fixed height for each item
|
|
>
|
|
{item.label}
|
|
<Check className={cn("ml-auto h-4 w-4", value === item.value ? "opacity-100" : "opacity-0")} />
|
|
</CommandItem>
|
|
))}
|
|
</VList>
|
|
</CommandGroup>
|
|
)}
|
|
</CommandList>
|
|
</Command>
|
|
</PopoverContent>
|
|
</Popover >
|
|
)
|
|
}
|
|
|
|
export function ComboboxWithoutVirtua({ value, setValue, onSelectItem, options, title, className, disabled }: Props) {
|
|
const [open, setOpen] = useState(false)
|
|
const [search, setSearch] = useState("")
|
|
|
|
const deferredSearch = useDeferredValue(search)
|
|
|
|
const filteredOptions = useMemo(() => {
|
|
return options.filter((item) =>
|
|
item.label.toLowerCase().includes(deferredSearch.toLowerCase())
|
|
)
|
|
}, [deferredSearch, options])
|
|
|
|
return (
|
|
<Popover open={open} onOpenChange={setOpen}>
|
|
<PopoverTrigger asChild disabled={disabled}>
|
|
<Button variant="outline" role="combobox" aria-expanded={open} className={cn("justify-between", className)}>
|
|
{value ? options.find((item) => item.value === value)?.label : `Pilih ${title || 'data'}...`}
|
|
<ChevronsUpDown className="opacity-50" />
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<PopoverContent className="p-0">
|
|
<Command shouldFilter={false}>
|
|
<CommandInput placeholder={`Cari ${title || 'data'}...`} value={search} onValueChange={(val) => setSearch(val)} />
|
|
<CommandList className="max-h-64 overflow-auto">
|
|
{/* MANUAL SEARCH */}
|
|
{filteredOptions.length === 0 ? (
|
|
<CommandEmpty>Data tidak ditemukan.</CommandEmpty>
|
|
) : (
|
|
<CommandGroup>
|
|
{filteredOptions.map((item) => (
|
|
<CommandItem
|
|
key={item.value}
|
|
value={item.value}
|
|
onSelect={() => {
|
|
setValue(item.value === value ? "" : item.value);
|
|
if (onSelectItem) onSelectItem(item)
|
|
setOpen(false)
|
|
setSearch('')
|
|
}}
|
|
>
|
|
{item.label}
|
|
<Check className={cn("ml-auto h-4 w-4", value === item.value ? "opacity-100" : "opacity-0")} />
|
|
</CommandItem>
|
|
))}
|
|
</CommandGroup>
|
|
)}
|
|
</CommandList>
|
|
</Command>
|
|
</PopoverContent>
|
|
</Popover >
|
|
)
|
|
}
|