mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## Problem The date picker pattern we currently have: - requires developers add classes and props themselves to get the expected result. This is cumbersome and prone to error. - does not visually convey the field invalid state like inputs do <img width="702" height="256" alt="image" src="https://github.com/user-attachments/assets/cacd6414-6789-42e3-8d06-88e44fd9fe0a" /> ## Solution Introduce a new `DatePicker` _UI pattern_ that wraps the `Popover` and `Button` components to ease the most common scenarios while still allowing full customization <img width="699" height="250" alt="image" src="https://github.com/user-attachments/assets/e80f842c-28b2-4a4f-b316-c2005e771912" /> ## Component usage Note how we manually pass the `isInvalid` prop to the `<DatePickerButton>` to avoid relying on `react-hook-form` contexts which would make the date picker unusable outside RHF forms. Alternative would be to also have a `<DatePickerInput>` that could be used instead of `<DatePickerButton>` when inside an RHF form. ```tsx <FormField control={form.control} name="expiryDate" render={({ field, fieldState }) => ( <FormItemLayout layout="horizontal" label="Date Picker" description="Date selection with calendar popover" > <FormControl className="col-span-6"> <DatePicker> <DatePickerTrigger asChild> <DatePickerButton isInvalid={fieldState.invalid}> {field.value ? format(field.value, 'PPP') : 'Pick a date'} </DatePickerButton> </DatePickerTrigger> <DatePickerContent> <Calendar mode="single" selected={field.value} onSelect={field.onChange} initialFocus /> </DatePickerContent> </DatePicker> </FormControl> </FormItemLayout> )} /> --------- Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
'use client'
|
|
|
|
import { addDays, format } from 'date-fns'
|
|
import * as React from 'react'
|
|
import { Calendar, Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from 'ui'
|
|
import {
|
|
DatePicker,
|
|
DatePickerButton,
|
|
DatePickerContent,
|
|
DatePickerTrigger,
|
|
} from 'ui-patterns/DatePicker'
|
|
|
|
export default function DatePickerWithPresets() {
|
|
const [date, setDate] = React.useState<Date>()
|
|
|
|
return (
|
|
<DatePicker>
|
|
<DatePickerTrigger asChild>
|
|
<DatePickerButton variant="outline" className="w-[280px]">
|
|
{date ? format(date, 'PPP') : <span>Pick a date</span>}
|
|
</DatePickerButton>
|
|
</DatePickerTrigger>
|
|
<DatePickerContent className="flex w-auto flex-col space-y-2 p-2">
|
|
<Select onValueChange={(value) => setDate(addDays(new Date(), parseInt(value)))}>
|
|
<SelectTrigger>
|
|
<SelectValue placeholder="Select" />
|
|
</SelectTrigger>
|
|
<SelectContent position="popper">
|
|
<SelectItem value="0">Today</SelectItem>
|
|
<SelectItem value="1">Tomorrow</SelectItem>
|
|
<SelectItem value="3">In 3 days</SelectItem>
|
|
<SelectItem value="7">In a week</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
<div className="rounded-md border">
|
|
<Calendar mode="single" selected={date} onSelect={setDate} />
|
|
</div>
|
|
</DatePickerContent>
|
|
</DatePicker>
|
|
)
|
|
}
|