Skip to content

Commit 64fc6ae

Browse files
committed
Make component react to UTC or Locale prop updates
The component will now change accordingly when UTC or Locale props are updated from the outside without having to manually trigger a re-render.
1 parent 89e5320 commit 64fc6ae

File tree

2 files changed

+110
-27
lines changed

2 files changed

+110
-27
lines changed

DateTime.js

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ var Datetime = React.createClass({
129129
date: props.dateFormat || '',
130130
time: props.timeFormat || ''
131131
},
132-
locale = this.localMoment( props.date ).localeData()
132+
locale = this.localMoment( props.date, null, props ).localeData()
133133
;
134134

135135
if ( formats.date === true ) {
@@ -153,23 +153,53 @@ var Datetime = React.createClass({
153153

154154
componentWillReceiveProps: function( nextProps ) {
155155
var formats = this.getFormats( nextProps ),
156-
update = {}
156+
updatedState = {}
157157
;
158158

159159
if ( nextProps.value !== this.props.value ||
160-
formats.datetime !== this.getFormats( this.props ).datetime ) {
161-
update = this.getStateFromProps( nextProps );
160+
formats.datetime !== this.getFormats( this.props ).datetime ) {
161+
updatedState = this.getStateFromProps( nextProps );
162162
}
163163

164-
if ( update.open === undefined ) {
165-
update.open = this.state.open;
164+
if ( updatedState.open === undefined ) {
165+
updatedState.open = this.state.open;
166166
}
167-
167+
168168
if ( nextProps.viewMode !== this.props.viewMode ) {
169-
update.currentView = nextProps.viewMode;
169+
updatedState.currentView = nextProps.viewMode;
170+
}
171+
172+
if ( nextProps.locale !== this.props.locale ) {
173+
if ( this.state.viewDate ) {
174+
var updatedViewDate = this.state.viewDate.clone().locale( nextProps.locale );
175+
updatedState.viewDate = updatedViewDate;
176+
}
177+
if ( this.state.selectedDate ) {
178+
var updatedSelectedDate = this.state.selectedDate.clone().locale( nextProps.locale );
179+
updatedState.selectedDate = updatedSelectedDate;
180+
updatedState.inputValue = updatedSelectedDate.format( formats.datetime );
181+
}
182+
}
183+
184+
if ( nextProps.utc !== this.props.utc ) {
185+
if ( nextProps.utc ) {
186+
if ( this.state.viewDate )
187+
updatedState.viewDate = this.state.viewDate.clone().utc();
188+
if ( this.state.selectedDate ) {
189+
updatedState.selectedDate = this.state.selectedDate.clone().utc();
190+
updatedState.inputValue = updatedState.selectedDate.format( formats.datetime );
191+
}
192+
} else {
193+
if ( this.state.viewDate )
194+
updatedState.viewDate = this.state.viewDate.clone().local();
195+
if ( this.state.selectedDate ) {
196+
updatedState.selectedDate = this.state.selectedDate.clone().local();
197+
updatedState.inputValue = updatedState.selectedDate.format(formats.datetime);
198+
}
199+
}
170200
}
171201

172-
this.setState( update );
202+
this.setState( updatedState );
173203
},
174204

175205
onInputChange: function( e ) {
@@ -337,11 +367,12 @@ var Datetime = React.createClass({
337367
}
338368
},
339369

340-
localMoment: function( date, format ) {
341-
var momentFn = this.props.utc ? moment.utc : moment;
342-
var m = momentFn( date, format, this.props.strictParsing );
343-
if ( this.props.locale )
344-
m.locale( this.props.locale );
370+
localMoment: function( date, format, props ) {
371+
props = props || this.props;
372+
var momentFn = props.utc ? moment.utc : moment;
373+
var m = momentFn( date, format, props.strictParsing );
374+
if ( props.locale )
375+
m.locale( props.locale );
345376
return m;
346377
},
347378

tests/datetime.spec.js

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,6 @@ describe('Datetime', () => {
156156
expect(utils.isOpen(component)).toBeTruthy();
157157
});
158158

159-
it('dateFormat -> prop changes and value updates accordingly', () => {
160-
const date = new Date(2000, 0, 15, 2, 2, 2, 2),
161-
component = utils.createDatetime({
162-
dateFormat: 'YYYY-MM-DD', timeFormat: false, defaultValue: date
163-
});
164-
165-
const valueBefore = utils.getInputValue(component);
166-
component.setProps({ dateFormat: 'DD.MM.YYYY'});
167-
const valueAfter = utils.getInputValue(component);
168-
169-
expect(valueBefore).not.toEqual(valueAfter);
170-
});
171-
172159
describe('with custom props', () => {
173160
it('input=false', () => {
174161
const component = utils.createDatetime({ input: false });
@@ -642,6 +629,71 @@ describe('Datetime', () => {
642629
expect(component.find('.rdtCounter').length).toEqual(1);
643630
});
644631
});
632+
633+
describe('being updated and should trigger update', () => {
634+
it('dateFormat -> value should change format', () => {
635+
const date = new Date(2000, 0, 15, 2, 2, 2, 2),
636+
component = utils.createDatetime({
637+
dateFormat: 'YYYY-MM-DD', timeFormat: false, defaultValue: date
638+
});
639+
640+
const valueBefore = utils.getInputValue(component);
641+
component.setProps({ dateFormat: 'DD.MM.YYYY'});
642+
const valueAfter = utils.getInputValue(component);
643+
644+
expect(valueBefore).not.toEqual(valueAfter);
645+
});
646+
647+
it('UTC -> value should change format (true->false)', () => {
648+
const date = new Date(2000, 0, 15, 2, 2, 2, 2),
649+
momentDate = moment(date),
650+
component = utils.createDatetime({ value: momentDate, utc: true });
651+
652+
const valueBefore = utils.getInputValue(component);
653+
component.setProps({ utc: false }, () => {
654+
const valueAfter = utils.getInputValue(component);
655+
656+
expect(valueBefore).not.toEqual(valueAfter);
657+
});
658+
});
659+
660+
it('UTC -> value should change format (false->true)', () => {
661+
const date = new Date(2000, 0, 15, 2, 2, 2, 2),
662+
momentDate = moment(date),
663+
component = utils.createDatetime({ value: momentDate, utc: false });
664+
665+
const valueBefore = utils.getInputValue(component);
666+
component.setProps({ utc: true }, () => {
667+
const valueAfter = utils.getInputValue(component);
668+
669+
expect(valueBefore).not.toEqual(valueAfter);
670+
});
671+
});
672+
673+
it('locale -> picker should change language (viewMode=days)', () => {
674+
const component = utils.createDatetime({ viewMode: 'days', locale: 'nl' }),
675+
weekdaysBefore = component.find('.rdtDays .dow').map((element) =>
676+
element.text()
677+
);
678+
679+
component.setProps({ locale: 'sv' });
680+
const weekdaysAfter = component.find('.rdtDays .dow').map((element) =>
681+
element.text()
682+
);
683+
684+
expect(weekdaysBefore).not.toEqual(weekdaysAfter);
685+
});
686+
687+
it('locale -> picker should change language (viewMode=months)', () => {
688+
const component = utils.createDatetime({ viewMode: 'months', locale: 'nl' }),
689+
monthsBefore = [utils.getNthMonth(component, 2).text(), utils.getNthMonth(component, 4).text()];
690+
691+
component.setProps({ locale: 'sv' });
692+
const monthsAfter = [utils.getNthMonth(component, 2).text(), utils.getNthMonth(component, 4).text()];
693+
694+
expect(monthsBefore).not.toEqual(monthsAfter);
695+
});
696+
});
645697
});
646698

647699
describe('event listeners', () => {

0 commit comments

Comments
 (0)