Switch.tsx 1.4 KB

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