Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

2.2 KB
Raw Blame
import React, { useLayoutEffect, useRef } from 'react'
import DatePicker from 'react-flatpickr'
import { CustomLocale } from 'flatpickr/dist/types/locale'

export const isDateRangeCalendarOpen = (): boolean =>
  !!document.querySelector('.flatpickr-calendar')

const calendarLocale: Partial<CustomLocale> = {
  weekdays: {
    shorthand: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
    longhand: [
      'Sunday',
      'Monday',
      'Tuesday',
      'Wednesday',
      'Thursday',
      'Friday',
      'Saturday'
    ]
  }
}

export interface DateRangeCalendarProps {
  id: string
  minDate?: string
  maxDate?: string
  defaultDates?: [string, string]
  onCloseWithNoSelection?: () => void
  onCloseWithSelection?: ([selectionStart, selectionEnd]: [Date, Date]) => void
}

export function DateRangeCalendar({
  id,
  minDate,
  maxDate,
  defaultDates,
  onCloseWithNoSelection,
  onCloseWithSelection
}: DateRangeCalendarProps) {
  const hideInputFieldClassName = '!invisible !h-0 !w-0 !p-0 !m-0 !border-0'
  const calendarRef = useRef<DatePicker>(null)
  useLayoutEffect(() => {
    // on Safari, this removes little arrow pointing to (hidden) input,
    // which didn't appear with other browsers
    calendarRef.current?.flatpickr?.calendarContainer?.classList.remove(
      'arrowTop',
      'arrowBottom',
      'arrowLeft',
      'arrowRight'
    )
  }, [])
  return (
    <DatePicker
      ref={calendarRef}
      className={hideInputFieldClassName}
      id={id}
      options={{
        animate: false,
        inline: true,
        mode: 'range',
        maxDate,
        minDate,
        defaultDate: defaultDates,
        showMonths: 1,
        locale: calendarLocale
      }}
      onClose={
        onCloseWithSelection || onCloseWithNoSelection
          ? ([selectionStart, selectionEnd]) => {
              if (selectionStart && selectionEnd) {
                if (onCloseWithSelection) {
                  onCloseWithSelection([selectionStart, selectionEnd])
                }
              } else {
                if (onCloseWithNoSelection) {
                  onCloseWithNoSelection()
                }
              }
            }
          : undefined
      }
    />
  )
}