您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

StatelessToolbarButton.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /**
  58. * Initializes new ToolbarButton instance.
  59. *
  60. * @param {Object} props - The read-only properties with which the new
  61. * instance is to be initialized.
  62. */
  63. constructor(props: Object) {
  64. super(props);
  65. // Bind methods to save the context
  66. this._onClick = this._onClick.bind(this);
  67. }
  68. /**
  69. * Implements React's {@link Component#render()}.
  70. *
  71. * @inheritdoc
  72. * @returns {ReactElement}
  73. */
  74. render(): ReactElement<*> {
  75. const { button } = this.props;
  76. const attributes = getButtonAttributesByProps(button);
  77. return (
  78. <a
  79. { ...attributes }
  80. onClick = { this._onClick }>
  81. { this.props.children }
  82. </a>
  83. );
  84. }
  85. /**
  86. * Wrapper on on click handler props for current button.
  87. *
  88. * @param {Event} event - Click event object.
  89. * @returns {void}
  90. * @private
  91. */
  92. _onClick(event: Event): void {
  93. const {
  94. button,
  95. onClick
  96. } = this.props;
  97. const {
  98. enabled,
  99. unclickable
  100. } = button;
  101. if (enabled && !unclickable && onClick) {
  102. onClick(event);
  103. }
  104. }
  105. }