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.

1.8 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 {
  FormControl,
  FormErrorMessage,
  FormLabel,
  LabelWithHelp,
  RadioGroup,
  RadioGroupItem
} from "@carbon/react";
import { useId } from "react";
import { useField } from "../hooks";
import { useFormStateContext } from "../internal/formStateContext";

type RadiosProps = {
  name: string;
  label?: string;
  termId?: TermId;
  options: { label: string; value: string }[];
  orientation?: "horizontal" | "vertical";
};

const Radios = ({
  name,
  label,
  termId,
  options,
  orientation = "vertical"
}: RadiosProps) => {
  const { getInputProps, error, isOptional: fieldIsOptional } = useField(name);
  const formState = useFormStateContext();
  const isDisabled = formState.isDisabled || formState.isReadOnly;
  const id = useId();

  return (
    <FormControl isInvalid={!!error}>
      {label && (
        <FormLabel htmlFor={name} isOptional={fieldIsOptional ?? false}>
          <LabelWithHelp termId={termId}>{label}</LabelWithHelp>
        </FormLabel>
      )}
      <RadioGroup
        {...getInputProps({
          id: name
        })}
        name={name}
        orientation={orientation}
        disabled={isDisabled}
      >
        {options.map(({ label, value }) => (
          <div key={value} className="flex items-center space-x-2">
            <RadioGroupItem value={value} id={`${id}:${value}`} />
            <label htmlFor={`${id}:${value}`}>{label}</label>
          </div>
        ))}
      </RadioGroup>
      {error && <FormErrorMessage>{error}</FormErrorMessage>}
    </FormControl>
  );
};

export default Radios;