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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. { this._renderChildComponentIfRequired() }
  88. </a>
  89. );
  90. }
  91. /**
  92. * Wrapper on on click handler props for current button.
  93. *
  94. * @param {Event} event - Click event object.
  95. * @returns {void}
  96. * @private
  97. */
  98. _onClick(event: Event): void {
  99. const {
  100. button,
  101. onClick
  102. } = this.props;
  103. const {
  104. enabled,
  105. unclickable
  106. } = button;
  107. if (enabled && !unclickable && onClick) {
  108. onClick(event);
  109. }
  110. }
  111. /**
  112. * Render any configured child component for the toolbar button.
  113. *
  114. * @returns {ReactElement|null}
  115. * @private
  116. */
  117. _renderChildComponentIfRequired(): ReactElement<*> | null {
  118. if (this.props.button.childComponent) {
  119. const Child = this.props.button.childComponent;
  120. return <Child />;
  121. }
  122. return null;
  123. }
  124. /**
  125. * If toolbar button should contain children elements
  126. * renders them.
  127. *
  128. * @returns {ReactElement|null}
  129. * @private
  130. */
  131. _renderInnerElementsIfRequired(): ReactElement<*> | null {
  132. if (this.props.button.html) {
  133. return this.props.button.html;
  134. }
  135. return null;
  136. }
  137. }