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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import PropTypes from 'prop-types';
  2. import React, { Component } from 'react';
  3. /**
  4. * Abstract (base) class for a button in {@link Toolbar}.
  5. *
  6. * @abstract
  7. */
  8. export default class AbstractToolbarButton extends Component {
  9. /**
  10. * AbstractToolbarButton component's property types.
  11. *
  12. * @static
  13. */
  14. static propTypes = {
  15. /**
  16. * A succinct description of what the button does. Used by accessibility
  17. * tools and torture tests.
  18. */
  19. accessibilityLabel: PropTypes.string,
  20. /**
  21. * The name of the Icon of this {@code AbstractToolbarButton}.
  22. */
  23. iconName: PropTypes.string,
  24. /**
  25. * The style of the Icon of this {@code AbstractToolbarButton}.
  26. */
  27. iconStyle: PropTypes.object,
  28. /**
  29. * On click handler.
  30. */
  31. onClick: PropTypes.func,
  32. /**
  33. * {@code AbstractToolbarButton} styles.
  34. */
  35. style:
  36. PropTypes.oneOfType([
  37. PropTypes.array,
  38. PropTypes.object
  39. ]),
  40. /**
  41. * The color underlaying the button.
  42. */
  43. underlayColor: PropTypes.any
  44. };
  45. /**
  46. * Initializes a new {@code AbstractToolbarButton} instance.
  47. *
  48. * @param {Object} props - The React {@code Component} props to initialize
  49. * the new {@code AbstractToolbarButton} instance with.
  50. */
  51. constructor(props) {
  52. super(props);
  53. // Bind event handlers so they are only bound once per instance.
  54. this._onClick = this._onClick.bind(this);
  55. }
  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. /**
  78. * Renders the icon of this {@code AbstractToolbarButton}.
  79. *
  80. * @param {string|ReactClass} type - The React Component type of the icon to
  81. * be rendered.
  82. * @protected
  83. * @returns {ReactElement} The icon of this {@code AbstractToolbarButton}.
  84. */
  85. _renderIcon(type) {
  86. const props = {};
  87. 'iconName' in this.props && (props.name = this.props.iconName);
  88. 'iconStyle' in this.props && (props.style = this.props.iconStyle);
  89. return React.createElement(type, props);
  90. }
  91. }