Skip to content

Instantly share code, notes, and snippets.

@jrwebdev
Last active May 17, 2016 06:58
Show Gist options
  • Save jrwebdev/b47cd09a20c9b7a54fc2b96f78b7fd8b to your computer and use it in GitHub Desktop.
Save jrwebdev/b47cd09a20c9b7a54fc2b96f78b7fd8b to your computer and use it in GitHub Desktop.
ngReact wrapper directive
const module = angular.module('toggle', ['react']);
const Toggle = (props) => (
<div onClick={() => props.onToggle(!props.value)}>
<span className={props.value ? 'selected' : ''}>
{props.trueLabel || 'Yes'}
</span>
<span className={!props.value ? 'selected' : ''}>
{props.falseLabel || 'No'}
</span>
</div>
);
Toggle.propTypes = {
value: React.PropTypes.bool,
trueLabel: React.PropTypes.string,
falseLabel: React.PropTypes.string,
onToggle: React.PropTypes.func
};
module.directive('reactToggle', ['reactDirective', function (reactDirective) {
return reactDirective(Toggle);
}]);
module.directive('toggle', function () {
return {
controllerAs: 'toggle',
bindToController: {
value: '=',
trueLabel: '@',
falseLabel: '@',
onToggle: '&'
},
controller: function () {
this.toggle = (newValue) => {
this.value = newValue;
this.onToggle({value: newValue});
};
},
template: `
<react-toggle value="toggle.value"
true-label="toggle.trueLabel"
false-label="toggle.falseLabel"
on-toggle="toggle.toggle">
</react-toggle>
`
}
});
<toggle value="value"
true-label="{{trueLabel}}"
false-label="{{falseLabel}}"
on-toggle="onToggle(value)">
</toggle>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment