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.

3.2 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 { CalendarDate } from "@internationalized/date";
import {
  getLocalTimeZone,
  isToday as isDateToday,
  isSameDay,
  isSameMonth
} from "@internationalized/date";
import { useCalendarCell } from "@react-aria/calendar";
import type { RangeCalendarState } from "@react-stately/calendar";
import clsx from "clsx";
import { useRef } from "react";

import { Button } from "../../Button";

export function CalendarRangeCell({
  state,
  date,
  currentMonth,
  isMarked = false,
  ...props
}: {
  state: RangeCalendarState;
  date: CalendarDate;
  currentMonth: CalendarDate;
  isRangeCalendar?: boolean;
  locale?: string;
  isMarked?: boolean;
}) {
  const ref = useRef<HTMLButtonElement>(null);
  const {
    cellProps,
    buttonProps,
    isSelected,
    isInvalid,
    isDisabled,
    isUnavailable,
    isFocused,
    formattedDate
  } = useCalendarCell({ date }, state, ref);

  const isOutsideMonth = !isSameMonth(currentMonth, date);
  const isToday = isDateToday(date, getLocalTimeZone());

  const isSelectionStart = state.highlightedRange
    ? isSameDay(date, state.highlightedRange.start)
    : isSelected;
  const isSelectionEnd = state.highlightedRange
    ? isSameDay(date, state.highlightedRange.end)
    : isSelected;

  const isRoundedLeft = isSelected && isSelectionStart;
  const isRoundedRight = isSelected && isSelectionEnd;

  return (
    <td
      className="text-center relative"
      data-date={date.toString()}
      {...cellProps}
    >
      <Button
        {...buttonProps}
        ref={ref}
        size="sm"
        variant={isSelected ? "primary" : "ghost"}
        className={clsx(
          "p-0 w-8 h-8 shadow-none font-normal hover:primary/10",
          {
            "bg-primary/50": isFocused,
            "bg-primary text-card hover:bg-primary": isSelected,
            "opacity-50 hover:bg-card focus:bg-card":
              isInvalid || isDisabled || isUnavailable,
            "hover:rounded-full": !isSelected,
            hidden: isOutsideMonth,
            "rounded-none": isSelected && !isRoundedLeft && !isRoundedRight,
            "!rounded-l-full !rounded-r-none": isRoundedLeft,
            "!rounded-r-full !rounded-l-none": isRoundedRight,
            "!rounded-full":
              (isFocused && !isSelected) ||
              (isSelected && isRoundedLeft && isRoundedRight)
          }
        )}
      >
        {formattedDate}
      </Button>
      {(isToday || isMarked) && !isOutsideMonth && (
        <span className="absolute bottom-1 left-1/2 flex -translate-x-1/2 gap-0.5 pointer-events-none">
          {isToday && (
            <span
              className={clsx("w-1 h-1 rounded-full", {
                "bg-card": isSelected,
                "bg-primary": !isSelected
              })}
            />
          )}
          {isMarked && (
            <span
              className={clsx("w-1 h-1 rounded-full", {
                "bg-card/70": isSelected,
                "bg-muted-foreground": !isSelected
              })}
            />
          )}
        </span>
      )}
    </td>
  );
}