DynamicFilter

Barra de filtros configurable por JSON. Soporta text, select, multiselect, date, daterange, boolean y number. Muestra conteo de filtros activos y permite limpiarlos todos de un clic.

pnpm dlx nuvo-ui add dynamic-filter

Demo interactivo

Todos los tipos de filtro conectados a una tabla de datos en tiempo real.

Filtros
—
0
Sin mínimo (0–100)
NombreRolEstadoScoreFechaVerificado
Ana GarcíaadminActivo922024-03-15✓
Luis MartínezeditorInactivo742024-01-08—
Sara LópezviewerActivo882024-06-22✓
Carlos RuizadminActivo552023-11-30—
Marta DíazeditorInactivo672024-09-04✓
Jorge PérezviewerActivo912024-02-17—

6 de 6 registros


Uso básico

import { DynamicFilter, type FilterConfig, type FilterValues } from "@/components/ui/dynamic-filter"
import { useState } from "react"

const filters: FilterConfig[] = [
  { key: "search",   label: "Buscar",   type: "text"        },
  { key: "status",   label: "Estado",   type: "select",
    options: [{ label: "Activo", value: "active" }, { label: "Inactivo", value: "inactive" }] },
  { key: "roles",    label: "Roles",    type: "multiselect",
    options: [{ label: "Admin", value: "admin" }, { label: "Editor", value: "editor" }] },
  { key: "date",     label: "Fecha",    type: "daterange"   },
  { key: "score",    label: "Score min",type: "number", min: 0, max: 100 },
  { key: "verified", label: "Verificados", type: "boolean"  },
]

export function MyPage() {
  const [values, setValues] = useState<FilterValues>({})

  // Leer valores:
  // values.search    → string | undefined
  // values.status    → string | undefined
  // values.roles     → string[] | undefined
  // values.date      → { from?: string; to?: string } | undefined
  // values.score     → string | undefined
  // values.verified  → true | undefined

  return (
    <DynamicFilter
      filters={filters}
      value={values}
      onChange={setValues}
    />
  )
}
Nota: El objeto values solo contiene las claves con valor. Un filtro sin seleccionar simplemente no aparece como propiedad (o es undefined).

Props — DynamicFilter

PropTipoDefaultDescripción
filtersFilterConfig[]—Configuración declarativa de los filtros a mostrar
valueFilterValues{}Objeto controlado con los valores actuales de los filtros
onChange(values: FilterValues) => void—Callback al cambiar cualquier filtro — recibe el objeto completo actualizado
titlestring"Filtros"Título del header de la barra de filtros
defaultExpandedbooleantrueSi los filtros se muestran expandidos al montar
classNamestring—Clases CSS adicionales en el contenedor raíz

Tipo FilterConfig

PropTipoDefaultDescripción
keystring—Clave única del filtro — se usa como propiedad en el objeto values
labelstring—Etiqueta visible encima del campo
type"text" | "select" | "multiselect" | "date" | "daterange" | "boolean" | "number"—Tipo de campo a renderizar
placeholderstring—Texto de placeholder o label del toggle boolean
optionsFilterOption[]—Opciones para select y multiselect: { label, value }[]
minstring | number—Valor mínimo para date y number
maxstring | number—Valor máximo para date y number