import * as React from "react"; import type { ChoicesProps, InputProps } from "ra-core"; import { FieldTitle, useChoices, useChoicesContext, useInput } from "ra-core"; import { cn } from "@/lib/utils"; import { FormField, FormControl, FormLabel, FormError, } from "@/components/admin/form"; import { Label } from "@/components/ui/label"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { Skeleton } from "@/components/ui/skeleton"; import { InputHelperText } from "@/components/admin/input-helper-text"; /** * Single-select input rendered as a list of radio buttons, arranged vertically or horizontally. * * Use `` when you have a small set of options (2-5) that users should * see all at once, like status, priority, or category fields. For longer lists, prefer * ``. Set `row` to `true` for horizontal layout. * * @see {@link https://marmelab.com/shadcn-admin-kit/docs/radiobuttongroupinput/ RadioButtonGroupInput documentation} * * @example * import { Edit, SimpleForm, TextInput, RadioButtonGroupInput } from '@/components/admin'; * * const PostEdit = () => ( * * * * * * * ); */ export const RadioButtonGroupInput = (inProps: RadioButtonGroupInputProps) => { const { choices: choicesProp, isFetching: isFetchingProp, isLoading: isLoadingProp, isPending: isPendingProp, resource: resourceProp, source: sourceProp, format, onBlur, onChange, parse, validate, disabled, readOnly, optionText, optionValue = "id", translateChoice, disableValue = "disabled", className, helperText, label, row, ...rest } = inProps; const { allChoices, isPending, error: fetchError, resource, source, } = useChoicesContext({ choices: choicesProp, isFetching: isFetchingProp, isLoading: isLoadingProp, isPending: isPendingProp, resource: resourceProp, source: sourceProp, }); if (source === undefined) { throw new Error( `If you're not wrapping the RadioButtonGroupInput inside a ReferenceArrayInput, you must provide the source prop`, ); } if (!isPending && !fetchError && allChoices === undefined) { throw new Error( `If you're not wrapping the RadioButtonGroupInput inside a ReferenceArrayInput, you must provide the choices prop`, ); } const { id, field, isRequired } = useInput({ format, onBlur, onChange, parse, resource, source, validate, disabled, readOnly, ...rest, }); const { getChoiceText, getChoiceValue, getDisableValue } = useChoices({ optionText, optionValue, translateChoice, disableValue, }); if (isPending) { return ; } return ( {label && ( )} {allChoices?.map((choice) => { const value = getChoiceValue(choice); const isDisabled = disabled || readOnly || getDisableValue(choice); return (
); })}
); }; export interface RadioButtonGroupInputProps extends Partial, ChoicesProps, Omit< React.ComponentProps, "defaultValue" | "onBlur" | "onChange" | "type" > { row?: boolean; }