Switch.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import React, { PureComponent } from 'react';
  2. import _ from 'lodash';
  3. export interface Props {
  4. label: string;
  5. checked: boolean;
  6. labelClass?: string;
  7. switchClass?: string;
  8. onChange: (event) => any;
  9. }
  10. export interface State {
  11. id: any;
  12. }
  13. export class Switch extends PureComponent<Props, State> {
  14. state = {
  15. id: _.uniqueId(),
  16. };
  17. internalOnChange = event => {
  18. event.stopPropagation();
  19. this.props.onChange(event);
  20. };
  21. render() {
  22. const { labelClass = '', switchClass = '', label, checked } = this.props;
  23. const labelId = `check-${this.state.id}`;
  24. const labelClassName = `gf-form-label ${labelClass} pointer`;
  25. const switchClassName = `gf-form-switch ${switchClass}`;
  26. return (
  27. <label htmlFor={labelId} className="gf-form-switch-container">
  28. {label && <div className={labelClassName}>{label}</div>}
  29. <div className={switchClassName}>
  30. <input id={labelId} type="checkbox" checked={checked} onChange={this.internalOnChange} />
  31. <span className="gf-form-switch__slider" />
  32. </div>
  33. </label>
  34. );
  35. }
  36. }