Skip to content

Commit 7b07f56

Browse files
author
Javier Marquez
committed
Updating value prop also updates view date now
1 parent 3c99ab8 commit 7b07f56

9 files changed

Lines changed: 61 additions & 38 deletions

File tree

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,23 @@ class MyDTPicker extends React.Component<MyDTPickerProps, MyDTPickerState> {
288288
```
289289
290290
## Contributions
291-
For information about how to contribute, see the [CONTRIBUTING](.github/CONTRIBUTING.md) file.
291+
react-datetime is a nice choice if you are looking for some open-source project to lay your hands on. It's a library used by thousands of developers, and the changes in this version make easier for everyone to understand it. It's not simple, but it's small enough to be get you initiated in a couple of hours.
292+
293+
If you are interested and want to start playing with its code, clone it and fire up the playground included in the repo:
292294
293-
## Development
294295
```sh
296+
git clone https://github.com/YouCanBookMe/react-datetime.git
297+
cd react-datetime
298+
npm install
295299
npm run playground
296300
```
297-
This will start a local development server building `src/index.js`. We can update react-datetime's sources then and the changes will be hot loaded by the development server.
301+
302+
This will start a local development server building `src/index.js`. We can update react-datetime's sources then and our changes will be hot loaded by the development server.
303+
304+
Looking for something to work on? Have a look at [the list of known issues](https://github.com/YouCanBookMe/react-datetime/issues), and maybe you can kill a bug making somebody happy! :)
305+
306+
Have some work done? That's great! But please, read the [react-datetime contributing guidelines](.github/CONTRIBUTING.md) before submitting it.
307+
298308
299309
### [Changelog](CHANGELOG.md)
300310

dist/react-datetime.cjs.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react-datetime.cjs.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react-datetime.umd.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react-datetime.umd.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

migrateToV3.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Those are the main changes that might break your app, if you weren't able to fin
3232
## What's new in react-datetime v3
3333
Version 3 is a big refactor of react-datetime. We have tried to not to change the API drastically, but some of the props has been renamed or removed, trying to make them clearer for the developer. A complete list of changes is:
3434

35-
* The props are read directly when possible, don't deriving the state from them when possible.
35+
* The props are read directly when possible, not deriving the state from them anymore.
3636
* `disableCloseOnClickOutside` prop is now `closeOnClickOutside` (avoid double negations).
3737
* `onBlur` and `onFocus` props are renamed to `onClose` and `onOpen` since they had nothing to do with the blur event and it was misleading for some users. If we want to listen to the input's `onBlur` and `onFocus` use `inputProps`.
3838
* Time is not updated anymore on right clicks.
@@ -44,17 +44,17 @@ Version 3 is a big refactor of react-datetime. We have tried to not to change th
4444
* Clicking on days from the previous or next month in the days view should work properly now.
4545
* Month, year and time views for locales that don't use gregorian numbers should work properly now.
4646
* A playground has been added to the repo, that makes simpler to work on react-datetime development and test out changes quickly. To run it: `npm run playground`.
47-
* Not using the deprecated method `componentWillReceiveProps` any more.
47+
* Not using the deprecated method `componentWillReceiveProps` anymore.
4848
* We are not using create-react-class anymore, bye bye 2016's react!
4949
* Updated typescript definitions.
5050
* Not depending on gulp to create the build anymore.
5151
* Updated most of the dependencies.
5252

53-
## Collaborate
53+
## Contribute
5454

55-
react-datetime is a nice choice if you are looking to some open source project to lay your hands on. It's a project used by thousands of developers, and the changes in this version makes easier for everyone to understand it. It's not simple, but it's small enough to be get you initiated in a couple of hours.
55+
react-datetime is a nice choice if you are looking for some open-source project to lay your hands on. It's a library used by thousands of developers, and the changes in this version make easier for everyone to understand it. It's not simple, but it's small enough to be get you initiated in a couple of hours.
5656

57-
If you are interested and want to start playing with it's code, clone it and fire up the playground included in the repo:
57+
If you are interested and want to start playing with its code, clone it and fire up the playground included in the repo:
5858

5959
```
6060
git clone https://github.com/YouCanBookMe/react-datetime.git
@@ -63,4 +63,8 @@ npm install
6363
npm run playground
6464
```
6565

66-
Have a look at [the list of known issues](https://github.com/YouCanBookMe/react-datetime/issues), and maybe you can kill a bug making somebody happy! :)
66+
This will start a local development server building `src/index.js`. We can update react-datetime's sources then and our changes will be hot loaded by the development server.
67+
68+
Looking for something to work on? Have a look at [the list of known issues](https://github.com/YouCanBookMe/react-datetime/issues), and maybe you can kill a bug making somebody happy! :)
69+
70+
Have some work done? That's great! But please, read the [react-datetime contributing guidelines](.github/CONTRIBUTING.md) before submitting it.

src/App.js

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,27 @@
33
import React from 'react';
44
import Datetime from './datetime/DateTime';
55

6-
class App extends React.Component {
7-
render() {
8-
return (
9-
<Datetime
10-
ref="datetime"
11-
renderView={(mode, renderDefault) =>
12-
this.renderView(mode, renderDefault)
13-
}
14-
/>
15-
);
6+
class App extends React.Component {
7+
state = {
8+
date: new Date()
169
}
1710

18-
renderView(mode, renderDefault) {
19-
// Only for years, months and days view
20-
if (mode === 'time') return renderDefault();
21-
11+
render() {
2212
return (
23-
<div className="wrapper">
24-
{renderDefault()}
25-
<div className="controls">
26-
<button onClick={() => this.goToToday()}>Today</button>
27-
</div>
13+
<div>
14+
<Datetime
15+
ref="datetime"
16+
value={ this.state.date }
17+
/>
18+
<button onClick={ this._update }>Update</button>
2819
</div>
2920
);
3021
}
31-
32-
goToToday() {
33-
// Reset
34-
this.refs.datetime.setViewDate(new Date());
35-
this.refs.datetime.navigate('days');
22+
23+
_update = () => {
24+
this.setState({
25+
date: new Date( this.state.date.getTime() + 10000000000 )
26+
});
3627
}
3728
}
3829

src/datetime/DateTime.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ export default class Datetime extends React.Component {
448448

449449
let needsUpdate = false;
450450
let thisProps = this.props;
451+
451452
['locale', 'utc', 'displayZone', 'dateFormat', 'timeFormat'].forEach( function(p) {
452453
prevProps[p] !== thisProps[p] && (needsUpdate = true);
453454
});
@@ -456,6 +457,10 @@ export default class Datetime extends React.Component {
456457
this.regenerateDates( this.props );
457458
}
458459

460+
if ( thisProps.value && thisProps.value !== prevProps.value ) {
461+
this.setViewDate( thisProps.value );
462+
}
463+
459464
this.checkTZ( this.props );
460465
}
461466

test/tests.spec.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ describe('Datetime', () => {
706706
onChange: updated => {
707707
expect(updated).toBe( invalidStrDate );
708708
done();
709-
}
709+
}
710710
})
711711
;
712712

@@ -1378,6 +1378,19 @@ describe('Datetime', () => {
13781378
component.find('.form-control').simulate('change', { target: { value: strDate }});
13791379
});
13801380

1381+
it('should update the view date when updating the value prop', done => {
1382+
const value1 = moment('2020-03-04T13:00:10.121Z');
1383+
const value2 = moment('2021-06-04T13:00:10.121Z');
1384+
1385+
let component = utils.createDatetime({ value: value1 });
1386+
expect( component.instance().state.viewDate.toISOString() ).toBe(value1.toISOString());
1387+
1388+
component.setProps({ value: value2 });
1389+
setTimeout( () => {
1390+
expect( component.instance().state.viewDate.toISOString() ).toBe(value2.toISOString());
1391+
done();
1392+
});
1393+
});
13811394
});
13821395

13831396
describe('View navigation', () => {

0 commit comments

Comments
 (0)