ToggleButtonGroup.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import React, { SFC, ReactNode, PureComponent, ReactElement } from 'react';
  2. interface ToggleButtonGroupProps {
  3. onChange: (value) => void;
  4. value?: any;
  5. label?: string;
  6. render: (props) => void;
  7. }
  8. export default class ToggleButtonGroup extends PureComponent<ToggleButtonGroupProps> {
  9. getValues() {
  10. const { children } = this.props;
  11. return React.Children.toArray(children).map((c: ReactElement<any>) => c.props.value);
  12. }
  13. smallChildren() {
  14. const { children } = this.props;
  15. return React.Children.toArray(children).every((c: ReactElement<any>) => c.props.className.includes('small'));
  16. }
  17. handleToggle(toggleValue) {
  18. const { value, onChange } = this.props;
  19. if (value && value === toggleValue) {
  20. return;
  21. }
  22. onChange(toggleValue);
  23. }
  24. render() {
  25. const { value, label } = this.props;
  26. const values = this.getValues();
  27. const selectedValue = value || values[0];
  28. const labelClassName = `gf-form-label ${this.smallChildren() ? 'small' : ''}`;
  29. return (
  30. <div className="gf-form">
  31. <div className="toggle-button-group">
  32. {label && <label className={labelClassName}>{label}</label>}
  33. {this.props.render({ selectedValue, onChange: this.handleToggle.bind(this) })}
  34. </div>
  35. </div>
  36. );
  37. }
  38. }
  39. interface ToggleButtonProps {
  40. onChange?: (value) => void;
  41. selected?: boolean;
  42. value: any;
  43. className?: string;
  44. children: ReactNode;
  45. }
  46. export const ToggleButton: SFC<ToggleButtonProps> = ({ children, selected, className = '', value, onChange }) => {
  47. const handleChange = event => {
  48. event.stopPropagation();
  49. if (onChange) {
  50. onChange(value);
  51. }
  52. };
  53. const btnClassName = `btn ${className} ${selected ? 'active' : ''}`;
  54. return (
  55. <button className={btnClassName} onClick={handleChange}>
  56. <span>{children}</span>
  57. </button>
  58. );
  59. };