Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed unnecessary recalculation of dates #1976

Merged
merged 4 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ export default class DatePickerRange extends Component {
state.end_date = newProps.end_date;
}

if (
force ||
newProps.max_date_allowed !== this.props.max_date_allowed
) {
state.max_date_allowed = convertToMoment(newProps, [
'max_date_allowed',
]).max_date_allowed;
}

if (
force ||
newProps.min_date_allowed !== this.props.min_date_allowed
) {
state.min_date_allowed = convertToMoment(newProps, [
'min_date_allowed',
]).min_date_allowed;
}

if (force || newProps.disabled_days !== this.props.disabled_days) {
state.disabled_days = convertToMoment(newProps, [
'disabled_days',
]).disabled_days;
}

if (Object.keys(state).length) {
this.setState(state);
}
Expand Down Expand Up @@ -82,17 +106,13 @@ export default class DatePickerRange extends Component {
}

isOutsideRange(date) {
const {max_date_allowed, min_date_allowed, disabled_days} =
convertToMoment(this.props, [
'max_date_allowed',
'min_date_allowed',
'disabled_days',
]);

return (
(min_date_allowed && date.isBefore(min_date_allowed)) ||
(max_date_allowed && date.isAfter(max_date_allowed)) ||
(disabled_days && disabled_days.some(d => date.isSame(d, 'day')))
(this.state.min_date_allowed &&
date.isBefore(this.state.min_date_allowed)) ||
(this.state.max_date_allowed &&
date.isAfter(this.state.max_date_allowed)) ||
(this.state.disabled_days &&
this.state.disabled_days.some(d => date.isSame(d, 'day')))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,60 @@ import convertToMoment from '../utils/convertToMoment';
export default class DatePickerSingle extends Component {
constructor() {
super();
this.propsToState = this.propsToState.bind(this);
this.isOutsideRange = this.isOutsideRange.bind(this);
this.onDateChange = this.onDateChange.bind(this);
this.state = {focused: false};
}

isOutsideRange(date) {
const {max_date_allowed, min_date_allowed, disabled_days} =
convertToMoment(this.props, [
propsToState(newProps, force = false) {
const state = {};

if (
force ||
newProps.max_date_allowed !== this.props.max_date_allowed
) {
state.max_date_allowed = convertToMoment(newProps, [
'max_date_allowed',
]).max_date_allowed;
}

if (
force ||
newProps.min_date_allowed !== this.props.min_date_allowed
) {
state.min_date_allowed = convertToMoment(newProps, [
'min_date_allowed',
]).min_date_allowed;
}

if (force || newProps.disabled_days !== this.props.disabled_days) {
state.disabled_days = convertToMoment(newProps, [
'disabled_days',
]);
]).disabled_days;
}

if (Object.keys(state).length) {
this.setState(state);
}
}

UNSAFE_componentWillReceiveProps(newProps) {
this.propsToState(newProps);
}

UNSAFE_componentWillMount() {
nickmelnikov82 marked this conversation as resolved.
Show resolved Hide resolved
this.propsToState(this.props, true);
}

isOutsideRange(date) {
return (
(min_date_allowed && date.isBefore(min_date_allowed)) ||
(max_date_allowed && date.isAfter(max_date_allowed)) ||
(disabled_days && disabled_days.some(d => date.isSame(d, 'day')))
(this.state.min_date_allowed &&
date.isBefore(this.state.min_date_allowed)) ||
(this.state.max_date_allowed &&
date.isAfter(this.state.max_date_allowed)) ||
(this.state.disabled_days &&
this.state.disabled_days.some(d => date.isSame(d, 'day')))
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from datetime import datetime, timedelta
import pandas as pd
import time

import pytest
from dash import Dash, Input, Output, html, dcc, no_update
Expand Down Expand Up @@ -179,3 +181,38 @@ def test_dtps013_disabled_days_arent_clickable(dash_dcc):
# open datepicker to take snapshot
date.click()
dash_dcc.percy_snapshot("dtps013 - disabled days")


def test_dtps0014_disabed_days_timeout(dash_dcc):
app = Dash(__name__)

min_date = pd.to_datetime("2010-01-01")
max_date = pd.to_datetime("2099-01-01")
disabled_days = [
x for x in pd.date_range(min_date, max_date, freq="D") if x.day != 1
]

app.layout = html.Div(
[
html.Label("Operating Date"),
dcc.DatePickerSingle(
id="dps",
min_date_allowed=min_date,
max_date_allowed=max_date,
disabled_days=disabled_days,
),
]
)
dash_dcc.start_server(app)
date = dash_dcc.wait_for_element("#dps", timeout=5)

"""
WebDriver click() function hangs at the time of the react code
execution, so it necessary to check execution time.
"""
start_time = time.time()
date.click()
assert time.time() - start_time < 5

dash_dcc.wait_for_element(".SingleDatePicker_picker", timeout=5)
assert dash_dcc.get_logs() == []