Radio component with its stories

This commit is contained in:
“Salar 2022-08-05 14:06:21 +03:00
parent 2395b1e2d8
commit f008d80b60
2 changed files with 78 additions and 37 deletions

View File

@ -1,46 +1,41 @@
import classNames from "classnames";
import React from "react"; import React from "react";
/** import { ReactComponent as Checkmark } from "assets/svg/check.svg";
* [*]This is a Radio component.
* export type Props = {
* [*]when you click on the label the radio will be selected
*
* [*] when you call this component pass the following:
* [1]. label
* [2]. id
* [3]. name
*
* [*] A good example will be:
* <Radio label=" Will Smith" id="one" name="radio" />
*/
type RadioProps = {
/** /**
* the text that will be shown besie the radio * An Element next to the Radio
*/ */
label: string; children?: React.ReactNode;
/**
* please provide a unique id
*/
id: string;
/**
* The name of radio element
*/
name: string;
} & Omit<React.HTMLProps<HTMLInputElement>, "type">; } & Omit<React.HTMLProps<HTMLInputElement>, "type">;
const Radio = ({ label, id, name, ...props }: RadioProps) => { const Radio = ({ children, className, ...props }: Props) => {
return ( return (
<div> <label
<input className={classNames(
className="hover:bg-sky-700 focus:bg-sky-900 active:bg-sky-300 " " group inline-flex gap-x-4 text-base select-none",
type="Radio" className
name={name} )}
value={label} htmlFor={props.id}
id={id} >
{...props} <input className="hidden peer" type="radio" {...props} />
/> <div
<label htmlFor={id}>{label}</label> className=" w-6 h-6 rounded-full box-border p-0.5 border-2 border-gray-300
</div> peer-checked:border-blue-500
group-hover:border-blue-500
transition-all
after:content-[''] after:w-full after:h-full after:block after:bg-blue-500
after:rounded-full after:scale-0 after:peer-checked:scale-100 duration-200
peer-disabled:border-gray-300 peer-disabled:bg-gray-75/5
after:peer-disabled:bg-[#8C8C8C]
hover:border-blue-500"
></div>
{children}
</label>
); );
}; };
export default Radio; export default Radio;

View File

@ -0,0 +1,46 @@
import Radio from "../components/Radio";
import { Meta, Story, ComponentStory, ComponentMeta } from "@storybook/react";
import React, { useState } from "react";
import { ReactComponent as Checkmark } from "assets/svg/check.svg";
import { boolean } from "yup/lib/locale";
export default {
title: "Radio",
component: Radio,
// More on argTypes: https://storybook.js.org/docs/react/api/argtypes
argTypes: {
checked: {
type: "boolean",
},
children: {
type: "string",
defaultValue: "Use light theme",
},
className: {
type: "string",
defaultValue: "mt-4 ml-4",
},
disabled: {
type: "boolean",
defaultValue: "false",
},
},
} as ComponentMeta<typeof Radio>;
const Template: ComponentStory<typeof Radio> = (args) => <Radio {...args} />;
export const Checked = Template.bind({});
Checked.args = {
checked: true,
children: "This is a custom Radio",
};
export const Unchecked = Template.bind({});
Unchecked.args = {
checked: false,
};
export const Disabled = Template.bind({});
Disabled.args = {
disabled: true,
};