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.

StatelessToolbarButton.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* @flow */
  2. import React from 'react';
  3. import AbstractToolbarButton from './AbstractToolbarButton';
  4. type MapOfAttributes = { [key: string]: * };
  5. /* eslint-disable flowtype/space-before-type-colon */
  6. /**
  7. * Takes toolbar button props and maps them to HTML attributes to set.
  8. *
  9. * @param {Object} props - Props set to the React component.
  10. * @returns {MapOfAttributes}
  11. */
  12. function getButtonAttributesByProps(props: Object = {})
  13. : MapOfAttributes {
  14. // XXX Make sure to not modify props.classNames because that'd be bad
  15. // practice.
  16. const classNames = (props.classNames && [ ...props.classNames ]) || [];
  17. props.toggled && classNames.push('toggled');
  18. props.unclickable && classNames.push('unclickable');
  19. const result: MapOfAttributes = {
  20. className: classNames.join(' '),
  21. 'data-container': 'body',
  22. 'data-placement': 'bottom',
  23. id: props.id
  24. };
  25. if (!props.enabled) {
  26. result.disabled = 'disabled';
  27. }
  28. if (props.hidden) {
  29. result.style = { display: 'none' };
  30. }
  31. if (props.tooltipText) {
  32. result.content = props.tooltipText;
  33. }
  34. return result;
  35. }
  36. /* eslint-enable flowtype/space-before-type-colon */
  37. /**
  38. * Represents a button in Toolbar on React.
  39. *
  40. * @class ToolbarButton
  41. * @extends AbstractToolbarButton
  42. */
  43. export default class StatelessToolbarButton extends AbstractToolbarButton {
  44. _onClick: Function;
  45. /**
  46. * Toolbar button component's property types.
  47. *
  48. * @static
  49. */
  50. static propTypes = {
  51. ...AbstractToolbarButton.propTypes,
  52. /**
  53. * Object describing button.
  54. */
  55. button: React.PropTypes.object.isRequired,
  56. /**
  57. * Handler for button's reference.
  58. */
  59. createRefToButton: React.PropTypes.func
  60. };
  61. /**
  62. * Initializes new ToolbarButton instance.
  63. *
  64. * @param {Object} props - The read-only properties with which the new
  65. * instance is to be initialized.
  66. */
  67. constructor(props: Object) {
  68. super(props);
  69. // Bind methods to save the context
  70. this._onClick = this._onClick.bind(this);
  71. }
  72. /**
  73. * Implements React's {@link Component#render()}.
  74. *
  75. * @inheritdoc
  76. * @returns {ReactElement}
  77. */
  78. render(): ReactElement<*> {
  79. const { button } = this.props;
  80. const attributes = getButtonAttributesByProps(button);
  81. return (
  82. <a
  83. { ...attributes }
  84. onClick = { this._onClick }
  85. ref = { this.props.createRefToButton }>
  86. { this._renderInnerElementsIfRequired() }
  87. </a>
  88. );
  89. }
  90. /**
  91. * Wrapper on on click handler props for current button.
  92. *
  93. * @param {Event} event - Click event object.
  94. * @returns {void}
  95. * @private
  96. */
  97. _onClick(event: Event): void {
  98. const {
  99. button,
  100. onClick
  101. } = this.props;
  102. const {
  103. enabled,
  104. unclickable
  105. } = button;
  106. if (enabled && !unclickable && onClick) {
  107. onClick(event);
  108. }
  109. }
  110. /**
  111. * If toolbar button should contain children elements
  112. * renders them.
  113. *
  114. * @returns {ReactElement|null}
  115. * @private
  116. */
  117. _renderInnerElementsIfRequired(): ReactElement<*> | null {
  118. if (this.props.button.html) {
  119. return this.props.button.html;
  120. }
  121. return null;
  122. }
  123. }