Switch.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. transparent?: boolean;
  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, transparent } = this.props;
  24. const labelId = `check-${this.state.id}`;
  25. const labelClassName = `gf-form-label ${labelClass} ${transparent ? 'gf-form-label--transparent' : ''} pointer`;
  26. const switchClassName = `gf-form-switch ${switchClass} ${transparent ? 'gf-form-switch--transparent' : ''}`;
  27. return (
  28. <label htmlFor={labelId} className="gf-form gf-form-switch-container">
  29. {label && <div className={labelClassName}>{label}</div>}
  30. <div className={switchClassName}>
  31. <input id={labelId} type="checkbox" checked={checked} onChange={this.internalOnChange} />
  32. <span className="gf-form-switch__slider" />
  33. </div>
  34. </label>
  35. );
  36. }
  37. }