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

Add disabled_days prop to datepickers #948

Merged
merged 7 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- `dcc.send_data_frame` - send a `DataFrame`, using one of its writer methods
- `dcc.send_bytes` - send a bytestring or the result of a bytestring writer
- `dcc.send_string` - send a string or the result of a string writer
- [#948](https://github.com/plotly/dash-core-components/pull/948)] Adds `disabled_days` prop to `DatePickerRange` and `DatePickerSingle` components. With this prop you can specify days that should be made unselectable in the date picker, in addition to those that fall outside of the range specified by `min_date_allowed` and `max_date_allowed`.

### Changed
- [#923](https://github.com/plotly/dash-core-components/pull/923)
Expand Down
8 changes: 8 additions & 0 deletions src/components/DatePickerRange.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ DatePickerRange.propTypes = {
*/
max_date_allowed: PropTypes.string,

/**
* Specifies additional days between min_date_allowed and max_date_allowed
* that should be disabled. Accepted datetime.datetime objects or strings
* in the format 'YYYY-MM-DD'
*/
disabled_days: PropTypes.arrayOf(PropTypes.string),

/**
* Specifies the month that is initially presented when the user
* opens the calendar. Accepts datetime.datetime objects or strings
Expand Down Expand Up @@ -284,6 +291,7 @@ DatePickerRange.defaultProps = {
updatemode: 'singledate',
persisted_props: ['start_date', 'end_date'],
persistence_type: 'local',
disabled_days: [],
};

export const propTypes = DatePickerRange.propTypes;
Expand Down
8 changes: 8 additions & 0 deletions src/components/DatePickerSingle.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ DatePickerSingle.propTypes = {
*/
max_date_allowed: PropTypes.string,

/**
* Specifies additional days between min_date_allowed and max_date_allowed
* that should be disabled. Accepted datetime.datetime objects or strings
* in the format 'YYYY-MM-DD'
*/
disabled_days: PropTypes.arrayOf(PropTypes.string),

/**
* Specifies the month that is initially presented when the user
* opens the calendar. Accepts datetime.datetime objects or strings
Expand Down Expand Up @@ -240,6 +247,7 @@ DatePickerSingle.defaultProps = {
disabled: false,
persisted_props: ['date'],
persistence_type: 'local',
disabled_days: [],
};

export const propTypes = DatePickerSingle.propTypes;
Expand Down
16 changes: 11 additions & 5 deletions src/fragments/DatePickerRange.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,20 @@ export default class DatePickerRange extends Component {
}

isOutsideRange(date) {
const {min_date_allowed, max_date_allowed} = convertToMoment(
this.props,
['min_date_allowed', 'max_date_allowed']
);
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))
(max_date_allowed && date.isAfter(max_date_allowed)) ||
(disabled_days && disabled_days.some(d => date.isSame(d, 'day')))
);
}

Expand Down
16 changes: 11 additions & 5 deletions src/fragments/DatePickerSingle.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ export default class DatePickerSingle extends Component {
}

isOutsideRange(date) {
const {max_date_allowed, min_date_allowed} = convertToMoment(
this.props,
['max_date_allowed', 'min_date_allowed']
);
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))
(max_date_allowed && date.isAfter(max_date_allowed)) ||
(disabled_days && disabled_days.some(d => date.isSame(d, 'day')))
);
}

Expand Down
4 changes: 4 additions & 0 deletions src/utils/convertToMoment.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export default (newProps, momentProps) => {
undefined
);
}
} else if (Array.isArray(value)) {
dest[key] = value.map(d =>
d === null || d === undefined ? null : moment(d)
tcbegley marked this conversation as resolved.
Show resolved Hide resolved
);
} else {
dest[key] = moment(value);

Expand Down