Switch.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. <label htmlFor={labelId} className="gf-form-switch-container">
  33. {label && <label className={labelClassName}>{label}</label>}
  34. <div className={switchClassName}>
  35. <input id={labelId} type="checkbox" checked={checked} onChange={this.internalOnChange} />
  36. <span className="gf-form-switch__slider" />
  37. </div>
  38. </label>
  39. );
  40. }
  41. }