Skip to content

Instantly share code, notes, and snippets.

@lucprincen
Created June 27, 2017 15:43
Show Gist options
  • Save lucprincen/105837804e91fb8c4cce8dc2b2f65950 to your computer and use it in GitHub Desktop.
Save lucprincen/105837804e91fb8c4cce8dc2b2f65950 to your computer and use it in GitHub Desktop.
const $ = require('jquery');
class RadioTabs {
constructor($component) {
this.$component = $component;
this.attachEventHandlers();
this.selectTabBasedOnRadioValue();
}
/**
* Attach event handlers to component
*
* @return {void}
*/
attachEventHandlers() {
this.$component.find('input[type="radio"]').on('change', () => this.selectTabBasedOnRadioValue());
}
/**
* Select the correct source based on the radio value
*
* @return {void}
*/
selectTabBasedOnRadioValue() {
let name = this.$component.find('input[type="radio"]:checked').val();
this.selectSource(name);
}
/**
* Show the source for the given name and hide the rest
*
* @param {string} name
* @return {void}
*/
selectSource(name) {
this.$component.find('[data-tab]').hide();
this.$component.find(`[data-tab="${name}"]`).show();
}
}
module.exports = RadioTabs;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment