Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Issue434 - Clear date in DatePickerSingle and DatePickerRange #435

Merged
merged 7 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Fixed
- Clear date in DatePickerSingle and DatePickerRange [#434](https://github.com/plotly/dash-core-components/issues/434)

## [0.42.1] - 2019-01-07
### Fixed
- Fix `dcc.Store` type changes [#427](https://github.com/plotly/dash-core-components/pull/427)
Expand Down
152 changes: 87 additions & 65 deletions dash_core_components/dash_core_components.dev.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dash_core_components/dash_core_components.dev.js.map

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions dash_core_components/dash_core_components.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dash_core_components/dash_core_components.min.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions dash_core_components/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2789,6 +2789,9 @@
},
{
"name": "string"
},
{
"name": "bool"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some previous commit the metadata was not updated correctly.

}
]
},
Expand Down
19 changes: 6 additions & 13 deletions src/components/DatePickerRange.react.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {DateRangePicker} from 'react-dates';
import moment from 'moment';
import PropTypes from 'prop-types';
import R from 'ramda';
import React, {Component} from 'react';
import './css/[email protected]';

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

/**
* DatePickerRange is a tailor made component designed for selecting
Expand All @@ -30,22 +30,15 @@ export default class DatePickerRange extends Component {
* - user modifiable attributes
* - moment converted attributes
*/
const newState = {};
const momentProps = [

const newState = convertToMoment(newProps, [
'start_date',
'end_date',
'initial_visible_month',
'max_date_allowed',
'min_date_allowed',
];
momentProps.forEach(prop => {
if (R.type(newProps[prop]) !== 'Undefined') {
newState[prop] = moment(newProps[prop]);
}
if (prop === 'max_date_allowed' && R.has(prop, newState)) {
newState[prop].add(1, 'days');
}
});
]);

this.setState(newState);
}

Expand Down
18 changes: 7 additions & 11 deletions src/components/DatePickerSingle.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import PropTypes from 'prop-types';
import R from 'ramda';
import React, {Component} from 'react';

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

/**
* DatePickerSingle is a tailor made component designed for selecting
* a single day off of a calendar.
Expand All @@ -15,6 +17,7 @@ import React, {Component} from 'react';
* This component is based off of Airbnb's react-dates react component
* which can be found here: https://github.com/airbnb/react-dates
*/

export default class DatePickerSingle extends Component {
constructor() {
super();
Expand All @@ -29,21 +32,14 @@ export default class DatePickerSingle extends Component {
* - user modifiable attributes
* - moment converted attributes
*/
const newState = {};
const momentProps = [

const newState = convertToMoment(newProps, [
'date',
'initial_visible_month',
'max_date_allowed',
'min_date_allowed',
];
momentProps.forEach(prop => {
if (R.type(newProps[prop]) !== 'Undefined') {
newState[prop] = moment(newProps[prop]);
}
if (prop === 'max_date_allowed' && R.has(prop, newState)) {
newState[prop].add(1, 'days');
}
});
]);

this.setState(newState);
}

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import Tab from './components/Tab.react';
import Store from './components/Store.react';
import LogoutButton from './components/LogoutButton.react';

import './components/css/[email protected]';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this style dependency to the top level -- jest is not able to handle css files correctly atm


export {
Checklist,
ConfirmDialog,
Expand Down
26 changes: 26 additions & 0 deletions src/utils/convertToMoment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import moment from 'moment';
import R from 'ramda';

export default (newProps, momentProps) => {
const dest = {};

momentProps.forEach(key => {
const value = newProps[key];

switch (R.type(value)) {
case 'Undefined':
return;
case 'Null':
dest[key] = null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Common logic between Single and Range + the new Null case

return;
default:
dest[key] = moment(value);
}

if (key === 'max_date_allowed' && R.has(key, dest)) {
dest[key].add(1, 'days');
}
});

return dest;
};
46 changes: 46 additions & 0 deletions test/unit/DatePickerRange.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import DatePickerRange from '../../src/components/DatePickerRange.react';
import R from 'ramda';
import React from 'react';
import { mount, render } from 'enzyme';

test('DatePickerRange renders', () => {
const dps = render(<DatePickerRange />);

expect(dps.html()).toBeDefined();
});

describe('Date can be set properly', () => {
const defaultProps = {

};

test('null date is not converted by moment', () => {
const props = R.merge(defaultProps, {
end_date: null,
start_date: null
});

const dps = mount(<DatePickerRange {...props} />);

expect(dps.props()).toBeDefined();
expect(dps.props().end_date).toEqual(props.end_date);
expect(dps.state().end_date).toEqual(null);
expect(dps.props().start_date).toEqual(props.start_date);
expect(dps.state().start_date).toEqual(null);
});

test('valid date is not converted by moment', () => {
const props = R.merge(defaultProps, {
end_date: "2019-01-01",
start_date: "2019-01-01"
});

const dps = mount(<DatePickerRange {...props} />);

expect(dps.props()).toBeDefined();
expect(dps.props().end_date).toEqual(props.end_date);
expect(dps.state().end_date).not.toEqual(null);
expect(dps.props().start_date).toEqual(props.start_date);
expect(dps.state().start_date).not.toEqual(null);
});
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basic tests for range: passing null sets null in the state, otherwise it sets a value (do not care about the exact value atm, just differentiating the 2 cases)

40 changes: 40 additions & 0 deletions test/unit/DatePickerSingle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import DatePickerSingle from '../../src/components/DatePickerSingle.react';
import R from 'ramda';
import React from 'react';
import { mount, render } from 'enzyme';

test('DatePickerSingle renders', () => {
const dps = render(<DatePickerSingle />);

expect(dps.html()).toBeDefined();
});

describe('Date can be set properly', () => {
const defaultProps = {

};

test('null date is not converted by moment', () => {
const props = R.merge(defaultProps, {
date: null
});

const dps = mount(<DatePickerSingle {...props} />);

expect(dps.props()).toBeDefined();
expect(dps.props().date).toEqual(props.date);
expect(dps.state().date).toEqual(null);
});

test('valid date is not converted by moment', () => {
const props = R.merge(defaultProps, {
date: "2019-01-01"
});

const dps = mount(<DatePickerSingle {...props} />);

expect(dps.props()).toBeDefined();
expect(dps.props().date).toEqual(props.date);
expect(dps.state().date).not.toEqual(null);
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basic tests for single: passing null sets null in the state, otherwise it sets a value (do not care about the exact value atm, just differentiating the 2 cases)

});