diff --git a/src/components/Checkbox.stories.tsx b/src/components/Checkbox.stories.tsx new file mode 100644 index 0000000..0b90e2c --- /dev/null +++ b/src/components/Checkbox.stories.tsx @@ -0,0 +1,58 @@ +import React from 'react'; + +import { ComponentMeta, ComponentStory } from '@storybook/react'; + +import Checkbox from './Checkbox'; + +export default { + // Title inside navigation bar + title: 'Checkbox', + // Component to test + component: Checkbox, + // Clarifying the way how to process specific + // properties of your component and which values + // it can accept. + argTypes: { + checked: { + options: [true, false], + control: { type: 'radio' }, + }, + }, +} as ComponentMeta; + +/** + * This is a way to define a tempalte for your component. + * + * This template should cover all the states. + * + * In most cases you should just distruct args attribute + * on a returning component. + */ +const Template: ComponentStory = (args) => ( + +); + +/* -------------------------------------------------------------------------- */ +/* States of your component */ +/* -------------------------------------------------------------------------- */ + +export const Unchecked = Template.bind({}); +Unchecked.args = { + label: "On/off lights", + checked: false, + children: undefined, +} +export const Checked = Template.bind({}); +Checked.args = { + children: "On/off lights", + checked: true, + label: undefined, +} + +export const EitherLabelChildren = Template.bind({}); +EitherLabelChildren.args = { + children: "On/off lights", + checked: true, + label: "Label!", +} +// GO Further \ No newline at end of file diff --git a/src/components/Checkbox.tsx b/src/components/Checkbox.tsx new file mode 100644 index 0000000..ae987d3 --- /dev/null +++ b/src/components/Checkbox.tsx @@ -0,0 +1,39 @@ +import classNames from "classnames"; +import React from "react"; + +type Props = { + /** + * The way to provide `children` inside a component + * via attribute instead of enclosed tag + */ + label?: React.ReactNode; + /** + * When you will have both `children` and `label` + * defined on a component it will choose `children` + * to display + */ + children?: React.ReactNode; +} & Omit, "type">; + +/** + * Customized `input[type="checkbox"]` component with label + * + * All common input properties are inherited. + * + * To define a label content either provide `label` attribute + * or use common `children` property + */ +const Checkbox = ({label, children, className, ...props}: Props) => { + + if(label && !children) { + children = label; + } + + return ( + + ); +}; +export default Checkbox;