Switch.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. <div className="gf-form">
  28. {label && (
  29. <label htmlFor={labelId} className={labelClassName}>
  30. {label}
  31. </label>
  32. )}
  33. <div className={switchClassName}>
  34. <input id={labelId} type="checkbox" checked={checked} onChange={this.internalOnChange} />
  35. <label htmlFor={labelId} />
  36. </div>
  37. </div>
  38. );
  39. }
  40. }