@@ -4,6 +4,7 @@ import { css } from '@patternfly/react-styles';
44import { PickOptional } from '../../helpers/typeUtils' ;
55import { TooltipPosition } from '../Tooltip' ;
66import { TextInput } from '../TextInput' ;
7+ import { Truncate , TruncateProps } from '../Truncate' ;
78import { GenerateId } from '../../helpers/GenerateId/GenerateId' ;
89import { ClipboardCopyButton } from './ClipboardCopyButton' ;
910import { ClipboardCopyToggle } from './ClipboardCopyToggle' ;
@@ -92,6 +93,8 @@ export interface ClipboardCopyProps extends Omit<React.HTMLProps<HTMLDivElement>
9293 children : string | string [ ] ;
9394 /** Additional actions for inline clipboard copy. Should be wrapped with ClipboardCopyAction. */
9495 additionalActions ?: React . ReactNode ;
96+ /** Enables and customizes truncation for an inline-compact ClipboardCopy. */
97+ truncation ?: boolean | Omit < TruncateProps , 'content' > ;
9598 /** Value to overwrite the randomly generated data-ouia-component-id.*/
9699 ouiaId ?: number | string ;
97100 /** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */
@@ -101,6 +104,7 @@ export interface ClipboardCopyProps extends Omit<React.HTMLProps<HTMLDivElement>
101104class ClipboardCopy extends React . Component < ClipboardCopyProps , ClipboardCopyState > {
102105 static displayName = 'ClipboardCopy' ;
103106 timer = null as number ;
107+ private clipboardRef : React . RefObject < any > ;
104108 constructor ( props : ClipboardCopyProps ) {
105109 super ( props ) ;
106110 const text = Array . isArray ( this . props . children ) ? this . props . children . join ( ' ' ) : ( this . props . children as string ) ;
@@ -110,6 +114,8 @@ class ClipboardCopy extends React.Component<ClipboardCopyProps, ClipboardCopySta
110114 copied : false ,
111115 textWhenExpanded : text
112116 } ;
117+
118+ this . clipboardRef = React . createRef ( ) ;
113119 }
114120
115121 static defaultProps : PickOptional < ClipboardCopyProps > = {
@@ -128,6 +134,7 @@ class ClipboardCopy extends React.Component<ClipboardCopyProps, ClipboardCopySta
128134 textAriaLabel : 'Copyable input' ,
129135 toggleAriaLabel : 'Show content' ,
130136 additionalActions : null ,
137+ truncation : false ,
131138 ouiaSafe : true
132139 } ;
133140
@@ -184,37 +191,59 @@ class ClipboardCopy extends React.Component<ClipboardCopyProps, ClipboardCopySta
184191 position,
185192 className,
186193 additionalActions,
194+ truncation,
187195 ouiaId,
188196 ouiaSafe,
189197 ...divProps
190198 } = this . props ;
191199 const textIdPrefix = 'text-input-' ;
192200 const toggleIdPrefix = 'toggle-' ;
193201 const contentIdPrefix = 'content-' ;
202+
203+ const copyableText = this . state . text ;
204+ const shouldTruncate = variant === ClipboardCopyVariant . inlineCompact && truncation ;
205+
194206 return (
195207 < div
196208 className = { css (
197209 styles . clipboardCopy ,
198- variant === 'inline-compact' && styles . modifiers . inline ,
210+ variant === ClipboardCopyVariant . inlineCompact && styles . modifiers . inline ,
199211 isBlock && styles . modifiers . block ,
200212 this . state . expanded && styles . modifiers . expanded ,
201213 className
202214 ) }
215+ ref = { this . clipboardRef }
203216 { ...divProps }
204217 { ...getOUIAProps ( ClipboardCopy . displayName , ouiaId , ouiaSafe ) }
205218 >
206- { variant === 'inline-compact' && (
219+ { variant === ClipboardCopyVariant . inlineCompact && (
207220 < GenerateId prefix = "" >
208221 { ( id ) => (
209222 < React . Fragment >
210223 { ! isCode && (
211224 < span className = { css ( styles . clipboardCopyText ) } id = { `${ textIdPrefix } ${ id } ` } >
212- { this . state . text }
225+ { shouldTruncate ? (
226+ < Truncate
227+ refToGetParent = { this . clipboardRef }
228+ content = { copyableText }
229+ { ...( typeof truncation === 'object' && truncation ) }
230+ />
231+ ) : (
232+ copyableText
233+ ) }
213234 </ span >
214235 ) }
215236 { isCode && (
216237 < code className = { css ( styles . clipboardCopyText , styles . modifiers . code ) } id = { `${ textIdPrefix } ${ id } ` } >
217- { this . state . text }
238+ { shouldTruncate ? (
239+ < Truncate
240+ refToGetParent = { this . clipboardRef }
241+ content = { copyableText }
242+ { ...( typeof truncation === 'object' && truncation ) }
243+ />
244+ ) : (
245+ copyableText
246+ ) }
218247 </ code >
219248 ) }
220249 < span className = { css ( styles . clipboardCopyActions ) } >
@@ -229,7 +258,7 @@ class ClipboardCopy extends React.Component<ClipboardCopyProps, ClipboardCopySta
229258 textId = { `text-input-${ id } ` }
230259 aria-label = { hoverTip }
231260 onClick = { ( event : any ) => {
232- onCopy ( event , this . state . text ) ;
261+ onCopy ( event , copyableText ) ;
233262 this . setState ( { copied : true } ) ;
234263 } }
235264 onTooltipHidden = { ( ) => this . setState ( { copied : false } ) }
@@ -244,20 +273,20 @@ class ClipboardCopy extends React.Component<ClipboardCopyProps, ClipboardCopySta
244273 ) }
245274 </ GenerateId >
246275 ) }
247- { variant !== 'inline-compact' && (
276+ { variant !== ClipboardCopyVariant . inlineCompact && (
248277 < GenerateId prefix = "" >
249278 { ( id ) => (
250279 < React . Fragment >
251280 < div className = { css ( styles . clipboardCopyGroup ) } >
252- { variant === ' expansion' && (
281+ { variant === ClipboardCopyVariant . expansion && (
253282 < ClipboardCopyToggle
254283 isExpanded = { this . state . expanded }
255284 onClick = { ( _event ) => {
256285 this . expandContent ( _event ) ;
257286 if ( this . state . expanded ) {
258287 this . setState ( { text : this . state . textWhenExpanded } ) ;
259288 } else {
260- this . setState ( { textWhenExpanded : this . state . text } ) ;
289+ this . setState ( { textWhenExpanded : copyableText } ) ;
261290 }
262291 } }
263292 id = { `${ toggleIdPrefix } ${ id } ` }
@@ -269,7 +298,7 @@ class ClipboardCopy extends React.Component<ClipboardCopyProps, ClipboardCopySta
269298 < TextInput
270299 readOnlyVariant = { isReadOnly || this . state . expanded ? 'default' : undefined }
271300 onChange = { this . updateText }
272- value = { this . state . expanded ? this . state . textWhenExpanded : this . state . text }
301+ value = { this . state . expanded ? this . state . textWhenExpanded : copyableText }
273302 id = { `text-input-${ id } ` }
274303 aria-label = { textAriaLabel }
275304 { ...( isCode && { dir : 'ltr' } ) }
@@ -283,7 +312,7 @@ class ClipboardCopy extends React.Component<ClipboardCopyProps, ClipboardCopySta
283312 textId = { `text-input-${ id } ` }
284313 aria-label = { hoverTip }
285314 onClick = { ( event : any ) => {
286- onCopy ( event , this . state . expanded ? this . state . textWhenExpanded : this . state . text ) ;
315+ onCopy ( event , this . state . expanded ? this . state . textWhenExpanded : copyableText ) ;
287316 this . setState ( { copied : true } ) ;
288317 } }
289318 onTooltipHidden = { ( ) => this . setState ( { copied : false } ) }
@@ -298,7 +327,7 @@ class ClipboardCopy extends React.Component<ClipboardCopyProps, ClipboardCopySta
298327 id = { `content-${ id } ` }
299328 onChange = { this . updateTextWhenExpanded }
300329 >
301- { this . state . text }
330+ { copyableText }
302331 </ ClipboardCopyExpanded >
303332 ) }
304333 </ React . Fragment >
0 commit comments