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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* @flow */
  2. import React, { 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 name of the Icon of this {@code AbstractToolbarButton}.
  15. */
  16. iconName: string,
  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. * The color underlaying the button.
  31. */
  32. underlayColor?: any
  33. };
  34. /**
  35. * Abstract (base) class for a button in {@link Toolbar}.
  36. *
  37. * @abstract
  38. */
  39. export default class AbstractToolbarButton<P: Props> extends Component<P> {
  40. /**
  41. * Initializes a new {@code AbstractToolbarButton} instance.
  42. *
  43. * @param {Object} props - The React {@code Component} props to initialize
  44. * the new {@code AbstractToolbarButton} instance with.
  45. */
  46. constructor(props: P) {
  47. super(props);
  48. // Bind event handlers so they are only bound once per instance.
  49. this._onClick = this._onClick.bind(this);
  50. }
  51. _onClick: (any) => any;
  52. /**
  53. * Handles clicking/pressing this {@code AbstractToolbarButton} by
  54. * forwarding the event to the {@code onClick} prop of this instance if any.
  55. *
  56. * @protected
  57. * @returns {*} The result returned by the invocation of the {@code onClick}
  58. * prop of this instance if any.
  59. */
  60. _onClick(...args) {
  61. const { onClick } = this.props;
  62. return onClick && onClick(...args);
  63. }
  64. /**
  65. * Implements React's {@link Component#render()}.
  66. *
  67. * @inheritdoc
  68. * @returns {ReactElement}
  69. */
  70. render() {
  71. return this._renderButton(this._renderIcon());
  72. }
  73. _renderButton: (React$Element<*> | null) => React$Element<*>;
  74. /**
  75. * Renders the icon of this {@code AbstractToolbarButton}.
  76. *
  77. * @param {string|ReactClass} type - The React Component type of the icon to
  78. * be rendered.
  79. * @protected
  80. * @returns {ReactElement|null} The icon of this
  81. * {@code AbstractToolbarButton}.
  82. */
  83. _renderIcon(type) {
  84. if (!type) {
  85. return null;
  86. }
  87. const props = {};
  88. 'iconName' in this.props && (props.name = this.props.iconName);
  89. 'iconStyle' in this.props && (props.style = this.props.iconStyle);
  90. return React.createElement(type, props);
  91. }
  92. }