diff --git a/examples/official-storybook/stories/core/args.stories.js b/examples/official-storybook/stories/core/args.stories.js index bdaafecb350d..1a1bb9ee72a1 100644 --- a/examples/official-storybook/stories/core/args.stories.js +++ b/examples/official-storybook/stories/core/args.stories.js @@ -1,6 +1,4 @@ import React, { useState } from 'react'; -import PropTypes from 'prop-types'; - import { useArgs } from '@storybook/client-api'; // eslint-disable-next-line react/prop-types @@ -44,17 +42,25 @@ export default { ], }; -export const PassedToStory = (inputArgs) => { +const Template = (args) => { return (

Input args:

-
{JSON.stringify(inputArgs)}
+
{JSON.stringify(args)}
); }; +export const PassedToStory = Template.bind({}); + PassedToStory.argTypes = { name: { defaultValue: 'initial', control: 'text' } }; -PassedToStory.propTypes = { - args: PropTypes.shape({}).isRequired, +export const OtherValues = Template.bind({}); + +OtherValues.argTypes = { name: { control: 'text' } }; + +export const DifferentSet = Template.bind({}); +DifferentSet.args = { + foo: 'bar', + bar: 2, }; diff --git a/lib/components/src/controls/Array.stories.tsx b/lib/components/src/controls/Array.stories.tsx index ff83cb0679bb..9feffc7da951 100644 --- a/lib/components/src/controls/Array.stories.tsx +++ b/lib/components/src/controls/Array.stories.tsx @@ -6,22 +6,21 @@ export default { component: ArrayControl, }; -export const Basic = () => { - const [value, setValue] = useState(['Bat', 'Cat', 'Rat']); +const Template = (initialValue: any) => { + const [value, setValue] = useState(initialValue); return ( <> - setValue(newVal)} /> + setValue(newVal)} + separator="," + /> ); }; -export const Null = () => { - const [value, setValue] = useState(null); - return ( - <> - setValue(newVal)} /> -
    {value && value.map((item) =>
  • {item}
  • )}
- - ); -}; +export const Basic = () => Template(['Bat', 'Cat', 'Rat']); + +export const Undefined = () => Template(undefined); diff --git a/lib/components/src/controls/Array.tsx b/lib/components/src/controls/Array.tsx index 6cdc6648d8ea..b43c9109aca5 100644 --- a/lib/components/src/controls/Array.tsx +++ b/lib/components/src/controls/Array.tsx @@ -7,7 +7,7 @@ import { ControlProps, ArrayValue, ArrayConfig } from './types'; const parse = (value: string, separator: string): ArrayValue => !value || value.trim() === '' ? [] : value.split(separator); -const format = (value: ArrayValue, separator: string) => { +const format = (value: ArrayValue | undefined, separator: string) => { return value && Array.isArray(value) ? value.join(separator) : ''; }; @@ -20,7 +20,6 @@ export const ArrayControl: FC = ({ name, value, onChange, - argType, separator = ',', onBlur, onFocus, diff --git a/lib/components/src/controls/Boolean.stories.tsx b/lib/components/src/controls/Boolean.stories.tsx index c0bd2bc53c2d..0d9050aac9e2 100644 --- a/lib/components/src/controls/Boolean.stories.tsx +++ b/lib/components/src/controls/Boolean.stories.tsx @@ -6,12 +6,16 @@ export default { component: BooleanControl, }; -export const Basic = () => { - const [value, setValue] = useState(false); +const Template = (initialValue?: boolean) => { + const [value, setValue] = useState(initialValue); return ( <> setValue(newVal)} /> -

value: {value.toString()}

+

value: {typeof value === 'boolean' ? value.toString() : value}

); }; + +export const Basic = () => Template(false); + +export const Undefined = () => Template(undefined); diff --git a/lib/components/src/controls/Color.stories.tsx b/lib/components/src/controls/Color.stories.tsx index 85b2ba9f3420..59bda246bd90 100644 --- a/lib/components/src/controls/Color.stories.tsx +++ b/lib/components/src/controls/Color.stories.tsx @@ -1,20 +1,13 @@ import React, { useState } from 'react'; import { ColorControl } from './Color'; -const presetColors = ['#FE4A49', '#FED766', '#009FB7', '#E6E6EA', '#F4F4F8']; - export default { title: 'Controls/Color', component: ColorControl, }; -export const Basic = () => { - const [value, setValue] = useState('#ff0'); - return setValue(newVal)} />; -}; - -export const WithPresetColors = () => { - const [value, setValue] = useState('#ff0'); +const Template = (initialValue?: string, presetColors?: string[]) => { + const [value, setValue] = useState(initialValue); return ( { /> ); }; + +export const Basic = () => Template('#ff0'); + +export const Undefined = () => Template(undefined); + +export const WithPresetColors = () => + Template('#ff0', ['#FE4A49', '#FED766', '#009FB7', '#E6E6EA', '#F4F4F8']); diff --git a/lib/components/src/controls/Date.stories.tsx b/lib/components/src/controls/Date.stories.tsx index 50fd587e374e..165464d02696 100644 --- a/lib/components/src/controls/Date.stories.tsx +++ b/lib/components/src/controls/Date.stories.tsx @@ -15,3 +15,13 @@ export const Basic = () => { ); }; + +export const Undefined = () => { + const [value, setValue] = useState(undefined); + return ( + <> + setValue(newVal)} /> +

{value && new Date(value).toISOString()}

+ + ); +}; diff --git a/lib/components/src/controls/Number.stories.tsx b/lib/components/src/controls/Number.stories.tsx index b15b7a1b4849..b58f47868ccc 100644 --- a/lib/components/src/controls/Number.stories.tsx +++ b/lib/components/src/controls/Number.stories.tsx @@ -15,3 +15,13 @@ export const Basic = () => { ); }; + +export const Undefined = () => { + const [value, setValue] = useState(undefined); + return ( + <> + setValue(newVal)} /> +

{value}

+ + ); +}; diff --git a/lib/components/src/controls/Object.stories.tsx b/lib/components/src/controls/Object.stories.tsx index eae894ad71f8..3b43e8528f6e 100644 --- a/lib/components/src/controls/Object.stories.tsx +++ b/lib/components/src/controls/Object.stories.tsx @@ -6,8 +6,8 @@ export default { component: ObjectControl, }; -export const Basic = () => { - const [value, setValue] = useState({ name: 'Michael', nested: { something: true } }); +const Template = (initialValue: any) => { + const [value, setValue] = useState(initialValue); return ( <> setValue(newVal)} /> @@ -16,15 +16,11 @@ export const Basic = () => { ); }; -export const Null = () => { - const [value, setValue] = useState(null); - return ( - <> - setValue(newVal)} /> -

{value && JSON.stringify(value)}

- - ); -}; +export const Basic = () => Template({ name: 'Michael', nested: { something: true } }); + +export const Null = () => Template(null); + +export const Undefined = () => Template(undefined); export const ValidatedAsArray = () => { const [value, setValue] = useState([]); diff --git a/lib/components/src/controls/Range.stories.tsx b/lib/components/src/controls/Range.stories.tsx index 4d8c36f554c7..a2214ef7b2ed 100644 --- a/lib/components/src/controls/Range.stories.tsx +++ b/lib/components/src/controls/Range.stories.tsx @@ -6,8 +6,8 @@ export default { component: RangeControl, }; -export const Basic = () => { - const [value, setValue] = useState(10); +const Template = (initialValue?: number) => { + const [value, setValue] = useState(initialValue); return ( <> { ); }; + +export const Basic = () => Template(10); + +export const Undefined = () => Template(undefined); diff --git a/lib/components/src/controls/Range.tsx b/lib/components/src/controls/Range.tsx index 16d0b298dcd5..2abee0d063b9 100644 --- a/lib/components/src/controls/Range.tsx +++ b/lib/components/src/controls/Range.tsx @@ -153,7 +153,7 @@ const RangeWrapper = styled.div({ export const RangeControl: FC = ({ name, - value = 50, + value, onChange, min = 0, max = 100, diff --git a/lib/components/src/controls/Text.stories.tsx b/lib/components/src/controls/Text.stories.tsx index 02d7bb90b1fc..2f8007ec5c41 100644 --- a/lib/components/src/controls/Text.stories.tsx +++ b/lib/components/src/controls/Text.stories.tsx @@ -6,8 +6,8 @@ export default { component: TextControl, }; -export const Basic = () => { - const [value, setValue] = useState('Hello text'); +const Template = (initialValue?: string) => { + const [value, setValue] = useState(initialValue); return ( <> setValue(newVal)} /> @@ -15,3 +15,7 @@ export const Basic = () => { ); }; + +export const Basic = () => Template('Hello text'); + +export const Undefined = () => Template(undefined); diff --git a/lib/components/src/controls/Text.tsx b/lib/components/src/controls/Text.tsx index 2444867aa507..5dd0fd804a44 100644 --- a/lib/components/src/controls/Text.tsx +++ b/lib/components/src/controls/Text.tsx @@ -4,12 +4,14 @@ import { styled } from '@storybook/theming'; import { Form } from '../form'; import { ControlProps, TextValue, TextConfig } from './types'; -export type TextProps = ControlProps & TextConfig; +export type TextProps = ControlProps & TextConfig; const Wrapper = styled.label({ display: 'flex', }); +const format = (value?: TextValue) => value || ''; + export const TextControl: FC = ({ name, value, onChange, onFocus, onBlur }) => { const handleChange = (event: ChangeEvent) => { onChange(name, event.target.value); @@ -21,7 +23,7 @@ export const TextControl: FC = ({ name, value, onChange, onFocus, onB onChange={handleChange} size="flex" placeholder="Adjust string dynamically" - {...{ name, value, onFocus, onBlur }} + {...{ name, value: format(value), onFocus, onBlur }} /> ); diff --git a/lib/components/src/controls/types.ts b/lib/components/src/controls/types.ts index 34afbe0d68d8..b8c737823d37 100644 --- a/lib/components/src/controls/types.ts +++ b/lib/components/src/controls/types.ts @@ -3,7 +3,7 @@ import { ArgType } from '../blocks'; /* eslint-disable @typescript-eslint/no-empty-interface */ export interface ControlProps { name: string; - value: T; + value?: T; defaultValue?: T; argType?: ArgType; onChange: (name: string, value: T) => T | void;