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 ,
@@ -13,6 +13,7 @@ import {
13
13
useKeyOnly ,
14
14
useKeyOrValueAndKey ,
15
15
useValueAndKey ,
16
+ useEventCallback ,
16
17
} from '../../lib'
17
18
import Icon from '../Icon/Icon'
18
19
import Image from '../Image/Image'
@@ -22,100 +23,95 @@ import LabelGroup from './LabelGroup'
22
23
/**
23
24
* A label displays content classification.
24
25
*/
25
- export default class Label extends Component {
26
- handleClick = ( e ) => {
27
- const { onClick } = this . props
28
-
29
- if ( onClick ) onClick ( e , this . props )
30
- }
31
-
32
- handleIconOverrides = ( predefinedProps ) => ( {
33
- onClick : ( e ) => {
34
- _ . invoke ( predefinedProps , 'onClick' , e )
35
- _ . invoke ( this . props , 'onRemove' , e , this . props )
36
- } ,
26
+ const Label = React . forwardRef ( function ( props , ref ) {
27
+ const {
28
+ active,
29
+ attached,
30
+ basic,
31
+ children,
32
+ circular,
33
+ className,
34
+ color,
35
+ content,
36
+ corner,
37
+ detail,
38
+ empty,
39
+ floating,
40
+ horizontal,
41
+ icon,
42
+ image,
43
+ onRemove,
44
+ pointing,
45
+ prompt,
46
+ removeIcon,
47
+ ribbon,
48
+ size,
49
+ tag,
50
+ } = props
51
+
52
+ const pointingClass =
53
+ ( pointing === true && 'pointing' ) ||
54
+ ( ( pointing === 'left' || pointing === 'right' ) && `${ pointing } pointing` ) ||
55
+ ( ( pointing === 'above' || pointing === 'below' ) && `pointing ${ pointing } ` )
56
+
57
+ const classes = cx (
58
+ 'ui' ,
59
+ color ,
60
+ pointingClass ,
61
+ size ,
62
+ useKeyOnly ( active , 'active' ) ,
63
+ useKeyOnly ( basic , 'basic' ) ,
64
+ useKeyOnly ( circular , 'circular' ) ,
65
+ useKeyOnly ( empty , 'empty' ) ,
66
+ useKeyOnly ( floating , 'floating' ) ,
67
+ useKeyOnly ( horizontal , 'horizontal' ) ,
68
+ useKeyOnly ( image === true , 'image' ) ,
69
+ useKeyOnly ( prompt , 'prompt' ) ,
70
+ useKeyOnly ( tag , 'tag' ) ,
71
+ useKeyOrValueAndKey ( corner , 'corner' ) ,
72
+ useKeyOrValueAndKey ( ribbon , 'ribbon' ) ,
73
+ useValueAndKey ( attached , 'attached' ) ,
74
+ 'label' ,
75
+ className ,
76
+ )
77
+ const rest = getUnhandledProps ( Label , props )
78
+ const ElementType = getElementType ( Label , props )
79
+
80
+ const handleClick = useEventCallback ( ( e ) => {
81
+ _ . invoke ( props , 'onClick' , e , props )
37
82
} )
38
83
39
- render ( ) {
40
- const {
41
- active,
42
- attached,
43
- basic,
44
- children,
45
- circular,
46
- className,
47
- color,
48
- content,
49
- corner,
50
- detail,
51
- empty,
52
- floating,
53
- horizontal,
54
- icon,
55
- image,
56
- onRemove,
57
- pointing,
58
- prompt,
59
- removeIcon,
60
- ribbon,
61
- size,
62
- tag,
63
- } = this . props
64
-
65
- const pointingClass =
66
- ( pointing === true && 'pointing' ) ||
67
- ( ( pointing === 'left' || pointing === 'right' ) && `${ pointing } pointing` ) ||
68
- ( ( pointing === 'above' || pointing === 'below' ) && `pointing ${ pointing } ` )
69
-
70
- const classes = cx (
71
- 'ui' ,
72
- color ,
73
- pointingClass ,
74
- size ,
75
- useKeyOnly ( active , 'active' ) ,
76
- useKeyOnly ( basic , 'basic' ) ,
77
- useKeyOnly ( circular , 'circular' ) ,
78
- useKeyOnly ( empty , 'empty' ) ,
79
- useKeyOnly ( floating , 'floating' ) ,
80
- useKeyOnly ( horizontal , 'horizontal' ) ,
81
- useKeyOnly ( image === true , 'image' ) ,
82
- useKeyOnly ( prompt , 'prompt' ) ,
83
- useKeyOnly ( tag , 'tag' ) ,
84
- useKeyOrValueAndKey ( corner , 'corner' ) ,
85
- useKeyOrValueAndKey ( ribbon , 'ribbon' ) ,
86
- useValueAndKey ( attached , 'attached' ) ,
87
- 'label' ,
88
- className ,
89
- )
90
- const rest = getUnhandledProps ( Label , this . props )
91
- const ElementType = getElementType ( Label , this . props )
92
-
93
- if ( ! childrenUtils . isNil ( children ) ) {
94
- return (
95
- < ElementType { ...rest } className = { classes } onClick = { this . handleClick } >
96
- { children }
97
- </ ElementType >
98
- )
99
- }
100
-
101
- const removeIconShorthand = _ . isUndefined ( removeIcon ) ? 'delete' : removeIcon
102
-
84
+ if ( ! childrenUtils . isNil ( children ) ) {
103
85
return (
104
- < ElementType className = { classes } onClick = { this . handleClick } { ...rest } >
105
- { Icon . create ( icon , { autoGenerateKey : false } ) }
106
- { typeof image !== 'boolean' && Image . create ( image , { autoGenerateKey : false } ) }
107
- { content }
108
- { LabelDetail . create ( detail , { autoGenerateKey : false } ) }
109
- { onRemove &&
110
- Icon . create ( removeIconShorthand , {
111
- autoGenerateKey : false ,
112
- overrideProps : this . handleIconOverrides ,
113
- } ) }
86
+ < ElementType { ...rest } className = { classes } onClick = { handleClick } ref = { ref } >
87
+ { children }
114
88
</ ElementType >
115
89
)
116
90
}
117
- }
118
91
92
+ const removeIconShorthand = _ . isUndefined ( removeIcon ) ? 'delete' : removeIcon
93
+
94
+ return (
95
+ < ElementType { ...rest } className = { classes } onClick = { handleClick } ref = { ref } >
96
+ { Icon . create ( icon , { autoGenerateKey : false } ) }
97
+ { typeof image !== 'boolean' && Image . create ( image , { autoGenerateKey : false } ) }
98
+ { content }
99
+ { LabelDetail . create ( detail , { autoGenerateKey : false } ) }
100
+ { onRemove &&
101
+ Icon . create ( removeIconShorthand , {
102
+ autoGenerateKey : false ,
103
+ overrideProps : ( predefinedProps ) => ( {
104
+ onClick : ( e ) => {
105
+ _ . invoke ( predefinedProps , 'onClick' , e )
106
+ _ . invoke ( props , 'onRemove' , e , props )
107
+ } ,
108
+ } ) ,
109
+ } ) }
110
+ </ ElementType >
111
+ )
112
+ } )
113
+
114
+ Label . displayName = 'Label'
119
115
Label . propTypes = {
120
116
/** An element type to render as (string or function). */
121
117
as : PropTypes . elementType ,
@@ -214,3 +210,5 @@ Label.Detail = LabelDetail
214
210
Label . Group = LabelGroup
215
211
216
212
Label . create = createShorthandFactory ( Label , ( value ) => ( { content : value } ) )
213
+
214
+ export default Label
0 commit comments