Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
Fix aria-describedby attribute on Choice component to use id, not raw…
Browse files Browse the repository at this point in the history
… text (#1018)
  • Loading branch information
jakubjanczyk authored Jan 27, 2022
1 parent 8eb2b8e commit 8e438a8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/components/Choice/Choice.Input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class ChoiceInput extends React.PureComponent {
state,
type,
value,
ariaDescribedBy,
'data-cy': dataCy,
} = this.props
const { isFocused } = this.state
Expand Down Expand Up @@ -149,7 +150,7 @@ class ChoiceInput extends React.PureComponent {
<InputUI className={componentClassName}>
<InputInputUI
autoFocus={autoFocus}
aria-describedby={helpText || undefined}
aria-describedby={ariaDescribedBy || undefined}
aria-invalid={state === 'error'}
checked={checked}
className={inputClassName}
Expand Down Expand Up @@ -212,6 +213,8 @@ ChoiceInput.propTypes = {
'data-cy': PropTypes.string,
disabled: PropTypes.bool,
helpText: PropTypes.string,
/** ID of element that describes this input */
ariaDescribedBy: PropTypes.string,
id: PropTypes.string,
inputRef: PropTypes.func,
innerRef: PropTypes.func,
Expand Down
4 changes: 3 additions & 1 deletion src/components/Choice/Choice.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ class Choice extends React.PureComponent {

getHelpTextMarkup = () => {
const { helpText, stacked, state } = this.props
const { id: choiceID } = this.state

const className = classNames('c-Choice__help-text', stacked && 'is-stacked')

return (
helpText && (
<ChoiceHelpTextUI className={className}>
<HelpText state={state} muted>
<HelpText state={state} muted id={`${choiceID}_description`}>
{helpText}
</HelpText>
</ChoiceHelpTextUI>
Expand Down Expand Up @@ -176,6 +177,7 @@ class Choice extends React.PureComponent {
disabled,
helpText,
id: choiceID,
ariaDescribedBy: `${choiceID}_description`,
inputRef,
innerRef,
kind,
Expand Down
14 changes: 14 additions & 0 deletions src/components/Choice/Choice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ describe('HelpText', () => {
expect(text.text()).toBe(helpText)
wrapper.unmount()
})

test('Assigns ID to a helpText and use it on input as described by', () => {
const helpText = 'Help Text'
const wrapper = mount(
<Choice label="Label" helpText={helpText} id="test-id" />
)
const text = wrapper.find('.c-HelpText').hostNodes()

expect(text.prop('id')).toBe('test-id_description')
expect(wrapper.find('input').prop('aria-describedby')).toBe(
'test-id_description'
)
wrapper.unmount()
})
})

describe('Label', () => {
Expand Down

0 comments on commit 8e438a8

Please sign in to comment.