{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-radio-group",
  "type": "registry:ui",
  "title": "react-tw/radio-group.tsx",
  "description": "Carbon `RadioButtonGroup` / `RadioButton` as React + Tailwind components, with native radio Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [
    "@purpurabog/ui@^2.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/tw-field",
    "@kanok-ui/tw-i18n",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/radio-group.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/radio-group.tsx",
      "content": "import { createContext, useCallback, useContext, useId, useMemo, useRef, useState, type ChangeEvent, type ReactNode, type ComponentProps } from 'react';\nimport { RadioGroup as RadioGroupPrimitive } from '@purpurabog/ui/headless';\nimport { FieldMessageLine, useFieldDisabled, fieldMessage, fieldLabelClasses, useGroupTouched } from './field.tsx';\nimport { useTranslation } from './lib/i18n.ts';\nimport { cn } from './lib/utils.ts';\n\n/**\n * Carbon `RadioButtonGroup` / `RadioButton` as React + Tailwind components, with native radio\n * inputs as Carbon React does (the browser gives arrow keys, wrapping and forms), drawn as\n * `<ui-radio>`: an 18px circle with an 8px dot, the focus ring on `:focus`. `readOnly` keeps the\n * value: clicks and arrow keys change nothing. `labelPosition=\"left\"` (Carbon, on the group or one radio) puts\n * the text before the circle, 8px apart; a vertical group then lines the circles up at its end. `required`: every native\n * radio is required, so the form won't submit until one is chosen; the error (`requiredText`) shows once the group is\n * touched, as `<ui-radio-group>`.\n */\n\n/** Carbon `labelPosition`: the text after the circle (`right`, the default) or before it (`left`). */\nexport type RadioLabelPosition = 'left' | 'right';\n\nexport interface GroupState {\n    name: string;\n    value: string;\n    disabled: boolean;\n    readOnly: boolean;\n    invalid: boolean;\n    describedBy?: string;\n    /** The group's `labelPosition`; a radio's own wins. */\n    labelPosition?: RadioLabelPosition;\n    /** The group's `required`, set on every native radio. */\n    required: boolean;\n    select: (value: string, event: ChangeEvent<HTMLInputElement>) => void;\n}\n\n/** What the radios need beyond the primitive's state: the group's `labelPosition` and `required`. */\nconst RadioLookContext = createContext<{ labelPosition: RadioLabelPosition; required: boolean } | null>(null);\n\n/** The surrounding RadioGroup's state, for other kinds of option (RadioTile); throws outside a group. */\nexport function useRadioGroup(component: string): GroupState {\n    const look = useContext(RadioLookContext);\n    if (!look) throw new Error(`<${component}> goes inside <RadioGroup>`);\n    return { ...RadioGroupPrimitive.useRadioGroupContext(`<${component}>`), labelPosition: look.labelPosition, required: look.required };\n}\n\nexport interface RadioGroupProps extends Omit<ComponentProps<'div'>, 'label' | 'hideLabel' | 'name' | 'value' | 'defaultValue' | 'onValueChange' | 'orientation' | 'labelPosition' | 'disabled' | 'readOnly' | 'required' | 'requiredText' | 'helperText' | 'invalid' | 'invalidText' | 'warn' | 'warnText' | 'lang' | 'className' | 'children'> {\n    /** Group label (`hideLabel` keeps it for screen readers only). */\n    label: ReactNode;\n    hideLabel?: boolean;\n    /** Submitted name; generated when absent. */\n    name?: string;\n    value?: string;\n    defaultValue?: string;\n    onValueChange?: (value: string, event: ChangeEvent<HTMLInputElement>) => void;\n    orientation?: 'vertical' | 'horizontal';\n    /** Carbon `labelPosition`: `left` puts each radio's text before its circle (default `right`, after it). */\n    labelPosition?: RadioLabelPosition;\n    disabled?: boolean;\n    readOnly?: boolean;\n    /** Lit `required`: one option must be chosen, or the form won't submit. */\n    required?: boolean;\n    /** Lit `required-text`: the error for an empty required group; defaults to the translation's \"Please select an option.\" */\n    requiredText?: string;\n    helperText?: ReactNode;\n    invalid?: boolean;\n    /** Error text; defaults to `requiredText`. */\n    invalidText?: ReactNode;\n    warn?: boolean;\n    warnText?: ReactNode;\n    lang?: string;\n    className?: string;\n    children: ReactNode;\n}\n\nexport function RadioGroup({ label, hideLabel, name, value, defaultValue = '', onValueChange, orientation = 'vertical', labelPosition = 'right', disabled: ownDisabled, readOnly = false, required = false, requiredText, helperText, invalid, invalidText, warn, warnText, lang, className, children, ...rest }: Readonly<RadioGroupProps>) {\n    const disabled = useFieldDisabled(ownDisabled);\n    const t = useTranslation(lang).field;\n    const id = useId();\n    const optionsRef = useRef<HTMLDivElement>(null);\n    const { touched, handlers } = useGroupTouched(optionsRef);\n    // the chosen value of an uncontrolled group, for `required` (the primitive keeps its own)\n    const [chosen, setChosen] = useState(defaultValue);\n    const onChoose = useCallback(\n        (next: string, event: ChangeEvent<HTMLInputElement>) => {\n            setChosen(next);\n            onValueChange?.(next, event);\n        },\n        [onValueChange],\n    );\n    const empty = required && (value ?? chosen) === '';\n    const interactive = !disabled && !readOnly;\n    const { message, content } = fieldMessage({ interactive, invalid: !!invalid || (touched && empty), invalidText, warn, warnText, helperText, fallbackError: requiredText || t.requiredOne });\n    const describedBy = message ? `${id}-message` : undefined;\n    const look = useMemo(() => ({ labelPosition, required }), [labelPosition, required]);\n    return (\n        <div {...rest} className={cn('block font-sans', className)}>\n            <div id={`${id}-label`} className={hideLabel ? 'sr-only' : cn(fieldLabelClasses, disabled && 'text-ink-disabled')}>\n                {label}\n            </div>\n            <RadioGroupPrimitive.Root\n                ref={optionsRef}\n                name={name ?? id}\n                value={value}\n                defaultValue={defaultValue}\n                onValueChange={onChoose}\n                orientation={orientation}\n                disabled={disabled}\n                readOnly={readOnly}\n                required={required}\n                invalid={message?.kind === 'error'}\n                aria-labelledby={`${id}-label`}\n                aria-describedby={describedBy}\n                onChange={handlers.onChange}\n                onInvalid={handlers.onInvalid}\n                onBlur={handlers.onBlur}\n                className={cn(\n                    'flex flex-col gap-1',\n                    orientation === 'horizontal' && 'flex-row flex-wrap gap-4',\n                    // Carbon radio-button-group--vertical.--label-left: align-items flex-end, the group as wide as its content\n                    orientation !== 'horizontal' && labelPosition === 'left' && 'w-fit items-end',\n                )}\n            >\n                <RadioLookContext.Provider value={look}>{children}</RadioLookContext.Provider>\n            </RadioGroupPrimitive.Root>\n            <FieldMessageLine id={`${id}-message`} message={message} content={content} disabled={disabled} withIcon />\n        </div>\n    );\n}\n\nexport interface RadioProps extends Omit<ComponentProps<'input'>, 'value' | 'disabled' | 'children' | 'className'> {\n    value: string;\n    disabled?: boolean;\n    /** Carbon `labelPosition`: `left` puts the text before the circle; unset, the group's applies. */\n    labelPosition?: RadioLabelPosition;\n    children: ReactNode;\n    className?: string;\n}\n\nexport function Radio({ value, disabled: ownDisabled, labelPosition, children, className, ...rest }: Readonly<RadioProps>) {\n    const group = useRadioGroup('Radio');\n    const disabled = group.disabled || !!ownDisabled;\n    const { readOnly } = group;\n    const left = (labelPosition ?? group.labelPosition) === 'left';\n    return (\n        <label\n            className={cn(\n                'relative box-border flex min-h-6 cursor-pointer items-start gap-[10px] type-body-sm text-ink',\n                // Carbon radio-button__label--left: row-reverse, 8px between the text and the circle, as wide as its content\n                left && 'w-fit flex-row-reverse gap-2',\n                disabled && 'cursor-not-allowed text-ink-disabled',\n                readOnly && !disabled && 'cursor-default',\n                className,\n            )}\n        >\n            <RadioGroupPrimitive.Item {...rest}\n                value={value}\n                disabled={ownDisabled}\n                required={group.required || undefined}\n                aria-describedby={[rest['aria-describedby'], group.describedBy].filter(Boolean).join(' ') || undefined}\n                className={cn('peer absolute top-[3px] z-1 m-0 size-[18px] cursor-[inherit] opacity-0', left ? 'end-0' : 'start-0')}\n            />\n            <span\n                aria-hidden=\"true\"\n                className={cn(\n                    // 3px above and below the circle and the text, as <ui-radio> (Carbon 24px row)\n                    // the radio settings (--ui-radio-border / -checked): Carbon draws in primary ink, a design can use its brand colour\n                    'my-[3px] box-border grid size-[18px] shrink-0 place-items-center rounded-[50%] border border-solid border-[color:var(--ui-radio-border,var(--ui-ink))] bg-transparent',\n                    'peer-checked:border-[color:var(--ui-radio-checked,var(--ui-ink))]',\n                    \"peer-checked:after:size-2 peer-checked:after:rounded-[50%] peer-checked:after:bg-[color:var(--ui-radio-checked,var(--ui-ink))] peer-checked:after:content-['']\",\n                    // Carbon radio-button:focus (not :focus-visible), so a click shows the ring too\n                    'peer-focus:outline-[length:var(--ui-focus-width,2px)] peer-focus:outline-offset-1 peer-focus:outline-focus peer-[:focus:not(:focus-visible)]:outline-[color:var(--ui-pointer-focus,var(--ui-focus))]',\n                    group.invalid && 'border-2 border-error peer-checked:border-error',\n                    disabled && 'border-ink-disabled peer-checked:border-ink-disabled peer-checked:after:bg-ink-disabled',\n                    readOnly && !disabled && 'border-ink-subtle peer-checked:border-ink-subtle peer-checked:after:bg-ink-muted',\n                    'forced-colors:border-[CanvasText] forced-colors:peer-checked:border-[CanvasText] forced-colors:[forced-color-adjust:none] forced-colors:peer-checked:after:bg-[Highlight]',\n                )}\n            />\n            <span className=\"my-[3px]\">{children}</span>\n        </label>\n    );\n}\n\n/** Carbon `RadioButtonSkeleton`: a 100×18 label bar (Carbon draws no circle). Hidden from assistive tech. */\nexport function RadioSkeleton({ className, ...rest }: Readonly<ComponentProps<'div'>>) {\n    return <div {...rest} aria-hidden=\"true\" className={cn('skeleton block h-[18px] w-[100px]', className)} />;\n}\n"
    }
  ]
}
