Skip to content

Commit 544aca3

Browse files
authored
Merge pull request #191 from data-driven-forms/fix-date-picker-initial-value
fix(pf3): stop date picker crash when value is string.
2 parents 3a98f3a + 7a347a9 commit 544aca3

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

packages/pf3-component-mapper/demo/demo-schemas/sandbox.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ const output = {
449449
title: 'Datepicker with past days',
450450
component: components.DATE_PICKER,
451451
variant: 'date-time',
452+
initialValue: '2019-11-04T12:31:00.000Z',
452453
},
453454
],
454455
component: components.SUB_FORM,

packages/pf3-component-mapper/src/form-fields/date-time-picker/date-time-picker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class DateTimePicker extends React.Component {
3131
super(props);
3232
this.state = {
3333
positionLeft: 0,
34-
selectedDay: props.value,
34+
selectedDay: props.value ? typeof props.value === 'string' ? new Date(props.value) : props.value : undefined,
3535
selectingMonth: false,
3636
selectingYear: false,
3737
isOpen: false,
@@ -169,7 +169,7 @@ DateTimePicker.propTypes = {
169169
showTodayButton: PropTypes.bool,
170170
isDisabled: PropTypes.bool,
171171
disabledDays: PropTypes.array,
172-
value: PropTypes.instanceOf(Date),
172+
value: PropTypes.oneOfType([ PropTypes.instanceOf(Date), PropTypes.string ]),
173173
closeOnDaySelect: PropTypes.bool,
174174
onChange: PropTypes.func.isRequired,
175175
isClearable: PropTypes.bool,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
4+
import { DateTimePicker } from '../../../form-fields/date-time-picker/date-time-picker';
5+
6+
describe('<DateTimePicker />', () => {
7+
it('should use value of type Date', () => {
8+
const wrapper = mount(<DateTimePicker value={ new Date() } />);
9+
expect(wrapper.state().selectedDay).toBeInstanceOf(Date);
10+
});
11+
12+
it('should convert string value into Date object', () => {
13+
const wrapper = mount(<DateTimePicker value='2019-11-01T12:31:00.000Z' />);
14+
expect(wrapper.state().selectedDay).toBeInstanceOf(Date);
15+
});
16+
17+
it('should not set state for undefined value', () => {
18+
const wrapper = mount(<DateTimePicker />);
19+
expect(wrapper.state().selectedDay).toBeUndefined();
20+
});
21+
});
22+

0 commit comments

Comments
 (0)