@@ -25,6 +25,7 @@ import ClickListener from '../../internal/ClickListener';
25
25
import { breakingChangesX , componentsX } from '../../internal/FeatureFlags' ;
26
26
import mergeRefs from '../../tools/mergeRefs' ;
27
27
import { keys , keyCodes , matches as keyDownMatch } from '../../tools/key' ;
28
+ import isRequiredOneOf from '../../prop-types/isRequiredOneOf' ;
28
29
29
30
const { prefix } = settings ;
30
31
@@ -173,11 +174,6 @@ class Tooltip extends Component {
173
174
PropTypes . func ,
174
175
] ) ,
175
176
176
- /**
177
- * The content to put into the trigger UI, except the (default) tooltip icon.
178
- */
179
- triggerText : PropTypes . node ,
180
-
181
177
/**
182
178
* The callback function to optionally render the icon element.
183
179
* It should be a component with React.forwardRef().
@@ -213,15 +209,16 @@ class Tooltip extends Component {
213
209
*/
214
210
iconName : PropTypes . string ,
215
211
216
- /**
217
- * The description of the default tooltip icon, to be put in its SVG 'aria-label' and 'alt' .
218
- */
219
- iconDescription : PropTypes . string ,
220
-
221
- /**
222
- * The title of the default tooltip icon, to be put in its SVG `<title>` element.
223
- */
224
- iconTitle : PropTypes . string ,
212
+ ...isRequiredOneOf ( {
213
+ /**
214
+ * The content to put into the trigger UI, except the (default) tooltip icon.
215
+ */
216
+ triggerText : PropTypes . node ,
217
+ /**
218
+ * The description of the default tooltip icon, to be put in its SVG 'aria-label' and 'alt' .
219
+ */
220
+ iconDescription : PropTypes . string ,
221
+ } ) ,
225
222
226
223
/**
227
224
* `true` if opening tooltip should be triggered by clicking the trigger button.
@@ -239,9 +236,7 @@ class Tooltip extends Component {
239
236
direction : DIRECTION_BOTTOM ,
240
237
renderIcon : ! componentsX ? undefined : Information ,
241
238
showIcon : true ,
242
- iconDescription : 'tooltip' ,
243
- iconTitle : '' ,
244
- triggerText : 'Provide triggerText' ,
239
+ triggerText : null ,
245
240
menuOffset : getMenuOffset ,
246
241
clickToOpen : breakingChangesX ,
247
242
} ;
@@ -422,7 +417,6 @@ class Tooltip extends Component {
422
417
showIcon,
423
418
icon,
424
419
iconName,
425
- iconTitle,
426
420
iconDescription,
427
421
renderIcon : IconCustomElement ,
428
422
menuOffset,
@@ -463,83 +457,61 @@ class Tooltip extends Component {
463
457
`${ prefix } --tooltip__label` ,
464
458
triggerClassName
465
459
) ;
466
- const ariaOwnsProps = ! open
467
- ? { }
468
- : {
469
- 'aria-owns' : tooltipId ,
470
- } ;
471
460
472
- const ariaDescribedbyProps = ! open
473
- ? { }
474
- : {
475
- 'aria-describedby' : tooltipId ,
476
- } ;
477
- const finalIcon = IconCustomElement ? (
478
- < IconCustomElement
479
- name = { iconName }
480
- aria-labelledby = { triggerId }
481
- aria-label = { iconDescription }
482
- ref = { mergeRefs ( ref , node => {
483
- this . triggerEl = node ;
484
- } ) }
485
- />
486
- ) : (
487
- < Icon
488
- icon = { ! icon && ! iconName ? iconInfoGlyph : icon }
489
- name = { iconName }
490
- description = { iconDescription }
491
- iconTitle = { iconTitle }
492
- iconRef = { mergeRefs ( ref , node => {
493
- this . triggerEl = node ;
494
- } ) }
495
- />
496
- ) ;
461
+ const refProp = mergeRefs ( ref , node => {
462
+ this . triggerEl = node ;
463
+ } ) ;
464
+
465
+ const iconProperties = { name : iconName , role : null , description : null } ;
466
+
467
+ const properties = {
468
+ role : 'button' ,
469
+ tabIndex : tabIndex ,
470
+ onClick : this . handleMouse ,
471
+ onKeyDown : this . handleKeyPress ,
472
+ onMouseOver : this . handleMouse ,
473
+ onMouseOut : this . handleMouse ,
474
+ onFocus : this . handleMouse ,
475
+ onBlur : this . handleMouse ,
476
+ 'aria-haspopup' : 'true' ,
477
+ 'aria-expanded' : open ,
478
+ // if the user provides property `triggerText`,
479
+ // then the button should use aria-describedby to point to its id,
480
+ // if the user doesn't provide property `triggerText`,
481
+ // then an aria-label will be provided via the `iconDescription` property.
482
+ ...( triggerText
483
+ ? {
484
+ 'aria-describedby' : triggerId ,
485
+ }
486
+ : {
487
+ 'aria-label' : iconDescription ,
488
+ } ) ,
489
+ } ;
497
490
498
491
return (
499
492
< >
500
493
< ClickListener onClickOutside = { this . handleClickOutside } >
501
494
{ showIcon ? (
502
- < div className = { triggerClasses } >
495
+ < div id = { triggerId } className = { triggerClasses } >
503
496
{ triggerText }
504
- < div
505
- role = "button"
506
- id = { triggerId }
507
- className = { `${ prefix } --tooltip__trigger` }
508
- tabIndex = { tabIndex }
509
- title = { iconTitle }
510
- onClick = { this . handleMouse }
511
- onKeyDown = { this . handleKeyPress }
512
- onMouseOver = { this . handleMouse }
513
- onMouseOut = { this . handleMouse }
514
- onFocus = { this . handleMouse }
515
- onBlur = { this . handleMouse }
516
- aria-haspopup = "true"
517
- aria-label = { iconDescription }
518
- aria-expanded = { open }
519
- { ...ariaDescribedbyProps }
520
- { ...ariaOwnsProps } >
521
- { finalIcon }
497
+ < div className = { `${ prefix } --tooltip__trigger` } { ...properties } >
498
+ { IconCustomElement ? (
499
+ < IconCustomElement ref = { refProp } { ...iconProperties } />
500
+ ) : (
501
+ < Icon
502
+ icon = { ! icon && ! iconName ? iconInfoGlyph : icon }
503
+ iconRef = { refProp }
504
+ { ...iconProperties }
505
+ />
506
+ ) }
522
507
</ div >
523
508
</ div >
524
509
) : (
525
510
< div
526
- role = "button"
527
- tabIndex = { tabIndex }
528
511
id = { triggerId }
529
512
className = { triggerClasses }
530
- ref = { mergeRefs ( ref , node => {
531
- this . triggerEl = node ;
532
- } ) }
533
- onClick = { this . handleMouse }
534
- onKeyDown = { this . handleKeyPress }
535
- onMouseOver = { this . handleMouse }
536
- onMouseOut = { this . handleMouse }
537
- onFocus = { this . handleMouse }
538
- onBlur = { this . handleMouse }
539
- aria-haspopup = "true"
540
- aria-expanded = { open }
541
- { ...ariaDescribedbyProps }
542
- { ...ariaOwnsProps } >
513
+ ref = { refProp }
514
+ { ...properties } >
543
515
{ triggerText }
544
516
</ div >
545
517
) }
0 commit comments