-
Notifications
You must be signed in to change notification settings - Fork 782
Expand file tree
/
Copy pathradio-button-group-input.tsx
More file actions
185 lines (171 loc) · 4.78 KB
/
Copy pathradio-button-group-input.tsx
File metadata and controls
185 lines (171 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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 `<RadioButtonGroupInput>` 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
* `<SelectInput>`. 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 = () => (
* <Edit>
* <SimpleForm>
* <TextInput source="title" />
* <RadioButtonGroupInput
* source="category"
* choices={[
* { id: 'tech', name: 'Tech' },
* { id: 'lifestyle', name: 'Lifestyle' },
* { id: 'people', name: 'People' },
* ]}
* />
* </SimpleForm>
* </Edit>
* );
*/
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 <Skeleton className="w-full h-9" />;
}
return (
<FormField id={id} className={className} name={field.name}>
{label && (
<FormLabel>
<FieldTitle
label={label}
source={source}
resource={resource}
isRequired={isRequired}
/>
</FormLabel>
)}
<FormControl>
<RadioGroup
{...rest}
value={field.value || ""}
onValueChange={field.onChange}
className={cn("flex", row ? "flex-row gap-4" : "flex-col gap-2")}
disabled={disabled || readOnly}
>
{allChoices?.map((choice) => {
const value = getChoiceValue(choice);
const isDisabled = disabled || readOnly || getDisableValue(choice);
return (
<div key={value} className="flex items-center space-x-2">
<RadioGroupItem
value={value}
id={`${id}-${value}`}
disabled={isDisabled}
/>
<Label
htmlFor={`${id}-${value}`}
className={cn(
"text-sm font-normal cursor-pointer",
isDisabled && "opacity-50 cursor-not-allowed",
)}
>
{getChoiceText(choice)}
</Label>
</div>
);
})}
</RadioGroup>
</FormControl>
<InputHelperText helperText={helperText} />
<FormError />
</FormField>
);
};
export interface RadioButtonGroupInputProps
extends
Partial<InputProps>,
ChoicesProps,
Omit<
React.ComponentProps<typeof RadioGroup>,
"defaultValue" | "onBlur" | "onChange" | "type"
> {
row?: boolean;
}