@@ -5,65 +5,81 @@ import ChipButton from './ChipButton';
5
5
import { Tooltip , TooltipPosition } from '../Tooltip' ;
6
6
import { TimesCircleIcon } from '@patternfly/react-icons' ;
7
7
import styles from '@patternfly/patternfly-next/components/Chip/chip.css' ;
8
- import { getUniqueId } from '../../internal/util ' ;
8
+ import GenerateId from '../../internal/GenerateId/GenerateId ' ;
9
9
10
- export const ChipVariant = {
11
- overflow : 'overflow' ,
12
- closable : 'closable'
13
- } ;
14
-
15
- const Chip = ( { variant, onClick, children, id, position, className, ...props } ) => {
16
- const idChip = id || getUniqueId ( )
17
- switch ( variant ) {
18
- case ChipVariant . overflow :
19
- return (
20
- < div className = { css ( styles . chip , styles . modifiers . overflow , className ) } { ...props } >
21
- < ChipButton onClick = { onClick } aria-label = "Expand chip" >
22
- < span className = { css ( styles . chipText ) } > { children } </ span >
23
- </ ChipButton >
24
- </ div >
25
- ) ;
26
- default :
27
- const ChipComponent = (
28
- < div className = { css ( styles . chip , className ) } { ...props } >
29
- < span className = { css ( styles . chipText ) } id = { idChip } >
30
- { children }
31
- </ span >
32
- < ChipButton onClick = { onClick } aria-label = "Remove" id = { `remove_${ idChip } ` } aria-labelledby = { `remove_${ idChip } ${ idChip } ` } >
33
- < TimesCircleIcon aria-hidden = "true" />
34
- </ ChipButton >
35
- </ div >
36
- ) ;
37
- return children . length < 16 ? (
38
- ChipComponent
39
- ) : (
10
+ class Chip extends React . Component {
11
+ renderOverflowChip = ( ) => {
12
+ const { children, className, onClick } = this . props ;
13
+ return (
14
+ < div className = { css ( styles . chip , styles . modifiers . overflow , className ) } >
15
+ < ChipButton onClick = { onClick } aria-label = "Expand Chip" >
16
+ < span className = { css ( styles . chipText ) } > { children } </ span >
17
+ </ ChipButton >
18
+ </ div >
19
+ ) ;
20
+ }
21
+ renderChip = ( randomId ) => {
22
+ const { children, position, className, onClick, minTooltipChars } = this . props ;
23
+ const ChipComponent = (
24
+ < div className = { css ( styles . chip , className ) } >
25
+ < span className = { css ( styles . chipText ) } id = { randomId } >
26
+ { children }
27
+ </ span >
28
+ < ChipButton onClick = { onClick } aria-label = "Remove" id = { `remove_${ randomId } ` } aria-labelledby = { `remove_${ randomId } ${ randomId } ` } >
29
+ < TimesCircleIcon aria-hidden = "true" />
30
+ </ ChipButton >
31
+ </ div >
32
+ ) ;
33
+ return children . length < minTooltipChars ? (
34
+ ChipComponent
35
+ ) : (
40
36
< Tooltip position = { position } content = { children } >
41
37
{ ChipComponent }
42
38
</ Tooltip >
43
39
) ;
40
+
44
41
}
45
- } ;
46
42
43
+ render ( ) {
44
+ const {
45
+ isOverflow,
46
+ } = this . props ;
47
+ return (
48
+ < GenerateId >
49
+ { ( randomId ) =>
50
+ (
51
+ < React . Fragment >
52
+ { isOverflow ? this . renderOverflowChip ( ) : this . renderChip ( randomId ) }
53
+ </ React . Fragment >
54
+ )
55
+ }
56
+ </ GenerateId >
57
+ )
58
+ }
59
+ }
47
60
Chip . propTypes = {
48
61
/** Content rendered inside the chip text */
49
62
children : PropTypes . string . isRequired ,
50
63
/** ID of the chip */
51
64
id : PropTypes . string ,
52
65
/** Additional classes added to the chip item */
53
66
className : PropTypes . string ,
54
- /** Builds the chip structure according to variant */
55
- variant : PropTypes . oneOf ( Object . values ( ChipVariant ) ) ,
67
+ /** Flag indicating if the chip has overflow */
68
+ isOverflow : PropTypes . bool ,
56
69
/** Position of the tooltip which is displayed if text is longer */
57
70
position : PropTypes . oneOf ( Object . values ( TooltipPosition ) ) ,
58
71
/** Function that is called when clicking on the chip button */
59
- onClick : PropTypes . func
72
+ onClick : PropTypes . func ,
73
+ /** Min character limit before showing tooltip. */
74
+ minTooltipChars : PropTypes . number
60
75
} ;
61
76
62
77
Chip . defaultProps = {
63
78
id : undefined ,
64
79
className : '' ,
65
80
position : 'top' ,
66
- variant : ChipVariant . closable
81
+ isOverflow : false ,
82
+ minTooltipChars : 16
67
83
} ;
68
84
69
85
export default Chip ;
0 commit comments