-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
RadioGroup
component (#76)
- Loading branch information
1 parent
e3bcdf0
commit f1fd843
Showing
7 changed files
with
184 additions
and
1 deletion.
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,48 @@ | ||
{ | ||
"name": "@halvaradop/ui-radio-group", | ||
"version": "0.1.0", | ||
"private": false, | ||
"description": "A customizable Radio Group component for @halvaradop/ui library with Tailwind CSS styling.", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "tsup --watch", | ||
"build": "tsup", | ||
"format": "prettier --write .", | ||
"format:check": "prettier --check .", | ||
"clean": "pnpm clean:build && pnpm clean:modules", | ||
"clean:build": "rm -rf dist", | ||
"clean:modules": "rm -rf node_modules" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/halvaradop/ui.git" | ||
}, | ||
"keywords": [ | ||
"react", | ||
"component", | ||
"tailwindcss", | ||
"react", | ||
"ui", | ||
"ui library" | ||
], | ||
"author": "Hernan Alvarado <[email protected]>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/halvaradop/ui/issues" | ||
}, | ||
"homepage": "https://github.com/halvaradop/ui#readme", | ||
"files": [ | ||
"dist" | ||
], | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs" | ||
} | ||
}, | ||
"dependencies": { | ||
"@halvaradop/ui-core": "workspace:*", | ||
"class-variance-authority": "0.7.0" | ||
} | ||
} |
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,43 @@ | ||
"use client" | ||
import { ComponentProps, forwardRef, useEffect, useRef } from "react" | ||
import { merge, type ArgsFunction } from "@halvaradop/ui-core" | ||
import { cva, type VariantProps } from "class-variance-authority" | ||
|
||
export type RadioGroupProps<T extends ArgsFunction> = VariantProps<T> & | ||
Omit<ComponentProps<"fieldset">, "children"> & { | ||
children: React.ReactNode | ||
} | ||
|
||
export const radioGroupVariants = cva("flex", { | ||
variants: { | ||
variant: { | ||
row: "flex-row gap-x-5", | ||
column: "flex-col gap-y-1", | ||
}, | ||
}, | ||
defaultVariants: { | ||
variant: "column", | ||
}, | ||
}) | ||
|
||
export const RadioGroup = forwardRef<HTMLFieldSetElement, RadioGroupProps<typeof radioGroupVariants>>(({ className, variant, name, defaultValue, children, ...props }, ref) => { | ||
// @ts-ignore | ||
const reference = useRef<HTMLFieldSetElement>(ref?.current) | ||
|
||
useEffect(() => { | ||
if (!reference.current) return | ||
const radioButtons = reference.current.querySelectorAll("input[type=radio]") | ||
radioButtons.forEach((radio) => { | ||
if (radio.getAttribute("value") === defaultValue) { | ||
radio.setAttribute("checked", "checked") | ||
} | ||
radio.setAttribute("name", name ?? "default-radio-group") | ||
}) | ||
}, []) | ||
|
||
return ( | ||
<fieldset className={merge(radioGroupVariants({ className, variant }))} defaultValue={defaultValue} ref={reference} {...props}> | ||
{children} | ||
</fieldset> | ||
) | ||
}) |
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,68 @@ | ||
import type { Meta, StoryObj } from "@storybook/react" | ||
import { RadioGroup } from "./index.js" | ||
import { Label } from "../../ui-label/src/index.js" | ||
import { Radio } from "../../ui-radio/src/index.js" | ||
|
||
const meta: Meta = { | ||
title: "ui-radio-group", | ||
tags: ["autodocs"], | ||
component: RadioGroup, | ||
parameters: { | ||
layout: "centered", | ||
}, | ||
} satisfies Meta<typeof RadioGroup> | ||
|
||
type Story = StoryObj<typeof meta> | ||
|
||
export const Column: Story = { | ||
render: () => { | ||
return ( | ||
<RadioGroup name="food"> | ||
<Label className="flex items-center gap-x-2"> | ||
<Radio value="pizza" name="food" /> | ||
Pizza | ||
</Label> | ||
<Label className="flex items-center gap-x-2"> | ||
<Radio value="hamburger" name="food" /> | ||
Hamburger | ||
</Label> | ||
</RadioGroup> | ||
) | ||
}, | ||
} | ||
|
||
export const Row: Story = { | ||
render: () => { | ||
return ( | ||
<RadioGroup variant="row" name="food"> | ||
<Label className="flex items-center gap-x-2"> | ||
<Radio value="pizza" name="food" /> | ||
Pizza | ||
</Label> | ||
<Label className="flex items-center gap-x-2"> | ||
<Radio value="hamburger" name="food" /> | ||
Hamburger | ||
</Label> | ||
</RadioGroup> | ||
) | ||
}, | ||
} | ||
|
||
export const DefaultChecked: Story = { | ||
render: () => { | ||
return ( | ||
<RadioGroup name="food" defaultValue="pizza"> | ||
<Label className="flex items-center gap-x-2"> | ||
<Radio value="pizza" name="food" /> | ||
Pizza | ||
</Label> | ||
<Label className="flex items-center gap-x-2"> | ||
<Radio value="hamburger" name="food" /> | ||
Hamburger | ||
</Label> | ||
</RadioGroup> | ||
) | ||
}, | ||
} | ||
|
||
export default meta |
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,9 @@ | ||
{ | ||
"extends": "@halvaradop/ui-core/tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"jsx": "react-jsx" | ||
}, | ||
"include": ["src"], | ||
"exclude": ["node_modules", "dist"] | ||
} |
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,4 @@ | ||
import { defineConfig } from "tsup" | ||
import { tsupConfig } from "@halvaradop/ui-core/tsup.config.base" | ||
|
||
export default defineConfig(tsupConfig) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.