Skip to content

Commit 4140bd6

Browse files
committed
Allow passing value of null to controlled Radio Group
1 parent ead70cd commit 4140bd6

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

.changeset/icy-areas-poke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@radix-ui/react-radio-group': patch
3+
---
4+
5+
Allow passing value of `null` to controlled Radio Group when all items are unchecked

apps/storybook/stories/radio-group.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export const LegacyChromatic = () => {
223223

224224
<h1>Controlled</h1>
225225
<h2>Unset</h2>
226-
<RadioGroup.Root className={styles.root} value="">
226+
<RadioGroup.Root className={styles.root} value={null}>
227227
<RadioGroup.Item className={styles.item} value="1">
228228
<RadioGroup.Indicator className={styles.indicator} />
229229
</RadioGroup.Item>

packages/react/radio-group/src/radio-group.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type RadioGroupContextValue = {
3030
name?: string;
3131
required: boolean;
3232
disabled: boolean;
33-
value: string;
33+
value: string | null;
3434
onValueChange(value: string): void;
3535
};
3636

@@ -48,7 +48,7 @@ interface RadioGroupProps extends PrimitiveDivProps {
4848
orientation?: RovingFocusGroupProps['orientation'];
4949
loop?: RovingFocusGroupProps['loop'];
5050
defaultValue?: string;
51-
value?: RadioGroupContextValue['value'];
51+
value?: string | null;
5252
onValueChange?: RadioGroupContextValue['onValueChange'];
5353
}
5454

@@ -71,8 +71,8 @@ const RadioGroup = React.forwardRef<RadioGroupElement, RadioGroupProps>(
7171
const direction = useDirection(dir);
7272
const [value, setValue] = useControllableState({
7373
prop: valueProp,
74-
defaultProp: defaultValue ?? '',
75-
onChange: onValueChange,
74+
defaultProp: defaultValue ?? null,
75+
onChange: onValueChange as (value: string | null) => void,
7676
caller: RADIO_GROUP_NAME,
7777
});
7878

0 commit comments

Comments
 (0)