Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ToolboxButtonWithPopup.tsx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import React from 'react';
  2. import Icon from '../../../icons/components/Icon';
  3. import Popover from '../../../popover/components/Popover.web';
  4. interface IProps {
  5. /**
  6. * The id of the element this button icon controls.
  7. */
  8. ariaControls?: string;
  9. /**
  10. * Whether the element popup is expanded.
  11. */
  12. ariaExpanded?: boolean;
  13. /**
  14. * Whether the element has a popup.
  15. */
  16. ariaHasPopup?: boolean;
  17. /**
  18. * Aria label for the Icon.
  19. */
  20. ariaLabel?: string;
  21. /**
  22. * The decorated component (ToolboxButton).
  23. */
  24. children: React.ReactNode;
  25. /**
  26. * Icon of the button.
  27. */
  28. icon?: Function;
  29. /**
  30. * Flag used for disabling the small icon.
  31. */
  32. iconDisabled?: boolean;
  33. /**
  34. * The ID of the icon button.
  35. */
  36. iconId?: string;
  37. /**
  38. * Popover close callback.
  39. */
  40. onPopoverClose: Function;
  41. /**
  42. * Popover open callback.
  43. */
  44. onPopoverOpen: Function;
  45. /**
  46. * The content that will be displayed inside the popover.
  47. */
  48. popoverContent: React.ReactNode;
  49. /**
  50. * Additional styles.
  51. */
  52. styles?: Object;
  53. /**
  54. * Whether the trigger for open/ close should be click or hover.
  55. */
  56. trigger?: 'hover' | 'click';
  57. /**
  58. * Whether or not the popover is visible.
  59. */
  60. visible: boolean;
  61. }
  62. /**
  63. * Displays the `ToolboxButtonWithIcon` component.
  64. *
  65. * @param {Object} props - Component's props.
  66. * @returns {ReactElement}
  67. */
  68. export default function ToolboxButtonWithPopup(props: IProps) {
  69. const {
  70. ariaControls,
  71. ariaExpanded,
  72. ariaHasPopup,
  73. ariaLabel,
  74. children,
  75. icon,
  76. iconDisabled,
  77. iconId,
  78. onPopoverClose,
  79. onPopoverOpen,
  80. popoverContent,
  81. styles,
  82. trigger,
  83. visible
  84. } = props;
  85. if (!icon) {
  86. return (
  87. <div
  88. className = 'settings-button-container'
  89. style = { styles }>
  90. <Popover
  91. content = { popoverContent }
  92. headingLabel = { ariaLabel }
  93. onPopoverClose = { onPopoverClose }
  94. onPopoverOpen = { onPopoverOpen }
  95. position = 'top'
  96. trigger = { trigger }
  97. visible = { visible }>
  98. {children}
  99. </Popover>
  100. </div>
  101. );
  102. }
  103. const iconProps: {
  104. ariaControls?: string;
  105. ariaExpanded?: boolean;
  106. className?: string;
  107. containerId?: string;
  108. role?: string;
  109. tabIndex?: number;
  110. } = {};
  111. if (iconDisabled) {
  112. iconProps.className
  113. = 'settings-button-small-icon settings-button-small-icon--disabled';
  114. } else {
  115. iconProps.className = 'settings-button-small-icon';
  116. iconProps.role = 'button';
  117. iconProps.tabIndex = 0;
  118. iconProps.ariaControls = ariaControls;
  119. iconProps.ariaExpanded = ariaExpanded;
  120. iconProps.containerId = iconId;
  121. }
  122. return (
  123. <div
  124. className = 'settings-button-container'
  125. style = { styles }>
  126. {children}
  127. <div className = 'settings-button-small-icon-container'>
  128. <Popover
  129. content = { popoverContent }
  130. headingLabel = { ariaLabel }
  131. onPopoverClose = { onPopoverClose }
  132. onPopoverOpen = { onPopoverOpen }
  133. position = 'top'
  134. visible = { visible }>
  135. <Icon
  136. { ...iconProps }
  137. ariaHasPopup = { ariaHasPopup }
  138. ariaLabel = { ariaLabel }
  139. size = { 16 }
  140. src = { icon } />
  141. </Popover>
  142. </div>
  143. </div>
  144. );
  145. }