Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f1341e
commit 1ccecbc
Showing
5 changed files
with
92 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ComponentMeta } from "@storybook/react"; | ||
|
||
import { ToggleGroup } from "@calcom/ui/v2/core/form/ToggleGroup"; | ||
|
||
export default { | ||
title: "Toggle Group", | ||
component: ToggleGroup, | ||
} as ComponentMeta<typeof ToggleGroup>; | ||
|
||
export const Default = () => { | ||
return ( | ||
<ToggleGroup | ||
defaultValue="12" | ||
options={[ | ||
{ value: "12", label: "12h" }, | ||
{ value: "24", label: "24h" }, | ||
]} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as RadixToggleGroup from "@radix-ui/react-toggle-group"; | ||
import { useEffect, useState } from "react"; | ||
|
||
import { classNames } from "@calcom/lib"; | ||
|
||
export const ToggleGroupItem = () => <div>hi</div>; | ||
|
||
interface ToggleGroupProps extends Omit<RadixToggleGroup.ToggleGroupSingleProps, "type"> { | ||
options: { value: string; label: string }[]; | ||
} | ||
|
||
export const ToggleGroup = ({ options, onValueChange, ...props }: ToggleGroupProps) => { | ||
const [value, setValue] = useState<string | undefined>(props.defaultValue); | ||
const [activeToggleElement, setActiveToggleElement] = useState<null | HTMLButtonElement>(null); | ||
|
||
useEffect(() => { | ||
if (value && onValueChange) onValueChange(value); | ||
}, [value, onValueChange]); | ||
|
||
return ( | ||
<> | ||
<RadixToggleGroup.Root | ||
type="single" | ||
{...props} | ||
onValueChange={setValue} | ||
className={classNames( | ||
"dark:border-darkgray-200 relative inline-flex rounded-md border border-gray-200 p-1", | ||
props.className | ||
)}> | ||
{/* Active toggle. It's a separate element so we can animate it nicely. */} | ||
<span | ||
aria-hidden | ||
className="dark:bg-darkgray-200 absolute top-[4px] bottom-[4px] left-0 z-[0] rounded-[4px] bg-gray-200 transition-all" | ||
style={{ left: activeToggleElement?.offsetLeft, width: activeToggleElement?.offsetWidth }} | ||
/> | ||
{options.map((option) => ( | ||
<RadixToggleGroup.Item | ||
key={option.value} | ||
value={option.value} | ||
className="relative rounded-[4px] px-3 py-1 text-sm dark:text-neutral-200 [&[aria-checked='false']]:hover:font-medium" | ||
ref={(node) => { | ||
if (node && value === option.value) { | ||
setActiveToggleElement(node); | ||
} | ||
return node; | ||
}}> | ||
{option.label} | ||
</RadixToggleGroup.Item> | ||
))} | ||
</RadixToggleGroup.Root> | ||
</> | ||
); | ||
}; |