diff --git a/packages/ui-radio-group/package.json b/packages/ui-radio-group/package.json new file mode 100644 index 0000000..ad73974 --- /dev/null +++ b/packages/ui-radio-group/package.json @@ -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 ", + "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" + } +} diff --git a/packages/ui-radio-group/src/index.tsx b/packages/ui-radio-group/src/index.tsx new file mode 100644 index 0000000..dd2445b --- /dev/null +++ b/packages/ui-radio-group/src/index.tsx @@ -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 = VariantProps & + Omit, "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>(({ className, variant, name, defaultValue, children, ...props }, ref) => { + // @ts-ignore + const reference = useRef(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 ( +
+ {children} +
+ ) +}) diff --git a/packages/ui-radio-group/src/radio-group.stories.tsx b/packages/ui-radio-group/src/radio-group.stories.tsx new file mode 100644 index 0000000..3569b58 --- /dev/null +++ b/packages/ui-radio-group/src/radio-group.stories.tsx @@ -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 + +type Story = StoryObj + +export const Column: Story = { + render: () => { + return ( + + + + + ) + }, +} + +export const Row: Story = { + render: () => { + return ( + + + + + ) + }, +} + +export const DefaultChecked: Story = { + render: () => { + return ( + + + + + ) + }, +} + +export default meta diff --git a/packages/ui-radio-group/tsconfig.json b/packages/ui-radio-group/tsconfig.json new file mode 100644 index 0000000..4738e10 --- /dev/null +++ b/packages/ui-radio-group/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "@halvaradop/ui-core/tsconfig.base.json", + "compilerOptions": { + "outDir": "dist", + "jsx": "react-jsx" + }, + "include": ["src"], + "exclude": ["node_modules", "dist"] +} diff --git a/packages/ui-radio-group/tsup.config.ts b/packages/ui-radio-group/tsup.config.ts new file mode 100644 index 0000000..75ee2d2 --- /dev/null +++ b/packages/ui-radio-group/tsup.config.ts @@ -0,0 +1,4 @@ +import { defineConfig } from "tsup" +import { tsupConfig } from "@halvaradop/ui-core/tsup.config.base" + +export default defineConfig(tsupConfig) diff --git a/packages/ui-radio/src/index.tsx b/packages/ui-radio/src/index.tsx index 461f5e4..b10e9d0 100644 --- a/packages/ui-radio/src/index.tsx +++ b/packages/ui-radio/src/index.tsx @@ -50,9 +50,11 @@ const internalVariants = cva("block absolute rounded-full", { export const Radio = ({ className, size, color, name, ...props }: RadioProps) => { return ( -