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

Fix crash when null date passed to TimePicker #27316

Merged
merged 2 commits into from
Nov 27, 2020
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
24 changes: 24 additions & 0 deletions packages/components/src/date-time/test/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,28 @@ describe( 'TimePicker', () => {

expect( monthInputIndex > dayInputIndex ).toBe( true );
} );

it( 'Should set a time when passed a null currentTime', () => {
const onChangeSpy = jest.fn();

render(
<TimePicker
currentTime={ null }
onChange={ onChangeSpy }
is12Hour
/>
);

const monthInput = screen.getByLabelText( 'Month' ).value;
const dayInput = screen.getByLabelText( 'Day' ).value;
const yearInput = screen.getByLabelText( 'Year' ).value;
const hoursInput = screen.getByLabelText( 'Hours' ).value;
const minutesInput = screen.getByLabelText( 'Minutes' ).value;

expect( Number.isNaN( parseInt( monthInput, 10 ) ) ).toBe( false );
expect( Number.isNaN( parseInt( dayInput, 10 ) ) ).toBe( false );
expect( Number.isNaN( parseInt( yearInput, 10 ) ) ).toBe( false );
expect( Number.isNaN( parseInt( hoursInput, 10 ) ) ).toBe( false );
expect( Number.isNaN( parseInt( minutesInput, 10 ) ) ).toBe( false );
} );
} );
4 changes: 3 additions & 1 deletion packages/components/src/date-time/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ export function TimePicker( { is12Hour, currentTime, onChange } ) {

// Reset the state when currentTime changed.
useEffect( () => {
setDate( moment( currentTime ).startOf( 'minutes' ) );
setDate(
currentTime ? moment( currentTime ).startOf( 'minutes' ) : moment()
);
}, [ currentTime ] );

const { day, month, year, minutes, hours, am } = useMemo(
Expand Down