Skip to content

Commit

Permalink
feat: automatically add [aria-describedby] and [for] attributes in Abide
Browse files Browse the repository at this point in the history
  • Loading branch information
ncoden committed Jan 16, 2018
1 parent 07b2d76 commit 88abca7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
10 changes: 5 additions & 5 deletions docs/pages/abide.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,18 @@ When the Form Errors cannot be placed next to its field, like in an Input Group,

```html_example
<form data-abide novalidate>
<div data-abide-error class="sr-only" aria-live="assertive">
<div data-abide-error class="sr-only">
There are some errors in your form.
</div>
<label>
<div>
Amount
<div class="input-group">
<span class="input-group-label">$</span>
<input class="input-group-field" id="example3Input" type="number" required pattern="number" aria-describedby="example3Error"/>
<input class="input-group-field" id="example3Input" type="number" required pattern="number"/>
</div>
<span class="form-error" id="example3Error" data-form-error-for="example3Input">Amount is required.</span>
</label>
<label class="form-error" data-form-error-for="example3Input">Amount is required.</label>
</div>
<button class="button" type="submit" value="Submit">Submit</button>
</form>
Expand Down
46 changes: 46 additions & 0 deletions js/foundation.abide.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import $ from 'jquery';
import { Plugin } from './foundation.plugin';
import { GetYoDigits } from './foundation.util.core';

/**
* Abide module.
Expand Down Expand Up @@ -32,6 +33,11 @@ class Abide extends Plugin {
_init() {
this.$inputs = this.$element.find('input, textarea, select');

// Add a11y attributes to all fields
this.$inputs.each((i, input) => {
this.addA11yAttributes($(input));
});

this._events();
}

Expand Down Expand Up @@ -198,6 +204,46 @@ class Abide extends Plugin {
});
}

/**
* Adds [for] attributes to all form error targetting $el,
* and [aria-describedby] attribute to $el toward the first form error.
* @param {Object} $el - jQuery object
*/
addA11yAttributes($el) {
let $errors = this.findFormError($el);
let $labels = $errors.filter('label');
let $error = $errors.first();
if (!$errors.length) return;

// Set [aria-describedby] on the input toward the first form error if it is not set
if (typeof $el.attr('aria-describedby') === 'undefined') {
// Get the first error ID or create one
let errorId = $error.attr('id');
if (typeof errorId === 'undefined') {
errorId = GetYoDigits(6, 'abide-error');
$error.attr('id', errorId);
};

$el.attr('aria-describedby', errorId);
}

if ($labels.filter('[for]').length < $labels.length) {
// Get the input ID or create one
let elemId = $el.attr('id');
if (typeof elemId === 'undefined') {
elemId = GetYoDigits(6, 'abide-input');
$el.attr('id', elemId);
};

// For each label targeting $el, set [for] if it is not set.
$labels.each((i, label) => {
const $label = $(label);
if (typeof $label.attr('for') === 'undefined')
$label.attr('for', elemId);
});
}
}

/**
* Remove CSS error classes etc from an entire radio button group
* @param {String} groupName - A string that specifies the name of a radio button group
Expand Down

0 comments on commit 88abca7

Please sign in to comment.