You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractToolbarButton.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* @flow */
  2. import { Component } from 'react';
  3. /**
  4. * The type of the React {@code Component} props of
  5. * {@link AbstractToolbarButton}.
  6. */
  7. export type Props = {
  8. /**
  9. * A succinct description of what the button does. Used by accessibility
  10. * tools and torture tests.
  11. */
  12. accessibilityLabel: string,
  13. /**
  14. * The Icon of this {@code AbstractToolbarButton}.
  15. */
  16. icon: Object,
  17. /**
  18. * The style of the Icon of this {@code AbstractToolbarButton}.
  19. */
  20. iconStyle?: Object,
  21. /**
  22. * On click handler.
  23. */
  24. onClick: Function,
  25. /**
  26. * {@code AbstractToolbarButton} styles.
  27. */
  28. style?: Array<string> | Object,
  29. /**
  30. * An optional modifier to render the button toggled.
  31. */
  32. toggled?: boolean,
  33. /**
  34. * The color underlaying the button.
  35. */
  36. underlayColor?: any
  37. };
  38. /**
  39. * Abstract (base) class for a button in {@link Toolbar}.
  40. *
  41. * @abstract
  42. */
  43. export default class AbstractToolbarButton<P: Props> extends Component<P> {
  44. /**
  45. * Initializes a new {@code AbstractToolbarButton} instance.
  46. *
  47. * @param {Object} props - The React {@code Component} props to initialize
  48. * the new {@code AbstractToolbarButton} instance with.
  49. */
  50. constructor(props: P) {
  51. super(props);
  52. // Bind event handlers so they are only bound once per instance.
  53. this._onClick = this._onClick.bind(this);
  54. }
  55. _onClick: (any) => any;
  56. /**
  57. * Handles clicking/pressing this {@code AbstractToolbarButton} by
  58. * forwarding the event to the {@code onClick} prop of this instance if any.
  59. *
  60. * @protected
  61. * @returns {*} The result returned by the invocation of the {@code onClick}
  62. * prop of this instance if any.
  63. */
  64. _onClick(...args) {
  65. const { onClick } = this.props;
  66. return onClick && onClick(...args);
  67. }
  68. /**
  69. * Implements React's {@link Component#render()}.
  70. *
  71. * @inheritdoc
  72. * @returns {ReactElement}
  73. */
  74. render() {
  75. return this._renderButton(this._renderIcon());
  76. }
  77. _renderButton: (React$Element<any> | null) => React$Element<any>;
  78. _renderIcon: () => React$Element<any> | null;
  79. }