Skip to content

Commit 2297a47

Browse files
author
Dan Forster
committed
Merge branch 'master' of github.com:YouCanBookMe/react-datetime into feature-render-input
* 'master' of github.com:YouCanBookMe/react-datetime: (24 commits) Twotenthree (arqex#434) Version 2.10.2 Fix URL for 'uncontrolled elements' Fix formatting in demo README Create proper demo application Move @types/react back to devDependencies Fix build files (arqex#411) Bump to v2.10.0 (arqex#410) if isValidDate is passed we need to verify if the view date is valid … (arqex#296) Add create-react-class dependency Use same tab indentiation in all JS files Enforce tab indentation Add TODO regarding complex ternary expression Use consistent indentation in README examples Add yarn installation instructions to README Add snapshot testing Version 2.9.0 Add TS definitions for onViewModeChange callback Add callback on view mode changes Update indentation and style for consistency ...
2 parents 0b20fee + 39b8275 commit 2297a47

36 files changed

+31008
-453
lines changed

.eslintrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ module.exports = {
3535
// Enforce consistent spacing inside parentheses
3636
// "space-in-parens": [2, "always"],
3737
// Enforce the consistent use of either backticks, double, or single quotes
38-
"quotes": [2, "single", { "avoidEscape": true, "allowTemplateLiterals": true }]
38+
"quotes": [2, "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
39+
// Enforce using tabs for indentation
40+
"indent": [2, "tab", { "SwitchCase": 1 }]
3941
}
4042
};

.npmignore

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
*~
1+
# Folders
22
node_modules
3+
example
4+
.idea
5+
6+
# Files
7+
*~
38
.DS_Store
49
npm-debug.log
5-
test_bundle.js
6-
test-built
7-
.idea
8-
examples
9-
bower.json
1010
webpack.config.js
11-
TODO.md
12-
karma.dev.js

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
Changelog
22
=========
3+
## 2.10.3
4+
* Update react-onclickoutside dependancy
5+
* Remove isValidDate check before rendering as implementation was causing crashes in some ednge cases.
6+
7+
## 2.10.2
8+
* Move @types/react back to devDependencies
9+
* Add [demo](https://youcanbookme.github.io/react-datetime) app.
10+
11+
## 2.10.1
12+
* Fix build files.
13+
14+
## 2.10.0
15+
* Add isValidDate check before rendering so it doesn't render with an invalid date.
16+
17+
## 2.9.0
18+
* Trigger callback method on view mode changes
19+
320
## 2.8.11
421
* Update TypeScript definitions
522
* Replace deprecated React method with non-deprecated method

DateTime.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ declare namespace ReactDatetimeClass {
8888
input (a string).
8989
*/
9090
onBlur?: EventOrValueHandler<FocusEvent<any>>;
91+
/*
92+
Callback trigger when the view mode changes. The callback receives the selected view mode
93+
string ('years', 'months', 'days', 'time') as only parameter.
94+
*/
95+
onViewModeChange?: (viewMode: string) => void;
9196
/*
9297
The default view to display when the picker is shown. ('years', 'months', 'days', 'time')
9398
*/

DateTime.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

33
var assign = require('object-assign'),
4-
PropTypes = require('prop-types'),
5-
createClass = require('create-react-class'),
4+
PropTypes = require('prop-types'),
5+
createClass = require('create-react-class'),
66
moment = require('moment'),
77
React = require('react'),
88
CalendarContainer = require('./src/CalendarContainer')
9-
;
9+
;
1010

1111
var TYPES = PropTypes;
1212
var Datetime = createClass({
@@ -16,6 +16,7 @@ var Datetime = createClass({
1616
onFocus: TYPES.func,
1717
onBlur: TYPES.func,
1818
onChange: TYPES.func,
19+
onViewModeChange: TYPES.func,
1920
locale: TYPES.string,
2021
utc: TYPES.bool,
2122
input: TYPES.bool,
@@ -41,6 +42,7 @@ var Datetime = createClass({
4142
onFocus: nof,
4243
onBlur: nof,
4344
onChange: nof,
45+
onViewModeChange: nof,
4446
timeFormat: true,
4547
timeConstraints: {},
4648
dateFormat: true,
@@ -66,7 +68,7 @@ var Datetime = createClass({
6668
var formats = this.getFormats( props ),
6769
date = props.value || props.defaultValue,
6870
selectedDate, viewDate, updateOn, inputValue
69-
;
71+
;
7072

7173
if ( date && typeof date === 'string' )
7274
selectedDate = this.localMoment( date, formats.datetime );
@@ -103,11 +105,9 @@ var Datetime = createClass({
103105
getUpdateOn: function( formats ) {
104106
if ( formats.date.match(/[lLD]/) ) {
105107
return 'days';
106-
}
107-
else if ( formats.date.indexOf('M') !== -1 ) {
108+
} else if ( formats.date.indexOf('M') !== -1 ) {
108109
return 'months';
109-
}
110-
else if ( formats.date.indexOf('Y') !== -1 ) {
110+
} else if ( formats.date.indexOf('Y') !== -1 ) {
111111
return 'years';
112112
}
113113

@@ -120,7 +120,7 @@ var Datetime = createClass({
120120
time: props.timeFormat || ''
121121
},
122122
locale = this.localMoment( props.date, null, props ).localeData()
123-
;
123+
;
124124

125125
if ( formats.date === true ) {
126126
formats.date = locale.longDateFormat('L');
@@ -192,21 +192,26 @@ var Datetime = createClass({
192192
}
193193
}
194194
}
195-
195+
//we should only show a valid date if we are provided a isValidDate function. Removed in 2.10.3
196+
/*if (this.props.isValidDate) {
197+
updatedState.viewDate = updatedState.viewDate || this.state.viewDate;
198+
while (!this.props.isValidDate(updatedState.viewDate)) {
199+
updatedState.viewDate = updatedState.viewDate.add(1, 'day');
200+
}
201+
}*/
196202
this.setState( updatedState );
197203
},
198204

199205
onInputChange: function( e ) {
200206
var value = e.target === null ? e : e.target.value,
201207
localMoment = this.localMoment( value, this.state.inputFormat ),
202208
update = { inputValue: value }
203-
;
209+
;
204210

205211
if ( localMoment.isValid() && !this.props.value ) {
206212
update.selectedDate = localMoment;
207213
update.viewDate = localMoment.clone().startOf('month');
208-
}
209-
else {
214+
} else {
210215
update.selectedDate = null;
211216
}
212217

@@ -224,6 +229,7 @@ var Datetime = createClass({
224229
showView: function( view ) {
225230
var me = this;
226231
return function() {
232+
me.state.currentView !== view && me.props.onViewModeChange( view );
227233
me.setState({ currentView: view });
228234
};
229235
},
@@ -240,6 +246,7 @@ var Datetime = createClass({
240246
viewDate: me.state.viewDate.clone()[ type ]( parseInt(e.target.getAttribute('data-value'), 10) ).startOf( type ),
241247
currentView: nextViews[ type ]
242248
});
249+
me.props.onViewModeChange( nextViews[ type ] );
243250
};
244251
},
245252

@@ -271,7 +278,7 @@ var Datetime = createClass({
271278
state = this.state,
272279
date = (state.selectedDate || state.viewDate).clone(),
273280
nextType
274-
;
281+
;
275282

276283
// It is needed to set all the time properties
277284
// to not to reset the time
@@ -296,7 +303,7 @@ var Datetime = createClass({
296303
viewDate = this.state.viewDate,
297304
currentDate = this.state.selectedDate || viewDate,
298305
date
299-
;
306+
;
300307

301308
if (target.className.indexOf('rdtDay') !== -1) {
302309
if (target.className.indexOf('rdtNew') !== -1)
@@ -385,7 +392,7 @@ var Datetime = createClass({
385392
var me = this,
386393
formats = this.getFormats( this.props ),
387394
props = {dateFormat: formats.date, timeFormat: formats.time}
388-
;
395+
;
389396

390397
this.componentProps.fromProps.forEach( function( name ) {
391398
props[ name ] = me.props[ name ];
@@ -401,11 +408,12 @@ var Datetime = createClass({
401408
},
402409

403410
render: function() {
411+
// TODO: Make a function or clean up this code,
412+
// logic right now is really hard to follow
404413
var className = 'rdt' + (this.props.className ?
405414
( Array.isArray( this.props.className ) ?
406415
' ' + this.props.className.join( ' ' ) : ' ' + this.props.className) : ''),
407-
children = []
408-
;
416+
children = [];
409417

410418
if ( this.props.input ) {
411419
var finalInputProps = assign({

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ A date and time picker in the same React.js component. It can be used as a datep
77

88
This project started as a fork of https://github.com/quri/react-bootstrap-datetimepicker but the code and the API has changed a lot.
99

10-
## Usage
10+
## Installation
1111

1212
Install using npm:
1313
```sh
1414
npm install --save react-datetime
1515
```
1616

17+
Install using yarn:
18+
```sh
19+
yarn add react-datetime
20+
```
21+
22+
## Usage
23+
1724
[React.js](http://facebook.github.io/react/) and [Moment.js](http://momentjs.com/) are peer dependencies for react-datetime. These dependencies are not installed along with react-datetime automatically, but your project needs to have them installed in order to make the datepicker work. You can then use the datepicker like in the example below.
1825

1926

@@ -23,7 +30,7 @@ require('react-datetime');
2330
...
2431

2532
render: function() {
26-
return <Datetime />;
33+
return <Datetime />;
2734
}
2835
```
2936
[See this example working](http://codepen.io/simeg/pen/mEmQmP).
@@ -35,7 +42,7 @@ render: function() {
3542
| Name | Type | Default | Description |
3643
| ------------ | ------- | ------- | ----------- |
3744
| **value** | `Date` | `new Date()` | Represents the selected date by the component, in order to use it as a [controlled component](https://facebook.github.io/react/docs/forms.html#controlled-components). This prop is parsed by Moment.js, so it is possible to use a date `string` or a `moment` object. |
38-
| **defaultValue** | `Date` | `new Date()` | Represents the selected date for the component to use it as a [uncontrolled component](https://facebook.github.io/react/docs/forms.html#uncontrolled-components). This prop is parsed by Moment.js, so it is possible to use a date `string` or a `moment` object. |
45+
| **defaultValue** | `Date` | `new Date()` | Represents the selected date for the component to use it as a [uncontrolled component](https://facebook.github.io/react/docs/uncontrolled-components.html). This prop is parsed by Moment.js, so it is possible to use a date `string` or a `moment` object. |
3946
| **dateFormat** | `boolean` or `string` | `true` | Defines the format for the date. It accepts any [Moment.js date format](http://momentjs.com/docs/#/displaying/format/) (not in localized format). If `true` the date will be displayed using the defaults for the current locale. If `false` the datepicker is disabled and the component can be used as timepicker, see [available units docs](#specify-available-units). |
4047
| **timeFormat** | `boolean` or `string` | `true` | Defines the format for the time. It accepts any [Moment.js time format](http://momentjs.com/docs/#/displaying/format/) (not in localized format). If `true` the time will be displayed using the defaults for the current locale. If `false` the timepicker is disabled and the component can be used as datepicker, see [available units docs](#specify-available-units). |
4148
| **input** | `boolean` | `true` | Whether to show an input field to edit the date manually. |
@@ -45,6 +52,7 @@ render: function() {
4552
| **onChange** | `function` | empty function | Callback trigger when the date changes. The callback receives the selected `moment` object as only parameter, if the date in the input is valid. If the date in the input is not valid, the callback receives the value of the input (a string). |
4653
| **onFocus** | `function` | empty function | Callback trigger for when the user opens the datepicker. |
4754
| **onBlur** | `function` | empty function | Callback trigger for when the user clicks outside of the input, simulating a regular onBlur. The callback receives the selected `moment` object as only parameter, if the date in the input is valid. If the date in the input is not valid, the callback returned. |
55+
| **onViewModeChange** | `function` | empty function | Callback trigger when the view mode changes. The callback receives the selected view mode string (`years`, `months`, `days` or `time`) as only parameter.|
4856
| **viewMode** | `string` or `number` | `'days'` | The default view to display when the picker is shown (`'years'`, `'months'`, `'days'`, `'time'`). |
4957
| **className** | `string` or `string array` | `''` | Extra class name for the outermost markup element. |
5058
| **inputProps** | `object` | `undefined` | Defines additional attributes for the input element of the component. For example: `placeholder`, `disabled`, `required`, `name` and `className` (`className` *sets* the class attribute for the input element). See [Customize the Input Appearance](#customize-the-input-appearance). |

demo/.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# See https://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
6+
# testing
7+
/coverage
8+
9+
# production
10+
/build
11+
12+
# misc
13+
.DS_Store
14+
.env.local
15+
.env.development.local
16+
.env.test.local
17+
.env.production.local
18+
19+
npm-debug.log*
20+
yarn-debug.log*
21+
yarn-error.log*

demo/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# react-datetime demo app
2+
3+
[Demo](https://YouCanBookMe.github.io/react-datetime)
4+
5+
#### How to Start
6+
7+
```bash
8+
npm start
9+
```
10+
11+
#### How to Deploy
12+
13+
Run "deploy" from the demo directory:
14+
15+
```bash
16+
cd ~/react-datetime/demo
17+
npm run deploy
18+
```
19+
20+
#### How to Run the demo with your local changes
21+
22+
If you are working on some change and you want to use the demo to test them out, you have to link your local "react-datetime" directory to the demo:
23+
24+
```bash
25+
cd ~/react-datetime
26+
npm link
27+
28+
cd demo
29+
npm link react-datetime
30+
31+
npm start
32+
```

0 commit comments

Comments
 (0)