Skip to content

Commit 8d92c77

Browse files
committed
Merge pull request plotly#561 from mmartinsky/master
Fix Min/Max DatePickerRange date
1 parent 648edbf commit 8d92c77

File tree

6 files changed

+73
-22
lines changed

6 files changed

+73
-22
lines changed

packages/dash-core-components/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [Unreleased]
6+
### Fixed
7+
- Fixed `min_date_allowed` and `max_date_allowed` bug in `DatePickerRange` [#551]https://github.com/plotly/dash-core-components/issues/551)
8+
69
### Changed
710
- Changed `dcc.Checklist` prop `values` to `value`, to match all the other input components [#558](https://github.com/plotly/dash-core-components/pull/558). Also improved prop types for `Dropdown` and `RadioItems` `value` props to consistently accept both strings and numbers.
811

packages/dash-core-components/demo/Demo.react.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import ReactDOM from 'react-dom';
33
import Playground from 'component-playground';
44
import {
55
Checklist,
6+
DatePickerRange,
67
Dropdown,
78
Graph,
89
Input,
@@ -297,6 +298,34 @@ class Controller extends Component {
297298
ReactDOM.render(<Controller/>, mountNode);`
298299

299300

301+
const DatePickerRangeExample = `
302+
const properties = {
303+
id: 'my date',
304+
start_date_id: 'start',
305+
end_date_id: 'end'
306+
};
307+
308+
class Controller extends Component {
309+
310+
render() {
311+
// Use last 7, next 7 days as allowed range
312+
const today = new Date();
313+
const min_date_allowed = new Date();
314+
const max_date_allowed = new Date();
315+
min_date_allowed.setDate(today.getDate() - 7);
316+
max_date_allowed.setDate(today.getDate() + 7);
317+
318+
return (<DatePickerRange
319+
// split on time to get YYYY-MM-DD
320+
min_date_allowed={min_date_allowed.toISOString().split('T')[0]}
321+
max_date_allowed={max_date_allowed.toISOString().split('T')[0]}
322+
{...properties}
323+
/>);
324+
}
325+
}
326+
327+
ReactDOM.render(<Controller/>, mountNode);`
328+
300329

301330
const examples = [
302331
{name: 'Upload', code: UploadExample},
@@ -309,22 +338,22 @@ const examples = [
309338
{name: 'Dropdown', code: DropdownExample},
310339
{name: 'Slider', code: SliderExample},
311340
{name: 'RangeSlider', code: RangeSliderExample},
312-
{name: 'Input', code: InputExample}
341+
{name: 'Input', code: InputExample},
342+
{name: 'DatePickerRange', code: DatePickerRangeExample}
313343
];
314344

315345
class Demo extends Component {
316346
render() {
317347
return (
318348
<div style={{'fontFamily': 'Sans-Serif'}}>
319349
<h1>Dash Core Component Suite Demo</h1>
320-
321350
{examples.map((example, index) =>
322351
<div key={index}>
323352
<div style={{'marginBottom': 150}}>
324353
<h3>{example.name}</h3>
325354
<Playground
326355
codeText={example.code}
327-
scope={{Component, React, ReactDOM, Checklist, Dropdown, Graph, Input, RadioItems, RangeSlider, Slider, SyntaxHighlighter, Interval, Markdown, Upload}}
356+
scope={{Component, React, ReactDOM, Checklist, DatePickerRange, Dropdown, Graph, Input, RadioItems, RangeSlider, Slider, SyntaxHighlighter, Interval, Markdown, Upload}}
328357
noRender={false}
329358
theme={'xq-light'}
330359
/>

packages/dash-core-components/package-lock.json

Lines changed: 19 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dash-core-components/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"react-markdown": "^4.0.6",
4545
"react-select-fast-filter-options": "^0.2.3",
4646
"react-syntax-highlighter": "^5.0.0",
47-
"react-virtualized-select": "^3.1.3"
47+
"react-virtualized-select": "^3.1.3",
48+
"uniqid": "^5.0.3"
4849
},
4950
"devDependencies": {
5051
"@babel/core": "^7.4.0",

packages/dash-core-components/src/components/DatePickerRange.react.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'react-dates/initialize';
22
import {DateRangePicker} from 'react-dates';
33
import PropTypes from 'prop-types';
44
import React, {Component} from 'react';
5+
import uniqid from 'uniqid';
56

67
import convertToMoment from '../utils/convertToMoment';
78

@@ -22,7 +23,11 @@ export default class DatePickerRange extends Component {
2223
this.propsToState = this.propsToState.bind(this);
2324
this.onDatesChange = this.onDatesChange.bind(this);
2425
this.isOutsideRange = this.isOutsideRange.bind(this);
25-
this.state = {focused: false};
26+
this.state = {
27+
focused: false,
28+
start_date_id: props.start_date_id || uniqid(),
29+
end_date_id: props.end_date_id || uniqid(),
30+
};
2631
}
2732

2833
propsToState(newProps) {
@@ -69,7 +74,7 @@ export default class DatePickerRange extends Component {
6974
}
7075

7176
isOutsideRange(date) {
72-
const {min_date_allowed, max_date_allowed} = this.state;
77+
const {min_date_allowed, max_date_allowed} = this.props;
7378

7479
return (
7580
(min_date_allowed && date.isBefore(min_date_allowed)) ||
@@ -169,8 +174,8 @@ export default class DatePickerRange extends Component {
169174
with_full_screen_portal && verticalFlag
170175
}
171176
withPortal={with_portal && verticalFlag}
172-
startDateId={start_date_id}
173-
endDateId={end_date_id}
177+
startDateId={start_date_id || this.state.start_date_id}
178+
endDateId={end_date_id || this.state.end_date_id}
174179
/>
175180
</div>
176181
);

packages/dash-core-components/test/test_integration.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,12 +894,16 @@ def test_gallery(self):
894894
html.Label('DatePickerRange'),
895895
dcc.DatePickerRange(
896896
id='date-picker-range',
897+
start_date_id='startDate',
898+
end_date_id='endDate',
897899
start_date=datetime(1997, 5, 3),
898900
end_date_placeholder_text='Select a date!'
899901
),
900902
html.Div([
901903
html.Label('DatePickerRange - empty input'),
902904
dcc.DatePickerRange(
905+
start_date_id='startDate',
906+
end_date_id='endDate',
903907
start_date_placeholder_text='Start date',
904908
end_date_placeholder_text='End date'
905909
),
@@ -908,6 +912,8 @@ def test_gallery(self):
908912
html.Div([
909913
html.Label('DatePickerRange - initial visible month (May 97)'),
910914
dcc.DatePickerRange(
915+
start_date_id='startDate',
916+
end_date_id='endDate',
911917
start_date_placeholder_text='Start date',
912918
end_date_placeholder_text='End date',
913919
initial_visible_month=datetime(1997, 5, 10)
@@ -1650,6 +1656,8 @@ def test_datepickerrange_updatemodes(self):
16501656
app.layout = html.Div([
16511657
dcc.DatePickerRange(
16521658
id='date-picker-range',
1659+
start_date_id='startDate',
1660+
end_date_id='endDate',
16531661
start_date_placeholder_text='Select a start date!',
16541662
end_date_placeholder_text='Select an end date!',
16551663
updatemode='bothdates'

0 commit comments

Comments
 (0)