Skip to content

Commit 99f7e2a

Browse files
author
Eric Olkowski
committed
Made updates per PR feedback
1 parent 281e611 commit 99f7e2a

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

packages/react-core/src/components/CalendarMonth/CalendarMonth.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import AngleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-right-i
88
import { css } from '@patternfly/react-styles';
99
import styles from '@patternfly/react-styles/css/components/CalendarMonth/calendar-month';
1010
import { getUniqueId } from '../../helpers/util';
11+
import { isValidDate } from '../../helpers/datetimeUtils';
1112

1213
export enum Weekday {
1314
Sunday = 0,
@@ -109,8 +110,6 @@ const buildCalendar = (year: number, month: number, weekStart: number, validator
109110
const isSameDate = (d1: Date, d2: Date) =>
110111
d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate();
111112

112-
export const isValidDate = (date: Date) => Boolean(date && !isNaN(date as any));
113-
114113
const today = new Date();
115114

116115
/** The main calendar month component. */

packages/react-core/src/components/DatePicker/DatePicker.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import { TextInput, TextInputProps } from '../TextInput/TextInput';
66
import { Popover, PopoverProps } from '../Popover/Popover';
77
import { InputGroup } from '../InputGroup/InputGroup';
88
import OutlinedCalendarAltIcon from '@patternfly/react-icons/dist/esm/icons/outlined-calendar-alt-icon';
9-
import { CalendarMonth, CalendarFormat, isValidDate } from '../CalendarMonth';
9+
import { CalendarMonth, CalendarFormat } from '../CalendarMonth';
1010
import { useImperativeHandle } from 'react';
1111
import { KeyTypes } from '../../helpers';
12+
import { isValidDate } from '../../helpers/datetimeUtils';
1213

1314
/** The main date picker component. */
1415

packages/react-core/src/components/Timestamp/Timestamp.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
import styles from '@patternfly/react-styles/css/components/Timestamp/timestamp';
33
import { css } from '@patternfly/react-styles';
44
import { Tooltip } from '../Tooltip';
5-
import { isValidDate } from '../CalendarMonth';
5+
import { isValidDate } from '../../helpers/datetimeUtils';
66

77
export enum TimestampFormat {
88
full = 'full',

packages/react-core/src/components/Timestamp/__tests__/Timestamp.test.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,31 @@ test('Renders with current date by default with default formatting', () => {
3636
expect(screen.getByText(new Date().toLocaleString())).toBeInTheDocument();
3737
});
3838

39+
test('Renders with correct datetime attribute with current date by default', () => {
40+
render(<Timestamp />);
41+
// Because there could be a .001 ms difference in the expected and received datetime value,
42+
// we want an ISO value without the ms to expect as the datetime value.
43+
const isoDateWithoutMS = new Date().toISOString().split('.')[0];
44+
45+
expect(screen.getByText(new Date().toLocaleString())).toHaveAttribute(
46+
'datetime',
47+
expect.stringMatching(isoDateWithoutMS)
48+
);
49+
});
50+
3951
test('Renders passed in date with default formatting', () => {
4052
render(<Timestamp date={new Date(2022, 0, 1)} />);
4153

4254
expect(screen.getByText('1/1/2022, 12:00:00 AM')).toBeInTheDocument();
4355
});
4456

57+
test('Renders with correct datetime attribute when date is passed in', () => {
58+
const passedDate = new Date(2022, 0, 1);
59+
render(<Timestamp date={passedDate} />);
60+
61+
expect(screen.getByText('1/1/2022, 12:00:00 AM')).toHaveAttribute('datetime', passedDate.toISOString());
62+
});
63+
4564
test('Renders with custom formatting when dateFormat and timeFormat are passed in', () => {
4665
render(
4766
<Timestamp date={new Date(2022, 0, 1)} dateFormat={TimestampFormat.full} timeFormat={TimestampFormat.short} />
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* @param {Date} date - A date to check the validity of
3+
*/
4+
export const isValidDate = (date: Date) => Boolean(date && !isNaN(date as any));

0 commit comments

Comments
 (0)