Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

5.4 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import type { DateValue } from "@internationalized/date";
import { useLingui } from "@lingui/react/macro";
import { useDatePicker } from "@react-aria/datepicker";
import { useDatePickerState } from "@react-stately/datepicker";
import type { DatePickerProps } from "@react-types/datepicker";
import { cva } from "class-variance-authority";
import type { ReactNode } from "react";
import { useRef } from "react";
import { LuBan, LuCalendarClock, LuInfo } from "react-icons/lu";
import { cn } from "..";
import { Button } from "../Button";
import { HStack } from "../HStack";
import { IconButton } from "../IconButton";
import { InputGroup } from "../Input";
import {
  Popover,
  PopoverContent,
  PopoverFooter,
  PopoverTrigger
} from "../Popover";
import { Tooltip, TooltipContent, TooltipTrigger } from "../Tooltip";
import { FieldButton } from "./components/Button";
import { Calendar } from "./components/Calendar";
import DateField from "./components/DateField";
import { dateTimePickerOptions } from "./dateTimePickerOptions";
import { TimeCombobox } from "./TimeCombobox";

const dateTimePickerFieldVariants = cva("flex w-full px-4", {
  variants: {
    size: {
      sm: "py-1",
      md: "py-2",
      lg: "py-3"
    }
  },
  defaultVariants: {
    size: "md"
  }
});

const DateTimePicker = (
  props: DatePickerProps<DateValue> & {
    className?: string;
    size?: "sm" | "md" | "lg";
    withButton?: boolean;
    inline?: ReactNode;
    helperText?: string;
  }
) => {
  const { t } = useLingui();
  const timeOptions = dateTimePickerOptions(props.hourCycle);

  const state = useDatePickerState({
    ...props,
    ...timeOptions,
    shouldCloseOnSelect: false
  });
  const ref = useRef<HTMLDivElement>(null);
  // The same options must reach `useDatePicker`, not just the state: it builds
  // `fieldProps`, so without them the visible field ignores what the state says.
  const { groupProps, fieldProps, buttonProps, dialogProps, calendarProps } =
    useDatePicker({ ...props, ...timeOptions }, state, ref);

  return (
    <Popover open={state.isOpen} onOpenChange={state.setOpen}>
      <div className="relative inline-flex flex-col w-full">
        <HStack className="w-full" spacing={0}>
          {props.inline ? (
            <>
              <div className="flex-grow">{props.inline}</div>
              <HStack spacing={0}>
                {props.helperText && (
                  <Tooltip>
                    <TooltipTrigger asChild>
                      <IconButton
                        icon={<LuInfo />}
                        variant="ghost"
                        size="sm"
                        aria-label="Helper information"
                      />
                    </TooltipTrigger>
                    <TooltipContent>{props.helperText}</TooltipContent>
                  </Tooltip>
                )}
                <PopoverTrigger asChild>
                  <IconButton
                    icon={<LuCalendarClock />}
                    variant="secondary"
                    size="sm"
                    aria-label="Open date time picker"
                    isDisabled={props.isDisabled}
                    {...buttonProps}
                  />
                </PopoverTrigger>
              </HStack>
            </>
          ) : (
            <>
              <InputGroup
                {...groupProps}
                ref={ref}
                className={cn("w-full inline-flex", props.className)}
                size={props.size}
              >
                <div
                  className={dateTimePickerFieldVariants({ size: props.size })}
                >
                  <DateField {...fieldProps} size={props.size} />
                  {state.isInvalid && (
                    <LuBan className="!text-destructive-foreground ml-auto shrink-0 self-center" />
                  )}
                </div>
                {props.withButton !== false && (
                  <div
                    className={cn(
                      "flex-shrink-0 -mr-px",
                      props.size === "sm" ? "mt-[-3px]" : "-mt-px"
                    )}
                  >
                    <PopoverTrigger tabIndex={-1}>
                      <FieldButton
                        {...buttonProps}
                        isPressed={state.isOpen}
                        size={props.size}
                      />
                    </PopoverTrigger>
                  </div>
                )}
              </InputGroup>
            </>
          )}
        </HStack>
        <PopoverContent align="end" {...dialogProps}>
          <Calendar {...calendarProps} />
          <div className="pt-3">
            <TimeCombobox
              value={state.timeValue}
              onChange={state.setTimeValue}
              isDisabled={props.isDisabled}
              hour24={props.hourCycle === 24}
              aria-label={t`Time`}
            />
          </div>
          {props.inline && (
            <PopoverFooter>
              <Button onClick={() => state.setValue(null)} variant="secondary">
                Clear
              </Button>
            </PopoverFooter>
          )}
        </PopoverContent>
      </div>
    </Popover>
  );
};

export default DateTimePicker;