Skip to content

Commit

Permalink
feat: RadioGroup component & story (#1478)
Browse files Browse the repository at this point in the history
* feat: `RadioGroup` component & story

* fix(RadioGroup): set text color only once
  • Loading branch information
BrickheadJohnny authored Sep 12, 2024
1 parent 14f0f25 commit 0913140
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@radix-ui/react-focus-scope": "^1.1.0",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-popover": "^1.1.1",
"@radix-ui/react-radio-group": "^1.2.0",
"@radix-ui/react-select": "^2.1.1",
"@radix-ui/react-separator": "^1.1.0",
"@radix-ui/react-slot": "^1.1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/v2/components/ui/Label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { cn } from "@/lib/utils"
import { ComponentPropsWithoutRef, ElementRef, forwardRef } from "react"

const labelVariants = cva(
"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
"font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
)

const Label = forwardRef<
Expand Down
37 changes: 37 additions & 0 deletions src/v2/components/ui/RadioGroup.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Meta, StoryObj } from "@storybook/react"
import { Label } from "./Label"
import { RadioGroup, RadioGroupItem, RadioGroupProps } from "./RadioGroup"

const RadioGroupExample = (props: RadioGroupProps) => (
<RadioGroup {...props} defaultValue="option-one">
<div className="flex items-center space-x-2">
<RadioGroupItem value="option-one" id="option-one" />
<Label htmlFor="option-one">Option One</Label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem value="option-two" id="option-two" />
<Label htmlFor="option-two">Option Two</Label>
</div>
</RadioGroup>
)

const meta: Meta<typeof RadioGroupExample> = {
title: "Design system/RadioGroup",
component: RadioGroupExample,
}

export default meta

type Story = StoryObj<typeof RadioGroupExample>

export const Default: Story = {
args: {
disabled: false,
},
argTypes: {
disabled: {
type: "boolean",
control: "boolean",
},
},
}
47 changes: 47 additions & 0 deletions src/v2/components/ui/RadioGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use client"

import { cn } from "@/lib/utils"
import { Check } from "@phosphor-icons/react/dist/ssr"
import * as RadioGroupPrimitive from "@radix-ui/react-radio-group"
import { ComponentPropsWithoutRef, ElementRef, forwardRef } from "react"

export type RadioGroupProps = ComponentPropsWithoutRef<
typeof RadioGroupPrimitive.Root
>

const RadioGroup = forwardRef<
ElementRef<typeof RadioGroupPrimitive.Root>,
RadioGroupProps
>(({ className, ...props }, ref) => {
return (
<RadioGroupPrimitive.Root
className={cn("grid gap-2", className)}
{...props}
ref={ref}
/>
)
})
RadioGroup.displayName = RadioGroupPrimitive.Root.displayName

const RadioGroupItem = forwardRef<
ElementRef<typeof RadioGroupPrimitive.Item>,
ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Item>
>(({ className, ...props }, ref) => {
return (
<RadioGroupPrimitive.Item
ref={ref}
className={cn(
"relative size-4 cursor-pointer rounded-full border-2 border-border text-white focus:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:border-0 data-[state=checked]:bg-primary [&~label]:cursor-pointer [&~label]:disabled:opacity-50",
className
)}
{...props}
>
<RadioGroupPrimitive.Indicator className="absolute inset-0 flex items-center justify-center rounded-full bg-primary">
<Check className="size-3" weight="bold" />
</RadioGroupPrimitive.Indicator>
</RadioGroupPrimitive.Item>
)
})
RadioGroupItem.displayName = RadioGroupPrimitive.Item.displayName

export { RadioGroup, RadioGroupItem }

0 comments on commit 0913140

Please sign in to comment.