This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Add API docs for src-radio
#902
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) => <Radio {...args} />; | ||
|
||
// ***************************************************************************** | ||
|
||
export const Playground = Template.bind({}); | ||
asPlayground(Playground); | ||
|
||
// ***************************************************************************** | ||
|
||
export const DefaultLightTheme = Template.bind({}); | ||
asChromaticStory(DefaultLightTheme); | ||
|
||
// ***************************************************************************** | ||
|
||
export const DefaultBrandTheme = (args: RadioProps) => ( | ||
<ThemeProvider theme={radioBrand}> | ||
<Template {...args} /> | ||
</ThemeProvider> | ||
); | ||
DefaultBrandTheme.parameters = { | ||
backgrounds: { | ||
default: 'brandBackground.primary', | ||
}, | ||
}; | ||
asChromaticStory(DefaultBrandTheme); | ||
|
||
// ***************************************************************************** | ||
|
||
export const SupportingTextLightTheme = Template.bind({}); | ||
SupportingTextLightTheme.args = { | ||
supporting: '#ff0000', | ||
}; | ||
asChromaticStory(SupportingTextLightTheme); | ||
|
||
// ***************************************************************************** | ||
|
||
export const SupportingTextBrandTheme: Story = (args: RadioProps) => ( | ||
<ThemeProvider theme={radioBrand}> | ||
<Template {...args} /> | ||
</ThemeProvider> | ||
); | ||
SupportingTextBrandTheme.parameters = { | ||
backgrounds: { | ||
default: 'brandBackground.primary', | ||
}, | ||
}; | ||
SupportingTextBrandTheme.args = { | ||
supporting: '#ff0000', | ||
}; | ||
asChromaticStory(SupportingTextBrandTheme); | ||
|
||
// ***************************************************************************** | ||
|
||
export const SupportingTextOnlyLightTheme = Template.bind({}); | ||
SupportingTextOnlyLightTheme.args = { | ||
supporting: '#ff0000', | ||
label: null, | ||
}; | ||
asChromaticStory(SupportingTextOnlyLightTheme); | ||
|
||
// ***************************************************************************** | ||
|
||
export const SupportingTextOnlyBrandTheme = (args: RadioProps) => ( | ||
<ThemeProvider theme={radioBrand}> | ||
<Template {...args} /> | ||
</ThemeProvider> | ||
); | ||
SupportingTextOnlyBrandTheme.story = { | ||
parameters: { | ||
backgrounds: { | ||
default: 'brandBackground.primary', | ||
}, | ||
}, | ||
}; | ||
SupportingTextOnlyBrandTheme.args = { | ||
supporting: '#ff0000', | ||
label: null, | ||
}; | ||
asChromaticStory(SupportingTextOnlyBrandTheme); | ||
|
||
// ***************************************************************************** | ||
|
||
export const UnlabelledLightTheme = Template.bind({}); | ||
UnlabelledLightTheme.args = { | ||
label: undefined, | ||
}; | ||
asChromaticStory(UnlabelledLightTheme); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import React, { ReactNode, InputHTMLAttributes } from 'react'; | ||
import { | ||
label, | ||
labelWithSupportingText, | ||
radio, | ||
labelText, | ||
labelTextWithSupportingText, | ||
supportingText, | ||
} from './styles'; | ||
import { Props } from '@guardian/src-helpers'; | ||
|
||
const LabelText = ({ | ||
hasSupportingText, | ||
children, | ||
}: { | ||
hasSupportingText?: boolean; | ||
children: ReactNode; | ||
}) => { | ||
return ( | ||
<div | ||
css={(theme) => [ | ||
hasSupportingText ? labelTextWithSupportingText : '', | ||
labelText(theme.radio && theme), | ||
]} | ||
className="src-radio-label-text" | ||
> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
const SupportingText = ({ children }: { children: ReactNode }) => { | ||
return ( | ||
<div css={(theme) => supportingText(theme.radio && theme)}> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
export interface RadioProps | ||
extends InputHTMLAttributes<HTMLInputElement>, | ||
Props { | ||
/** | ||
* Whether radio button is checked. This is necessary when using the | ||
* [controlled approach](https://reactjs.org/docs/forms.html#controlled-components) | ||
* (recommended) to form state management. | ||
* | ||
* _Note: if you pass the `checked` prop, you MUST also pass an `onChange` | ||
* handler, or the field will be rendered as read-only._ | ||
*/ | ||
checked?: boolean; | ||
/** | ||
* When using the [uncontrolled approach](https://reactjs.org/docs/uncontrolled-components.html), | ||
* use defaultChecked to indicate the initially checked button. | ||
*/ | ||
defaultChecked?: boolean; | ||
/** | ||
* 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. | ||
*/ | ||
label?: string | ReactNode; | ||
/** | ||
* Additional text or a component that appears below the label | ||
*/ | ||
supporting?: string | ReactNode; | ||
} | ||
|
||
/** | ||
* [Storybook](https://guardian.github.io/source/?path=/docs/source-src-radio-radio--demo) • | ||
* [Design System](https://theguardian.design/2a1e5182b/p/2891dd-radio-button/b/46940d) • | ||
* [GitHub](https://github.com/guardian/source/tree/main/src/core/components/radio) • | ||
* [NPM](https://www.npmjs.com/package/@guardian/src-radio) | ||
* | ||
* Radio buttons allow users to make a single selection from a set of options. | ||
* | ||
* The following themes are supported: `default`, `brand` | ||
*/ | ||
export const Radio = ({ | ||
label: labelContent, | ||
value, | ||
supporting, | ||
checked, | ||
defaultChecked, | ||
cssOverrides, | ||
...props | ||
}: RadioProps) => { | ||
const isChecked = (): boolean => { | ||
if (checked != null) { | ||
return checked; | ||
} | ||
|
||
return !!defaultChecked; | ||
}; | ||
const radioControl = ( | ||
<input | ||
type="radio" | ||
css={(theme) => [radio(theme.radio && theme), cssOverrides]} | ||
value={value} | ||
aria-checked={isChecked()} | ||
defaultChecked={defaultChecked != null ? defaultChecked : undefined} | ||
checked={checked != null ? isChecked() : undefined} | ||
{...props} | ||
/> | ||
); | ||
|
||
const labelledRadioControl = ( | ||
<label | ||
css={(theme) => [ | ||
label(theme.radio && theme), | ||
supporting ? labelWithSupportingText : '', | ||
]} | ||
> | ||
{radioControl} | ||
{supporting ? ( | ||
<div> | ||
<LabelText hasSupportingText={true}> | ||
{labelContent} | ||
</LabelText> | ||
<SupportingText>{supporting}</SupportingText> | ||
</div> | ||
) : ( | ||
<LabelText>{labelContent}</LabelText> | ||
)} | ||
</label> | ||
); | ||
|
||
return ( | ||
<>{labelContent || supporting ? labelledRadioControl : radioControl}</> | ||
); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this needed now?