Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AbstractToolbarButton.ts 2.5KB

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