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

[Discover] Add Copy to clipboard ability for column name of Document Explorer #123892

Merged
merged 12 commits into from
Feb 28, 2022
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import { buildCopyColumnNameButton } from './copy_column_name_button';
import { EuiButton } from '@elastic/eui';
import { mountWithIntl } from '@kbn/test-jest-helpers';

const execCommandMock = (global.document.execCommand = jest.fn());
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {});

describe('Copy to clipboard button', () => {
it('should copy to clipboard on click', () => {
const { label, iconType, onClick } = buildCopyColumnNameButton('test-field-name');
execCommandMock.mockImplementationOnce(() => true);

const wrapper = mountWithIntl(
<EuiButton iconType={iconType} onClick={onClick}>
{label}
</EuiButton>
);

wrapper.find(EuiButton).simulate('click');

expect(execCommandMock).toHaveBeenCalledWith('copy');
expect(warn).not.toHaveBeenCalled();
});

it('should not copy to clipboard on click', () => {
const { label, iconType, onClick } = buildCopyColumnNameButton('test-field-name');
execCommandMock.mockImplementationOnce(() => false);

const wrapper = mountWithIntl(
<EuiButton iconType={iconType} onClick={onClick}>
{label}
</EuiButton>
);

wrapper.find(EuiButton).simulate('click');

expect(execCommandMock).toHaveBeenCalledWith('copy');
expect(warn).toHaveBeenCalledWith('Unable to copy to clipboard.');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import { copyToClipboard, EuiListGroupItemProps } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';

export function buildCopyColumnNameButton(columnName: string) {
const copyToClipBoardButton: EuiListGroupItemProps = {
size: 'xs',
label: (
<FormattedMessage
id="discover.grid.copyToClipBoardButton"
defaultMessage="Copy to clipboard"
/>
),
iconType: 'copyClipboard',
iconProps: { size: 'm' },
onClick: () => copyToClipboard(columnName),
};

return copyToClipBoardButton;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ describe('Discover grid columns', function () {
Array [
Object {
"actions": Object {
"additional": Array [
Object {
"iconProps": Object {
"size": "m",
},
"iconType": "copyClipboard",
"label": <FormattedMessage
defaultMessage="Copy to clipboard"
id="discover.grid.copyToClipBoardButton"
values={Object {}}
/>,
"onClick": [Function],
"size": "xs",
},
],
"showHide": Object {
"iconType": "cross",
"label": "Remove column",
Expand All @@ -39,6 +54,21 @@ describe('Discover grid columns', function () {
},
Object {
"actions": Object {
"additional": Array [
Object {
"iconProps": Object {
"size": "m",
},
"iconType": "copyClipboard",
"label": <FormattedMessage
defaultMessage="Copy to clipboard"
id="discover.grid.copyToClipBoardButton"
values={Object {}}
/>,
"onClick": [Function],
"size": "xs",
},
],
"showHide": Object {
"iconType": "cross",
"label": "Remove column",
Expand Down Expand Up @@ -68,6 +98,21 @@ describe('Discover grid columns', function () {
Array [
Object {
"actions": Object {
"additional": Array [
Object {
"iconProps": Object {
"size": "m",
},
"iconType": "copyClipboard",
"label": <FormattedMessage
defaultMessage="Copy to clipboard"
id="discover.grid.copyToClipBoardButton"
values={Object {}}
/>,
"onClick": [Function],
"size": "xs",
},
],
"showHide": false,
"showMoveLeft": false,
"showMoveRight": false,
Expand All @@ -83,6 +128,21 @@ describe('Discover grid columns', function () {
},
Object {
"actions": Object {
"additional": Array [
Object {
"iconProps": Object {
"size": "m",
},
"iconType": "copyClipboard",
"label": <FormattedMessage
defaultMessage="Copy to clipboard"
id="discover.grid.copyToClipBoardButton"
values={Object {}}
/>,
"onClick": [Function],
"size": "xs",
},
],
"showHide": false,
"showMoveLeft": false,
"showMoveRight": false,
Expand All @@ -109,6 +169,21 @@ describe('Discover grid columns', function () {
Array [
Object {
"actions": Object {
"additional": Array [
Object {
"iconProps": Object {
"size": "m",
},
"iconType": "copyClipboard",
"label": <FormattedMessage
defaultMessage="Copy to clipboard"
id="discover.grid.copyToClipBoardButton"
values={Object {}}
/>,
"onClick": [Function],
"size": "xs",
},
],
"showHide": false,
"showMoveLeft": true,
"showMoveRight": true,
Expand Down Expand Up @@ -138,6 +213,21 @@ describe('Discover grid columns', function () {
},
Object {
"actions": Object {
"additional": Array [
Object {
"iconProps": Object {
"size": "m",
},
"iconType": "copyClipboard",
"label": <FormattedMessage
defaultMessage="Copy to clipboard"
id="discover.grid.copyToClipBoardButton"
values={Object {}}
/>,
"onClick": [Function],
"size": "xs",
},
],
"showHide": Object {
"iconType": "cross",
"label": "Remove column",
Expand All @@ -156,6 +246,21 @@ describe('Discover grid columns', function () {
},
Object {
"actions": Object {
"additional": Array [
Object {
"iconProps": Object {
"size": "m",
},
"iconType": "copyClipboard",
"label": <FormattedMessage
defaultMessage="Copy to clipboard"
id="discover.grid.copyToClipBoardButton"
values={Object {}}
/>,
"onClick": [Function],
"size": "xs",
},
],
"showHide": Object {
"iconType": "cross",
"label": "Remove column",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { buildCellActions } from './discover_grid_cell_actions';
import { getSchemaByKbnType } from './discover_grid_schema';
import { SelectButton } from './discover_grid_document_selection';
import { defaultTimeColumnWidth } from './constants';
import { buildCopyColumnNameButton } from './copy_column_name_button';

export function getLeadControlColumns() {
return [
Expand Down Expand Up @@ -80,6 +81,7 @@ export function buildEuiGridColumn(
},
showMoveLeft: !defaultColumns,
showMoveRight: !defaultColumns,
additional: columnName === '_source' ? undefined : [buildCopyColumnNameButton(columnName)],
},
cellActions: indexPatternField ? buildCellActions(indexPatternField) : [],
};
Expand Down