forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChip.tsx
More file actions
201 lines (189 loc) · 6.09 KB
/
Chip.tsx
File metadata and controls
201 lines (189 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import * as React from 'react';
import { css } from '@patternfly/react-styles';
import { Button } from '../Button';
import { Tooltip, TooltipPosition } from '../Tooltip';
import TimesIcon from '@patternfly/react-icons/dist/esm/icons/times-icon';
import styles from '@patternfly/react-styles/css/components/Chip/chip';
import { GenerateId } from '../../helpers/GenerateId/GenerateId';
import { getOUIAProps, OUIAProps, getDefaultOUIAId } from '../../helpers';
export interface ChipProps extends React.HTMLProps<HTMLDivElement>, OUIAProps {
/** Badge to add to the chip. The badge will be rendered after the chip text. */
badge?: React.ReactNode;
/** Content rendered inside the chip text */
children?: React.ReactNode;
/** Aria Label for close button */
closeBtnAriaLabel?: string;
/** Additional classes added to the chip item */
className?: string;
/** Flag indicating if the chip is an overflow chip */
isOverflowChip?: boolean;
/** Flag indicating if chip is read only */
isReadOnly?: boolean;
/** Function that is called when clicking on the chip close button */
onClick?: (event: React.MouseEvent) => void;
/** Component that will be used for chip. It is recommended that <button> or <li> are used when the chip is an overflow chip. */
component?: React.ReactNode;
/** Position of the tooltip which is displayed if text is longer */
tooltipPosition?:
| TooltipPosition
| 'auto'
| 'top'
| 'bottom'
| 'left'
| 'right'
| 'top-start'
| 'top-end'
| 'bottom-start'
| 'bottom-end'
| 'left-start'
| 'left-end'
| 'right-start'
| 'right-end';
/** Css property expressed in percentage or any css unit that overrides the default value of the max-width of the chip's text */
textMaxWidth?: string;
/** Value to overwrite the randomly generated data-ouia-component-id.*/
ouiaId?: number | string;
}
interface ChipState {
isTooltipVisible: boolean;
ouiaStateId: string;
}
class Chip extends React.Component<ChipProps, ChipState> {
static displayName = 'Chip';
constructor(props: ChipProps) {
super(props);
this.state = {
isTooltipVisible: false,
ouiaStateId: getDefaultOUIAId(Chip.displayName)
};
}
span = React.createRef<HTMLSpanElement>();
static defaultProps: ChipProps = {
closeBtnAriaLabel: 'close',
className: '',
isOverflowChip: false,
isReadOnly: false,
tooltipPosition: 'top' as 'auto' | 'top' | 'bottom' | 'left' | 'right',
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onClick: (_e: React.MouseEvent) => undefined as any,
component: 'div' as React.ReactNode
};
componentDidMount() {
this.setState({
isTooltipVisible: Boolean(this.span.current && this.span.current.offsetWidth < this.span.current.scrollWidth)
});
}
setChipStyle = () => ({
'--pf-v5-c-chip__text--MaxWidth': this.props.textMaxWidth
});
renderOverflowChip = () => {
const {
badge,
children,
className,
onClick,
ouiaId,
textMaxWidth,
style,
component,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
tooltipPosition,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
isOverflowChip,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
closeBtnAriaLabel,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
isReadOnly,
...props
} = this.props;
const Component = component as any;
return (
<Component
onClick={onClick}
{...(textMaxWidth && {
style: this.setChipStyle(),
...(style as any)
})}
className={css(styles.chip, styles.modifiers.overflow, className)}
{...(component === 'button' ? { type: 'button' } : {})}
{...getOUIAProps('OverflowChip', ouiaId !== undefined ? ouiaId : this.state.ouiaStateId)}
{...props}
>
<span className={css(styles.chipContent)}>
<span className={css(styles.chipText)}>{children}</span>
{badge && badge}
</span>
</Component>
);
};
renderInnerChip(id: string) {
const {
badge,
children,
className,
onClick,
closeBtnAriaLabel,
isReadOnly,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
isOverflowChip,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
tooltipPosition,
component,
ouiaId,
...props
} = this.props;
const Component = component as any;
return (
<Component
{...(this.props.textMaxWidth && {
style: this.setChipStyle()
})}
className={css(styles.chip, className)}
{...(this.state.isTooltipVisible && { tabIndex: 0 })}
{...getOUIAProps(Chip.displayName, ouiaId !== undefined ? ouiaId : this.state.ouiaStateId)}
{...props}
>
<span className={css(styles.chipContent)}>
<span ref={this.span} className={css(styles.chipText)} id={id}>
{children}
</span>
{badge && badge}
</span>
{!isReadOnly && (
<span className={css(styles.chipActions)}>
<Button
onClick={onClick}
variant="plain"
aria-label={closeBtnAriaLabel}
id={`remove_${id}`}
aria-labelledby={`remove_${id} ${id}`}
ouiaId={ouiaId || closeBtnAriaLabel}
>
<TimesIcon aria-hidden="true" />
</Button>
</span>
)}
</Component>
);
}
renderChip = (randomId: string) => {
const { children, tooltipPosition } = this.props;
if (this.state.isTooltipVisible) {
return (
<Tooltip position={tooltipPosition} content={children}>
{this.renderInnerChip(randomId)}
</Tooltip>
);
}
return this.renderInnerChip(randomId);
};
render() {
const { isOverflowChip } = this.props;
return (
<GenerateId>
{(randomId) => (isOverflowChip ? this.renderOverflowChip() : this.renderChip((this.props.id || randomId) as string))}
</GenerateId>
);
}
}
export { Chip };