-
Notifications
You must be signed in to change notification settings - Fork 19
feat(DateRangePickerInput/web): max range #196
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import dateFnsIsBefore from 'date-fns/is_before'; | |
| import dateFnsFormat from 'date-fns/format'; | ||
| import dateFnsDifferenceInCalendarDays from 'date-fns/difference_in_calendar_days'; | ||
| import DayPicker from 'react-day-picker'; | ||
| import inRange from '../../utils/inRange'; | ||
| import Card from '../../Card/web'; | ||
| import Flex from '../../Flex/web'; | ||
| import Space from '../../Space/web'; | ||
|
|
@@ -29,6 +30,10 @@ class DateRangePickerInput extends React.Component { | |
| }; | ||
| this.datePickerHasFocus = false; | ||
| this.timeout = {}; | ||
| this.range = { | ||
| min: this.props.range.min >= 0 ? this.props.range.min : 1, | ||
| max: this.props.range.max >= 1 ? this.props.range.max : Infinity, | ||
| }; | ||
| } | ||
|
|
||
| componentDidMount() { | ||
|
|
@@ -104,7 +109,6 @@ class DateRangePickerInput extends React.Component { | |
| onDateRangeChange, | ||
| onFromDateChange, | ||
| onToDateChange, | ||
| minRange, | ||
| } = this.props; | ||
| const { formik } = this.context; | ||
|
|
||
|
|
@@ -145,7 +149,8 @@ class DateRangePickerInput extends React.Component { | |
| }); | ||
| } | ||
| } else if (isOpen === 'to') { | ||
| if (dateFnsDifferenceInCalendarDays(day, from) >= minRange) { | ||
| const selectionRange = dateFnsDifferenceInCalendarDays(day, from); | ||
| if (inRange(selectionRange, this.range.min, this.range.max + 1)) { | ||
| this.setState({ | ||
| isOpen: false, | ||
| to: day, | ||
|
|
@@ -377,8 +382,11 @@ DateRangePickerInput.propTypes = { | |
| from: PropTypes.bool, | ||
| to: PropTypes.bool, | ||
| }), | ||
| range: PropTypes.shape({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also this prop can be renamed to |
||
| min: PropTypes.number, | ||
| max: PropTypes.number, | ||
| }), | ||
| format: PropTypes.string, | ||
| minRange: PropTypes.number, | ||
| fromMonth: PropTypes.instanceOf(Date), | ||
| toMonth: PropTypes.instanceOf(Date), | ||
| modifiers: PropTypes.object, | ||
|
|
@@ -426,7 +434,6 @@ DateRangePickerInput.defaultProps = { | |
| from: false, | ||
| to: false, | ||
| }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can't ignore defaultProps. That's the reason other stories are breaking since they are getting range as undefined in the constructor that's where defaultProps helps you |
||
| minRange: 1, | ||
| format: 'YYYY-MM-DD', | ||
| onDateRangeChange: () => {}, | ||
| onFromDateChange: () => {}, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export default from 'lodash/inRange'; |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather have this in
renderthan here since we should not block props reflow. You can definitely argue that this prop will not change throughout the lifecycle of this component but you can't enforce that on your users/consumers I can haveThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this?
and from render you can call this
const selectionRange = makeSelectionRange(this.props.selectionRange)