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 24, 2023
1 parent 070d553 commit 87a565a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ describe('RadioButton', () => {
const inputEl = screen.getByRole('radio');
expect(inputEl).toHaveAccessibleName(defaultProps.label);
});

it('should have a description', () => {
render(<RadioButton {...defaultProps} description="Some explanation" />);
const helperEl = screen.getAllByText('Some explanation');
expect(helperEl[0]).toBeVisible();

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

View workflow job for this annotation

GitHub Actions / ci (16)

RadioButton › Structure & Semantics › should have a description

TypeError: Cannot read properties of null (reading 'parentElement') at Array.Resolver (eval at compile (node_modules/nwsapi/src/nwsapi.js:768:17), <anonymous>:3:116) at match_assert (node_modules/nwsapi/src/nwsapi.js:1351:13) at Object._matches [as match] (node_modules/nwsapi/src/nwsapi.js:1369:16) at exports.matchesDontThrow (node_modules/jsdom/lib/jsdom/living/helpers/selectors.js:29:36) at matches (node_modules/jsdom/lib/jsdom/living/helpers/style-rules.js:50:10) at node_modules/jsdom/lib/jsdom/living/helpers/style-rules.js:35:18 at Array.forEach (<anonymous>) at handleSheet (node_modules/jsdom/lib/jsdom/living/helpers/style-rules.js:26:13) at Array.forEach (<anonymous>) at exports.forEachMatchingSheetRuleOfElement (node_modules/jsdom/lib/jsdom/living/helpers/style-rules.js:46:11) at Window.getComputedStyle (node_modules/jsdom/lib/jsdom/browser/Window.js:805:5) at isStyleVisible (node_modules/@testing-library/jest-dom/dist/to-be-visible.js:18:7) at isElementVisible (node_modules/@testing-library/jest-dom/dist/to-be-visible.js:35:10) at Object.toBeVisible (node_modules/@testing-library/jest-dom/dist/to-be-visible.js:43:37) at __EXTERNAL_MATCHER_TRAP__ (node_modules/expect/build/index.js:317:30) at Object.throwingMatcher [as toBeVisible] (node_modules/expect/build/index.js:318:15) at Object.toBeVisible (packages/circuit-ui/components/RadioButton/RadioButton.spec.tsx:76:27)

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

View workflow job for this annotation

GitHub Actions / ci (18)

RadioButton › Structure & Semantics › should have a description

TypeError: Cannot read properties of null (reading 'parentElement') at Array.Resolver (eval at compile (node_modules/nwsapi/src/nwsapi.js:768:17), <anonymous>:3:116) at match_assert (node_modules/nwsapi/src/nwsapi.js:1351:13) at Object._matches [as match] (node_modules/nwsapi/src/nwsapi.js:1369:16) at exports.matchesDontThrow (node_modules/jsdom/lib/jsdom/living/helpers/selectors.js:29:36) at matches (node_modules/jsdom/lib/jsdom/living/helpers/style-rules.js:50:10) at node_modules/jsdom/lib/jsdom/living/helpers/style-rules.js:35:18 at Array.forEach (<anonymous>) at handleSheet (node_modules/jsdom/lib/jsdom/living/helpers/style-rules.js:26:13) at Array.forEach (<anonymous>) at exports.forEachMatchingSheetRuleOfElement (node_modules/jsdom/lib/jsdom/living/helpers/style-rules.js:46:11) at Window.getComputedStyle (node_modules/jsdom/lib/jsdom/browser/Window.js:805:5) at isStyleVisible (node_modules/@testing-library/jest-dom/dist/to-be-visible.js:18:7) at isElementVisible (node_modules/@testing-library/jest-dom/dist/to-be-visible.js:35:10) at Object.toBeVisible (node_modules/@testing-library/jest-dom/dist/to-be-visible.js:43:37) at __EXTERNAL_MATCHER_TRAP__ (node_modules/expect/build/index.js:317:30) at Object.throwingMatcher [as toBeVisible] (node_modules/expect/build/index.js:318:15) at Object.toBeVisible (packages/circuit-ui/components/RadioButton/RadioButton.spec.tsx:76:27)
const labelEl = screen.getByText(defaultProps.label);
expect(labelEl).toHaveDescription('Some explanation');
});
});

