From b4d79e489d706cb598c62ded91c2b9c6027b9c71 Mon Sep 17 00:00:00 2001 From: Igor Brasileiro Date: Wed, 14 Jul 2021 16:44:02 -0300 Subject: [PATCH] chore(store-ui): Improve atoms pattern consistency (#819) * Create type to avoid wrong props inside StoryBook argtypes * Change TextArea state prop to variant * Change Input state prop to variant * Improve atoms argtypes type * Improve molecules argtypes * Fix Checkbox export * Fix request changes --- .../atoms/Button/stories/Button.stories.tsx | 23 ++++++------ packages/store-ui/src/atoms/Checkbox/index.ts | 2 +- .../Checkbox/stories/Checkbox.stories.tsx | 7 ++-- .../src/atoms/Icon/stories/Icon.stories.tsx | 33 +++++++++-------- .../store-ui/src/atoms/Input/Input.test.tsx | 4 +-- packages/store-ui/src/atoms/Input/Input.tsx | 14 ++++---- .../src/atoms/Input/stories/Input.stories.tsx | 33 +++++++++-------- .../atoms/Popover/stories/Popover.stories.tsx | 9 +++-- .../src/atoms/Price/stories/Price.stories.tsx | 21 ++++++----- .../src/atoms/TextArea/TextArea.test.tsx | 6 ++-- .../store-ui/src/atoms/TextArea/TextArea.tsx | 14 ++++---- .../TextArea/stories/TextArea.stories.tsx | 15 ++++---- packages/store-ui/src/deprecated/index.ts | 8 ++--- packages/store-ui/src/index.ts | 3 ++ .../Bullets/stories/Bullets.stories.tsx | 35 ++++++++++--------- .../stories/SearchInput.stories.tsx | 23 ++++++------ packages/store-ui/src/typings/utils.d.ts | 5 +++ 17 files changed, 144 insertions(+), 111 deletions(-) create mode 100644 packages/store-ui/src/typings/utils.d.ts diff --git a/packages/store-ui/src/atoms/Button/stories/Button.stories.tsx b/packages/store-ui/src/atoms/Button/stories/Button.stories.tsx index 4ac3a8a817..7f4bf8d951 100644 --- a/packages/store-ui/src/atoms/Button/stories/Button.stories.tsx +++ b/packages/store-ui/src/atoms/Button/stories/Button.stories.tsx @@ -1,6 +1,7 @@ import type { Story } from '@storybook/react' import React from 'react' +import type { ComponentArgTypes } from '../../../typings/utils' import type { ButtonProps } from '../Button' import Component from '../Button' import mdx from './Button.mdx' @@ -13,19 +14,21 @@ const ButtonTemplate: Story = ({ children, onClick, testId }) => ( export const Button = ButtonTemplate.bind({}) +const argTypes: ComponentArgTypes = { + children: { + control: { type: 'text' }, + defaultValue: 'Button', + }, + onClick: { + action: 'Button clicked', + table: { disable: true }, + }, +} + export default { title: 'Atoms/Button', component: Button, - argTypes: { - children: { - control: { type: 'text' }, - defaultValue: 'Button', - }, - onClick: { - action: 'Button clicked', - table: { disable: true }, - }, - }, + argTypes, parameters: { docs: { page: mdx, diff --git a/packages/store-ui/src/atoms/Checkbox/index.ts b/packages/store-ui/src/atoms/Checkbox/index.ts index a671f3183b..c44fd9c41a 100644 --- a/packages/store-ui/src/atoms/Checkbox/index.ts +++ b/packages/store-ui/src/atoms/Checkbox/index.ts @@ -1,2 +1,2 @@ export { default } from './Checkbox' -export * from './Checkbox' +export { Props as CheckboxProps } from './Checkbox' diff --git a/packages/store-ui/src/atoms/Checkbox/stories/Checkbox.stories.tsx b/packages/store-ui/src/atoms/Checkbox/stories/Checkbox.stories.tsx index 7b542f8eee..5f576fd3f5 100644 --- a/packages/store-ui/src/atoms/Checkbox/stories/Checkbox.stories.tsx +++ b/packages/store-ui/src/atoms/Checkbox/stories/Checkbox.stories.tsx @@ -1,6 +1,7 @@ -import type { Story, Meta, ArgTypes } from '@storybook/react' +import type { Story, Meta } from '@storybook/react' import React from 'react' +import type { ComponentArgTypes } from '../../../typings/utils' import type { Props as CheckboxProps } from '../Checkbox' import Component from '../Checkbox' import mdx from './Checkbox.mdx' @@ -11,7 +12,7 @@ const CheckboxTemplate: Story = (props) => ( export const Checkbox = CheckboxTemplate.bind({}) -const controls: ArgTypes = { +const controls: ComponentArgTypes = { checked: { control: { type: 'boolean', @@ -19,7 +20,7 @@ const controls: ArgTypes = { }, } -const actions: ArgTypes = { +const actions: ComponentArgTypes = { onClick: { action: 'clicked', table: { disable: true } }, } diff --git a/packages/store-ui/src/atoms/Icon/stories/Icon.stories.tsx b/packages/store-ui/src/atoms/Icon/stories/Icon.stories.tsx index f8c5be91eb..6f496ffea8 100644 --- a/packages/store-ui/src/atoms/Icon/stories/Icon.stories.tsx +++ b/packages/store-ui/src/atoms/Icon/stories/Icon.stories.tsx @@ -5,6 +5,7 @@ import Component from '../Icon' import type { IconProps } from '../Icon' import mdx from './Icon.mdx' import ShoppingCart from './assets/ShoppingCart' +import type { ComponentArgTypes } from '../../../typings/utils' const IconTemplate: Story = ({ style }) => ( } /> @@ -12,24 +13,26 @@ const IconTemplate: Story = ({ style }) => ( export const Icon = IconTemplate.bind({}) +const argTypes: ComponentArgTypes> = { + style: { + control: { type: 'object' }, + defaultValue: { + fontSize: '32px', + color: 'red', + display: 'inline-block', + lineHeight: 0, + }, + }, + onClick: { + action: 'Icon clicked', + table: { disable: true }, + }, +} + export default { title: 'Atoms/Icon', component: Icon, - argTypes: { - style: { - control: { type: 'object' }, - defaultValue: { - fontSize: '32px', - color: 'red', - display: 'inline-block', - lineHeight: 0, - }, - }, - onClick: { - action: 'Icon clicked', - table: { disable: true }, - }, - }, + argTypes, parameters: { docs: { page: mdx, diff --git a/packages/store-ui/src/atoms/Input/Input.test.tsx b/packages/store-ui/src/atoms/Input/Input.test.tsx index ebda256450..6e051ee27e 100644 --- a/packages/store-ui/src/atoms/Input/Input.test.tsx +++ b/packages/store-ui/src/atoms/Input/Input.test.tsx @@ -15,14 +15,14 @@ describe('Input', () => { }) it('`data-error` is present', () => { const { getByPlaceholderText } = render( - + ) expect(getByPlaceholderText('Hello World!')).toHaveAttribute('data-error') }) it('`data-success` is present', () => { const { getByPlaceholderText } = render( - + ) expect(getByPlaceholderText('Hello World!')).toHaveAttribute('data-success') diff --git a/packages/store-ui/src/atoms/Input/Input.tsx b/packages/store-ui/src/atoms/Input/Input.tsx index 0d53863181..ed9f9d4d52 100644 --- a/packages/store-ui/src/atoms/Input/Input.tsx +++ b/packages/store-ui/src/atoms/Input/Input.tsx @@ -4,9 +4,9 @@ import React, { forwardRef } from 'react' export interface InputProps extends InputHTMLAttributes { /** - * Current state of the input. + * Current variant of the input. */ - state?: 'success' | 'error' + variant?: 'success' | 'error' /** * ID to find this component in testing tools (e.g.: cypress, testing library, and jest). */ @@ -14,12 +14,12 @@ export interface InputProps extends InputHTMLAttributes { } const Input = forwardRef(function Input( - { state, testId = 'store-input', ...props }, + { variant, testId = 'store-input', ...props }, ref ) { - const states = { - 'data-error': state === 'error' || undefined, - 'data-success': state === 'success' || undefined, + const variants = { + 'data-error': variant === 'error' || undefined, + 'data-success': variant === 'success' || undefined, } return ( @@ -27,7 +27,7 @@ const Input = forwardRef(function Input( ref={ref} data-store-input data-testid={testId} - {...states} + {...variants} {...props} /> ) diff --git a/packages/store-ui/src/atoms/Input/stories/Input.stories.tsx b/packages/store-ui/src/atoms/Input/stories/Input.stories.tsx index 769e86a70c..bc1a34bd61 100644 --- a/packages/store-ui/src/atoms/Input/stories/Input.stories.tsx +++ b/packages/store-ui/src/atoms/Input/stories/Input.stories.tsx @@ -1,12 +1,13 @@ import type { Story } from '@storybook/react' import React from 'react' +import type { ComponentArgTypes } from '../../../typings/utils' import type { InputProps } from '../Input' import Component from '../Input' import mdx from './Input.mdx' -const InputTemplate: Story = ({ placeholder, state }) => { - const colorByState = { +const InputTemplate: Story = ({ placeholder, variant }) => { + const colorByVariant = { default: 'black', success: 'green', error: 'red', @@ -15,31 +16,33 @@ const InputTemplate: Story = ({ placeholder, state }) => { return ( ) } export const Input = InputTemplate.bind({}) +const argTypes: ComponentArgTypes = { + variant: { + options: ['default', 'success', 'error'], + defaultValue: 'default', + control: { type: 'select' }, + }, + placeholder: { + control: { type: 'text' }, + defaultValue: 'Input', + }, +} + export default { title: 'Atoms/Input', component: Input, - argTypes: { - state: { - options: ['default', 'success', 'error'], - defaultValue: 'default', - control: { type: 'select' }, - }, - placeholder: { - control: { type: 'text' }, - defaultValue: 'Input', - }, - }, + argTypes, parameters: { docs: { page: mdx, diff --git a/packages/store-ui/src/atoms/Popover/stories/Popover.stories.tsx b/packages/store-ui/src/atoms/Popover/stories/Popover.stories.tsx index 30359ef16b..d1a71b89b2 100644 --- a/packages/store-ui/src/atoms/Popover/stories/Popover.stories.tsx +++ b/packages/store-ui/src/atoms/Popover/stories/Popover.stories.tsx @@ -5,6 +5,7 @@ import Component from '../Popover' import Input from '../../Input' import type { PopoverProps } from '../Popover' import mdx from './Popover.mdx' +import type { ComponentArgTypes } from '../../../typings/utils' const PopoverTemplate: Story = ({ children }) => { const ref = useRef(null) @@ -27,11 +28,13 @@ const PopoverTemplate: Story = ({ children }) => { export const Popover = PopoverTemplate.bind({}) +const argTypes: ComponentArgTypes> = { + children: 'Whoa! Look at me!', +} + export default { title: 'Atoms/Popover', - argTypes: { - children: 'Whoa! Look at me!', - }, + argTypes, parameters: { docs: { page: mdx, diff --git a/packages/store-ui/src/atoms/Price/stories/Price.stories.tsx b/packages/store-ui/src/atoms/Price/stories/Price.stories.tsx index 3e67fb629e..bdb7d1dbc6 100644 --- a/packages/store-ui/src/atoms/Price/stories/Price.stories.tsx +++ b/packages/store-ui/src/atoms/Price/stories/Price.stories.tsx @@ -4,6 +4,7 @@ import type { Story } from '@storybook/react' import Component from '../Price' import type { PriceProps } from '../Price' import mdx from './Price.mdx' +import type { ComponentArgTypes } from '../../../typings/utils' const priceValue = 62.5 @@ -108,17 +109,19 @@ Custom.args = { value: priceValue, } +const argTypes: ComponentArgTypes> = { + variant: { + options: ['selling', 'listing', 'spot', 'savings', 'installment'], + control: { type: 'select' }, + }, + formatter: { + table: { disable: true }, + }, +} + export default { title: 'Atoms/Price', - argTypes: { - variant: { - options: ['selling', 'listing', 'spot', 'savings', 'installment'], - control: { type: 'select' }, - }, - formatter: { - table: { disable: true }, - }, - }, + argTypes, parameters: { docs: { page: mdx, diff --git a/packages/store-ui/src/atoms/TextArea/TextArea.test.tsx b/packages/store-ui/src/atoms/TextArea/TextArea.test.tsx index 37818ba3cd..172cb77f0e 100644 --- a/packages/store-ui/src/atoms/TextArea/TextArea.test.tsx +++ b/packages/store-ui/src/atoms/TextArea/TextArea.test.tsx @@ -13,14 +13,16 @@ describe('TextArea', () => { }) it('state is error', () => { - const { getByTestId } = render(