Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ClipboardCopy): Add string[] type to children #11177

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export interface ClipboardCopyProps extends Omit<React.HTMLProps<HTMLDivElement>
/** A function that is triggered on changing the text. */
onChange?: (event: React.FormEvent, text?: string) => void;
/** The text which is copied. */
children: string;
children: string | string[];
/** Additional actions for inline clipboard copy. Should be wrapped with ClipboardCopyAction. */
additionalActions?: React.ReactNode;
/** Value to overwrite the randomly generated data-ouia-component-id.*/
Expand All @@ -103,7 +103,7 @@ class ClipboardCopy extends React.Component<ClipboardCopyProps, ClipboardCopySta
timer = null as number;
constructor(props: ClipboardCopyProps) {
super(props);
const text = Array.isArray(this.props.children) ? this.props.children.join('') : (this.props.children as string);
const text = Array.isArray(this.props.children) ? this.props.children.join(' ') : (this.props.children as string);
this.state = {
text,
expanded: this.props.isExpanded,
Expand Down Expand Up @@ -134,7 +134,9 @@ class ClipboardCopy extends React.Component<ClipboardCopyProps, ClipboardCopySta
// eslint-disable-next-line @typescript-eslint/no-unused-vars
componentDidUpdate = (prevProps: ClipboardCopyProps, prevState: ClipboardCopyState) => {
if (prevProps.children !== this.props.children) {
const newText = this.props.children as string;
const newText = Array.isArray(this.props.children)
? this.props.children.join(' ')
: (this.props.children as string);
this.setState({ text: newText, textWhenExpanded: newText });
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,21 @@ test('Does not call onCopy when ClipboardCopyButton is not clicked', async () =>
expect(onCopyMock).not.toHaveBeenCalled();
});

test('Can take array of strings as children', async () => {
const onCopyMock = jest.fn();
const user = userEvent.setup();
const childrenArray = children.split(' ');

// sanity check to ensure we are checking an array with at least 2 elements
expect(childrenArray.length).toBeGreaterThan(1);

render(<ClipboardCopy onCopy={onCopyMock}>{childrenArray}</ClipboardCopy>);

await user.click(screen.getByRole('button', { name: 'Test CCB clicker' }));

expect(onCopyMock).toHaveBeenCalledWith(expect.any(Object), children);
});

test('Matches snapshot', () => {
const { asFragment } = render(
<ClipboardCopy id="snapshot" ouiaId="snapshot">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports[`Matches snapshot 1`] = `
<input
aria-invalid="false"
aria-label="Copyable input"
data-ouia-component-id="OUIA-Generated-TextInputBase-26"
data-ouia-component-id="OUIA-Generated-TextInputBase-27"
data-ouia-component-type="PF6/TextInput"
data-ouia-safe="true"
id="text-input-generated-id"
Expand Down
Loading