Last active
March 22, 2017 07:40
-
-
Save lukesargeant/8af366045f584a7604c643331cd38b51 to your computer and use it in GitHub Desktop.
Radio-set component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Component.extend({ | |
isActive: false, | |
click() { | |
if (this.get('isActive')) { | |
console.log('clicked'); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
const RadioSetComponent = Ember.Component.extend({ | |
/** | |
* The active item in the radio-set. | |
*/ | |
activeItem: null, | |
/** | |
* A provided function called when an item is selected. | |
*/ | |
selectItem() {}, | |
/** | |
* An optional array of item values to bind to element selection. | |
*/ | |
items: null, | |
/** | |
* Whether to clear the active item when it is reselected. | |
*/ | |
allowDeselect: false, | |
/** | |
* Overridable CSS class to apply to the active item's element. | |
*/ | |
activeClass: 'radio-set--active', | |
/** | |
* Intercept click event, determine whether it was a radio item and call selectItem(). | |
*/ | |
click(event) { | |
const $tab = Ember.$(event.target).closest('[data-value]'); | |
const activeItem = this.get('activeItem'); | |
const allowDeselect = this.get('allowDeselect'); | |
let value = $tab.attr('data-value'); | |
if (allowDeselect && value === activeItem) { | |
value = null; | |
} | |
this.selectItem(value); | |
}, | |
/** | |
* Decorate the active radio item when the radio-set renders. | |
*/ | |
didRender() { | |
const activeItem = this.get('activeItem'); | |
const activeClass = this.get('activeClass'); | |
this.$().children().removeClass(activeClass) | |
.filter(`[data-value="${activeItem}"]`).addClass(activeClass); | |
} | |
}); | |
RadioSetComponent.reopenClass({ | |
positionalParams : ['activeItem', 'selectItem'] | |
}); | |
export default RadioSetComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle', | |
activeItem: null, | |
activeItem2: null, | |
activeItem3: null, | |
selectItem(item) { | |
this.set('activeItem', item); | |
}, | |
selectItem2(item) { | |
this.set('activeItem2', item); | |
}, | |
selectItem3(item) { | |
this.set('activeItem3', item); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
margin: 12px 16px; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 12pt; | |
} | |
.tabs { | |
display: flex; | |
} | |
.tab { | |
background: #eee; | |
margin: 1px; | |
padding: 5px 10px; | |
cursor: pointer; | |
} | |
.radio-set--active { | |
background: #5fb5ff; | |
} | |
.tab--weird { | |
border-radius: 50%; | |
margin-left: 30px; | |
width: 10px; | |
text-align: center; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.11.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.11.0", | |
"ember-data": "2.11.0", | |
"ember-template-compiler": "2.11.0", | |
"ember-testing": "2.11.0" | |
}, | |
"addons": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment