Skip to content

Commit

Permalink
feat: add a helper text to radio buttons options
Browse files Browse the repository at this point in the history
  • Loading branch information
sirineJ committed Jul 21, 2023
1 parent 070d553 commit 3082b75
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ describe('RadioButton', () => {
const inputEl = screen.getByRole('radio');
expect(inputEl).toHaveAccessibleName(defaultProps.label);
});

it('should have a helper text', () => {
const ref = createRef<HTMLInputElement>();
render(<RadioButton ref={ref} {...defaultProps} helperText="Some explanation" />);

Check failure on line 75 in packages/circuit-ui/components/RadioButton/RadioButton.spec.tsx

View workflow job for this annotation

GitHub Actions / ci (16)

Replace `<RadioButton·ref={ref}·{...defaultProps}·helperText="Some·explanation"·/>` with `⏎········<RadioButton⏎··········ref={ref}⏎··········{...defaultProps}⏎··········helperText="Some·explanation"⏎········/>,⏎······`

Check failure on line 75 in packages/circuit-ui/components/RadioButton/RadioButton.spec.tsx

View workflow job for this annotation

GitHub Actions / ci (18)

Replace `<RadioButton·ref={ref}·{...defaultProps}·helperText="Some·explanation"·/>` with `⏎········<RadioButton⏎··········ref={ref}⏎··········{...defaultProps}⏎··········helperText="Some·explanation"⏎········/>,⏎······`
const helperEl = screen.getByTestId(`${defaultProps.value}-helper`);
expect(helperEl).toBeInTheDocument();
const labelEl = screen.getByText(defaultProps.label);
expect(labelEl).toHaveDescription("Some explanation")

Check failure on line 79 in packages/circuit-ui/components/RadioButton/RadioButton.spec.tsx

View workflow job for this annotation

GitHub Actions / ci (16)

Replace `"Some·explanation")` with `'Some·explanation');`

Check failure on line 79 in packages/circuit-ui/components/RadioButton/RadioButton.spec.tsx

View workflow job for this annotation

GitHub Actions / ci (16)

Strings must use singlequote

Check failure on line 79 in packages/circuit-ui/components/RadioButton/RadioButton.spec.tsx

View workflow job for this annotation

GitHub Actions / ci (16)

Missing semicolon

Check failure on line 79 in packages/circuit-ui/components/RadioButton/RadioButton.spec.tsx

View workflow job for this annotation

GitHub Actions / ci (18)

Replace `"Some·explanation")` with `'Some·explanation');`

Check failure on line 79 in packages/circuit-ui/components/RadioButton/RadioButton.spec.tsx

View workflow job for this annotation

GitHub Actions / ci (18)

Strings must use singlequote

Check failure on line 79 in packages/circuit-ui/components/RadioButton/RadioButton.spec.tsx

View workflow job for this annotation

GitHub Actions / ci (18)

Missing semicolon
});
});

