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

Allow same day input when minimumNights={0} #555

Merged
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
2 changes: 2 additions & 0 deletions src/components/DateRangePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ export default class DateRangePicker extends React.Component {
readOnly,
phrases,
isOutsideRange,
minimumNights,
withPortal,
withFullScreenPortal,
displayFormat,
Expand Down Expand Up @@ -439,6 +440,7 @@ export default class DateRangePicker extends React.Component {
reopenPickerOnClearDates={reopenPickerOnClearDates}
keepOpenOnDateSelect={keepOpenOnDateSelect}
isOutsideRange={isOutsideRange}
minimumNights={minimumNights}
withFullScreenPortal={withFullScreenPortal}
onDatesChange={onDatesChange}
onFocusChange={this.onDateRangePickerInputFocus}
Expand Down
13 changes: 8 additions & 5 deletions src/components/DateRangePickerInputController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import moment from 'moment';

import momentPropTypes from 'react-moment-proptypes';
import { forbidExtraProps } from 'airbnb-prop-types';
import { forbidExtraProps, nonNegativeInteger } from 'airbnb-prop-types';

import { DateRangePickerInputPhrases } from '../defaultPhrases';
import getPhrasePropTypes from '../utils/getPhrasePropTypes';
Expand All @@ -15,7 +15,7 @@ import toLocalizedDateString from '../utils/toLocalizedDateString';
import toISODateString from '../utils/toISODateString';

import isInclusivelyAfterDay from '../utils/isInclusivelyAfterDay';
import isInclusivelyBeforeDay from '../utils/isInclusivelyBeforeDay';
import isBeforeDay from '../utils/isBeforeDay';

import { START_DATE, END_DATE } from '../../constants';

Expand All @@ -41,6 +41,7 @@ const propTypes = forbidExtraProps({
keepOpenOnDateSelect: PropTypes.bool,
reopenPickerOnClearDates: PropTypes.bool,
withFullScreenPortal: PropTypes.bool,
minimumNights: nonNegativeInteger,
isOutsideRange: PropTypes.func,
displayFormat: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),

Expand Down Expand Up @@ -85,6 +86,7 @@ const defaultProps = {
keepOpenOnDateSelect: false,
reopenPickerOnClearDates: false,
withFullScreenPortal: false,
minimumNights: 1,
isOutsideRange: day => !isInclusivelyAfterDay(day, moment()),
displayFormat: () => moment.localeData().longDateFormat('L'),

Expand Down Expand Up @@ -130,14 +132,15 @@ export default class DateRangePickerInputController extends React.Component {
const {
startDate,
isOutsideRange,
minimumNights,
keepOpenOnDateSelect,
onDatesChange,
} = this.props;

const endDate = toMomentObject(endDateString, this.getDisplayFormat());

const isEndDateValid = endDate && !isOutsideRange(endDate) &&
!isInclusivelyBeforeDay(endDate, startDate);
!(startDate && isBeforeDay(endDate, startDate.clone().add(minimumNights, 'days')));
if (isEndDateValid) {
onDatesChange({ startDate, endDate });
if (!keepOpenOnDateSelect) this.onClearFocus();
Expand Down Expand Up @@ -166,10 +169,10 @@ export default class DateRangePickerInputController extends React.Component {
const startDate = toMomentObject(startDateString, this.getDisplayFormat());

let { endDate } = this.props;
const { isOutsideRange, onDatesChange, onFocusChange } = this.props;
const { isOutsideRange, minimumNights, onDatesChange, onFocusChange } = this.props;
const isStartDateValid = startDate && !isOutsideRange(startDate);
if (isStartDateValid) {
if (isInclusivelyBeforeDay(endDate, startDate)) {
if (startDate && isBeforeDay(endDate, startDate.clone().add(minimumNights, 'days'))) {
endDate = null;
}

Expand Down
Loading