Skip to content

Commit a95cd86

Browse files
Greg Smithmartijnrusschen
Greg Smith
authored andcommitted
Changes to support disabling auto correction of dates (Hacker0x01#769)
* Changes to support disabling auto correction of dates * Added onChangeDate prop to unit test
1 parent 78812d9 commit a95cd86

File tree

5 files changed

+81
-10
lines changed

5 files changed

+81
-10
lines changed

docs-site/src/example_components.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import hljs from 'highlight.js'
33
import Default from './examples/default'
44
import CodeExampleComponent from './code_example_component'
55

6+
import DisableDateAutoCorrection from './examples/disable_date_auto_correction'
67
import CustomDateFormat from './examples/custom_date_format'
78
import CustomClassName from './examples/custom_class_name'
89
import CustomCalendarClassName from './examples/custom_calendar_class_name'
@@ -183,6 +184,10 @@ export default React.createClass({
183184
{
184185
title: 'Get raw input value on change',
185186
component: <RawChange/>
187+
},
188+
{
189+
title: 'Disable date auto correction',
190+
component: <DisableDateAutoCorrection />
186191
}
187192
],
188193

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react'
2+
import moment from 'moment'
3+
import DatePicker from 'react-datepicker'
4+
5+
export default React.createClass({
6+
displayName: 'Disable Date Auto Correction',
7+
8+
getInitialState () {
9+
return {
10+
startDate: moment()
11+
}
12+
},
13+
14+
handleChange (date) {
15+
this.setState({
16+
startDate: date
17+
})
18+
},
19+
20+
render () {
21+
return <div className="row">
22+
<pre className="column example__code">
23+
<code className="jsx">
24+
{'<DatePicker'}<br />
25+
    <strong>{'disableDateAutoCorrection'}<br /></strong>
26+
    {'selected={this.state.startDate}'}<br />
27+
    {'onChange={this.handleChange} />'}
28+
</code>
29+
</pre>
30+
<div className="column">
31+
<DatePicker
32+
disableDateAutoCorrection
33+
selected={this.state.startDate}
34+
onChange={this.handleChange} />
35+
</div>
36+
</div>
37+
}
38+
})

src/date_input.jsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var DateInput = React.createClass({
1313
React.PropTypes.array
1414
]),
1515
disabled: React.PropTypes.bool,
16+
disableDateAutoCorrection: React.PropTypes.bool,
1617
excludeDates: React.PropTypes.array,
1718
filterDate: React.PropTypes.func,
1819
includeDates: React.PropTypes.array,
@@ -27,7 +28,8 @@ var DateInput = React.createClass({
2728

2829
getDefaultProps () {
2930
return {
30-
dateFormat: 'L'
31+
dateFormat: 'L',
32+
disableDateAutoCorrection: false
3133
}
3234
},
3335

@@ -42,9 +44,11 @@ var DateInput = React.createClass({
4244
!isSameUtcOffset(newProps.date, this.props.date) ||
4345
newProps.locale !== this.props.locale ||
4446
newProps.dateFormat !== this.props.dateFormat) {
45-
this.setState({
46-
value: this.safeDateFormat(newProps)
47-
})
47+
if (!this.props.disableDateAutoCorrection || (newProps.date && newProps.date.isValid())) {
48+
this.setState({
49+
value: this.safeDateFormat(newProps)
50+
})
51+
}
4852
}
4953
},
5054

@@ -67,6 +71,8 @@ var DateInput = React.createClass({
6771
this.props.onChangeDate(date)
6872
} else if (value === '') {
6973
this.props.onChangeDate(null)
74+
} else if (this.props.disableDateAutoCorrection && !date.isValid()) {
75+
this.props.onChangeDate(null)
7076
}
7177
}
7278
this.setState({value})
@@ -79,9 +85,12 @@ var DateInput = React.createClass({
7985
},
8086

8187
handleBlur (event) {
82-
this.setState({
83-
value: this.safeDateFormat(this.props)
84-
})
88+
let val = this.safeDateFormat(this.props)
89+
if (!this.props.disableDateAutoCorrection || val !== '') {
90+
this.setState({
91+
value: val
92+
})
93+
}
8594
if (this.props.onBlur) {
8695
this.props.onBlur(event)
8796
}
@@ -92,7 +101,7 @@ var DateInput = React.createClass({
92101
},
93102

94103
render () {
95-
const { customInput, date, locale, minDate, maxDate, excludeDates, includeDates, filterDate, dateFormat, onChangeDate, onChangeRaw, ...rest } = this.props // eslint-disable-line no-unused-vars
104+
const { customInput, date, disableDateAutoCorrection, locale, minDate, maxDate, excludeDates, includeDates, filterDate, dateFormat, onChangeDate, onChangeRaw, ...rest } = this.props // eslint-disable-line no-unused-vars
96105

97106
if (customInput) {
98107
return React.cloneElement(customInput, {

src/datepicker.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var DatePicker = React.createClass({
3030
]),
3131
dateFormatCalendar: React.PropTypes.string,
3232
disabled: React.PropTypes.bool,
33+
disableDateAutoCorrection: React.PropTypes.bool,
3334
disabledKeyboardNavigation: React.PropTypes.bool,
3435
dropdownMode: React.PropTypes.oneOf(['scroll', 'select']).isRequired,
3536
endDate: React.PropTypes.object,
@@ -84,6 +85,7 @@ var DatePicker = React.createClass({
8485
dateFormatCalendar: 'MMMM YYYY',
8586
onChange () {},
8687
disabled: false,
88+
disableDateAutoCorrection: false,
8789
disabledKeyboardNavigation: false,
8890
dropdownMode: 'scroll',
8991
onFocus () {},
@@ -202,7 +204,7 @@ var DatePicker = React.createClass({
202204
return
203205
}
204206

205-
if (!isSameDay(this.props.selected, changedDate)) {
207+
if (!isSameDay(this.props.selected, changedDate) || this.props.disableDateAutoCorrection) {
206208
if (changedDate !== null) {
207209
if (this.props.selected) {
208210
changedDate = moment(changedDate).set({
@@ -349,6 +351,7 @@ var DatePicker = React.createClass({
349351
})
350352
return <DateInput
351353
ref="input"
354+
disableDateAutoCorrection={this.props.disableDateAutoCorrection}
352355
id={this.props.id}
353356
name={this.props.name}
354357
autoFocus={this.props.autoFocus}

test/date_input_test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ describe('DateInput', function () {
271271
})
272272

273273
it('should empty the input if no date selected and input is invalid', function () {
274-
var dateInput = shallow(
274+
var dateInput = mount(
275275
<DateInput />
276276
)
277277
var inputNode = dateInput.find('input')
@@ -284,6 +284,22 @@ describe('DateInput', function () {
284284
inputNode.simulate('blur')
285285
expect(inputNode.prop('value')).to.equal('')
286286
})
287+
288+
it('should leave invalid input when disableDateAutoCorrection is set', function () {
289+
var onChangeDate = sinon.spy()
290+
var dateInput = mount(
291+
<DateInput disableDateAutoCorrection onChangeDate={onChangeDate} />
292+
)
293+
var inputNode = dateInput.find('input')
294+
inputNode.simulate('change', {
295+
isDefaultPrevented: () => false,
296+
target: {
297+
value: 'invalid'
298+
}
299+
})
300+
inputNode.simulate('blur')
301+
expect(inputNode.prop('value')).to.equal('invalid')
302+
})
287303
})
288304

289305
describe('localization', function () {

0 commit comments

Comments
 (0)