Botones
Search Input
Barra de búsqueda con una cruz al final que limpia el texto (o un spinner mientras busca). Política: toda barra de búsqueda usa este componente.
Dependencias: lucide-react
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 search-inputVer el código
'use client';
import { Loader2, X } from 'lucide-react';
import { cn } from '@/lib/utils';
interface SearchInputProps {
value: string;
onChange: (value: string) => void;
placeholder?: string;
/** Muestra un spinner en lugar de la cruz mientras se busca. */
loading?: boolean;
autoFocus?: boolean;
maxLength?: number;
'aria-label'?: string;
/** Clases del contenedor. */
className?: string;
/** Clases del input (alto, borde…); por defecto el estilo de la barra del DataGrid. */
inputClassName?: string;
}
/**
* Barra de búsqueda con una cruz al final que limpia el texto. Todas las barras de
* búsqueda de la app deben usar este componente.
*/
export function SearchInput({
value,
onChange,
placeholder = 'Buscar',
loading,
autoFocus,
maxLength,
'aria-label': ariaLabel,
className,
inputClassName,
}: SearchInputProps) {
return (
<div className={cn('relative w-full', className)}>
<input
type='text'
autoFocus={autoFocus}
maxLength={maxLength}
value={value}
onChange={(e) => onChange(e.target.value)}
placeholder={placeholder}
aria-label={ariaLabel ?? placeholder}
aria-busy={loading || undefined}
className={cn(
'border-input bg-background focus:border-primary focus:ring-primary/20 h-10 w-full rounded-md border px-3 pr-10 text-sm shadow-sm outline-none focus:ring-2',
inputClassName,
)}
/>
{loading ? (
<Loader2 aria-hidden className='text-primary pointer-events-none absolute top-1/2 right-3 h-4 w-4 -translate-y-1/2 animate-spin' />
) : (
value && (
<button
type='button'
aria-label='Limpiar búsqueda'
title='Limpiar búsqueda'
onClick={() => onChange('')}
className='absolute top-1/2 right-2 inline-flex h-6 w-6 -translate-y-1/2 items-center justify-center rounded text-gray-400 transition hover:bg-gray-100 hover:text-gray-600 dark:hover:bg-slate-700 dark:hover:text-gray-200'
>
<X className='h-4 w-4' />
</button>
)
)}
</div>
);
}