Skip to content

Commit

Permalink
refactor: use attributeChangedCallback for aria-label
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan committed Feb 8, 2019
1 parent a3592f5 commit 3dbcd07
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
31 changes: 20 additions & 11 deletions src/vaadin-button.html
Expand Up @@ -77,7 +77,7 @@
<slot name="suffix"></slot>
</div>
</div>
<button id="button" type="button"></button>
<button id="button" type="button" aria-label$="[[_ariaLabel]]"></button>
</template>
<script>
(function() {
Expand Down Expand Up @@ -144,13 +144,16 @@
* default) means that the `aria-label` attribute is not present at
* all.
*/
ariaLabel: {
type: String,
observer: '_ariaLabelChanged'
_ariaLabel: {
type: String
}
};
}

static get observedAttributes() {
return super.observedAttributes.concat(['aria-label']);
}

ready() {
super.ready();

Expand All @@ -162,7 +165,7 @@
this.$.button.setAttribute('role', 'presentation');

this._addActiveListeners();
this._ariaLabelChanged(this.ariaLabel);
this._updateAriaLabel(this.getAttribute('aria-label'));
}

/**
Expand All @@ -178,6 +181,16 @@
}
}

/**
* @protected
*/
attributeChangedCallback(attr, oldVal, newVal) {
super.attributeChangedCallback(attr, oldVal, newVal);
if (attr === 'aria-label') {
this._updateAriaLabel(newVal);
}
}

_addActiveListeners() {
Polymer.Gestures.addListener(this, 'down', () => !this.disabled && this.setAttribute('active', ''));
Polymer.Gestures.addListener(this, 'up', () => this.removeAttribute('active'));
Expand All @@ -186,12 +199,8 @@
this.addEventListener('blur', () => this.removeAttribute('active'));
}

_ariaLabelChanged(ariaLabel) {
if (ariaLabel !== undefined && ariaLabel !== null) {
this.$.button.setAttribute('aria-label', ariaLabel);
} else {
this.$.button.setAttribute('aria-label', this.innerText);
}
_updateAriaLabel(ariaLabel) {
this._ariaLabel = ariaLabel !== undefined && ariaLabel !== null ? ariaLabel : this.innerText;
}

/**
Expand Down
15 changes: 12 additions & 3 deletions test/vaadin-button_test.html
Expand Up @@ -130,17 +130,26 @@
expect(vaadinButton.hasAttribute('active')).to.be.false;
});

it('should set buttom text to native button aria-label', () => {
it('should set button text to native button aria-label', () => {
expect(nativeButton.hasAttribute('aria-label')).to.be.true;
// Remove extra line breaks
expect(nativeButton.getAttribute('aria-label').replace(/(\r\n|\n|\r)/gm, ''))
.to.contain(vaadinButton.innerText.replace(/(\r\n|\n|\r)/gm, ''));
});

it('should set button aria-label to native button aria-label', () => {
vaadinButton.ariaLabel = 'accessible';
vaadinButton.setAttribute('aria-label', 'accessible');
expect(nativeButton.hasAttribute('aria-label')).to.be.true;
expect(nativeButton.getAttribute('aria-label')).to.be.eql(vaadinButton.ariaLabel);
expect(nativeButton.getAttribute('aria-label')).to.be.eql('accessible');
});

it('should set button aria-label when adding dynamically', () => {
const button = document.createElement('vaadin-button');
button.setAttribute('aria-label', '1');
button.textContent = '2';
document.body.appendChild(button);
expect(button.shadowRoot.querySelector('button').getAttribute('aria-label')).to.equal('1');
document.body.removeChild(button);
});
});
</script>
Expand Down

0 comments on commit 3dbcd07

Please sign in to comment.