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.1 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 { NumberFieldProps } from "@carbon/react";
import { cn, NumberField, NumberInput } from "@carbon/react";
import type { PostgrestSingleResponse } from "@supabase/supabase-js";
import { useRef } from "react";
import type { EditableTableCellComponentProps } from "~/components/Editable";

const EditableNumber = <T extends object>(
  mutation: (
    accessorKey: string,
    newValue: string,
    row: T
  ) => Promise<PostgrestSingleResponse<unknown>>,
  numberFieldProps?: NumberFieldProps | ((row: T) => NumberFieldProps),
  options?: {
    clearable?: boolean;
    /** Classes for the editor's input — e.g. `text-right` for an amount
     *  column, so the value does not jump sides when the cell opens. */
    inputClassName?: string;
  }
) => {
  const EditableNumberEditor = ({
    value,
    row,
    accessorKey,
    onError,
    onUpdate
  }: EditableTableCellComponentProps<T>) => {
    const resolvedProps =
      typeof numberFieldProps === "function"
        ? numberFieldProps(row)
        : numberFieldProps;

    const numericValue =
      value === null || value === undefined || value === ""
        ? undefined
        : Number(value);

    const latestValue = useRef<number | undefined>(numericValue);

    const clamp = (n: number) => {
      const { minValue, maxValue } = resolvedProps ?? {};
      let v = n;
      if (typeof maxValue === "number") v = Math.min(v, maxValue);
      if (typeof minValue === "number") v = Math.max(v, minValue);
      return v;
    };

    const commit = (raw: number | undefined) => {
      const isEmpty = raw === undefined || !Number.isFinite(raw);
      const next = isEmpty ? null : clamp(raw as number);
      if (next === (numericValue ?? null)) return;
      if (
        isEmpty &&
        (!options?.clearable || value === null || value === undefined)
      )
        return;

      onUpdate({ [accessorKey]: next });

      // @ts-expect-error
      mutation(accessorKey, isEmpty ? "" : next, row)
        .then(({ error }) => {
          if (error) {
            onError();
            onUpdate({ [accessorKey]: value });
          }
        })
        .catch(() => {
          onError();
          onUpdate({ [accessorKey]: value });
        });
    };

    return (
      <NumberField
        {...resolvedProps}
        defaultValue={numericValue}
        onChange={(next) => {
          latestValue.current = Number.isNaN(next) ? undefined : next;
          resolvedProps?.onChange?.(next);
        }}
      >
        <NumberInput
          size="sm"
          className={cn(
            "w-full rounded-none outline-none border-none shadow-none focus-visible:ring-0",
            options?.inputClassName
          )}
          autoFocus
          onFocus={(e) => e.currentTarget.select()}
          onBlur={() => commit(latestValue.current)}
        />
      </NumberField>
    );
  };

  return EditableNumberEditor;
};

export default EditableNumber;