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

ref: wagtail#10300
  • Loading branch information
Stormheg committed Jul 27, 2023
1 parent 915fcdd commit 9d3ddec
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 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,31 @@ export class FieldBlock {

this.prefix = prefix;

const extraAttributes = {};

// 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) {
extraAttributes['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) {
extraAttributes.required = '';
}

const options = {
attributes: extraAttributes,
};

try {
this.widget = this.blockDef.widget.render(
widgetElement,
prefix,
prefix,
initialState,
this.parentCapabilities,
options,
);
} catch (e) {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit 9d3ddec

Please sign in to comment.