Skip to content

Commit

Permalink
BREAKING CHANGE: restructured and restyled radio box options componen…
Browse files Browse the repository at this point in the history
…ts to simplify its usage
  • Loading branch information
coderwelsch committed Jan 29, 2024
1 parent 3dc07eb commit eeef154
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 62 deletions.
75 changes: 35 additions & 40 deletions src/components/form-field/radio-box/radio-box-option.tsx
Original file line number Diff line number Diff line change
@@ -1,83 +1,78 @@
import { RadioGroup } from "@headlessui/react";
import React from "react";
import React, { Fragment } from "react";
import { classNames } from "../../../util/class-names";
import { RecommendationTag } from "../../recommendation-tag/recommendation-tag";

export interface RadioBoxOptionProps {
interface RadioBoxOptionProps {
children: React.ReactNode;
value: string;
disabled?: boolean;
recommendationText?: string;
className?: string;
}

export const radioBoxContainerStyles = {
base: "group relative flex items-center gap-3 rounded-lg bg-neutral-0 border p-4 border-neutral-300 hover:border-primary-500 hover:bg-primary-50",
checked: "border-primary-500 bg-primary-500 hover:bg-primary-500 hover:text-neutral-0",
const radioBoxContainerStyles = {
base: "group relative flex items-center gap-3 rounded-lg bg-neutral-0 border p-4 border-neutral-300 hover:border-primary-600 hover:bg-primary-50 cursor-pointer focus:outline-none",
checked: "border-primary-600 bg-primary-600 hover:bg-primary-600 hover:text-neutral-0",
disabled:
"border-neutral-400 bg-neutral-100 group-hover:border-neutral-400 group-hover:bg-neutral-100 hover:border-neutral-400 hover:bg-neutral-100",
"border-neutral-400 bg-neutral-100 group-hover:border-neutral-400 group-hover:bg-neutral-100 hover:border-neutral-400 hover:bg-neutral-100 cursor-not-allowed",
active: "ring-2 ring-primary-200",
};

export const radioBoxCircleStyles = {
const radioBoxCircleStyles = {
base: "relative inline-block h-4 w-4 shrink-0 rounded-full bg-neutral-0 border border-neutral-400",
unchecked: "group-hover:border-primary-500 group-hover:bg-neutral-0",
unchecked: "group-hover:border-primary-600 group-hover:bg-neutral-0",
checked: "border-transparent",
disabled: "bg-neutral-100 group-hover:border-neutral-400 group-hover:bg-neutral-100",
};

export const RadioBoxOption = ({
children,
value,
disabled,
recommendationText,
className,
}: RadioBoxOptionProps) => {
const Title = ({ children }: { children: React.ReactNode }) => (
<h2 className="headline-500 text-neutral-900 group-[.is-checked]:text-neutral-0 group-[.is-disabled]:text-neutral-500">
{children}
</h2>
);

const Description = ({ children }: { children: React.ReactNode }) => (
<p className="paragraph-200 text-neutral-800 group-[.is-checked]:text-primary-100 group-[.is-disabled]:text-neutral-500">
{children}
</p>
);

export const RadioBoxOption = ({ children, value, disabled, className }: RadioBoxOptionProps) => {
return (
<RadioGroup.Option
value={value}
className="w-full cursor-pointer focus:outline-none"
disabled={disabled}
>
{({ checked, disabled: optionDisabled }) => (
<RadioGroup.Option value={value} disabled={disabled} as={Fragment}>
{({ checked, disabled: optionDisabled, active }) => (
<div
className={classNames(
radioBoxContainerStyles.base,
checked && radioBoxContainerStyles.checked,
optionDisabled && radioBoxContainerStyles.disabled
checked && classNames("is-checked", radioBoxContainerStyles.checked),
optionDisabled &&
classNames("is-disabled", radioBoxContainerStyles.disabled),
active && radioBoxContainerStyles.active
)}
>
{recommendationText && (
<RecommendationTag>{recommendationText}</RecommendationTag>
)}

<div
className={classNames(
radioBoxCircleStyles.base,
!checked && radioBoxCircleStyles.unchecked,
checked && radioBoxCircleStyles.checked,
optionDisabled && radioBoxCircleStyles.disabled
)}
>
{checked && (
<div
className={classNames(
"absolute inset-0 m-auto block h-2 w-2 rounded-full bg-primary-500",
"absolute inset-0 m-auto block h-2 w-2 rounded-full bg-primary-600",
optionDisabled && "bg-neutral-500"
)}
/>
)}
</div>

<div
className={classNames(
"flex flex-col gap-2",
checked && "text-neutral-0",
optionDisabled && "text-neutral-600",
className
)}
>
{children}
</div>
<div className={classNames("flex flex-col gap-2", className)}>{children}</div>
</div>
)}
</RadioGroup.Option>
);
};

RadioBoxOption.Title = Title;
RadioBoxOption.Description = Description;
44 changes: 22 additions & 22 deletions src/components/form-field/radio-box/radio-box.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Meta, StoryObj } from "@storybook/react";
import React, { useState } from "react";
import { FormField } from "../form-field";
import { RadioBox } from "./radio-box";
import { FeaturedTag } from "../../featured-tag/featured-tag";

const meta: Meta<typeof RadioBox> = {
title: "Input/RadioBox",
Expand All @@ -18,34 +19,33 @@ const RadioBoxWithHooks = () => {
return (
<FormField>
<FormField.RadioBox id="value" value={value} onChange={setValue}>
<FormField.RadioBox.Option value="value_1" recommendationText="Recommended">
<div className="flex flex-col gap-2">
<h2 className="headline-500">Option 1</h2>
<p className="paragraph-200">
To be, or not to be, that is the question: Whether ’tis nobler in the
mind to suffer The slings and arrows of outrageous fortune, …
</p>
</div>
<FormField.RadioBox.Option value="value_1">
<FeaturedTag>Recommended</FeaturedTag>

<FormField.RadioBox.Option.Title>Option 1</FormField.RadioBox.Option.Title>

<FormField.RadioBox.Option.Description>
To be, or not to be, that is the question: Whether ’tis nobler in the mind
to suffer The slings and arrows of outrageous fortune, …
</FormField.RadioBox.Option.Description>
</FormField.RadioBox.Option>

<FormField.RadioBox.Option value="value_2">
<div className="flex flex-col gap-2">
<h2 className="headline-500">Option 2</h2>
<p className="paragraph-200">
To be, or not to be, that is the question: Whether ’tis nobler in the
mind to suffer The slings and arrows of outrageous fortune, …
</p>
</div>
<FormField.RadioBox.Option.Title>Option 2</FormField.RadioBox.Option.Title>

<FormField.RadioBox.Option.Description>
To be, or not to be, that is the question: Whether ’tis nobler in the mind
to suffer The slings and arrows of outrageous fortune, …
</FormField.RadioBox.Option.Description>
</FormField.RadioBox.Option>

<FormField.RadioBox.Option value="value_3" disabled>
<div className="flex flex-col gap-2">
<h2 className="headline-500">Option 3</h2>
<p className="paragraph-200">
To be, or not to be, that is the question: Whether ’tis nobler in the
mind to suffer The slings and arrows of outrageous fortune, …
</p>
</div>
<FormField.RadioBox.Option.Title>Option 3</FormField.RadioBox.Option.Title>

<FormField.RadioBox.Option.Description>
To be, or not to be, that is the question: Whether ’tis nobler in the mind
to suffer The slings and arrows of outrageous fortune, …
</FormField.RadioBox.Option.Description>
</FormField.RadioBox.Option>
</FormField.RadioBox>
</FormField>
Expand Down

0 comments on commit eeef154

Please sign in to comment.