Urgent.News

What's breaking now, across hundreds of outlets.

Tech

shadcn's date picker lists presets, but its visible code has none

The shadcn/ui date picker page says it has "range and presets." The visible code has no presets, so you will write them yourself. This article covers the range picker the page does show, adds the presets in code, and explains which variant to use for each form. It ends with what OTF can and cannot claim about its kits. What the shadcn page covers The page describes the component as "A date picker…

The shadcn/ui date picker page claims it features range and preset options. However, its visible code lacks any preset functionality. This article aims to address the range picker as displayed on the page, incorporates preset functionality in the code, and explains which variant to use for various forms. The page describes the component as a date picker with range and preset capabilities.

It lists six variants, including a basic picker, range picker, date of birth picker, input picker, time picker, and natural language picker. The natural language variant utilizes the chrono-node library to parse dates entered in plain language format. The other variants rely solely on the date and calendar components. The picker is composed of a Popover containing a trigger and a Calendar component.

The usage example employs Calendar mode= 'single', selected={date}, and onSelect={setDate} within a PopoverContent with className= 'w-auto p-0'. The page imports format from date-fns for date display and an icon from lucide-react for the trigger. Before proceeding, ensure you have installed the Popover and Calendar components as outlined in the installation steps.

To create a range picker, you need a single state value to store both the start and end dates of the selection. Utilize React DayPicker's DateRange type, which provides a structure with optional from and to properties. Start with an empty range, then pass it to the Calendar component in range mode. Import * as React from 'react'; import { format } from 'date-fns'; import type { DateRange } from 'react-day-picker'; import { Calendar } from '@/components/ui/calendar'; export function DateRangeField() { const [range, setRange] = React.useState<DateRange | undefined>(undefined); return ( <div className="flex flex-col gap-2"> <Calendar mode="range" selected={range} onSelect={setRange} numberOfMonths={2} /> {range?.from && ( <p className="text-sm text-muted-foreground">{format(range.from, 'PPP')} {range.to ? ` to ${format(range.to, 'PPP')}` : ''}</p> ) } </div> ); } The range.to value remains empty after the first selection, which is accurate.

The user has selected a start date, and an end date has not been chosen yet. The display line handles both states, ensuring the form never presents a partially completed range as a complete entry. When the field should be placed within a form, wrap the Calendar component in a Popover. The trigger will display the selected range, and the popover will open the Calendar interface.

Maintain the trigger text as the source of truth for the value users see, allowing a closed field to still display the previously entered dates. To add preset buttons, you will need to implement them manually, as the shadcn page does not include them in its visible code. Preset buttons are plain buttons that update the same state the calendar utilizes.

Each preset calculates a from and to date using date-fns. Define an array of presets with labels and the number of days for each preset. Create a function called RangePresets that accepts onPick as a prop, which is a function to handle the selected range. Render a div with flex-wrap and gap-2 classes. Map through the presets array and render a Button component for each preset.

On button click, calculate the from and to dates using startOfDay from date-fns and subDays to subtract the specified number of days from the current date. Pass the setRange function you provided to the calendar as the onPick prop. The preset will then update the calendar's highlighted dates accordingly.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

Evidence Grades for Agent Claims: Terms, Branches, and Six Leaves

A composite night review, not a logged incident from this account, starts with a ticket already marked ready to close. Three agent notes sit on it. One says the suite passed.

  • Agent claims undergo a structured grading process with limited decision-making capabilities
  • Evidence packets contain job ID, command, exit code, and log excerpt for grading
  • Six leaves in the grading system handle different claim sources and actions

More from Tuesday 13 October →