Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui-source-code-editor): link label to input field programmatically #1865

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class LanguageExamples extends React.Component {
<Flex.Item padding="0 0 0 large" shouldGrow shouldShrink>
<FormField label="SourceCodeEditor with syntax highlight">
<SourceCodeEditor
label={`${this.state.currentLanguage} code editor`}
label={`${this.state.currentLanguage} SourceCodeEditor with syntax highlight`}
language={this.state.currentLanguage}
value={this.state.currentValue}
onChange={(value) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,19 @@ describe('<SourceCodeEditor />', () => {
expect(activeLine[1]).toHaveStyle({ color: '#0000ff' })
expect(activeLine[2]).toHaveStyle({ color: '#116644' })
})

it('should link editor element to label using aria-labelledby attribute', async () => {
const { container } = render(
<SourceCodeEditor
label="test"
language="jsx"
defaultValue="const a = 2;"
/>
)
const editorElement = container.querySelector('[role="textbox"]')
const labelId = container.querySelector('[class$="-label"]')?.id

expect(editorElement).toHaveAttribute('aria-labelledby', labelId)
})
})
})
12 changes: 11 additions & 1 deletion packages/ui-source-code-editor/src/SourceCodeEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ class SourceCodeEditor extends Component<SourceCodeEditorProps> {
if (indentOnLoad) {
this.indentAll()
}
this.assignAriaLabel()
}

componentWillUnmount() {
Expand Down Expand Up @@ -644,6 +645,15 @@ class SourceCodeEditor extends Component<SourceCodeEditorProps> {
}
}

assignAriaLabel = () => {
if (this._containerRef) {
const editorDiv = this._containerRef.querySelector('[role="textbox"]')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because the CodeMirror does not provide any way to reach this element, I had to find it like this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative way to find textbox is to go through child elements of the containerRef if it is an element but this is good as well.

if (editorDiv) {
editorDiv.setAttribute('aria-labelledby', `${this._id}`)
}
}
}

render() {
const { label, styles, ...restProps } = this.props

Expand All @@ -655,7 +665,7 @@ class SourceCodeEditor extends Component<SourceCodeEditorProps> {
omitProps(restProps, SourceCodeEditor.allowedProps)
)}
>
<label css={styles?.label} htmlFor={this._id}>
<label css={styles?.label} id={this._id}>
Copy link
Contributor Author

@ToMESSKa ToMESSKa Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

htmlFor does not work for div elements and the textbox is a div element in CodeMirror so replaced with id

<ScreenReaderContent>{label}</ScreenReaderContent>
<div
ref={this.handleContainerRef}
Expand Down
Loading