describe('State & Interactions', () => {
Expand Down
41 changes: 36 additions & 5 deletions packages/circuit-ui/components/RadioButton/RadioButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export interface RadioButtonProps
* A clear and concise description of the option's purpose.
*/
label: string;
/**
* Further details about the option's purpose.
*/
helperText?: string;
/**
* Triggers error styles on the component.
*/
Expand Down Expand Up @@ -72,7 +76,7 @@ const labelBaseStyles = ({ theme }: StyleProps) => css`
content: '';
display: block;
position: absolute;
top: 50%;
top: calc(${theme.typography.body.one.lineHeight} / 2) ;

Check failure on line 79 in packages/circuit-ui/components/RadioButton/RadioButton.tsx

View workflow job for this annotation

GitHub Actions / ci (16)

Delete `·`

Check failure on line 79 in packages/circuit-ui/components/RadioButton/RadioButton.tsx

View workflow job for this annotation

GitHub Actions / ci (18)

Delete `·`
left: 0;
transform: translateY(-50%);
transition: border ${theme.transitions.default};
Expand All @@ -87,14 +91,23 @@ const labelBaseStyles = ({ theme }: StyleProps) => css`
content: '';
display: block;
position: absolute;
top: 50%;
top: calc(${theme.typography.body.one.lineHeight} / 2) ;

Check failure on line 94 in packages/circuit-ui/components/RadioButton/RadioButton.tsx

View workflow job for this annotation

GitHub Actions / ci (16)

Delete `·`

Check failure on line 94 in packages/circuit-ui/components/RadioButton/RadioButton.tsx

View workflow job for this annotation

GitHub Actions / ci (18)

Delete `·`
left: ${theme.spacings.bit};
transform: translateY(-50%) scale(0, 0);
opacity: 0;
transition: transform ${theme.transitions.default},
opacity ${theme.transitions.default};
}
`;
const helperTextBaseStyles = ({ theme }: StyleProps) => css`
color: var(--cui-fg-subtle);
display: inline-block;
position: relative;
cursor: pointer;
font-size: ${theme.typography.body.two.fontSize};
line-height: ${theme.typography.body.two.lineHeight};
margin-bottom: ${theme.spacings.bit}

Check failure on line 109 in packages/circuit-ui/components/RadioButton/RadioButton.tsx

View workflow job for this annotation

GitHub Actions / ci (16)

Insert `;`

Check failure on line 109 in packages/circuit-ui/components/RadioButton/RadioButton.tsx

View workflow job for this annotation

GitHub Actions / ci (18)

Insert `;`
`;

const labelInvalidStyles = ({ invalid }: LabelElProps) =>
invalid &&
Expand All @@ -113,6 +126,9 @@ const RadioButtonLabel = styled('label')<LabelElProps>(
labelBaseStyles,
labelInvalidStyles,
);
const RadioButtonHelperText = styled('span')<LabelElProps>(

Check failure on line 129 in packages/circuit-ui/components/RadioButton/RadioButton.tsx

View workflow job for this annotation

GitHub Actions / ci (16)

Replace `·styled('span')<LabelElProps>(⏎····helperTextBaseStyles,⏎` with `⏎··styled('span')<LabelElProps>(helperTextBaseStyles`

Check failure on line 129 in packages/circuit-ui/components/RadioButton/RadioButton.tsx

View workflow job for this annotation

GitHub Actions / ci (18)

Replace `·styled('span')<LabelElProps>(⏎····helperTextBaseStyles,⏎` with `⏎··styled('span')<LabelElProps>(helperTextBaseStyles`
helperTextBaseStyles,
);

type InputElProps = Pick<RadioButtonProps, 'invalid'>;

Expand Down Expand Up @@ -149,7 +165,9 @@ const inputBaseStyles = css`
}
&:disabled + label,
&[disabled] + label {
&:disabled + label span,
&[disabled] + label,
&[disabled] + label span {
pointer-events: none;
color: var(--cui-fg-normal-disabled);
Expand Down Expand Up @@ -203,6 +221,7 @@ export const RadioButton = forwardRef(
{
onChange,
label,
helperText,
id: customId,
name,
value,
Expand Down Expand Up @@ -246,7 +265,7 @@ export const RadioButton = forwardRef(
name={name}
id={id}
value={value}
invalid={invalid}
invalid={invalid ?? false}
aria-invalid={invalid && 'true'}
disabled={disabled}
checked={checked}
Expand All @@ -255,11 +274,23 @@ export const RadioButton = forwardRef(
/>
<RadioButtonLabel
htmlFor={id}
invalid={invalid}
invalid={invalid ?? false}
className={className}
style={style}
aria-describedby={helperText ? `${id}-helper` : undefined}
>
{label}
{helperText &&

Check failure on line 283 in packages/circuit-ui/components/RadioButton/RadioButton.tsx

View workflow job for this annotation

GitHub Actions / ci (16)

Replace `··{helperText·&&⏎················(` with `{helperText·&&·(⏎············`

Check failure on line 283 in packages/circuit-ui/components/RadioButton/RadioButton.tsx

View workflow job for this annotation

GitHub Actions / ci (18)

Replace `··{helperText·&&⏎················(` with `{helperText·&&·(⏎············`
(<>
<br/>

Check failure on line 285 in packages/circuit-ui/components/RadioButton/RadioButton.tsx

View workflow job for this annotation

GitHub Actions / ci (16)

Replace `····················<br` with `··············<br·`

Check failure on line 285 in packages/circuit-ui/components/RadioButton/RadioButton.tsx

View workflow job for this annotation

GitHub Actions / ci (18)

Replace `····················<br` with `··············<br·`
<RadioButtonHelperText
id={`${id}-helper`}
data-testid={`${value}-helper`}
>
{helperText}
</RadioButtonHelperText>
</>)
}
</RadioButtonLabel>
</Fragment>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ Base.args = {
label: 'Choose your favourite fruit',
defaultValue: 'banana',
options: [
{ label: 'Apple', value: 'apple' },
{ label: 'Banana', value: 'banana' },
{ label: 'Apple', value: 'apple', helperText: "Keeps the doctor away" },
{ label: 'Banana', value: 'banana', helperText: "Rich in Mg" },
{ label: 'Mango', value: 'mango' },
],
// Storybook displays the default mocked function props poorly,
Expand Down Expand Up @@ -89,9 +89,9 @@ Validations.args = {
label: 'Choose your favourite fruit',
optionalLabel: 'Optional',
options: [
{ label: 'Apple', value: 'apple' },
{ label: 'Banana', value: 'banana' },
{ label: 'Mango', value: 'mango' },
{ label: 'Apple', value: 'apple', helperText: "Keeps the doctor away" },
{ label: 'Banana', value: 'banana', helperText: "Rich in Mg" },
{ label: 'Mango', value: 'mango' },
],
};

Expand All @@ -102,9 +102,9 @@ export const Disabled = (args: RadioButtonGroupProps) => (
name="fully-disabled"
disabled
options={[
{ label: 'Apple', value: 'apple' },
{ label: 'Banana', value: 'banana' },
{ label: 'Mango', value: 'mango' },
{ label: 'Apple', value: 'apple', helperText: "Keeps the doctor away" },
{ label: 'Banana', value: 'banana', helperText: "Rich in Mg" },
{ label: 'Mango', value: 'mango' },
]}
validationHint="All fruits are sold out"
style={storyStyles}
Expand All @@ -113,9 +113,9 @@ export const Disabled = (args: RadioButtonGroupProps) => (
{...args}
name="partially-disabled"
options={[
{ label: 'Apple', value: 'apple' },
{ label: 'Banana', value: 'banana' },
{ label: 'Mango', value: 'mango', disabled: true },
{ label: 'Apple', value: 'apple', helperText: "Keeps the doctor away" },
{ label: 'Banana', value: 'banana', helperText: "Rich in Mg", disabled: true },
{ label: 'Mango', value: 'mango' },
]}
validationHint="Some fruits are sold out"
style={storyStyles}
Expand Down

0 comments on commit 3082b75

Please sign in to comment.