Skip to content

Commit a2b25fa

Browse files
AhmadYasser1sahitya-chandraeunjae-lee
authored
fix: prevent deselecting active option in booker layout toggle (calcom#27748)
* fix: prevent deselecting active option in ToggleGroup Radix UI's single-type ToggleGroup allows deselection by default, calling onValueChange("") when the active item is clicked. Fix by converting to controlled mode with proper dual-mode support: - Controlled (value prop): parent owns state, component just filters empty values from onValueChange. Parent can still reject changes. - Uncontrolled (defaultValue prop): internal useState prevents deselection by only updating state for non-empty values. * Update ToggleGroup.tsx --------- Co-authored-by: Sahitya Chandra <sahityajb@gmail.com> Co-authored-by: Eunjae Lee <hey@eunjae.dev>
1 parent 14c151b commit a2b25fa

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

packages/ui/components/form/toggleGroup/ToggleGroup.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as RadixToggleGroup from "@radix-ui/react-toggle-group";
22
import type { ReactNode } from "react";
3+
import { useState } from "react";
34

45
import classNames from "@calcom/ui/classNames";
56

@@ -42,15 +43,26 @@ export const ToggleGroup = ({
4243
isFullWidth,
4344
orientation = "horizontal",
4445
customClassNames,
46+
defaultValue,
4547
...props
4648
}: ToggleGroupProps & { customClassNames?: string }) => {
49+
const isControlled = props.value !== undefined;
50+
const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue ?? "");
51+
4752
return (
4853
<>
4954
<RadixToggleGroup.Root
5055
type="single"
5156
{...props}
57+
{...(isControlled ? { value: props.value } : { value: uncontrolledValue })}
5258
orientation={orientation}
53-
onValueChange={onValueChange}
59+
onValueChange={(value) => {
60+
if (!value) return;
61+
if (!isControlled) {
62+
setUncontrolledValue(value);
63+
}
64+
onValueChange?.(value);
65+
}}
5466
style={{
5567
// @ts-expect-error --toggle-group-shadow is not a valid CSS property but can be a variable
5668
"--toggle-group-shadow":

0 commit comments

Comments
 (0)