Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from PolymerElements/a11y-docs
Browse files Browse the repository at this point in the history
set aria-invalid and write docs
  • Loading branch information
Yvonne Yip committed May 21, 2015
2 parents 56d1e5c + d956a76 commit 2baeda2
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 7 deletions.
31 changes: 25 additions & 6 deletions iron-validatable-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-meta/iron-meta.html">

<!--
<script>

/*
Use `Polymer.IronValidatableBehavior` to implement an element that validates user input.
-->
<script>
### Accessiblity
Changing the `invalid` property, either manually or by calling `validate()` will update the
`aria-invalid` attribute.
@polymerBehavior
*/

/** @polymerBehavior */
Polymer.IronValidatableBehavior = {

properties: {
Expand Down Expand Up @@ -52,6 +58,10 @@

},

observers: [
'_invalidChanged(invalid)'
],

get _validator() {
return this._validatorMeta && this._validatorMeta.byKey(this.validator);
},
Expand All @@ -60,15 +70,24 @@
this._validatorMeta = new Polymer.IronMeta({type: this.validatorType});
},

_invalidChanged: function() {
if (this.invalid) {
this.setAttribute('aria-invalid', 'true');
} else {
this.removeAttribute('aria-invalid');
}
},

/**
* Returns true if there is a validator.
* @return {Boolean} True if the validator `validator` exists.
*/
hasValidator: function() {
return this._validator != null;
},

/**
* Validates the value.
* @param {Object} values Passed to the validator's `validate()` function.
* @return {Boolean} True if `values` is valid.
*/
validate: function(values) {
var valid = this._validator && this._validator.validate(values);
Expand Down
4 changes: 3 additions & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
<script>

/* no tests */
WCT.loadSuites([]);
WCT.loadSuites([
'iron-validatable-behavior.html'
]);

</script>

Expand Down
54 changes: 54 additions & 0 deletions test/iron-validatable-behavior.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!doctype html>
<!--
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>

<title>iron-validatable-behavior tests</title>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes">

<script src="../../webcomponentsjs/webcomponents-lite.js"></script>

<script src="../../web-component-tester/browser.js"></script>
<script src="../../test-fixture/test-fixture-mocha.js"></script>

<script src="../../iron-test-helpers/test-helpers.js"></script>

<link rel="import" href="../../test-fixture/test-fixture.html">
<link rel="import" href="test-validatable.html">

</head>
<body>

<test-fixture id="basic">
<template>
<test-validatable></test-validatable>
</template>
</test-fixture>

<script>

suite('basic', function() {

test('setting `invalid` sets `aria-invalid=true`', function() {
var node = fixture('basic');
node.invalid = true;
assert.equal(node.getAttribute('aria-invalid'), 'true', 'aria-invalid is set');
node.invalid = false;
assert.isFalse(node.hasAttribute('aria-invalid'), 'aria-invalid is unset');
});

});

</script>

</body>
28 changes: 28 additions & 0 deletions test/test-validatable.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!--
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../iron-validatable-behavior.html">

<dom-module id="test-validatable">
<template>
<content></content>
</template>
</dom-module>

<script>

Polymer({

is: 'test-validatable',

behaviors: [
Polymer.IronValidatableBehavior
]

});
</script>

0 comments on commit 2baeda2

Please sign in to comment.