DatePicker

DatePicker is a native component for date and range selection. It provides a standard picker with support for multiple selection modes: single date, date range, and year-only selection.

Basic Usage

The component acts as a controlled or uncontrolled input, where value and onChange handle the synchronization of selected timestamps.

import { DatePicker, View, Text } from "@zynthjs/components";

function Picker() {
  const [date, setDate] = createSignal(Date.now());

  return (
    <View>
      <Text>Selected: {new Date(date()).toLocaleDateString()}</Text>
      
      <DatePicker 
        mode="date" 
        value={date()} 
        onChange={setDate}
      >
        <DatePicker.Trigger>
          <View style={{ padding: 12, border: "1px solid #ccc" }}>
            <Text>Choose a date</Text>
          </View>
        </DatePicker.Trigger>
      </DatePicker>
    </View>
  );
}

Date Range Selection

Use mode="range" to enable selecting a start and end interval. This mode returns a DateRangeValue object containing timestamps for both points.

function RangePicker() {
  const [range, setRange] = createSignal<DateRangeValue>({ 
    start: null, 
    end: null 
  });

  return (
    <DatePicker 
      mode="range" 
      value={range()} 
      onRangeChange={setRange}
      confirmText="Apply Range"
    />
  );
}

Year Selection

For use cases where only the year is required (e.g., birth years or financial periods), set mode="year".

<DatePicker 
  mode="year" 
  title="Select Fiscal Year"
  onChange={(year) => console.log("Year selected:", year)}
/>

Customized Buttons

The confirmText and cancelText props allow for modifying the primary and secondary action labels within the picker UI.

<DatePicker 
  confirmText="Done" 
  cancelText="Go Back"
  onCancel={() => console.log("User cancelled")}
/>

Props

PropTypeDescription
modedate | range | yearPicker selection mode.
titlestringHeader text shown on the picker.
valuenumber | Date | DateRangeValueThe selected timestamp or range.
defaultValuenumber | Date | DateRangeValueInitial value if uncontrolled.
confirmTextstringLabel for the positive action.
cancelTextstringLabel for the negative action.
onChange(value: number) => voidEvent for single date and year picks.
onRangeChange(value: DateRangeValue) => voidEvent for range mode selection.
onCancel() => voidEvent triggered by the cancel action.
onDismiss() => voidEvent triggered when the picker is closed.
styleStyleOuter container styling.
childrenJSX.ElementContent placed inside the picker root (triggers).

Imperative Ref

A ref provides an interface to manually control the picker’s visibility:

const pickerRef = createDatePickerRef();

// Opens the picker programmatically
pickerRef.open();

// Closes the picker programmatically
pickerRef.dismiss();