Switch.tsx 1.2 KB

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