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.

4.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 { TermId } from "@carbon/content/glossary";
import {
  cn,
  FormControl,
  FormErrorMessage,
  FormHelperText,
  FormLabel,
  HStack,
  Label,
  LabelWithHelp,
  Switch,
  VStack
} from "@carbon/react";
import { forwardRef, useEffect } from "react";
import { useControlField, useField } from "../hooks";
import { useFormStateContext } from "../internal/formStateContext";

type FormBooleanProps = {
  name: string;
  variant?: "large" | "small";
  label?: string;
  termId?: TermId;
  value?: boolean;
  helperText?: string;
  isDisabled?: boolean;
  isOptional?: boolean;
  bordered?: boolean;
  className?: string;
  description?: string | JSX.Element;
  onChange?: (value: boolean) => void;
};

const Boolean = forwardRef<HTMLInputElement, FormBooleanProps>(
  (
    {
      name,
      label,
      termId,
      description,
      helperText,
      onChange,
      variant,
      bordered,
      isDisabled: isDisabledProp,
      isOptional,
      value: controlledValue,
      className,
      ...props
    },
    ref
  ) => {
    const {
      getInputProps,
      error,
      isOptional: fieldIsOptional
    } = useField(name);
    const formState = useFormStateContext();
    const isDisabled =
      formState.isDisabled || formState.isReadOnly || isDisabledProp;
    const [value, setValue] = useControlField<boolean>(name);
    // A switch shown with only a description (a day of the week) is named by it.
    const accessibleName =
      label ?? (typeof description === "string" ? description : undefined);

    useEffect(() => {
      if (controlledValue !== null && controlledValue !== undefined)
        setValue(controlledValue);
    }, [controlledValue, setValue]);

    if (bordered) {
      return (
        <FormControl isInvalid={!!error} className={className}>
          <HStack className="justify-between items-center gap-4 border border-border rounded-lg p-4">
            <VStack spacing={1}>
              {label && (
                <Label
                  htmlFor={name}
                  className="text-sm text-foreground cursor-pointer"
                >
                  <LabelWithHelp termId={termId}>{label}</LabelWithHelp>
                </Label>
              )}
              {description && (
                <p className="text-xs text-muted-foreground">{description}</p>
              )}
            </VStack>
            <Switch
              id={name}
              variant={variant}
              {...getInputProps()}
              checked={value}
              disabled={isDisabled}
              onCheckedChange={(checked) => {
                setValue(checked);
                onChange?.(checked);
              }}
              aria-label={accessibleName}
              {...props}
            />
          </HStack>
          {error ? (
            <FormErrorMessage>{error}</FormErrorMessage>
          ) : (
            helperText && <FormHelperText>{helperText}</FormHelperText>
          )}
        </FormControl>
      );
    }

    return (
      <FormControl isInvalid={!!error} className={cn("pt-2", className)}>
        {label && (
          <FormLabel
            htmlFor={name}
            isOptional={isOptional ?? fieldIsOptional ?? false}
          >
            <LabelWithHelp termId={termId}>{label}</LabelWithHelp>
          </FormLabel>
        )}
        <HStack className="max-md:-ml-1">
          <Switch
            variant={variant}
            {...getInputProps()}
            checked={value}
            disabled={isDisabled}
            onCheckedChange={(checked) => {
              setValue(checked);
              onChange?.(checked);
            }}
            aria-label={accessibleName}
            {...props}
          />
          {description && (
            <p className="text-muted-foreground text-sm">{description}</p>
          )}
        </HStack>

        {error ? (
          <FormErrorMessage>{error}</FormErrorMessage>
        ) : (
          helperText && <FormHelperText>{helperText}</FormHelperText>
        )}
      </FormControl>
    );
  }
);

Boolean.displayName = "Boolean";

export default Boolean;