Pristine
Pristine copied to clipboard
Accessibility improve when show error text
Improve in _showError
- Generate dinamic id to
errorTextElementfromfield.inputby id. - Set in
field.input,aria-describedbyattribute with id error value. - Set in
field.input,aria-validattribute withtruevalue. - Set in
errorTextElement,idattribute with id error value. - Set in
errorTextElement,roleattribute withalertvalue.
// file src/pristine.js
...
function _showError() {
...
const { input } = field;
const inputId = input.id || Math.floor(new Date().valueOf() * Math.random());
const errorId = `error-${inputId}`;
if(errorClassElement){
...
input.setAttribute('aria-describedby', errorId);
input.setAttribute('aria-invalid', 'true');
}
if(errorTextElement){
errorTextElement.setAttribute('id', errorId);
errorTextElement.setAttribute('role', 'alert');
...
}
}
...
With improves above on _showError method, born a requeriment to refactor _removeError method, the mission is remove attributes added on field.input and errorTextElement.
Refactor in _removeError
- Remove attribute
aria-describedbyfromfield.input. - Remove attribute
aria-invalifromfield.input. - Remove attribute
idfromerrorTextElement. - Remove attribute
rolefromerrorTextElement.
// file src/pristine.js
...
function _removeError() {
...
const { input } = field;
if(errorClassElement){
...
input.removeAttribute('aria-describedby');
input.removeAttribute('aria-invalid');
}
if(errorTextElement){
errorTextElement.removeAttribute('id');
errorTextElement.removeAttribute('role');
...
}
...
}
...
Awesome!! This really improves accessibility.
Nice!
Hello, @sha256, Will it be possible to merge this pull request so that this great client side validation plugin can have the accessibility feature. Its a humble request to you. It will be a great help as we are using this library
@enriqueprieto will it possible to resolve the conflicts of this branch in case @sha256 decide to merge this.
Merged and published to npm. Thank you @enriqueprieto and @shayanpaul
Thank you @sha256 and @shayanpaul