From 8f60b7ce51496cb88f32d14460902d939e102f52 Mon Sep 17 00:00:00 2001 From: Jamie Lynch Date: Mon, 16 Aug 2021 14:33:47 +0100 Subject: [PATCH] Add API docs for `src-radio` (#902) * extend story stub with default args and arg types * remove api docs from readme Co-authored-by: Jamie Lynch * extract radio components into separate modules Co-authored-by: Jamie Lynch * refactor stories into component-specific stories files Co-authored-by: Jamie Lynch Co-authored-by: Simon Adcock Co-authored-by: Jamie Lynch --- src/@types/storybook-emotion-10-fixes.ts | 5 +- src/core/components/radio/README.md | 96 +-------- src/core/components/radio/Radio.stories.tsx | 124 ++++++++++++ src/core/components/radio/Radio.tsx | 132 ++++++++++++ .../components/radio/RadioGroup.stories.tsx | 156 +++++++++++++++ src/core/components/radio/RadioGroup.tsx | 106 ++++++++++ src/core/components/radio/index.tsx | 188 +----------------- .../components/radio/radio-group.stories.tsx | 15 -- src/core/components/radio/radio.stories.tsx | 11 - .../radio/stories/radio-group/controlled.tsx | 34 ---- .../radio/stories/radio-group/error.tsx | 54 ----- .../radio/stories/radio-group/horizontal.tsx | 14 -- .../stories/radio-group/legend-error.tsx | 53 ----- .../radio-group/legend-supporting-media.tsx | 51 ----- .../radio-group/legend-supporting-text.tsx | 51 ----- .../radio/stories/radio-group/legend.tsx | 73 ------- .../radio/stories/radio-group/vertical.tsx | 43 ---- .../stories/radio/supporting-text-only.tsx | 76 ------- .../radio/stories/radio/supporting-text.tsx | 79 -------- .../radio/stories/radio/ungrouped.tsx | 25 --- .../radio/stories/radio/unlabelled.tsx | 15 -- 21 files changed, 527 insertions(+), 874 deletions(-) create mode 100644 src/core/components/radio/Radio.stories.tsx create mode 100644 src/core/components/radio/Radio.tsx create mode 100644 src/core/components/radio/RadioGroup.stories.tsx create mode 100644 src/core/components/radio/RadioGroup.tsx delete mode 100644 src/core/components/radio/radio-group.stories.tsx delete mode 100644 src/core/components/radio/radio.stories.tsx delete mode 100644 src/core/components/radio/stories/radio-group/controlled.tsx delete mode 100644 src/core/components/radio/stories/radio-group/error.tsx delete mode 100644 src/core/components/radio/stories/radio-group/horizontal.tsx delete mode 100644 src/core/components/radio/stories/radio-group/legend-error.tsx delete mode 100644 src/core/components/radio/stories/radio-group/legend-supporting-media.tsx delete mode 100644 src/core/components/radio/stories/radio-group/legend-supporting-text.tsx delete mode 100644 src/core/components/radio/stories/radio-group/legend.tsx delete mode 100644 src/core/components/radio/stories/radio-group/vertical.tsx delete mode 100644 src/core/components/radio/stories/radio/supporting-text-only.tsx delete mode 100644 src/core/components/radio/stories/radio/supporting-text.tsx delete mode 100644 src/core/components/radio/stories/radio/ungrouped.tsx delete mode 100644 src/core/components/radio/stories/radio/unlabelled.tsx diff --git a/src/@types/storybook-emotion-10-fixes.ts b/src/@types/storybook-emotion-10-fixes.ts index d5e6e1f4f..a07e67e30 100644 --- a/src/@types/storybook-emotion-10-fixes.ts +++ b/src/@types/storybook-emotion-10-fixes.ts @@ -13,9 +13,10 @@ export type Parameters = { [key: string]: any; }; -export type Story = { - (arg0: any): JSX.Element; +export type Story = { + (arg0: Args & T): JSX.Element; args?: Args; parameters?: Parameters; + argTypes?: Args; storyName?: string; }; diff --git a/src/core/components/radio/README.md b/src/core/components/radio/README.md index cc3318524..f4b881e67 100644 --- a/src/core/components/radio/README.md +++ b/src/core/components/radio/README.md @@ -10,101 +10,13 @@ $ yarn add @guardian/src-radio ## Use -```js -import { RadioGroup, Radio } from '@guardian/src-radio'; +### API -const Form = () => { - const [selected, setSelected] = useState(null); +See [storybook](https://guardian.github.io/source/?path=/docs/source-src-radio-radio--demo) - return ( -
- - - , - -
- ); -}; -``` - -## `RadioGroup` Props - -### `name` - -**`string`** - -Gets passed as the name attribute for each radio button - -### `label` - -**`string`** - -Appears as a legend at the top of the radio group - -### `hideLabel` - -**`boolean`** _= "false"_ - -Visually hides the label. - -### `supporting` - -**`string | JSX.Element` ** - -Additional text or component that appears below the label - -### `orientation` - -**`"vertical" | "horizontal"`** _= "vertical"_ - -The direction in which radio buttons are stacked - -### `error` - -**`string`** - -If passed, error styling should applies to this radio group. The string appears as an inline error message. - -## `Radio` Props - -### `label` - -**`ReactNode`** - -Appears to the right of the radio button. If a visible label is undesirable (e.g. for layout reasons) use `aria-label` instead. - -If label is omitted, supporting text will not appear either. - -### `supporting` - -**`ReactNode`** - -Additional text or a component that appears below the label - -### `checked` - -**`boolean`** - -Whether radio button is checked. This is necessary when using the [controlled approach](https://reactjs.org/docs/forms.html#controlled-components) to form state management. +### How to use -**Note:** if you pass the `checked` prop, you **must** also pass an `onChange` handler, or the field will be rendered as read-only. +For context and visual guides relating to usage see the [Source Design System website](https://theguardian.design/2a1e5182b/p/2891dd-radio-button/b/46940d). ## Supported themes diff --git a/src/core/components/radio/Radio.stories.tsx b/src/core/components/radio/Radio.stories.tsx new file mode 100644 index 000000000..7af4f80a6 --- /dev/null +++ b/src/core/components/radio/Radio.stories.tsx @@ -0,0 +1,124 @@ +import React from 'react'; +import { Radio } from './Radio'; +import type { RadioProps } from './Radio'; +import { radioBrand } from './index'; +import { ThemeProvider } from '@emotion/react'; +import type { Story } from '../../../@types/storybook-emotion-10-fixes'; +import { asPlayground, asChromaticStory } from '../../../lib/story-intents'; + +// These types are the right types, but don't work with Storybook v6 which uses Emotion v10 +// import type { Args, Story } from '@storybook/react'; + +export default { + title: 'Source/src-radio/Radio', + component: Radio, + argTypes: { + label: { + control: { + type: 'text', + }, + }, + supporting: { + control: { + type: 'text', + }, + }, + cssOverrides: { + control: null, + }, + }, + args: { + label: 'Red', + value: 'red', + supporting: '', + checked: true, + }, +}; + +const Template: Story = (args: RadioProps) => ; + +// ***************************************************************************** + +export const Playground = Template.bind({}); +asPlayground(Playground); + +// ***************************************************************************** + +export const DefaultLightTheme = Template.bind({}); +asChromaticStory(DefaultLightTheme); + +// ***************************************************************************** + +export const DefaultBrandTheme = (args: RadioProps) => ( + +