yes-no-switch.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import React from 'react';
  2. export default class extends React.Component {
  3. getClassName() {
  4. if (this.props.value) {
  5. return "btn btn-yes-no btn-yes-no-on";
  6. } else {
  7. return "btn btn-yes-no btn-yes-no-off";
  8. }
  9. }
  10. getIcon() {
  11. if (!!this.props.value) {
  12. return this.props.iconOn || 'check_box';
  13. } else {
  14. return this.props.iconOff || 'check_box_outline_blank';
  15. }
  16. }
  17. getLabel() {
  18. if (!!this.props.value) {
  19. return this.props.labelOn || gettext("yes");
  20. } else {
  21. return this.props.labelOff || gettext("no");
  22. }
  23. }
  24. /* jshint ignore:start */
  25. toggle = () => {
  26. this.props.onChange({
  27. target: {
  28. value: !this.props.value
  29. }
  30. });
  31. };
  32. /* jshint ignore:end */
  33. render() {
  34. /* jshint ignore:start */
  35. return <button type="button"
  36. onClick={this.toggle}
  37. className={this.getClassName()}
  38. id={this.props.id || null}
  39. aria-describedby={this.props['aria-describedby'] || null}
  40. disabled={this.props.disabled || false}>
  41. <span className="material-icon">
  42. {this.getIcon()}
  43. </span>
  44. {this.getLabel()}
  45. </button>;
  46. /* jshint ignore:end */
  47. }
  48. }