From 6c2af9a034672ed152c3bdb40375a493e6762681 Mon Sep 17 00:00:00 2001 From: AssisrMatheus Date: Tue, 11 Apr 2023 15:22:43 -0400 Subject: [PATCH] feat: adds toggle input --- CHANGELOG.md | 1 + .../ToggleInput/ToggleInput.stories.tsx | 82 +++++++++++++++++++ src/components/ToggleInput/index.tsx | 64 +++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 src/components/ToggleInput/ToggleInput.stories.tsx create mode 100644 src/components/ToggleInput/index.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index d01443a..a772066 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added examples for time picker on `DatePickerInput` +- Added `ToggleInput` component ### Fixed diff --git a/src/components/ToggleInput/ToggleInput.stories.tsx b/src/components/ToggleInput/ToggleInput.stories.tsx new file mode 100644 index 0000000..a12279b --- /dev/null +++ b/src/components/ToggleInput/ToggleInput.stories.tsx @@ -0,0 +1,82 @@ +// also exported from '@storybook/react' if you can deal with breaking changes in 6.1 +import { Meta, Story } from '@storybook/react/types-6-0'; +import React, { useState } from 'react'; +import { ToggleInput, ToggleInputProps } from '.'; +import { colorOptions } from '../../prebuiltTailwindTheme'; + +export default { + title: 'Components/Inputs/Toggle', + component: ToggleInput, + argTypes: { + label: { defaultValue: 'Input' }, + color: { + defaultValue: 'pui-primary', + control: { + type: 'select', + options: colorOptions + } + }, + defaultChecked: { + control: { + type: 'boolean' + } + }, + placeholder: { + defaultValue: 'Type here...', + control: { + type: 'text' + } + }, + defaultValue: { + control: { + type: 'boolean' + } + }, + disabled: { + control: { + type: 'boolean' + } + }, + readOnly: { + control: { + type: 'boolean' + } + }, + className: { + control: { + type: 'text' + } + }, + onChange: { action: 'onChange' }, + onBlur: { action: 'onBlur' }, + onFocus: { action: 'onFocus' } + } +} as Meta; + +/** + * A story that displays a ToggleInput example + * + * @param props the story props + */ +const Template: Story = (props) => { + const [checked, setChecked] = useState(false); + + return setChecked(e.target.checked)} />; +}; + +export const WithLabel = Template.bind({}); + +export const NoLabel = Template.bind({}); +NoLabel.args = { + label: undefined +}; + +export const WithHelp = Template.bind({}); +WithHelp.args = { + help: 'You can also have a help text' +}; + +export const WithError = Template.bind({}); +WithError.args = { + error: 'Input is required' +}; diff --git a/src/components/ToggleInput/index.tsx b/src/components/ToggleInput/index.tsx new file mode 100644 index 0000000..261f9b0 --- /dev/null +++ b/src/components/ToggleInput/index.tsx @@ -0,0 +1,64 @@ +import classnames from 'classnames'; +import { motion } from 'framer-motion'; +import React from 'react'; +import { HTMLParsedContent } from '../HTMLParsedContent'; + +const spring = { + type: 'spring', + stiffness: 700, + damping: 30 +}; + +export type ToggleInputProps = Omit< + React.DetailedHTMLProps, HTMLInputElement>, + 'children' | 'type' +> & { + label?: string | React.ReactNode; + error?: string; + help?: string; +}; + +/** + * A toggle input component + * + * @param props the component props + * @param props.label the label of the input + * @param props.error the error message + * @param props.help help message if any + */ +export const ToggleInput: React.FC = ({ label, error, help, ...props }) => ( +
+ +
+ {help && !error &&

{help}

} + {error &&

{error}

} +
+
+);