Skip to content

Commit ae70bc1

Browse files
committed
chore(radio-group): implement Radio component
1 parent a82155b commit ae70bc1

File tree

6 files changed

+38
-47
lines changed

6 files changed

+38
-47
lines changed

packages/ui-radio-group/package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
"types": "./dist/index.d.ts",
4141
"import": "./dist/index.js",
4242
"require": "./dist/index.cjs"
43+
},
44+
"./radio": {
45+
"types": "./dist/radio.d.ts",
46+
"import": "./dist/radio.js",
47+
"require": "./dist/radio.cjs"
4348
}
4449
},
4550
"dependencies": {
@@ -52,7 +57,7 @@
5257
"@halvaradop/ui-utils": "workspace:*"
5358
},
5459
"peerDependencies": {
55-
"react": "^18.0.0",
56-
"react-dom": "^18.0.0"
60+
"react": "^19.0.0",
61+
"react-dom": "^19.0.0"
5762
}
5863
}

packages/ui-radio-group/src/context.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createContext, useContext, ChangeEventHandler } from "react"
33
export interface RadioGroupContextType {
44
name?: string
55
selectedValue?: string
6-
onChange?: ChangeEventHandler<HTMLInputElement>
6+
onChange: ChangeEventHandler<HTMLInputElement>
77
}
88

99
export const RadioGroupContext = createContext<RadioGroupContextType>({

packages/ui-radio-group/src/index.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"use client"
2-
import { ChangeEvent, useState } from "react"
2+
import { ChangeEvent, ChangeEventHandler, useState } from "react"
33
import { merge, type ComponentProps, type WithChildrenProps, type ArgsFunction } from "@halvaradop/ui-core"
44
import { cva, type VariantProps } from "class-variance-authority"
55
import { RadioGroupContext } from "./context.js"
66

77
export type RadioGroupProps<T extends ArgsFunction> = VariantProps<T> &
8-
WithChildrenProps<ComponentProps<"fieldset", "defaultValue">> & {
8+
WithChildrenProps<ComponentProps<"fieldset", "defaultValue" | "onChange">> & {
99
defaultValue?: string
10+
onChange?: ChangeEventHandler<HTMLInputElement>
1011
}
1112

1213
export const radioGroupVariants = cva("flex", {
@@ -28,11 +29,13 @@ export const RadioGroup = ({
2829
defaultValue,
2930
children,
3031
ref,
32+
onChange,
3133
...props
3234
}: RadioGroupProps<typeof radioGroupVariants>) => {
3335
const [selectedValue, setSelectedValue] = useState(defaultValue)
3436

3537
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
38+
onChange?.(event)
3639
setSelectedValue(event.target.value)
3740
}
3841

packages/ui-radio-group/src/radio-group.stories.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ export const Base: Story = {
4545
render: ({ variant }) => (
4646
<RadioGroup name="food" variant={variant}>
4747
<Label className="flex items-center gap-x-2">
48-
<Radio value="pizza" name="food" />
48+
<Radio value="pizza" />
4949
Pizza
5050
</Label>
5151
<Label className="flex items-center gap-x-2">
52-
<Radio value="hamburger" name="food" />
52+
<Radio value="hamburger" />
5353
Hamburger
5454
</Label>
5555
</RadioGroup>

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

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
1-
export * from "@halvaradop/ui-radio"
2-
export type * from "@halvaradop/ui-radio"
1+
import { Radio as DefaultRadio, radioVariants, type RadioProps } from "@halvaradop/ui-radio"
2+
import { useRadioGroup } from "./context.js"
3+
4+
const Radio = ({ name, value, ref, ...props }: Omit<RadioProps<typeof radioVariants>, "onChange">) => {
5+
const { name: defaultName, selectedValue, onChange } = useRadioGroup()
6+
7+
return (
8+
<DefaultRadio
9+
ref={ref}
10+
name={name ?? defaultName}
11+
value={value}
12+
checked={selectedValue === value}
13+
onChange={onChange}
14+
{...props}
15+
/>
16+
)
17+
}
18+
19+
export { Radio, radioVariants }

pnpm-lock.yaml

+4-38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)