Last active
May 17, 2016 06:58
-
-
Save jrwebdev/b47cd09a20c9b7a54fc2b96f78b7fd8b to your computer and use it in GitHub Desktop.
ngReact wrapper directive
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
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> | |
` | |
} | |
}); |
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
<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