{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "selection-group-internal",
  "type": "registry:lib",
  "title": "Selection group internals",
  "description": "Selection group internals canonical source.",
  "dependencies": [],
  "registryDependencies": [
    "https://ghost-ui.com/r/ghost-tokens.json",
    "https://ghost-ui.com/r/radio-group.json",
    "https://ghost-ui.com/r/checkbox-group.json",
    "https://ghost-ui.com/r/checkbox.json"
  ],
  "files": [
    {
      "path": "src/components/ui/internal/selection-group.tsx",
      "type": "registry:component",
      "target": "@ui/internal/selection-group.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { Checkbox } from \"../checkbox\";\nimport { CheckboxGroup } from \"../checkbox-group\";\nimport { RadioGroup } from \"../radio-group\";\n\ntype SharedProps = Omit<React.ComponentPropsWithoutRef<\"div\">, \"defaultValue\" | \"onChange\"> & {\n  name?: string;\n  form?: string;\n  disabled?: boolean;\n  readOnly?: boolean;\n  required?: boolean;\n};\ntype SingleProps = SharedProps & { type?: \"single\"; value?: string | null; defaultValue?: string | null; onValueChange?: (value: string | null) => void; max?: never };\ntype MultipleProps = SharedProps & { type: \"multiple\"; value?: string[]; defaultValue?: string[]; onValueChange?: (value: string[]) => void; max?: number };\nexport type SelectionGroupProps = SingleProps | MultipleProps;\ntype SelectionContextValue = {\n  type: \"single\" | \"multiple\";\n  disabled: boolean;\n  readOnly: boolean;\n  atLimit: (value: string) => boolean;\n  name?: string;\n  form?: string;\n  required?: boolean;\n};\nconst SelectionContext = React.createContext<SelectionContextValue | null>(null);\nexport function useOptionalSelectionItemContext() {\n  return React.useContext(SelectionContext);\n}\nexport function useSelectionItemContext() {\n  const context = useOptionalSelectionItemContext();\n  if (!context) throw new Error(\"Selection items require their group.\");\n  return context;\n}\nfunction assignRef<T>(ref: React.ForwardedRef<T>, node: T | null) {\n  if (typeof ref === \"function\") ref(node);\n  else if (ref) ref.current = node;\n}\nexport function useFormReset(ref: React.RefObject<HTMLElement | null>, formId: string | undefined, reset: () => void, enabled = true) {\n  React.useEffect(() => {\n    if (!enabled) return;\n    const form = formId ? document.getElementById(formId) : ref.current?.closest(\"form\");\n    if (!form) return;\n    const handle = (event: Event) => queueMicrotask(() => { if (!event.defaultPrevented) reset(); });\n    form.addEventListener(\"reset\", handle, true);\n    return () => form.removeEventListener(\"reset\", handle, true);\n  }, [ref, formId, reset, enabled]);\n}\nexport const SelectionGroup = React.forwardRef<HTMLDivElement, SelectionGroupProps>(function SelectionGroup(props, ref) {\n  return props.type === \"multiple\" ? <MultipleGroup {...props} ref={ref} /> : <SingleGroup {...props} ref={ref} />;\n});\nconst SingleGroup = React.forwardRef<HTMLDivElement, SingleProps>(function SingleGroup(\n  { type: _type, max: _max, value, defaultValue = null, onValueChange, disabled = false, readOnly = false, ...props }, ref,\n) {\n  const [current, setCurrent] = React.useState(defaultValue);\n  const selected = value === undefined ? current : value;\n  const defaults = React.useRef(defaultValue);\n  const root = React.useRef<HTMLDivElement>(null);\n  useFormReset(root, props.form, React.useCallback(() => { if (value === undefined) setCurrent(defaults.current); }, [value]));\n  return <SelectionContext.Provider value={{ type: \"single\", disabled, readOnly, atLimit: () => false, name: props.name, form: props.form, required: props.required }}>\n    <RadioGroup {...props} ref={node => { root.current = node; assignRef(ref, node); }} disabled={disabled} readOnly={readOnly} value={selected}\n      onValueChange={(next: string | null) => { if (disabled || readOnly) return; if (value === undefined) setCurrent(next); onValueChange?.(next); }} />\n  </SelectionContext.Provider>;\n});\nconst MultipleGroup = React.forwardRef<HTMLDivElement, MultipleProps>(function MultipleGroup(\n  { type: _type, max, value, defaultValue = [], onValueChange, disabled = false, readOnly = false, name, form, required, ...props }, ref,\n) {\n  const [current, setCurrent] = React.useState(defaultValue);\n  const selected = value === undefined ? current : value;\n  const defaults = React.useRef(defaultValue);\n  const root = React.useRef<HTMLDivElement>(null);\n  useFormReset(root, form, React.useCallback(() => { if (value === undefined) setCurrent(defaults.current); }, [value]));\n  const full = max !== undefined && (!Number.isInteger(max) || max < 0 || selected.length >= max);\n  return <SelectionContext.Provider value={{ type: \"multiple\", disabled, readOnly, atLimit: item => full && !selected.includes(item), name, form, required: required && selected.length === 0 }}>\n    <CheckboxGroup {...props} ref={node => { root.current = node; assignRef(ref, node); }} disabled={disabled} value={selected} aria-required={required || undefined}\n      onValueChange={next => {\n        if (disabled || readOnly || (full && next.some(item => !selected.includes(item)))) return;\n        if (value === undefined) setCurrent(next);\n        onValueChange?.(next);\n      }} />\n  </SelectionContext.Provider>;\n});\n\n// The semantic owners keep their handlers, hidden inputs and state. These\n// compositions supply their own surfaces instead of the owners' 18px glyphs.\nfunction surface(element: React.ReactElement | undefined, props: React.HTMLAttributes<HTMLElement>) {\n  const className = props.className?.split(/\\s+/).filter(name => name !== \"ui-radio\" && name !== \"ui-check\").join(\" \");\n  return React.cloneElement(element ?? <span />, { ...props, className });\n}\nexport const SelectionRadioItem = React.forwardRef<HTMLSpanElement, React.ComponentPropsWithoutRef<typeof RadioGroup.Item>>(function SelectionRadioItem({ render, ...props }, ref) {\n  return <RadioGroup.Item {...props} ref={ref} nativeButton={false} render={nativeProps => surface(React.isValidElement(render) ? render : undefined, nativeProps)} />;\n});\nexport const SelectionCheckboxItem = React.forwardRef<HTMLSpanElement, React.ComponentPropsWithoutRef<typeof Checkbox.Root>>(function SelectionCheckboxItem({ render, ...props }, ref) {\n  const context = useOptionalSelectionItemContext();\n  return <Checkbox.Root {...props} ref={ref} readOnly={props.readOnly || context?.readOnly} disabled={props.disabled || context?.disabled} nativeButton={false} render={nativeProps => surface(React.isValidElement(render) ? render : undefined, nativeProps)} />;\n});\n"
    }
  ]
}
