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

[EuiSuperDatePicker] Fix duplicate value when pasting absolute value #8311

Merged
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
4 changes: 4 additions & 0 deletions packages/eui/changelogs/upcoming/8311.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**Bug fixes**

- Fixed a bug on `EuiSuperDatePicker` where pasting an absolute date would append the date instead of replace it

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
*/

import React from 'react';
import { fireEvent } from '@testing-library/react';
import { fireEvent, waitFor } from '@testing-library/react';
import { render } from '../../../../test/rtl';

import { EuiAbsoluteTab } from './absolute_tab';
import { LocaleSpecifier } from 'moment';
import { userEvent } from '@storybook/test';

// Mock EuiDatePicker - 3rd party datepicker lib causes render issues
jest.mock('../../date_picker', () => ({
Expand Down Expand Up @@ -69,7 +70,7 @@ describe('EuiAbsoluteTab', () => {
expect(queryByText(formatHelpText)).toHaveClass('euiFormErrorText');
});

it('immediately parses pasted text without needing an extra click or keypress', () => {
it('immediately parses pasted text without needing an extra click or keypress', async () => {
const { getByTestSubject, queryByText } = render(
<EuiAbsoluteTab {...props} />
);
Expand All @@ -80,9 +81,21 @@ describe('EuiAbsoluteTab', () => {
fireEvent.paste(input, {
clipboardData: { getData: () => '1970-01-01' },
});

expect(input).not.toBeInvalid();
expect(input.value).toContain('Jan 1, 1970');

// Additional pasting check as userEvent.paste more accurately simulates
// the pasting behavior with side effects
await waitFor(() => {
input.focus();
userEvent.paste('1970-01-02');
});

// ensure value is replaced, not appended
expect(input.value).not.toEqual('Jan 1, 1970 @ 00:00:00.0001970-01-02');
expect(input.value).toEqual('Jan 2, 1970 @ 00:00:00.000');

input.value = '';
fireEvent.paste(input, {
clipboardData: { getData: () => 'not a date' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ export const EuiAbsoluteTab: FunctionComponent<EuiAbsoluteTabProps> = ({
value={textInputValue}
onChange={handleTextChange}
onPaste={(event) => {
// preventing default here ensures no additional onChange is
// triggered which otherwise results in input duplication
event.preventDefault();

setTextInputValue(event.clipboardData.getData('text'));
setIsReadyToParse(true);
}}
Expand Down