Skip to content

Commit 49c42db

Browse files
authored
Merge pull request plotly#854 from plotly/hg-700-persistence
PersistenceTransforms for date in datePickerRange and datePickerSingle
2 parents 8a36e62 + 0519d89 commit 49c42db

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

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+
- [#854](https://github.com/plotly/dash-core-components/pull/854) Used `persistenceTransforms` to strip the time part of the datetime in the persited props of DatePickerSingle (date) and DatePickerRange (end_date, start_date), fixing [dcc#700](https://github.com/plotly/dash-core-components/issues/700).
8+
69
### Added
710
- [#850](https://github.com/plotly/dash-core-components/pull/850) Add property `prependData` to `Graph` to support `Plotly.prependTraces`
811
+ refactored the existing `extendTraces` API to be a single `mergeTraces` API that can handle both `prepend` as well as `extend`.

src/components/DatePickerRange.react.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import PropTypes from 'prop-types';
22
import React, {Component, lazy, Suspense} from 'react';
33
import datePickerRange from '../utils/LazyLoader/datePickerRange';
4+
import transformDate from '../utils/DatePickerPersistence';
45

56
const RealDatePickerRange = lazy(datePickerRange);
67

@@ -263,6 +264,11 @@ DatePickerRange.propTypes = {
263264
persistence_type: PropTypes.oneOf(['local', 'session', 'memory']),
264265
};
265266

267+
DatePickerRange.persistenceTransforms = {
268+
end_date: transformDate,
269+
start_date: transformDate,
270+
};
271+
266272
DatePickerRange.defaultProps = {
267273
calendar_orientation: 'horizontal',
268274
is_RTL: false,

src/components/DatePickerSingle.react.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import PropTypes from 'prop-types';
22
import React, {Component, lazy, Suspense} from 'react';
33
import datePickerSingle from '../utils/LazyLoader/datePickerSingle';
4+
import transformDate from '../utils/DatePickerPersistence';
45

56
const RealDateSingleRange = lazy(datePickerSingle);
67

@@ -220,6 +221,10 @@ DatePickerSingle.propTypes = {
220221
persistence_type: PropTypes.oneOf(['local', 'session', 'memory']),
221222
};
222223

224+
DatePickerSingle.persistenceTransforms = {
225+
date: transformDate,
226+
};
227+
223228
DatePickerSingle.defaultProps = {
224229
calendar_orientation: 'horizontal',
225230
is_RTL: false,

src/utils/DatePickerPersistence.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import moment from 'moment';
2+
import {isNil} from 'ramda';
3+
4+
export default {
5+
extract: propValue => {
6+
if (!isNil(propValue)) {
7+
return moment(propValue)
8+
.startOf('day')
9+
.format('YYYY-MM-DD');
10+
}
11+
return propValue;
12+
},
13+
apply: storedValue => storedValue,
14+
};
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from datetime import datetime
2+
3+
import dash
4+
import dash_html_components as html
5+
import dash_core_components as dcc
6+
from dash.dependencies import Input, Output
7+
8+
9+
def test_rdpr001_persisted_dps(dash_dcc):
10+
app = dash.Dash(__name__)
11+
app.layout = html.Div(
12+
[
13+
html.Button("fire callback", id="btn", n_clicks=1),
14+
html.Div(children=[html.Div(id="container"), html.P("dps", id="dps-p")]),
15+
]
16+
)
17+
18+
# changing value of date with each callback to verify
19+
# persistenceTransforms is stripping the time-part from the date-time
20+
@app.callback(Output("container", "children"), [Input("btn", "n_clicks")])
21+
def update_output(value):
22+
return dcc.DatePickerSingle(
23+
id="dps",
24+
min_date_allowed=datetime(2020, 1, 1),
25+
max_date_allowed=datetime(2020, 1, 7),
26+
date=datetime(2020, 1, 3, 1, 1, 1, value),
27+
persistence=True,
28+
persistence_type="session",
29+
)
30+
31+
@app.callback(Output("dps-p", "children"), [Input("dps", "date")])
32+
def display_dps(value):
33+
return value
34+
35+
dash_dcc.start_server(app)
36+
37+
dash_dcc.select_date_single("dps", day="2")
38+
dash_dcc.wait_for_text_to_equal("#dps-p", "2020-01-02")
39+
dash_dcc.find_element("#btn").click()
40+
dash_dcc.wait_for_text_to_equal("#dps-p", "2020-01-02")
41+
42+
43+
def test_rdpr002_persisted_dpr(dash_dcc):
44+
app = dash.Dash(__name__)
45+
app.layout = html.Div(
46+
[
47+
html.Button("fire callback", id="btn", n_clicks=1),
48+
html.Div(
49+
children=[
50+
html.Div(id="container"),
51+
html.P("dpr", id="dpr-p-start"),
52+
html.P("dpr", id="dpr-p-end"),
53+
]
54+
),
55+
]
56+
)
57+
58+
# changing value of start_date and end_date with each callback to verify
59+
# persistenceTransforms is stripping the time-part from the date-time
60+
@app.callback(Output("container", "children"), [Input("btn", "n_clicks")])
61+
def update_output(value):
62+
return dcc.DatePickerRange(
63+
id="dpr",
64+
min_date_allowed=datetime(2020, 1, 1),
65+
max_date_allowed=datetime(2020, 1, 7),
66+
start_date=datetime(2020, 1, 3, 1, 1, 1, value),
67+
end_date=datetime(2020, 1, 4, 1, 1, 1, value),
68+
persistence=True,
69+
persistence_type="session",
70+
)
71+
72+
@app.callback(Output("dpr-p-start", "children"), [Input("dpr", "start_date")])
73+
def display_dpr_start(value):
74+
return value
75+
76+
@app.callback(Output("dpr-p-end", "children"), [Input("dpr", "end_date")])
77+
def display_dpr_end(value):
78+
return value
79+
80+
dash_dcc.start_server(app)
81+
82+
dash_dcc.select_date_range("dpr", (2, 5))
83+
dash_dcc.wait_for_text_to_equal("#dpr-p-start", "2020-01-02")
84+
dash_dcc.wait_for_text_to_equal("#dpr-p-end", "2020-01-05")
85+
dash_dcc.find_element("#btn").click()
86+
dash_dcc.wait_for_text_to_equal("#dpr-p-start", "2020-01-02")
87+
dash_dcc.wait_for_text_to_equal("#dpr-p-end", "2020-01-05")

0 commit comments

Comments
 (0)