Etiquetas y estados
Icon Picker
Combobox para elegir un icono del registro (icon-selector de ZasExpress).
Dependencias: icons, popover y command de zasui
Uso
Instalación
Con la CLI de zasui (ver Introducción): instala el componente con sus dependencias y queda listo para usar.
pnpm dlx @medialityc/zasui add icon-pickerVer el código
'use client';
import * as React from 'react';
import { ChevronDown } from 'lucide-react';
import { cn } from '@/lib/utils';
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from '@/components/ui/command';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import { IconRegistryRenderer, iconRegistry, type IconKey } from '@/components/ui/icons/icon-registry';
// Base: icon/icon-selector.tsx de ZasExpress (busca por nombre o clave y pinta icono + nombre + clave).
// Cambia: combobox con popover/command de zasui en vez del autocomplete de react-hook-form de Express.
const iconOptions = Object.entries(iconRegistry).map(([key, value]) => ({
key: key as IconKey,
name: value.name,
component: value.component,
}));
interface IconPickerProps {
value?: string | null;
onChange: (key: IconKey) => void;
placeholder?: string;
disabled?: boolean;
className?: string;
}
export function IconPicker({ value, onChange, placeholder = 'Seleccionar icono', disabled, className }: IconPickerProps) {
const [open, setOpen] = React.useState(false);
const selected = value ? iconRegistry[value as IconKey] : undefined;
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<button
type='button'
disabled={disabled}
className={cn(
'border-input bg-background flex h-10 w-full items-center gap-2 rounded-md border px-3 text-sm disabled:cursor-not-allowed disabled:opacity-50',
className,
)}
>
{selected ? (
<>
<IconRegistryRenderer tag={value!} className='h-4 w-4' />
<span className='truncate'>{selected.name}</span>
</>
) : (
<span className='text-muted-foreground'>{placeholder}</span>
)}
<ChevronDown className='ml-auto h-4 w-4 opacity-50' />
</button>
</PopoverTrigger>
<PopoverContent className='w-(--radix-popover-trigger-width) p-0' align='start'>
<Command>
<CommandInput placeholder='Buscar icono...' />
<CommandList>
<CommandEmpty>Sin resultados</CommandEmpty>
<CommandGroup>
{iconOptions.map((option) => (
<CommandItem
key={option.key}
value={`${option.name} ${option.key}`}
onSelect={() => {
onChange(option.key);
setOpen(false);
}}
>
<div className='flex items-center gap-2'>
<option.component className='h-4 w-4' />
<span>{option.name}</span>
<span className='text-xs text-gray-500'>({option.key})</span>
</div>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
);
}