Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

1.5 KB
Raw Blame
import type { CSSProperties } from "react";

import { PublicComponentStylesWrapper } from "embedding-sdk-bundle/components/private/PublicComponentStylesWrapper";
import { DatePicker } from "metabase/ui";

export type DateRangeValue = [string | null, string | null];

export interface DateRangeCalendarProps {
  value?: DateRangeValue;
  defaultValue?: DateRangeValue;
  onChange?: (value: DateRangeValue) => void;
  minDate?: string;
  maxDate?: string;
  numberOfColumns?: number;
  className?: string;
  style?: CSSProperties;
}

export const DateRangeCalendar = ({
  value,
  defaultValue,
  onChange,
  minDate,
  maxDate,
  numberOfColumns = 2,
  className,
  style,
}: DateRangeCalendarProps) => (
  // Mantine scopes its CSS variables to `.mb-wrapper`, which this wrapper
  // carries. Without it the calendar renders unstyled inside a data app.
  <PublicComponentStylesWrapper
    className={className}
    style={{
      display: "inline-block",
      width: "fit-content",
      height: "auto",
      ...style,
    }}
  >
    <DatePicker
      type="range"
      value={value}
      defaultValue={defaultValue}
      // The bare calendar opens on today's month whatever the value is; start it
      // on the selected range instead, so a reopened popover shows the pick.
      defaultDate={value?.[0] ?? defaultValue?.[0] ?? undefined}
      onChange={onChange}
      minDate={minDate}
      maxDate={maxDate}
      numberOfColumns={numberOfColumns}
      allowSingleDateInRange
    />
  </PublicComponentStylesWrapper>
);