Skip to content
This repository was archived by the owner on Nov 16, 2018. It is now read-only.

Commit f5d4cb1

Browse files
committed
Merge pull request #28 from quri/free-input-value
Free input value
2 parents 0003dc7 + a3a9ee9 commit f5d4cb1

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ DateTimeField
3131

3232
| Name | Type | Default | Description |
3333
| ------------ | ------- | ------- | ----------- |
34-
| **dateTime** | string | "1234567" | Represents the inital dateTime, this string is then parsed by moment.js |
35-
| **format** | string | "X" | Defines the format moment.js should use to parse and output the date to onChange |
36-
| **inputFormat** | string | "MM/DD/YY H:mm A" | Defines the way the date is represented in the HTML input |
34+
| **dateTime** | string | moment().format('x') | Represents the inital dateTime, this string is then parsed by moment.js |
35+
| **format** | string | "x" | Defines the format moment.js should use to parse and output the date to onChange |
36+
| **inputFormat** | string | "MM/DD/YY h:mm A" | Defines the way the date is represented in the HTML input |
3737
| **onChange** | function | x => console.log(x) | Callback trigger when the date changes |
3838
| **showToday** | boolean | true | Highlights today's date |
3939
| **daysOfWeekDisabled** | array of integer | [] | Disables clicking on some days. Goes from 0 (Sunday) to 6 (Saturday). |

src/DateTimeField.jsx

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ DateTimeField = React.createClass({
1717
},
1818
getDefaultProps: function() {
1919
return {
20-
dateTime: "1234567",
21-
format: 'X',
22-
inputFormat: "MM/DD/YY H:mm A",
20+
dateTime: moment().format('x'),
21+
format: 'x',
22+
inputFormat: "MM/DD/YY h:mm A",
2323
showToday: true,
2424
viewMode: 'days',
2525
daysOfWeekDisabled: [],
@@ -38,31 +38,35 @@ DateTimeField = React.createClass({
3838
left: -9999,
3939
zIndex: '9999 !important'
4040
},
41-
viewDate: moment(this.props.dateTime, this.props.format).startOf("month"),
42-
selectedDate: moment(this.props.dateTime, this.props.format),
43-
inputValue: moment(this.props.dateTime, this.props.format).format(this.props.inputFormat)
41+
viewDate: moment(this.props.dateTime, this.props.format, true).startOf("month"),
42+
selectedDate: moment(this.props.dateTime, this.props.format, true),
43+
inputValue: moment(this.props.dateTime, this.props.format, true).format(this.props.inputFormat)
4444
};
4545
},
4646
componentWillReceiveProps: function(nextProps) {
47-
return this.setState({
48-
viewDate: moment(nextProps.dateTime, nextProps.format).startOf("month"),
49-
selectedDate: moment(nextProps.dateTime, nextProps.format),
50-
inputValue: moment(nextProps.dateTime, nextProps.format).format(nextProps.inputFormat)
51-
});
47+
if(moment(nextProps.dateTime, nextProps.format, true).isValid()) {
48+
return this.setState({
49+
viewDate: moment(nextProps.dateTime, nextProps.format, true).startOf("month"),
50+
selectedDate: moment(nextProps.dateTime, nextProps.format, true),
51+
inputValue: moment(nextProps.dateTime, nextProps.format, true).format(nextProps.inputFormat)
52+
});
53+
}
5254
},
5355
onChange: function(event) {
54-
if (moment(event.target.value, this.props.format).isValid()) {
56+
var value = event.target == null ? event : event.target.value;
57+
if (moment(value, this.props.inputFormat, true).isValid()) {
5558
this.setState({
56-
selectedDate: moment(event.target.value, this.props.format),
57-
inputValue: moment(event.target.value, this.props.format).format(this.props.inputFormat)
59+
selectedDate: moment(value, this.props.inputFormat, true),
60+
viewDate: moment(value, this.props.inputFormat, true).startOf("month")
5861
});
59-
} else {
60-
this.setState({
61-
inputValue: event.target.value
62-
});
63-
console.log("This is not a valid date");
6462
}
65-
return this.props.onChange(this.state.selectedDate.format(this.props.format));
63+
64+
return this.setState({
65+
inputValue: value
66+
}, function() {
67+
return this.props.onChange(moment(this.state.inputValue, this.props.inputFormat, true).format(this.props.format));
68+
});
69+
6670
},
6771
setSelectedDate: function(e) {
6872
return this.setState({
@@ -167,13 +171,9 @@ DateTimeField = React.createClass({
167171
},
168172
togglePeriod: function() {
169173
if (this.state.selectedDate.hour() > 12) {
170-
return this.setState({
171-
selectedDate: this.state.selectedDate.clone().subtract(12, 'hours')
172-
});
174+
return this.onChange(this.state.selectedDate.clone().subtract(12, 'hours').format(this.props.inputFormat));
173175
} else {
174-
return this.setState({
175-
selectedDate: this.state.selectedDate.clone().add(12, 'hours')
176-
});
176+
return this.onChange(this.state.selectedDate.clone().add(12, 'hours').format(this.props.inputFormat));
177177
}
178178
},
179179
togglePicker: function() {
@@ -285,7 +285,7 @@ DateTimeField = React.createClass({
285285
togglePeriod={this.togglePeriod}
286286
/>
287287
<div className="input-group date" ref="datetimepicker">
288-
<input type="text" className="form-control" onChange={this.onChange} value={this.state.selectedDate.format(this.props.inputFormat)} />
288+
<input type="text" className="form-control" onChange={this.onChange} value={this.state.inputValue} />
289289
<span className="input-group-addon" onClick={this.onClick} onBlur={this.onBlur} ref="dtpbutton"><Glyphicon glyph="calendar" /></span>
290290
</div>
291291
</div>

0 commit comments

Comments
 (0)