Skip to content

Commit

Permalink
FieldBlock: render 'required' and 'aria-describedby' attributes when …
Browse files Browse the repository at this point in the history
…appropriate
  • Loading branch information
Stormheg committed Jul 27, 2023
1 parent 9ed3b6b commit 44263aa
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions client/src/components/StreamField/blocks/FieldBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ export class FieldBlock {

this.prefix = prefix;

const options = { attributes: this.getAttributes() };

try {
this.widget = this.blockDef.widget.render(
widgetElement,
prefix,
prefix,
initialState,
this.parentCapabilities,
options,
);
} catch (e) {
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -137,6 +140,24 @@ export class FieldBlock {
}
}

getAttributes() {
const prefix = this.prefix;
const attributes = {};

// If the block has help text, we should associate this with the input rendered by the widget.
// To accomplish this, we must tell the widget to render an aria-describedby attribute referring
// to the help text id in its HTML.
if (this.blockDef.meta.helpText) {
attributes['aria-describedby'] = `${prefix}-helptext`;
}
// If the block is required, we must tell the widget to render a required attribute in its HTML.
if (this.blockDef.meta.required) {
attributes.required = '';
}

return attributes;
}

getState() {
return this.widget.getState();
}
Expand Down

0 comments on commit 44263aa

Please sign in to comment.