Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
31 changes: 29 additions & 2 deletions src/DateRangePickerInput/makeStories.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,38 @@ export default (storiesOf, {
onToDateChange={action('onToDateChange')}
/>
))
.add('same day', () => (
.add('range max 0', () => (
<DateRangePickerInput
name={{ from: 'TGIM', to: 'TGIF' }}
disabledDays={[{ daysOfWeek: [0, 6] }]}
minRange={0}
range={{
min: 0,
}}
onDateRangeChange={action('onDateRangeChange')}
onFromDateChange={action('onFromDateChange')}
onToDateChange={action('onToDateChange')}
/>
))
.add('range max 5', () => (
<DateRangePickerInput
name={{ from: 'TGIM', to: 'TGIF' }}
disabledDays={[{ daysOfWeek: [0, 6] }]}
range={{
max: 5,
}}
onDateRangeChange={action('onDateRangeChange')}
onFromDateChange={action('onFromDateChange')}
onToDateChange={action('onToDateChange')}
/>
))
.add('range min 2 max 5', () => (
<DateRangePickerInput
name={{ from: 'TGIM', to: 'TGIF' }}
disabledDays={[{ daysOfWeek: [0, 6] }]}
range={{
min: 2,
max: 5,
}}
onDateRangeChange={action('onDateRangeChange')}
onFromDateChange={action('onFromDateChange')}
onToDateChange={action('onToDateChange')}
Expand Down
15 changes: 11 additions & 4 deletions src/DateRangePickerInput/web/DateRangePickerInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Copy link
Copy Markdown
Contributor

@kamleshchandnani kamleshchandnani May 4, 2019

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 render than 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 have

<DateRangePickerInput
range={{ min: someValueFromState, max: someValueFromState}}
/>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How about this?

makeSelectionRange = (selectionRange) => ({
    min: selectionRange.hasOwnProperty('min') ? selectionRange.min : 1,
    max: selectionRange.hasOwnProperty('max') ? selectionRange.max : Infinity,
  })

and from render you can call this
const selectionRange = makeSelectionRange(this.props.selectionRange)

max: this.props.range.max >= 1 ? this.props.range.max : Infinity,
};
}

componentDidMount() {
Expand Down Expand Up @@ -104,7 +109,6 @@ class DateRangePickerInput extends React.Component {
onDateRangeChange,
onFromDateChange,
onToDateChange,
minRange,
} = this.props;
const { formik } = this.context;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -377,8 +382,11 @@ DateRangePickerInput.propTypes = {
from: PropTypes.bool,
to: PropTypes.bool,
}),
range: PropTypes.shape({
Copy link
Copy Markdown
Contributor

@kamleshchandnani kamleshchandnani May 4, 2019

Choose a reason for hiding this comment

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

also this prop can be renamed to selectionRange because it's too confusing otherwise?

min: PropTypes.number,
max: PropTypes.number,
}),
format: PropTypes.string,
minRange: PropTypes.number,
fromMonth: PropTypes.instanceOf(Date),
toMonth: PropTypes.instanceOf(Date),
modifiers: PropTypes.object,
Expand Down Expand Up @@ -426,7 +434,6 @@ DateRangePickerInput.defaultProps = {
from: false,
to: false,
},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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: () => {},
Expand Down
1 change: 1 addition & 0 deletions src/utils/inRange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default from 'lodash/inRange';