1
1
import cx from 'clsx'
2
2
import _ from 'lodash'
3
3
import PropTypes from 'prop-types'
4
- import React , { Component } from 'react'
4
+ import React from 'react'
5
5
6
6
import {
7
7
childrenUtils ,
@@ -20,83 +20,82 @@ import Label from '../../elements/Label'
20
20
/**
21
21
* An item sub-component for Dropdown component.
22
22
*/
23
- class DropdownItem extends Component {
24
- handleClick = ( e ) => {
25
- _ . invoke ( this . props , 'onClick' , e , this . props )
23
+ const DropdownItem = React . forwardRef ( function ( props , ref ) {
24
+ const {
25
+ active,
26
+ children,
27
+ className,
28
+ content,
29
+ disabled,
30
+ description,
31
+ flag,
32
+ icon,
33
+ image,
34
+ label,
35
+ selected,
36
+ text,
37
+ } = props
38
+
39
+ const handleClick = ( e ) => {
40
+ _ . invoke ( props , 'onClick' , e , props )
26
41
}
27
42
28
- render ( ) {
29
- const {
30
- active,
31
- children,
32
- className,
33
- content,
34
- disabled,
35
- description,
36
- flag,
37
- icon,
38
- image,
39
- label,
40
- selected,
41
- text,
42
- } = this . props
43
-
44
- const classes = cx (
45
- useKeyOnly ( active , 'active' ) ,
46
- useKeyOnly ( disabled , 'disabled' ) ,
47
- useKeyOnly ( selected , 'selected' ) ,
48
- 'item' ,
49
- className ,
50
- )
51
- // add default dropdown icon if item contains another menu
52
- const iconName = _ . isNil ( icon )
53
- ? childrenUtils . someByType ( children , 'DropdownMenu' ) && 'dropdown'
54
- : icon
55
- const rest = getUnhandledProps ( DropdownItem , this . props )
56
- const ElementType = getElementType ( DropdownItem , this . props )
57
- const ariaOptions = {
58
- role : 'option' ,
59
- 'aria-disabled' : disabled ,
60
- 'aria-checked' : active ,
61
- 'aria-selected' : selected ,
62
- }
63
-
64
- if ( ! childrenUtils . isNil ( children ) ) {
65
- return (
66
- < ElementType { ...rest } { ...ariaOptions } className = { classes } onClick = { this . handleClick } >
67
- { children }
68
- </ ElementType >
69
- )
70
- }
71
-
72
- const flagElement = Flag . create ( flag , { autoGenerateKey : false } )
73
- const iconElement = Icon . create ( iconName , { autoGenerateKey : false } )
74
- const imageElement = Image . create ( image , { autoGenerateKey : false } )
75
- const labelElement = Label . create ( label , { autoGenerateKey : false } )
76
- const descriptionElement = createShorthand ( 'span' , ( val ) => ( { children : val } ) , description , {
77
- defaultProps : { className : 'description' } ,
78
- autoGenerateKey : false ,
79
- } )
80
- const textElement = createShorthand (
81
- 'span' ,
82
- ( val ) => ( { children : val } ) ,
83
- childrenUtils . isNil ( content ) ? text : content ,
84
- { defaultProps : { className : 'text' } , autoGenerateKey : false } ,
85
- )
43
+ const classes = cx (
44
+ useKeyOnly ( active , 'active' ) ,
45
+ useKeyOnly ( disabled , 'disabled' ) ,
46
+ useKeyOnly ( selected , 'selected' ) ,
47
+ 'item' ,
48
+ className ,
49
+ )
50
+ // add default dropdown icon if item contains another menu
51
+ const iconName = _ . isNil ( icon )
52
+ ? childrenUtils . someByType ( children , 'DropdownMenu' ) && 'dropdown'
53
+ : icon
54
+ const rest = getUnhandledProps ( DropdownItem , props )
55
+ const ElementType = getElementType ( DropdownItem , props )
56
+ const ariaOptions = {
57
+ role : 'option' ,
58
+ 'aria-disabled' : disabled ,
59
+ 'aria-checked' : active ,
60
+ 'aria-selected' : selected ,
61
+ }
86
62
63
+ if ( ! childrenUtils . isNil ( children ) ) {
87
64
return (
88
- < ElementType { ...rest } { ...ariaOptions } className = { classes } onClick = { this . handleClick } >
89
- { imageElement }
90
- { iconElement }
91
- { flagElement }
92
- { labelElement }
93
- { descriptionElement }
94
- { textElement }
65
+ < ElementType { ...rest } { ...ariaOptions } className = { classes } onClick = { handleClick } ref = { ref } >
66
+ { children }
95
67
</ ElementType >
96
68
)
97
69
}
98
- }
99
70
71
+ const flagElement = Flag . create ( flag , { autoGenerateKey : false } )
72
+ const iconElement = Icon . create ( iconName , { autoGenerateKey : false } )
73
+ const imageElement = Image . create ( image , { autoGenerateKey : false } )
74
+ const labelElement = Label . create ( label , { autoGenerateKey : false } )
75
+ const descriptionElement = createShorthand ( 'span' , ( val ) => ( { children : val } ) , description , {
76
+ defaultProps : { className : 'description' } ,
77
+ autoGenerateKey : false ,
78
+ } )
79
+ const textElement = createShorthand (
80
+ 'span' ,
81
+ ( val ) => ( { children : val } ) ,
82
+ childrenUtils . isNil ( content ) ? text : content ,
83
+ { defaultProps : { className : 'text' } , autoGenerateKey : false } ,
84
+ )
85
+
86
+ return (
87
+ < ElementType { ...rest } { ...ariaOptions } className = { classes } onClick = { handleClick } ref = { ref } >
88
+ { imageElement }
89
+ { iconElement }
90
+ { flagElement }
91
+ { labelElement }
92
+ { descriptionElement }
93
+ { textElement }
94
+ </ ElementType >
95
+ )
96
+ } )
97
+
98
+ DropdownItem . displayName = 'DropdownItem'
100
99
DropdownItem . propTypes = {
101
100
/** An element type to render as (string or function). */
102
101
as : PropTypes . elementType ,
0 commit comments