Skip to content

Instantly share code, notes, and snippets.

@zachpendleton
Created August 24, 2013 04:20
Show Gist options
  • Select an option

  • Save zachpendleton/6326041 to your computer and use it in GitHub Desktop.

Select an option

Save zachpendleton/6326041 to your computer and use it in GitHub Desktop.
Accessible Ember buttonset component.
<!-- see http://jsbin.com/erIBAba/1 -->
<h1>Buttonset example</h1>
{{#zp-buttonset bindTo=filter}}
{{zp-button label="Open"}}
{{zp-button label="Upcoming"}}
{{zp-button label="Closed"}}
{{/zp-buttonset}}
ApplicationController = Ember.Controller.extend({
filter: null
});
<button type="button"
role="radio"
{{bindAttr class="isSelected:selected"}}
{{bindAttr aria-checked="isChecked"}}>
{{label}}
</button>
ZpButtonComponent = Ember.Component.extend({
didInsertElement: function() {
this.get('parentView').addButton(this);
},
click: function() {
this.get('parentView').select(this);
},
isSelected: function() {
return this.get('parentView.selected') === this;
}.property('parentView.selected'),
isChecked: function() {
return this.get('isSelected').toString();
}.property('isSelected')
});
ZpButtonsetComponent = Ember.Component.extend({
classNames: ['buttonset'],
attributeBindings: ['role'],
role: 'radiogroup',
selected: null,
buttons: [],
addButton: function(button) {
if (this.get('buttons').length === 0) this.select(button);
this.get('buttons').addObject(button);
},
select: function(button) {
this.set('bindTo', button.get('label'));
this.set('selected', button);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment