Skip to content
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
2 changes: 2 additions & 0 deletions packages/eui/changelogs/upcoming/9199.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Updated `EuiSuperDatePicker` to have a more forgiving manual input for absolute dates.

Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,31 @@ describe('EuiAbsoluteTab', () => {
expect(input).toHaveValue('Jan 31st 01');
});

it('can be forgiving with `dateFormat`', () => {
const { getByTestSubject } = render(
<EuiAbsoluteTab
{...props}
dateFormat="MMM D, YYYY HH:mm:ss"
locale="en"
/>
);
const input = getByTestSubject(
'superDatePickerAbsoluteDateInput'
) as HTMLInputElement;

changeInput(input, 'Jan 31, 1993');
expect(input).not.toBeInvalid();
expect(input).toHaveValue('Jan 31, 1993 00:00:00');

changeInput(input, 'Jan 31');
expect(input).not.toBeInvalid();
expect(input.value).toContain('Jan 31, ');

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

describe('parses date string in locale', () => {
test.each<{
locale: LocaleSpecifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,22 @@ export const EuiAbsoluteTab: FunctionComponent<EuiAbsoluteTabProps> = ({
return;
}

// We can be forgiving for `dateFormat` if we are certain
// we're not expecting any of the other formats allowed;
// otherwise we can get valid but inaccurate results e.g.
// `1970-01-01` -> `Jan 19, 1970 @ 01:01:00.000`
const strictModeForPassedFormat = moment(
textInputValue,
ALLOWED_USER_DATE_FORMATS,
true
).isValid();

// Attempt to parse with passed `dateFormat` and `locale`
let valueAsMoment = moment(
textInputValue,
dateFormat,
typeof locale === 'string' ? locale : 'en', // Narrow the union type to string
true
strictModeForPassedFormat
);
let dateIsValid = valueAsMoment.isValid();

Expand Down