UIPackage React Native
Search
Menu

Date Range Picker

Two coordinated native date controls for one inclusive range with local calendar days and limits.

Install

First install? Set up the registry in your app.

npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add date-range-picker

Source files

Expand a file to read the source the installer copies into your app.

components/ui/date-range-picker/DateRangePicker.tsx
import * as React from 'react'
import { View, type ViewProps } from 'react-native'
import { Button } from '@/components/ui/button'
import { DateTimePicker } from '@/components/ui/date-time-picker'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'

export interface DateRangeValue {
  /** Endpoints are inclusive local calendar dates. Clock times are ignored. */
  start: Date
  end: Date
}

export interface DateRangePickerProps extends Omit<ViewProps, 'children'> {
  className?: string
  value?: DateRangeValue
  defaultValue?: DateRangeValue
  onValueChange?: (range: DateRangeValue) => void
  minimumDate?: Date
  maximumDate?: Date
  disabled?: boolean
  startLabel?: string
  endLabel?: string
}

function day(value: Date | undefined) {
  if (!(value instanceof Date) || value.getFullYear() < 1 || value.getFullYear() > 9999) return undefined
  const timestamp = new Date(value).setHours(12, 0, 0, 0)
  return Number.isFinite(timestamp) ? timestamp : undefined
}

export function DateRangePicker({
  className,
  value,
  defaultValue,
  onValueChange,
  minimumDate,
  maximumDate,
  disabled = false,
  startLabel = 'Start date',
  endLabel = 'End date',
  ...props
}: DateRangePickerProps) {
  const [today] = React.useState(() => day(new Date())!)
  const [internal, setInternal] = React.useState(defaultValue)
  const minimum = day(minimumDate)
  const maximum = day(maximumDate)
  const reversed = minimum !== undefined && maximum !== undefined && minimum > maximum
  const range = value ?? internal
  const clamp = (timestamp: number) => Math.max(minimum ?? -Infinity, Math.min(maximum ?? Infinity, timestamp))
  const savedStart = day(range?.start)
  const savedEnd = day(range?.end)
  const start = clamp(savedStart ?? today)
  const end = Math.max(start, clamp(savedEnd ?? start))
  const adjusted = value !== undefined && (savedStart !== start || savedEnd !== end)
  const dateFormat = React.useMemo(
    () => new Intl.DateTimeFormat(undefined, { year: 'numeric', month: 'long', day: 'numeric' }),
    [],
  )

  function emit(start: number, end: number) {
    if (value === undefined) setInternal({ start: new Date(start), end: new Date(end) })
    onValueChange?.({ start: new Date(start), end: new Date(end) })
  }

  function change(endpoint: 'start' | 'end', selected: Date) {
    const date = day(selected)
    if (disabled || reversed || date === undefined || date !== clamp(date) || (endpoint === 'end' && date < start))
      return
    const nextStart = endpoint === 'start' ? date : start
    const nextEnd = endpoint === 'start' ? Math.max(date, end) : date
    if (nextStart === start && nextEnd === end) return
    emit(nextStart, nextEnd)
  }

  return (
    <View {...props} className={cn('gap-3', className)}>
      <View className="gap-1">
        <Text size="sm" className="font-medium" aria-hidden>
          {startLabel}
        </Text>
        <DateTimePicker
          value={new Date(start)}
          accessibilityLabel={`${startLabel}, ${dateFormat.format(start)}`}
          minimumDate={minimum === undefined ? undefined : new Date(minimum)}
          maximumDate={maximum === undefined ? undefined : new Date(maximum)}
          disabled={disabled || reversed}
          onValueChange={(date) => change('start', date)}
        />
      </View>
      <View className="gap-1">
        <Text size="sm" className="font-medium" aria-hidden>
          {endLabel}
        </Text>
        <DateTimePicker
          value={new Date(end)}
          accessibilityLabel={`${endLabel}, ${dateFormat.format(end)}`}
          minimumDate={new Date(start)}
          maximumDate={maximum === undefined ? undefined : new Date(maximum)}
          disabled={disabled || reversed}
          onValueChange={(date) => change('end', date)}
        />
      </View>
      {reversed ? (
        <Text size="sm" variant="muted">
          No dates are available.
        </Text>
      ) : null}
      {adjusted && !reversed ? (
        <View className="gap-2">
          <Text size="sm">The saved dates are outside the available range.</Text>
          {onValueChange ? (
            <Button variant="outline" disabled={disabled} onPress={() => emit(start, end)}>
              Use displayed dates
            </Button>
          ) : null}
        </View>
      ) : null}
    </View>
  )
}
components/ui/date-range-picker/index.ts
export { DateRangePicker, type DateRangePickerProps, type DateRangeValue } from './DateRangePicker'

Dependencies

Browse controls and interactions