describe('State & Interactions', () => {
Expand Down
25 changes: 22 additions & 3 deletions packages/circuit-ui/components/RadioButton/RadioButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ import { uniqueId } from '../../util/id';
import { useClickEvent, TrackingProps } from '../../hooks/useClickEvent';
import { AccessibilityError } from '../../util/errors';
import { deprecate } from '../../util/logger';
import { FieldDescription } from '../FieldAtoms';

export interface RadioButtonProps
extends InputHTMLAttributes<HTMLInputElement> {
/**
* A clear and concise description of the option's purpose.
*/
label: string;
/**
* Further details about the option's purpose.
*/
description?: string;
/**
* Triggers error styles on the component.
*/
Expand Down Expand Up @@ -72,7 +77,7 @@ const labelBaseStyles = ({ theme }: StyleProps) => css`
content: '';
display: block;
position: absolute;
top: 50%;
top: calc(${theme.typography.body.one.lineHeight} / 2);
left: 0;
transform: translateY(-50%);
transition: border ${theme.transitions.default};
Expand All @@ -87,7 +92,7 @@ const labelBaseStyles = ({ theme }: StyleProps) => css`
content: '';
display: block;
position: absolute;
top: 50%;
top: calc(${theme.typography.body.one.lineHeight} / 2);
left: ${theme.spacings.bit};
transform: translateY(-50%) scale(0, 0);
opacity: 0;
Expand Down Expand Up @@ -149,7 +154,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 +210,7 @@ export const RadioButton = forwardRef(
{
onChange,
label,
description,
id: customId,
name,
value,
Expand Down Expand Up @@ -258,9 +266,20 @@ export const RadioButton = forwardRef(
invalid={invalid}
className={className}
style={style}
aria-describedby={description ? `${id}-description` : undefined}
>
{label}
{description && (
<FieldDescription aria-hidden="true">
{description}
</FieldDescription>
)}
</RadioButtonLabel>
{description && (
<p id={`${id}-description`} css={hideVisually}>
{description}
</p>
)}
</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', description: 'Keeps the doctor away' },
{ label: 'Banana', value: 'banana', description: 'Rich in Mg' },
{ label: 'Mango', value: 'mango' },
],
// Storybook displays the default mocked function props poorly,
Expand Down Expand Up @@ -89,8 +89,8 @@ Validations.args = {
label: 'Choose your favourite fruit',
optionalLabel: 'Optional',
options: [
{ label: 'Apple', value: 'apple' },
{ label: 'Banana', value: 'banana' },
{ label: 'Apple', value: 'apple', description: 'Keeps the doctor away' },
{ label: 'Banana', value: 'banana', description: 'Rich in Mg' },
{ label: 'Mango', value: 'mango' },
],
};
Expand All @@ -102,8 +102,12 @@ export const Disabled = (args: RadioButtonGroupProps) => (
name="fully-disabled"
disabled
options={[
{ label: 'Apple', value: 'apple' },
{ label: 'Banana', value: 'banana' },
{
label: 'Apple',
value: 'apple',
description: 'Keeps the doctor away',
},
{ label: 'Banana', value: 'banana', description: 'Rich in Mg' },
{ label: 'Mango', value: 'mango' },
]}
validationHint="All fruits are sold out"
Expand All @@ -113,9 +117,18 @@ 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',
description: 'Keeps the doctor away',
},
{
label: 'Banana',
value: 'banana',
description: 'Rich in Mg',
disabled: true,
},
{ label: 'Mango', value: 'mango' },
]}
validationHint="Some fruits are sold out"
style={storyStyles}
Expand Down

0 comments on commit 87a565a

Please sign in to comment.