Skip to content

Slider

The Slider component lets users select a number (or range) by dragging a thumb along a track. It is ideal for volume controls, price filters, or any scenario requiring a bounded numeric input.

Además de las propiedades comunes, el componente slider acepta las siguientes propiedades específicas:

PropertyTypeRequiredDefaultDescription
type"slider"YesIdentifies the field as a slider
schemaz.ZodTypeYesEsquema Zod para validación. Requerido. Debe validar number o number[]
defaultValuenumber[]No[0]Initial value. Array con 1 elemento para valor único, 2 elementos para rango
minnumberNo0Minimum value
maxnumberNo100Maximum value
stepnumberNo1Step interval (incremento entre valores)

El valor de un slider puede ser:

  • Valor único: number cuando defaultValue tiene 1 elemento
  • Rango: number[] cuando defaultValue tiene 2 elementos (desde-hasta)
// Para valor único
{
type: "slider",
defaultValue: [50],
min: 0,
max: 100,
schema: z.number().min(0).max(100)
}
// Para rango
{
type: "slider",
defaultValue: [20, 80],
min: 0,
max: 100,
schema: z.array(z.number()).length(2).refine(([min, max]) => max >= min, {
message: "El máximo debe ser mayor o igual al mínimo"
})
}
A basic slider with min 0, max 100
basic-slider.tsx
import { z } from "zod";
import { createExampleForm } from "../createExampleForm";
export const BasicSlider = createExampleForm({
formId: "basic-slider",
fields: {
volume: {
type: "slider",
label: "Volume",
defaultValue: [50],
min: 0,
max: 100,
step: 1,
schema: z.array(z.number()).nonempty(),
},
},
steps: {
step1: {
id: "step1",
fields: ["volume"],
},
},
});
  1. Show the current value near the slider (@enlolab/forms does this by default).
  2. Use logical min/max bounds—avoid huge ranges.
  3. Combine with helper text to explain the metric.
  4. Validate with Zod to enforce numeric constraints.
  • Supports keyboard via arrow keys and Home/End.
  • Screen readers announce the value.
  • Track and thumb colors follow your Tailwind theme.