react-dates
Version:
A responsive and accessible date range picker component built with React
115 lines (94 loc) • 4.83 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
import isDayVisible from './isDayVisible';
import toISODateString from './toISODateString';
import toISOMonthString from './toISOMonthString';
import getPreviousMonthMemoLast from './getPreviousMonthMemoLast';
import { VERTICAL_SCROLLABLE } from '../constants';
export function addModifier(updatedDays, day, modifier, props, state) {
var numberOfVisibleMonths = props.numberOfMonths,
enableOutsideDays = props.enableOutsideDays,
orientation = props.orientation;
var firstVisibleMonth = state.currentMonth,
visibleDays = state.visibleDays;
var currentMonth = firstVisibleMonth;
var numberOfMonths = numberOfVisibleMonths;
if (orientation === VERTICAL_SCROLLABLE) {
numberOfMonths = Object.keys(visibleDays).length;
} else {
currentMonth = getPreviousMonthMemoLast(currentMonth);
numberOfMonths += 2;
}
if (!day || !isDayVisible(day, currentMonth, numberOfMonths, enableOutsideDays)) {
return updatedDays;
}
var iso = toISODateString(day);
var updatedDaysAfterAddition = _objectSpread({}, updatedDays);
if (enableOutsideDays) {
var monthsToUpdate = Object.keys(visibleDays).filter(function (monthKey) {
return Object.keys(visibleDays[monthKey]).indexOf(iso) > -1;
});
updatedDaysAfterAddition = monthsToUpdate.reduce(function (acc, monthIso) {
var month = updatedDays[monthIso] || visibleDays[monthIso];
if (!month[iso] || !month[iso].has(modifier)) {
var modifiers = new Set(month[iso]);
modifiers.add(modifier);
acc[monthIso] = _objectSpread({}, month, _defineProperty({}, iso, modifiers));
}
return acc;
}, updatedDaysAfterAddition);
} else {
var monthIso = toISOMonthString(day);
var month = updatedDays[monthIso] || visibleDays[monthIso] || {};
if (!month[iso] || !month[iso].has(modifier)) {
var modifiers = new Set(month[iso]);
modifiers.add(modifier);
updatedDaysAfterAddition[monthIso] = _objectSpread({}, month, _defineProperty({}, iso, modifiers));
}
}
return updatedDaysAfterAddition;
}
export function deleteModifier(updatedDays, day, modifier, props, state) {
var numberOfVisibleMonths = props.numberOfMonths,
enableOutsideDays = props.enableOutsideDays,
orientation = props.orientation;
var firstVisibleMonth = state.currentMonth,
visibleDays = state.visibleDays;
var currentMonth = firstVisibleMonth;
var numberOfMonths = numberOfVisibleMonths;
if (orientation === VERTICAL_SCROLLABLE) {
numberOfMonths = Object.keys(visibleDays).length;
} else {
currentMonth = getPreviousMonthMemoLast(currentMonth);
numberOfMonths += 2;
}
if (!day || !isDayVisible(day, currentMonth, numberOfMonths, enableOutsideDays)) {
return updatedDays;
}
var iso = toISODateString(day);
var updatedDaysAfterDeletion = _objectSpread({}, updatedDays);
if (enableOutsideDays) {
var monthsToUpdate = Object.keys(visibleDays).filter(function (monthKey) {
return Object.keys(visibleDays[monthKey]).indexOf(iso) > -1;
});
updatedDaysAfterDeletion = monthsToUpdate.reduce(function (acc, monthIso) {
var month = updatedDays[monthIso] || visibleDays[monthIso];
if (month[iso] && month[iso].has(modifier)) {
var modifiers = new Set(month[iso]);
modifiers["delete"](modifier);
acc[monthIso] = _objectSpread({}, month, _defineProperty({}, iso, modifiers));
}
return acc;
}, updatedDaysAfterDeletion);
} else {
var monthIso = toISOMonthString(day);
var month = updatedDays[monthIso] || visibleDays[monthIso] || {};
if (month[iso] && month[iso].has(modifier)) {
var modifiers = new Set(month[iso]);
modifiers["delete"](modifier);
updatedDaysAfterDeletion[monthIso] = _objectSpread({}, month, _defineProperty({}, iso, modifiers));
}
}
return updatedDaysAfterDeletion;
}