// This file is part of Shadcn Admin Kit (https://github.com/marmelab/shadcn-admin-kit)
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";
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;
}