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.

ToolboxButtonWithIcon.tsx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import React from 'react';
  2. import { NOTIFY_CLICK_MODE } from '../../../../toolbox/types';
  3. import Icon from '../../../icons/components/Icon';
  4. import Tooltip from '../../../tooltip/components/Tooltip';
  5. interface IProps {
  6. /**
  7. * The id of the element this button icon controls.
  8. */
  9. ariaControls?: string;
  10. /**
  11. * Whether the element popup is expanded.
  12. */
  13. ariaExpanded?: boolean;
  14. /**
  15. * Whether the element has a popup.
  16. */
  17. ariaHasPopup?: boolean;
  18. /**
  19. * Aria label for the Icon.
  20. */
  21. ariaLabel?: string;
  22. /**
  23. * The button's key.
  24. */
  25. buttonKey?: string;
  26. /**
  27. * The decorated component (ToolboxButton).
  28. */
  29. children: React.ReactNode;
  30. /**
  31. * Icon of the button.
  32. */
  33. icon: Function;
  34. /**
  35. * Flag used for disabling the small icon.
  36. */
  37. iconDisabled: boolean;
  38. /**
  39. * The ID of the icon button.
  40. */
  41. iconId: string;
  42. /**
  43. * The tooltip used for the icon.
  44. */
  45. iconTooltip: string;
  46. /**
  47. * Notify mode for `toolbarButtonClicked` event -
  48. * whether to only notify or to also prevent button click routine.
  49. */
  50. notifyMode?: string;
  51. /**
  52. * Click handler for the small icon.
  53. */
  54. onIconClick: Function;
  55. /**
  56. * Keydown handler for icon.
  57. */
  58. onIconKeyDown?: Function;
  59. /**
  60. * Additional styles.
  61. */
  62. styles?: Object;
  63. }
  64. /**
  65. * Displays the `ToolboxButtonWithIcon` component.
  66. *
  67. * @param {Object} props - Component's props.
  68. * @returns {ReactElement}
  69. */
  70. export default function ToolboxButtonWithIcon(props: IProps) {
  71. const {
  72. children,
  73. icon,
  74. iconDisabled,
  75. iconTooltip,
  76. buttonKey,
  77. notifyMode,
  78. onIconClick,
  79. onIconKeyDown,
  80. styles,
  81. ariaLabel,
  82. ariaHasPopup,
  83. ariaControls,
  84. ariaExpanded,
  85. iconId
  86. } = props;
  87. const iconProps: {
  88. ariaControls?: string;
  89. ariaExpanded?: boolean;
  90. containerId?: string;
  91. onClick?: (e?: React.MouseEvent) => void;
  92. onKeyDown?: Function;
  93. role?: string;
  94. tabIndex?: number;
  95. } = {};
  96. let className = '';
  97. if (iconDisabled) {
  98. className
  99. = 'settings-button-small-icon settings-button-small-icon--disabled';
  100. } else {
  101. className = 'settings-button-small-icon';
  102. iconProps.onClick = (e?: React.MouseEvent) => {
  103. if (typeof APP !== 'undefined' && notifyMode) {
  104. APP.API.notifyToolbarButtonClicked(
  105. buttonKey, notifyMode === NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY
  106. );
  107. }
  108. if (notifyMode !== NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY) {
  109. onIconClick(e);
  110. }
  111. };
  112. iconProps.onKeyDown = onIconKeyDown;
  113. iconProps.role = 'button';
  114. iconProps.tabIndex = 0;
  115. iconProps.ariaControls = ariaControls;
  116. iconProps.ariaExpanded = ariaExpanded;
  117. iconProps.containerId = iconId;
  118. }
  119. return (
  120. <div
  121. className = 'settings-button-container'
  122. style = { styles }>
  123. {children}
  124. <div>
  125. <Tooltip
  126. containerClassName = { className }
  127. content = { iconTooltip }
  128. position = 'top'>
  129. <Icon
  130. { ...iconProps }
  131. ariaHasPopup = { ariaHasPopup }
  132. ariaLabel = { ariaLabel }
  133. size = { 16 }
  134. src = { icon } />
  135. </Tooltip>
  136. </div>
  137. </div>
  138. );
  139. }