Skip to content

Commit cb3204f

Browse files
committed
fix(ClipboardCopy): Add string[] type to children
The ClipboardCopy component already handles string[] but didn't have it written in the props as valid.
1 parent d6fa6b4 commit cb3204f

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

packages/react-core/src/components/ClipboardCopy/ClipboardCopy.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export interface ClipboardCopyProps extends Omit<React.HTMLProps<HTMLDivElement>
8989
/** A function that is triggered on changing the text. */
9090
onChange?: (event: React.FormEvent, text?: string) => void;
9191
/** The text which is copied. */
92-
children: string;
92+
children: string | string[];
9393
/** Additional actions for inline clipboard copy. Should be wrapped with ClipboardCopyAction. */
9494
additionalActions?: React.ReactNode;
9595
/** Value to overwrite the randomly generated data-ouia-component-id.*/
@@ -103,7 +103,7 @@ class ClipboardCopy extends React.Component<ClipboardCopyProps, ClipboardCopySta
103103
timer = null as number;
104104
constructor(props: ClipboardCopyProps) {
105105
super(props);
106-
const text = Array.isArray(this.props.children) ? this.props.children.join('') : (this.props.children as string);
106+
const text = Array.isArray(this.props.children) ? this.props.children.join(' ') : (this.props.children as string);
107107
this.state = {
108108
text,
109109
expanded: this.props.isExpanded,
@@ -134,7 +134,9 @@ class ClipboardCopy extends React.Component<ClipboardCopyProps, ClipboardCopySta
134134
// eslint-disable-next-line @typescript-eslint/no-unused-vars
135135
componentDidUpdate = (prevProps: ClipboardCopyProps, prevState: ClipboardCopyState) => {
136136
if (prevProps.children !== this.props.children) {
137-
const newText = this.props.children as string;
137+
const newText = Array.isArray(this.props.children)
138+
? this.props.children.join(' ')
139+
: (this.props.children as string);
138140
this.setState({ text: newText, textWhenExpanded: newText });
139141
}
140142
};

packages/react-core/src/components/ClipboardCopy/__tests__/ClipboardCopy.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,21 @@ test('Does not call onCopy when ClipboardCopyButton is not clicked', async () =>
336336
expect(onCopyMock).not.toHaveBeenCalled();
337337
});
338338

339+
test('Can take array of strings as children', async () => {
340+
const onCopyMock = jest.fn();
341+
const user = userEvent.setup();
342+
const childrenArray = children.split(' ');
343+
344+
// sanity check to ensure we are checking an array with at least 2 elements
345+
expect(childrenArray.length).toBeGreaterThan(1);
346+
347+
render(<ClipboardCopy onCopy={onCopyMock}>{childrenArray}</ClipboardCopy>);
348+
349+
await user.click(screen.getByRole('button', { name: 'Test CCB clicker' }));
350+
351+
expect(onCopyMock).toHaveBeenCalledWith(children);
352+
});
353+
339354
test('Matches snapshot', () => {
340355
const { asFragment } = render(
341356
<ClipboardCopy id="snapshot" ouiaId="snapshot">

packages/react-core/src/components/ClipboardCopy/__tests__/__snapshots__/ClipboardCopy.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ exports[`Matches snapshot 1`] = `
1818
<input
1919
aria-invalid="false"
2020
aria-label="Copyable input"
21-
data-ouia-component-id="OUIA-Generated-TextInputBase-26"
21+
data-ouia-component-id="OUIA-Generated-TextInputBase-27"
2222
data-ouia-component-type="PF6/TextInput"
2323
data-ouia-safe="true"
2424
id="text-input-generated-id"

0 commit comments

Comments
 (0)