Skip to content

Commit

Permalink
Fix crashing React loop if toggling the calendar popover from an inva…
Browse files Browse the repository at this point in the history
…lid selected date
  • Loading branch information
cee-chen committed May 22, 2024
1 parent 2403755 commit 6e73cd4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 12 additions & 3 deletions packages/eui/src/components/date_picker/date_picker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
*/

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

import { EuiDatePicker } from './date_picker';
import { EuiContext } from '../context';
Expand All @@ -21,14 +22,22 @@ describe('EuiDatePicker', () => {
expect(container.firstChild).toMatchSnapshot();
});

it('handles invalid `selected` dates', () => {
it('handles invalid `selected` dates', async () => {
const { container } = render(
<EuiDatePicker selected={moment('invalid')} />
);
const datepickerInput = container.querySelector('input.euiDatePicker');

expect(datepickerInput).toHaveValue('Invalid date');
expect(datepickerInput).toBeInvalid();

// The calendar date picker should load in a popover, but no date should be selected
fireEvent.focus(datepickerInput!);
await waitForEuiPopoverOpen();
const calendar = document.querySelector('.react-datepicker__month');
expect(calendar).toBeInTheDocument();
const selected = document.querySelector('.react-datepicker__day--selected');
expect(selected).not.toBeInTheDocument();
});

test('compressed', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ export default class DatePicker extends React.Component {
return {
open: this.props.startOpen || false,
preventFocus: false,
preSelection: this.props.selected
preSelection: this.props.selected?._isValid
? newDate(this.props.selected)
: boundedPreSelection,
// transforming highlighted days (perhaps nested array)
Expand Down Expand Up @@ -696,7 +696,7 @@ export default class DatePicker extends React.Component {
useWeekdaysShort={this.props.useWeekdaysShort}
formatWeekDay={this.props.formatWeekDay}
dropdownMode={this.props.dropdownMode}
selected={this.props.selected}
selected={this.props.selected?._isValid ? this.props.selected : undefined}
preSelection={this.state.preSelection}
onSelect={this.handleSelect}
onWeekSelect={this.props.onWeekSelect}
Expand Down

0 comments on commit 6e73cd4

Please sign in to comment.