Socket
Socket
Sign inDemoInstall

react-day-picker

Package Overview
Dependencies
Maintainers
2
Versions
243
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-day-picker - npm Package Compare versions

Comparing version 9.0.4 to 9.0.5

examples/MultipleRequired.tsx

22

dist/cjs/selection/useRange.js

@@ -12,3 +12,2 @@ "use strict";

const { mode, disabled, excludeDisabled, selected: initiallySelected, required, onSelect } = props;
const { differenceInCalendarDays } = dateLib;
const [selected, setSelected] = react_1.default.useState(initiallySelected);

@@ -27,22 +26,6 @@ // Update the selected date if the required flag is set.

const select = (triggerDate, modifiers, e) => {
const { min, max } = props;
const newRange = triggerDate
? (0, index_js_1.addToRange)(triggerDate, selected, dateLib)
? (0, index_js_1.addToRange)(triggerDate, selected, min, max, required, dateLib)
: undefined;
const { min, max } = props;
if (min) {
if (newRange?.from &&
newRange.to &&
differenceInCalendarDays(newRange.to, newRange.from) < min - 1) {
newRange.from = triggerDate;
newRange.to = undefined;
}
}
if (max) {
if (newRange?.from &&
newRange.to &&
differenceInCalendarDays(newRange.to, newRange.from) >= max) {
newRange.from = triggerDate;
newRange.to = undefined;
}
}
if (newRange?.from && newRange.to) {

@@ -55,2 +38,3 @@ let newDate = newRange.from;

(0, index_js_1.dateMatchModifiers)(newDate, disabled, dateLib)) {
// if a disabled days is found, the range is reset
newRange.from = triggerDate;

@@ -57,0 +41,0 @@ newRange.to = undefined;

@@ -10,4 +10,8 @@ import type { DateRange, DateLib } from "../types/index.js";

*/
export declare function addToRange(date: Date, range: DateRange | undefined,
export declare function addToRange(
/** The date to add to the range. */
date: Date,
/** The range where to add `date`. */
initialRange: DateRange | undefined, min?: number, max?: number, required?: boolean,
/** @ignore */
dateLib?: DateLib): DateRange;
dateLib?: DateLib): DateRange | undefined;

@@ -13,39 +13,83 @@ "use strict";

*/
function addToRange(date, range,
function addToRange(
/** The date to add to the range. */
date,
/** The range where to add `date`. */
initialRange, min = 0, max = 0, required = false,
/** @ignore */
dateLib = index_js_1.dateLib) {
const { from, to } = range || {};
const { from, to } = initialRange || {};
const { isSameDay, isAfter, isBefore } = dateLib;
if (from && to) {
if (isSameDay(to, date) && isSameDay(from, date)) {
return { from: undefined, to: undefined };
let range;
if (!from && !to) {
// the range is empty, add the date
range = { from: date, to: min > 0 ? undefined : date };
}
else if (from && !to) {
// adding date to an incomplete range
if (isSameDay(from, date)) {
// adding a date equal to the start of the range
if (required) {
range = { from, to: undefined };
}
else {
range = undefined;
}
}
if (isSameDay(to, date)) {
return { from: to, to: undefined };
else if (isBefore(date, from)) {
// adding a date before the start of the range
range = { from: date, to: from };
}
if (isSameDay(from, date)) {
return { from: undefined, to: undefined };
else {
// adding a date after the start of the range
range = { from, to: date };
}
if (isAfter(from, date)) {
return { from: date, to };
}
return { from, to: date };
}
if (to) {
if (isAfter(date, to)) {
return { from: to, to: date };
else if (from && to) {
// adding date to a complete range
if (isSameDay(from, date) && isSameDay(to, date)) {
// adding a date that is equal to both start and end of the range
if (required) {
range = { from, to };
}
else {
range = undefined;
}
}
return { from: date, to };
else if (isSameDay(from, date)) {
// adding a date equal to the the start of the range
range = { from, to: min > 0 ? undefined : date };
}
else if (isSameDay(to, date)) {
// adding a dare equal to the end of the range
range = { from: date, to: min > 0 ? undefined : date };
}
else if (isBefore(date, from)) {
// adding a date before the start of the range
range = { from: date, to: to };
}
else if (isAfter(date, from)) {
// adding a date after the start of the range
range = { from, to: date };
}
else if (isAfter(date, to)) {
// adding a date after the end of the range
range = { from, to: date };
}
else {
throw new Error("Invalid range");
}
}
if (from) {
if (isBefore(date, from)) {
return { from: date, to: from };
// check for min / max
if (range?.from && range?.to) {
const diff = dateLib.differenceInCalendarDays(range.to, range.from);
if (max > 0 && diff > max) {
range = { from: date, to: undefined };
}
if (isSameDay(date, from)) {
return { from: undefined, to: undefined };
else if (min > 1 && diff < min) {
range = { from: date, to: undefined };
}
return { from, to: date };
}
return { from: date, to: undefined };
return range;
}
//# sourceMappingURL=addToRange.js.map

@@ -6,3 +6,2 @@ import React from "react";

const { mode, disabled, excludeDisabled, selected: initiallySelected, required, onSelect } = props;
const { differenceInCalendarDays } = dateLib;
const [selected, setSelected] = React.useState(initiallySelected);

@@ -21,22 +20,6 @@ // Update the selected date if the required flag is set.

const select = (triggerDate, modifiers, e) => {
const { min, max } = props;
const newRange = triggerDate
? addToRange(triggerDate, selected, dateLib)
? addToRange(triggerDate, selected, min, max, required, dateLib)
: undefined;
const { min, max } = props;
if (min) {
if (newRange?.from &&
newRange.to &&
differenceInCalendarDays(newRange.to, newRange.from) < min - 1) {
newRange.from = triggerDate;
newRange.to = undefined;
}
}
if (max) {
if (newRange?.from &&
newRange.to &&
differenceInCalendarDays(newRange.to, newRange.from) >= max) {
newRange.from = triggerDate;
newRange.to = undefined;
}
}
if (newRange?.from && newRange.to) {

@@ -49,2 +32,3 @@ let newDate = newRange.from;

dateMatchModifiers(newDate, disabled, dateLib)) {
// if a disabled days is found, the range is reset
newRange.from = triggerDate;

@@ -51,0 +35,0 @@ newRange.to = undefined;

@@ -10,4 +10,8 @@ import type { DateRange, DateLib } from "../types/index.js";

*/
export declare function addToRange(date: Date, range: DateRange | undefined,
export declare function addToRange(
/** The date to add to the range. */
date: Date,
/** The range where to add `date`. */
initialRange: DateRange | undefined, min?: number, max?: number, required?: boolean,
/** @ignore */
dateLib?: DateLib): DateRange;
dateLib?: DateLib): DateRange | undefined;

@@ -10,39 +10,83 @@ import { dateLib as defaultDateLib } from "../lib/index.js";

*/
export function addToRange(date, range,
export function addToRange(
/** The date to add to the range. */
date,
/** The range where to add `date`. */
initialRange, min = 0, max = 0, required = false,
/** @ignore */
dateLib = defaultDateLib) {
const { from, to } = range || {};
const { from, to } = initialRange || {};
const { isSameDay, isAfter, isBefore } = dateLib;
if (from && to) {
if (isSameDay(to, date) && isSameDay(from, date)) {
return { from: undefined, to: undefined };
let range;
if (!from && !to) {
// the range is empty, add the date
range = { from: date, to: min > 0 ? undefined : date };
}
else if (from && !to) {
// adding date to an incomplete range
if (isSameDay(from, date)) {
// adding a date equal to the start of the range
if (required) {
range = { from, to: undefined };
}
else {
range = undefined;
}
}
if (isSameDay(to, date)) {
return { from: to, to: undefined };
else if (isBefore(date, from)) {
// adding a date before the start of the range
range = { from: date, to: from };
}
if (isSameDay(from, date)) {
return { from: undefined, to: undefined };
else {
// adding a date after the start of the range
range = { from, to: date };
}
if (isAfter(from, date)) {
return { from: date, to };
}
return { from, to: date };
}
if (to) {
if (isAfter(date, to)) {
return { from: to, to: date };
else if (from && to) {
// adding date to a complete range
if (isSameDay(from, date) && isSameDay(to, date)) {
// adding a date that is equal to both start and end of the range
if (required) {
range = { from, to };
}
else {
range = undefined;
}
}
return { from: date, to };
else if (isSameDay(from, date)) {
// adding a date equal to the the start of the range
range = { from, to: min > 0 ? undefined : date };
}
else if (isSameDay(to, date)) {
// adding a dare equal to the end of the range
range = { from: date, to: min > 0 ? undefined : date };
}
else if (isBefore(date, from)) {
// adding a date before the start of the range
range = { from: date, to: to };
}
else if (isAfter(date, from)) {
// adding a date after the start of the range
range = { from, to: date };
}
else if (isAfter(date, to)) {
// adding a date after the end of the range
range = { from, to: date };
}
else {
throw new Error("Invalid range");
}
}
if (from) {
if (isBefore(date, from)) {
return { from: date, to: from };
// check for min / max
if (range?.from && range?.to) {
const diff = dateLib.differenceInCalendarDays(range.to, range.from);
if (max > 0 && diff > max) {
range = { from: date, to: undefined };
}
if (isSameDay(date, from)) {
return { from: undefined, to: undefined };
else if (min > 1 && diff < min) {
range = { from: date, to: undefined };
}
return { from, to: date };
}
return { from: date, to: undefined };
return range;
}
//# sourceMappingURL=addToRange.js.map

@@ -41,2 +41,3 @@ export * from "./AccessibleDatePicker";

export * from "./MultipleMinMax";
export * from "./MultipleRequired";
export * from "./MultipleMonths";

@@ -49,2 +50,3 @@ export * from "./MultipleMonthsPaged";

export * from "./RangeMinMax";
export * from "./RangeRequired";
export * from "./RangeShiftKey";

@@ -51,0 +53,0 @@ export * from "./Rtl";

{
"name": "react-day-picker",
"version": "9.0.4",
"version": "9.0.5",
"description": "Customizable Date Picker for React",

@@ -147,3 +147,3 @@ "author": "Giampaolo Bellavite <io@gpbl.dev>",

"@types/react-dom": "^18.3.0",
"@typescript-eslint/eslint-plugin": "^7.16.1",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.16.0",

@@ -173,3 +173,3 @@ "date-fns": "^3.6.0",

"tslib": "^2.6.3",
"typescript": "~5.5.3",
"typescript": "~5.5.4",
"typescript-css-modules": "^1.0.4"

@@ -176,0 +176,0 @@ },

@@ -14,3 +14,3 @@ # React DayPicker

- 🦮 Complies with WCAG 2.1 AA requirements for [accessibility](./docs/accessibility.mdx).
- ⚙️ [Customizable components](./guides/custom-components.mdx) for more complex use cases.
- ⚙️ [Customizable components](./guides/custom-components.mdx) to extend the rendered elements.
- 🔤 Easy integration [with input fields](./guides/input-fields.mdx).

@@ -17,0 +17,0 @@

@@ -1,119 +0,117 @@

import { addDays, subDays } from "date-fns";
import { addToRange } from "./addToRange";
import type { DateRange } from "../types";
describe("addToRange", () => {
test("add a date to an undefined range", () => {
const date = new Date(2022, 0, 1);
const range = addToRange(date, undefined);
expect(range).toEqual({ from: date, to: date });
});
import { addToRange } from "./addToRange";
test("add a date to an empty range", () => {
const date = new Date(2022, 0, 1);
const range = addToRange(date, { from: undefined, to: undefined });
expect(range).toEqual({ from: date, to: date });
});
describe('when no "from" is the range', () => {
const range = { from: undefined };
const day = new Date();
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
test("add a date to an incomplete range with same start date", () => {
const date = new Date(2022, 0, 1);
const range = addToRange(date, { from: date, to: undefined });
expect(range).toEqual(undefined);
});
test('should set "from" as the given day', () => {
expect(result).toEqual({ from: day, to: undefined });
test("add a date to an incomplete range with earlier date", () => {
const from = new Date(2022, 0, 1);
const earlierDate = new Date(2021, 11, 31);
const range = addToRange(earlierDate, { from: from, to: undefined });
expect(range).toEqual({ from: earlierDate, to: from });
});
});
describe('when no "to" is the range', () => {
const day = new Date();
const range = { from: day, to: undefined };
describe('and the day is the same as the "from" day', () => {
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
});
test("should reset the range", () => {
expect(result).toEqual({ from: undefined, to: undefined });
});
test("add a date to an incomplete range with later date", () => {
const from = new Date(2022, 0, 1);
const date = new Date(2022, 0, 2);
const range = addToRange(date, { from: from, to: undefined });
expect(range).toEqual({ from: from, to: date });
});
describe('and the day is before "from" day', () => {
const day = subDays(range.from, 1);
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
});
test('should set the day as the "from" range', () => {
expect(result).toEqual({ from: day, to: range.from });
});
test("add a date to a complete range with same start and end date", () => {
const date = new Date(2022, 0, 1);
const from = date;
const to = date;
const range = addToRange(date, { from, to }, 0, 0, false);
expect(range).toEqual(undefined);
});
describe('and the day is after the "from" day', () => {
const day = addDays(range.from, 1);
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
});
test('should set the day as the "to" date', () => {
expect(result).toEqual({ from: range.from, to: day });
});
test("add a date to a complete range with same start date", () => {
const date = new Date(2022, 0, 1);
const to = new Date(2022, 0, 2);
const range = addToRange(date, { from: date, to: to });
expect(range).toEqual({ from: date, to: date });
});
});
describe('when "from", "to" and "day" are the same', () => {
const day = new Date();
const range = { from: day, to: day };
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
test("add a date to a complete range with same end date", () => {
const date = new Date(2022, 0, 2);
const from = new Date(2022, 0, 1);
const range = addToRange(date, { from: from, to: date });
expect(range).toEqual({ from: date, to: date });
});
test("should return an undefined range (reset)", () => {
expect(result).toEqual({ from: undefined, to: undefined });
test("add a date when inside the range", () => {
const date = new Date(2022, 0, 1);
const from = new Date(2021, 11, 31);
const to = new Date(2022, 0, 2);
const range = addToRange(date, { from, to });
expect(range).toEqual({ from, to: date });
});
});
describe('when "to" and "day" are the same', () => {
const from = new Date();
const to = addDays(from, 4);
const day = to;
const range = { from, to };
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
test("add an earlier date to a complete range", () => {
const from = new Date(2022, 0, 1);
const to = new Date(2022, 0, 2);
const date = new Date(2021, 11, 31);
const range = addToRange(date, { from, to });
expect(range).toEqual({ from: date, to: to });
});
test('should set "to" to undefined', () => {
expect(result).toEqual({ from: to, to: undefined });
test("add a later date to a complete range", () => {
const date = new Date(2022, 0, 2);
const from = new Date(2021, 11, 31);
const to = new Date(2022, 0, 1);
const range = addToRange(date, { from, to });
expect(range).toEqual({ from: from, to: date });
});
});
describe('when "from" and "day" are the same', () => {
const from = new Date();
const to = addDays(from, 4);
const day = from;
const range = { from, to };
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
test("add a date with min > 0", () => {
const date = new Date(2022, 0, 1);
const range = addToRange(date, undefined, 1, 0, false);
expect(range).toEqual({ from: date, to: undefined });
});
test("should reset the range", () => {
expect(result).toEqual({ from: undefined, to: undefined });
test("add a date with max > 0", () => {
const date = new Date(2022, 0, 1);
const range = addToRange(date, undefined, 0, 1, false);
expect(range).toEqual({ from: date, to: date });
});
});
describe('when "from" is after "day"', () => {
const day = new Date();
const from = addDays(day, 1);
const to = addDays(from, 4);
const range = { from, to };
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
test("add a date with required set to true", () => {
const date = new Date(2022, 0, 1);
const range = addToRange(date, undefined, 0, 0, true);
expect(range).toEqual({ from: date, to: date });
});
test('should set the day as "from"', () => {
expect(result).toEqual({ from: day, to: range.to });
test("when exceeding max, set the start of the range", () => {
const from = new Date(2022, 0, 1);
const to = new Date(2022, 0, 2);
const date = new Date(2022, 0, 4);
const max = 2;
const range = addToRange(date, { from, to }, 0, max, false);
expect(range).toEqual({ from: date, to: undefined });
});
});
describe('when "from" is before "day"', () => {
const day = new Date();
const from = subDays(day, 1);
const to = addDays(from, 4);
const range = { from, to };
let result: DateRange | undefined;
beforeAll(() => {
result = addToRange(day, range);
test("when below min, set the start of the range", () => {
const from = new Date(2021, 11, 20);
const to = new Date(2022, 0, 2);
const date = new Date(2021, 11, 21);
const min = 5;
const range = addToRange(date, { from, to }, min, 0, false);
expect(range).toEqual({ from: date, to: undefined });
});
test('should set the day as "to"', () => {
expect(result).toEqual({ from: range.from, to: day });
});
});

@@ -13,40 +13,76 @@ import { dateLib as defaultDateLib } from "../lib/index.js";

export function addToRange(
/** The date to add to the range. */
date: Date,
range: DateRange | undefined,
/** The range where to add `date`. */
initialRange: DateRange | undefined,
min = 0,
max = 0,
required = false,
/** @ignore */
dateLib: DateLib = defaultDateLib
): DateRange {
const { from, to } = range || {};
): DateRange | undefined {
const { from, to } = initialRange || {};
const { isSameDay, isAfter, isBefore } = dateLib;
if (from && to) {
if (isSameDay(to, date) && isSameDay(from, date)) {
return { from: undefined, to: undefined };
}
if (isSameDay(to, date)) {
return { from: to, to: undefined };
}
let range: DateRange | undefined;
if (!from && !to) {
// the range is empty, add the date
range = { from: date, to: min > 0 ? undefined : date };
} else if (from && !to) {
// adding date to an incomplete range
if (isSameDay(from, date)) {
return { from: undefined, to: undefined };
// adding a date equal to the start of the range
if (required) {
range = { from, to: undefined };
} else {
range = undefined;
}
} else if (isBefore(date, from)) {
// adding a date before the start of the range
range = { from: date, to: from };
} else {
// adding a date after the start of the range
range = { from, to: date };
}
if (isAfter(from, date)) {
return { from: date, to };
} else if (from && to) {
// adding date to a complete range
if (isSameDay(from, date) && isSameDay(to, date)) {
// adding a date that is equal to both start and end of the range
if (required) {
range = { from, to };
} else {
range = undefined;
}
} else if (isSameDay(from, date)) {
// adding a date equal to the the start of the range
range = { from, to: min > 0 ? undefined : date };
} else if (isSameDay(to, date)) {
// adding a dare equal to the end of the range
range = { from: date, to: min > 0 ? undefined : date };
} else if (isBefore(date, from)) {
// adding a date before the start of the range
range = { from: date, to: to };
} else if (isAfter(date, from)) {
// adding a date after the start of the range
range = { from, to: date };
} else if (isAfter(date, to)) {
// adding a date after the end of the range
range = { from, to: date };
} else {
throw new Error("Invalid range");
}
return { from, to: date };
}
if (to) {
if (isAfter(date, to)) {
return { from: to, to: date };
// check for min / max
if (range?.from && range?.to) {
const diff = dateLib.differenceInCalendarDays(range.to, range.from);
if (max > 0 && diff > max) {
range = { from: date, to: undefined };
} else if (min > 1 && diff < min) {
range = { from: date, to: undefined };
}
return { from: date, to };
}
if (from) {
if (isBefore(date, from)) {
return { from: date, to: from };
}
if (isSameDay(date, from)) {
return { from: undefined, to: undefined };
}
return { from, to: date };
}
return { from: date, to: undefined };
return range;
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc