Ir al contenido
Español
Selector de rango de fechas viene con Pro. La vista previa aparece al desbloquearlo.

Cómo funciona

A start → end trigger (the same filled field as the text input, with an accent underline, while open, under the segment the next click will set) and a floating panel with a quick-range rail and a month calendar. The first click sets the start, hovering previews the band, and the second click sets the end and closes. A click before the start restarts the selection. The value is always a complete pair or empty, so closing with only a start discards it. Dates are 'YYYY-MM-DD' strings handled in local time, never parsed through UTC.

Úsalo para

  • Report, statement and analytics filters where the period matters.
  • Booking and travel forms that need a check-in and check-out date.
  • Any range with a cap, using maxDays and max to grey out dates that cannot be chosen.

Busca otra opción cuando

  • A single date: use a date picker.
  • Choosing a time of day: use a time picker.
  • Only a few fixed periods such as today or last week: use a segmented control or a select.

Uso

import { useState } from 'react'
import { DateRangePicker } from './DateRangePicker.jsx'

// Use local year, month and day for today; toISOString() is UTC and can be a day off
const pad = (n) => String(n).padStart(2, '0')
const today = () => {
  const d = new Date()
  return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
}

export function ReportFilter() {
  const [range, setRange] = useState(['', ''])
  return (
    <DateRangePicker
      value={range}
      onChange={(next) => {
        setRange(next)
        if (next[0]) loadReport(next[0], next[1])
      }}
      max={today()}
      maxDays={31}
      startInputProps={{ name: 'from', 'aria-label': 'Report start date' }}
      endInputProps={{ name: 'to', 'aria-label': 'Report end date' }}
    />
  )
}

Propiedades

PropiedadTipoPor defectoQué hace
value[string, string]—Controlled value [start, end], both 'YYYY-MM-DD'; ['', ''] means empty. An incomplete pair or a start after the end counts as empty.
defaultValue[string, string]['', '']Initial value when uncontrolled.
onChange(value: [string, string]) => void—Called after a pair is picked, a typed date is confirmed or the range is cleared. Not called while only the start is set.
openboolean—Controlled: whether the panel is open.
defaultOpenbooleanfalseUncontrolled: whether it starts open (an initially open panel does not take focus).
onOpenChange(open: boolean) => void—Called when the panel opens or closes.
size'sm' | 'md' | 'lg''md'Trigger height 36 / 44 / 52px, radius 10 / 12 / 14px and font size 14 / 15 / 16px. The panel keeps its size.
disabledbooleanfalseDisabled: the field stays filled without its hairline, the text is muted, and it cannot open.
invalidbooleanfalseFailed validation: red hairline, a faint red fill, a red halo while focused, and aria-invalid on both inputs. The error message is up to the surrounding form field.
placeholder[string, string]['Start date', 'End date']Placeholder text for the two segments.
clearablebooleantrueShows a clear button when there is a value. Emptying either segment also clears the whole range.
editablebooleantrueWhether dates can be typed. Pass false on touch screens so the keyboard does not pop up.
minstring—Earliest selectable date (inclusive), 'YYYY-MM-DD'.
maxstring—Latest selectable date (inclusive), 'YYYY-MM-DD'.
disabledDate(date: string) => boolean—Dates returning true cannot be a start or an end (they may sit inside a range).
maxDaysnumber—Maximum number of days, both ends counted (one day is 1). Once the start is set, later dates beyond it are faded and the panel explains why.
weekStartsOn0 | 11First day of the week: 1 Monday, 0 Sunday.
months1 | 21Months shown side by side. 2 is for wide screens and falls back to 1 below 720px viewport width.
shortcutsboolean | { label: string; value: [string, string] | (() => [string, string]) }[]trueQuick ranges in a filled rail left of the calendar (a row of pills on narrow screens); the one matching the current range is a raised chip. true shows the built-in Today / Last 7 days / Last 30 days / This month / Last month, false hides them, or pass your own. Parts outside min / max are trimmed; items over maxDays are disabled.
labelsobject—Replaces built-in text (weekdaysShort, weekdaysLong, months, monthTitle, titleSeparator, dayLabel, startSuffix, endSuffix, insideSuffix, todaySuffix, tooFarSuffix, unavailableSuffix, tooFarTitle, startDate, endDate, openCalendar, openCalendarWith, clear, dialog, shortcutsGroup, prevYear, prevMonth, nextMonth, nextYear, shortcutToday, shortcutLast7, shortcutLast30, shortcutThisMonth, shortcutLastMonth, shortcutInvalid, shortcutOutOfRange, shortcutBlocked, shortcutTooLong, hintFormat, hintMin, hintMax, hintBlocked, hintTooLongRetype, hintNeedStart, hintEndBeforeStart, hintTooLong, statusTooFar, statusRange, statusPickEndLimited, statusPickEnd, statusPickStart, announceStart). Weekday lists start on Sunday. Shallow-merged over the English defaults.
onInvalidInput(text: string, reason: 'format' | 'unavailable' | 'order' | 'span') => void—Called when typed text is rejected: format is not a valid date, unavailable the day cannot be picked, order the end is before the start, span over maxDays.
startInputProps / endInputPropsInputHTMLAttributes<HTMLInputElement>—Passed to the two real <input> elements: id, name, required, aria-label (defaults to “Start date” and “End date”), and so on.
...restHTMLAttributes<HTMLDivElement>—className, style and other attributes go to the root element. It fills its container by default; change the width with style or className.

Accesibilidad

  • Both segments are real inputs with their own labels, and the panel is a dialog with a labelled calendar grid.
  • The calendar uses the grid pattern: arrows move by day or week, Home and End go to the week edges, PageUp and PageDown change month, and Shift with them changes year.
  • Every day has a full spoken label: weekday, date, start or end, today, unavailable and why.
  • The range does not rely on colour alone: the start and end are solid circles, the days between sit on a band, a previewed end is an outlined circle, and today has a dot. Unavailable days are faded and cannot be focused.
  • Esc closes the panel and returns focus; a selection with only a start is discarded.
  • Hints for rejected typing use role=alert, and the new month is announced through a live region.

Movimiento

The field's focus halo grows from 0 to 4px over 220ms and the underline under the segment the next click will set stretches out over 320ms. Panel enter 220ms ease-out (moves 6px and scales 0.96 to 1 from the corner touching the trigger, with a fade), exit 140ms ease-in. The start and end circles pop in (scale 0.6 to 1, 380ms spring). Changing month slides the new page in 12px over 280ms. With reduced motion the panel only fades and nothing else moves.

Añádelo a tu proyecto

Los miembros Pro copian un prompt y su agente de programación instala el código, igual que con los gratuitos.

DateRangePicker.jsxdate-range-picker.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Formularios
Cuadrícula de disponibilidad viene con Pro. La vista previa aparece al desbloquearlo.
Operaciones booleanas viene con Pro. La vista previa aparece al desbloquearlo.
Selector en cascada viene con Pro. La vista previa aparece al desbloquearlo.