@navikt/ds-react 1.3.7 → 1.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/_docs.json +4956 -2568
- package/cjs/date/DateInput.js +79 -0
- package/cjs/date/datepicker/DatePicker.js +113 -0
- package/cjs/date/datepicker/DatePickerStandalone.js +81 -0
- package/cjs/date/datepicker/DayButton.js +43 -0
- package/cjs/date/datepicker/caption/Caption.js +23 -0
- package/cjs/date/datepicker/caption/DropdownCaption.js +38 -0
- package/cjs/date/datepicker/caption/index.js +10 -0
- package/cjs/date/datepicker/caption/package.json +6 -0
- package/cjs/date/hooks/index.js +15 -0
- package/cjs/date/hooks/package.json +6 -0
- package/cjs/date/hooks/useDateInputContext.js +17 -0
- package/cjs/date/hooks/useDatepicker.js +135 -0
- package/cjs/date/hooks/useMonthPicker.js +139 -0
- package/cjs/date/hooks/useRangeDatepicker.js +215 -0
- package/cjs/date/hooks/useSharedMonthContext.js +63 -0
- package/cjs/date/index.js +14 -0
- package/cjs/date/monthpicker/MonthButton.js +80 -0
- package/cjs/date/monthpicker/MonthCaption.js +47 -0
- package/cjs/date/monthpicker/MonthPicker.js +74 -0
- package/cjs/date/monthpicker/MonthPickerStandalone.js +54 -0
- package/cjs/date/monthpicker/MonthSelector.js +79 -0
- package/cjs/date/package.json +6 -0
- package/cjs/date/utils/check-dates.js +17 -0
- package/cjs/date/utils/dates-disabled.js +29 -0
- package/cjs/date/utils/format-date.js +12 -0
- package/cjs/date/utils/get-dates.js +43 -0
- package/cjs/date/utils/get-initial-year.js +21 -0
- package/cjs/date/utils/index.js +33 -0
- package/cjs/date/utils/is-match.js +61 -0
- package/cjs/date/utils/labels.js +85 -0
- package/cjs/date/utils/locale.js +21 -0
- package/cjs/date/utils/navigation.js +155 -0
- package/cjs/date/utils/package.json +6 -0
- package/cjs/date/utils/parse-date.js +39 -0
- package/cjs/form/ConfirmationPanel.js +4 -3
- package/cjs/index.js +1 -0
- package/cjs/util/AnimateHeight.js +2 -2
- package/esm/date/DateInput.d.ts +30 -0
- package/esm/date/DateInput.js +51 -0
- package/esm/date/DateInput.js.map +1 -0
- package/esm/date/datepicker/DatePicker.d.ts +95 -0
- package/esm/date/datepicker/DatePicker.js +85 -0
- package/esm/date/datepicker/DatePicker.js.map +1 -0
- package/esm/date/datepicker/DatePickerStandalone.d.ts +12 -0
- package/esm/date/datepicker/DatePickerStandalone.js +53 -0
- package/esm/date/datepicker/DatePickerStandalone.js.map +1 -0
- package/esm/date/datepicker/DayButton.d.ts +3 -0
- package/esm/date/datepicker/DayButton.js +17 -0
- package/esm/date/datepicker/DayButton.js.map +1 -0
- package/esm/date/datepicker/caption/Caption.d.ts +4 -0
- package/esm/date/datepicker/caption/Caption.js +17 -0
- package/esm/date/datepicker/caption/Caption.js.map +1 -0
- package/esm/date/datepicker/caption/DropdownCaption.d.ts +4 -0
- package/esm/date/datepicker/caption/DropdownCaption.js +32 -0
- package/esm/date/datepicker/caption/DropdownCaption.js.map +1 -0
- package/esm/date/datepicker/caption/index.d.ts +2 -0
- package/esm/date/datepicker/caption/index.js +3 -0
- package/esm/date/datepicker/caption/index.js.map +1 -0
- package/esm/date/hooks/index.d.ts +5 -0
- package/esm/date/hooks/index.js +6 -0
- package/esm/date/hooks/index.js.map +1 -0
- package/esm/date/hooks/useDateInputContext.d.ts +18 -0
- package/esm/date/hooks/useDateInputContext.js +14 -0
- package/esm/date/hooks/useDateInputContext.js.map +1 -0
- package/esm/date/hooks/useDatepicker.d.ts +37 -0
- package/esm/date/hooks/useDatepicker.js +132 -0
- package/esm/date/hooks/useDatepicker.js.map +1 -0
- package/esm/date/hooks/useMonthPicker.d.ts +33 -0
- package/esm/date/hooks/useMonthPicker.js +136 -0
- package/esm/date/hooks/useMonthPicker.js.map +1 -0
- package/esm/date/hooks/useRangeDatepicker.d.ts +39 -0
- package/esm/date/hooks/useRangeDatepicker.js +212 -0
- package/esm/date/hooks/useRangeDatepicker.js.map +1 -0
- package/esm/date/hooks/useSharedMonthContext.d.ts +21 -0
- package/esm/date/hooks/useSharedMonthContext.js +36 -0
- package/esm/date/hooks/useSharedMonthContext.js.map +1 -0
- package/esm/date/index.d.ts +6 -0
- package/esm/date/index.js +4 -0
- package/esm/date/index.js.map +1 -0
- package/esm/date/monthpicker/MonthButton.d.ts +11 -0
- package/esm/date/monthpicker/MonthButton.js +51 -0
- package/esm/date/monthpicker/MonthButton.js.map +1 -0
- package/esm/date/monthpicker/MonthCaption.d.ts +3 -0
- package/esm/date/monthpicker/MonthCaption.js +41 -0
- package/esm/date/monthpicker/MonthCaption.js.map +1 -0
- package/esm/date/monthpicker/MonthPicker.d.ts +90 -0
- package/esm/date/monthpicker/MonthPicker.js +46 -0
- package/esm/date/monthpicker/MonthPicker.js.map +1 -0
- package/esm/date/monthpicker/MonthPickerStandalone.d.ts +11 -0
- package/esm/date/monthpicker/MonthPickerStandalone.js +26 -0
- package/esm/date/monthpicker/MonthPickerStandalone.js.map +1 -0
- package/esm/date/monthpicker/MonthSelector.d.ts +3 -0
- package/esm/date/monthpicker/MonthSelector.js +50 -0
- package/esm/date/monthpicker/MonthSelector.js.map +1 -0
- package/esm/date/utils/check-dates.d.ts +4 -0
- package/esm/date/utils/check-dates.js +12 -0
- package/esm/date/utils/check-dates.js.map +1 -0
- package/esm/date/utils/dates-disabled.d.ts +1 -0
- package/esm/date/utils/dates-disabled.js +26 -0
- package/esm/date/utils/dates-disabled.js.map +1 -0
- package/esm/date/utils/format-date.d.ts +1 -0
- package/esm/date/utils/format-date.js +9 -0
- package/esm/date/utils/format-date.js.map +1 -0
- package/esm/date/utils/get-dates.d.ts +2 -0
- package/esm/date/utils/get-dates.js +39 -0
- package/esm/date/utils/get-dates.js.map +1 -0
- package/esm/date/utils/get-initial-year.d.ts +5 -0
- package/esm/date/utils/get-initial-year.js +18 -0
- package/esm/date/utils/get-initial-year.js.map +1 -0
- package/esm/date/utils/index.d.ts +10 -0
- package/esm/date/utils/index.js +11 -0
- package/esm/date/utils/index.js.map +1 -0
- package/esm/date/utils/is-match.d.ts +4 -0
- package/esm/date/utils/is-match.js +57 -0
- package/esm/date/utils/is-match.js.map +1 -0
- package/esm/date/utils/labels.d.ts +6 -0
- package/esm/date/utils/labels.js +79 -0
- package/esm/date/utils/labels.js.map +1 -0
- package/esm/date/utils/locale.d.ts +2 -0
- package/esm/date/utils/locale.js +15 -0
- package/esm/date/utils/locale.js.map +1 -0
- package/esm/date/utils/navigation.d.ts +2 -0
- package/esm/date/utils/navigation.js +152 -0
- package/esm/date/utils/navigation.js.map +1 -0
- package/esm/date/utils/parse-date.d.ts +3 -0
- package/esm/date/utils/parse-date.js +36 -0
- package/esm/date/utils/parse-date.js.map +1 -0
- package/esm/form/ConfirmationPanel.js +5 -4
- package/esm/form/ConfirmationPanel.js.map +1 -1
- package/esm/index.d.ts +1 -0
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -1
- package/esm/typography/Detail.d.ts +1 -1
- package/esm/util/AnimateHeight.js +2 -2
- package/esm/util/AnimateHeight.js.map +1 -1
- package/package.json +6 -4
- package/src/chat/chat.stories.tsx +15 -15
- package/src/date/DateInput.tsx +167 -0
- package/src/date/datepicker/DatePicker.tsx +252 -0
- package/src/date/datepicker/DatePickerStandalone.tsx +120 -0
- package/src/date/datepicker/DayButton.tsx +26 -0
- package/src/date/datepicker/caption/Caption.tsx +51 -0
- package/src/date/datepicker/caption/DropdownCaption.tsx +98 -0
- package/src/date/datepicker/caption/index.ts +2 -0
- package/src/date/datepicker/datepicker.stories.tsx +235 -0
- package/src/date/hooks/index.ts +8 -0
- package/src/date/hooks/useDateInputContext.tsx +32 -0
- package/src/date/hooks/useDatepicker.tsx +225 -0
- package/src/date/hooks/useMonthPicker.tsx +223 -0
- package/src/date/hooks/useRangeDatepicker.tsx +348 -0
- package/src/date/hooks/useSharedMonthContext.tsx +68 -0
- package/src/date/index.ts +16 -0
- package/src/date/monthpicker/MonthButton.tsx +109 -0
- package/src/date/monthpicker/MonthCaption.tsx +94 -0
- package/src/date/monthpicker/MonthPicker.tsx +204 -0
- package/src/date/monthpicker/MonthPickerStandalone.tsx +87 -0
- package/src/date/monthpicker/MonthSelector.tsx +85 -0
- package/src/date/monthpicker/monthpicker.stories.tsx +128 -0
- package/src/date/utils/__tests__/check-dates.test.ts +47 -0
- package/src/date/utils/__tests__/dates-disabled.test.ts +48 -0
- package/src/date/utils/__tests__/format-dates.test.ts +22 -0
- package/src/date/utils/__tests__/get-dates.test.ts +79 -0
- package/src/date/utils/__tests__/get-initial-year.test.ts +94 -0
- package/src/date/utils/__tests__/is-match.test.ts +42 -0
- package/src/date/utils/__tests__/parse-dates.test.ts +81 -0
- package/src/date/utils/check-dates.ts +17 -0
- package/src/date/utils/dates-disabled.ts +26 -0
- package/src/date/utils/format-date.ts +17 -0
- package/src/date/utils/get-dates.ts +44 -0
- package/src/date/utils/get-initial-year.ts +25 -0
- package/src/date/utils/index.ts +21 -0
- package/src/date/utils/is-match.ts +88 -0
- package/src/date/utils/labels.ts +84 -0
- package/src/date/utils/locale.ts +15 -0
- package/src/date/utils/navigation.ts +292 -0
- package/src/date/utils/parse-date.ts +46 -0
- package/src/form/ConfirmationPanel.tsx +9 -2
- package/src/form/checkbox/Checkbox.test.tsx +2 -2
- package/src/form/radio/radio.stories.tsx +4 -4
- package/src/form/stories/textarea.stories.tsx +1 -3
- package/src/help-text/help-text.stories.tsx +3 -0
- package/src/index.ts +1 -0
- package/src/typography/Detail.tsx +1 -1
- package/src/util/AnimateHeight.tsx +6 -7
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { dateIsInCurrentMonth, hasNextYear } from "..";
|
|
2
|
+
|
|
3
|
+
const selectedDate = new Date("Feb 1 1994");
|
|
4
|
+
|
|
5
|
+
describe("Returns if date is in current month", () => {
|
|
6
|
+
test("Date should be within current month (true)", () => {
|
|
7
|
+
expect(dateIsInCurrentMonth(new Date(), new Date())).toBeTruthy();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test("Date should not be within current month (false)", () => {
|
|
11
|
+
expect(
|
|
12
|
+
dateIsInCurrentMonth(new Date("Sep 2 2021"), selectedDate)
|
|
13
|
+
).toBeFalsy();
|
|
14
|
+
expect(
|
|
15
|
+
dateIsInCurrentMonth(new Date("Sep 1 2021"), selectedDate)
|
|
16
|
+
).toBeFalsy();
|
|
17
|
+
expect(
|
|
18
|
+
dateIsInCurrentMonth(new Date("Sep 30 2021"), selectedDate)
|
|
19
|
+
).toBeFalsy();
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const years = [
|
|
24
|
+
new Date(),
|
|
25
|
+
new Date("Aug 5 2023"),
|
|
26
|
+
new Date("Aug 5 2024"),
|
|
27
|
+
new Date("Aug 5 2025"),
|
|
28
|
+
new Date("Aug 5 2026"),
|
|
29
|
+
new Date("Aug 5 2027"),
|
|
30
|
+
new Date("Aug 5 2028"),
|
|
31
|
+
new Date("Aug 5 2029"),
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
describe("Returns if year is at start or end of range", () => {
|
|
35
|
+
test("Should have next year (true)", () => {
|
|
36
|
+
expect(hasNextYear(new Date(), years, 1)).toBeTruthy();
|
|
37
|
+
});
|
|
38
|
+
test("Should have previous year (true)", () => {
|
|
39
|
+
expect(hasNextYear(new Date("Aug 3 2023"), years, -1)).toBeTruthy();
|
|
40
|
+
});
|
|
41
|
+
test("Should not have next year (false)", () => {
|
|
42
|
+
expect(hasNextYear(new Date("Aug 3 2029"), years, 1)).toBeFalsy();
|
|
43
|
+
});
|
|
44
|
+
test("Should not have previous year (false)", () => {
|
|
45
|
+
expect(hasNextYear(new Date(), years, -1)).toBeFalsy();
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { disableDate } from "..";
|
|
2
|
+
|
|
3
|
+
describe("Returns if date should be disabled", () => {
|
|
4
|
+
test("Should be disabled using Date (true)", () => {
|
|
5
|
+
const dateToDisable = new Date("Aug 3 2022");
|
|
6
|
+
expect(disableDate(dateToDisable, new Date("Aug 3 2022"))).toBe(true);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
test("Should not be disabled using Date (false)", () => {
|
|
10
|
+
const dateToDisable = new Date("Aug 3 2022");
|
|
11
|
+
expect(disableDate(dateToDisable, new Date("Okt 11 2019"))).toBe(false);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("Should be disabled using Array (true)", () => {
|
|
15
|
+
const dateToDisable = [
|
|
16
|
+
new Date("Nov 2 2019"),
|
|
17
|
+
new Date("Aug 13 2021"),
|
|
18
|
+
new Date("Jul 17 2018"),
|
|
19
|
+
];
|
|
20
|
+
expect(disableDate(dateToDisable, new Date("Jul 17 2018"))).toBe(true);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("Should not be disabled using Array (false)", () => {
|
|
24
|
+
const dateToDisable = [
|
|
25
|
+
new Date("Nov 2 2019"),
|
|
26
|
+
new Date("Aug 13 2021"),
|
|
27
|
+
new Date("Jul 17 2018"),
|
|
28
|
+
];
|
|
29
|
+
expect(disableDate(dateToDisable, new Date("Jul 18 2018"))).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("Should be disabled using Array with range (true)", () => {
|
|
33
|
+
const dateToDisable = [
|
|
34
|
+
{ from: new Date("Jul 05 2018"), to: new Date("Sept 09 2020") },
|
|
35
|
+
{ from: new Date("Sep 18 2020"), to: new Date("Des 09 2020") },
|
|
36
|
+
new Date("Nov 2 2019"),
|
|
37
|
+
];
|
|
38
|
+
expect(disableDate(dateToDisable, new Date("Jul 18 2018"))).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("Should not be disabled using Array with range (false)", () => {
|
|
42
|
+
const dateToDisable = [
|
|
43
|
+
{ from: new Date("Jul 05 2018"), to: new Date("Sept 09 2020") },
|
|
44
|
+
new Date("Nov 2 2019"),
|
|
45
|
+
];
|
|
46
|
+
expect(disableDate(dateToDisable, new Date("Jul 4 2018"))).toBe(false);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { parseDate } from "../parse-date";
|
|
2
|
+
import nb from "date-fns/locale/nb";
|
|
3
|
+
import { formatDateForInput } from "../format-date";
|
|
4
|
+
|
|
5
|
+
const parse = (inp: string) => parseDate(inp, new Date(), nb, "date");
|
|
6
|
+
|
|
7
|
+
describe("Format date to correct output", () => {
|
|
8
|
+
test("formatDateForInput", () => {
|
|
9
|
+
expect(formatDateForInput(parse("15/05/22"), nb, "date")).toEqual(
|
|
10
|
+
"15.05.2022"
|
|
11
|
+
);
|
|
12
|
+
expect(formatDateForInput(parse("1/5/22"), nb, "date")).toEqual(
|
|
13
|
+
"01.05.2022"
|
|
14
|
+
);
|
|
15
|
+
expect(formatDateForInput(parse("1/05/22"), nb, "date")).toEqual(
|
|
16
|
+
"01.05.2022"
|
|
17
|
+
);
|
|
18
|
+
expect(formatDateForInput(parse("15/5/22"), nb, "date")).toEqual(
|
|
19
|
+
"15.05.2022"
|
|
20
|
+
);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { getMonths, getYears } from "..";
|
|
2
|
+
|
|
3
|
+
describe("Extracts correct months", () => {
|
|
4
|
+
test("March - October (8)", () => {
|
|
5
|
+
const t = {
|
|
6
|
+
start: new Date(2019, 2, 22),
|
|
7
|
+
end: new Date(2019, 9, 22),
|
|
8
|
+
current: new Date(2019, 6, 22),
|
|
9
|
+
res: 8,
|
|
10
|
+
};
|
|
11
|
+
expect(getMonths(t.start, t.end, t.current).length).toEqual(t.res);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("Only first 7 months", () => {
|
|
15
|
+
const t = {
|
|
16
|
+
start: new Date(2019, 0, 22),
|
|
17
|
+
end: new Date(2022, 7, 22),
|
|
18
|
+
current: new Date(2022, 6, 22),
|
|
19
|
+
res: 8,
|
|
20
|
+
};
|
|
21
|
+
expect(getMonths(t.start, t.end, t.current).length).toEqual(t.res);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("All of 2019 when only 2019 avaliable (12)", () => {
|
|
25
|
+
const t = {
|
|
26
|
+
start: new Date(2019, 0, 22),
|
|
27
|
+
end: new Date(2019, 11, 22),
|
|
28
|
+
current: new Date(2019, 6, 22),
|
|
29
|
+
res: 12,
|
|
30
|
+
};
|
|
31
|
+
expect(getMonths(t.start, t.end, t.current).length).toEqual(t.res);
|
|
32
|
+
});
|
|
33
|
+
test("All of 2019 when start + current is 2019 and end is later(12)", () => {
|
|
34
|
+
const t = {
|
|
35
|
+
start: new Date(2019, 0, 22),
|
|
36
|
+
end: new Date(2022, 7, 22),
|
|
37
|
+
current: new Date(2019, 6, 22),
|
|
38
|
+
res: 12,
|
|
39
|
+
};
|
|
40
|
+
expect(getMonths(t.start, t.end, t.current).length).toEqual(t.res);
|
|
41
|
+
});
|
|
42
|
+
test("All of 2020 when between 2019 and 2022 (12)", () => {
|
|
43
|
+
const t = {
|
|
44
|
+
start: new Date(2019, 0, 22),
|
|
45
|
+
end: new Date(2022, 7, 22),
|
|
46
|
+
current: new Date(2020, 6, 22),
|
|
47
|
+
res: 12,
|
|
48
|
+
};
|
|
49
|
+
expect(getMonths(t.start, t.end, t.current).length).toEqual(t.res);
|
|
50
|
+
});
|
|
51
|
+
test("Tail part of 2019, including starting month (12)", () => {
|
|
52
|
+
const t = {
|
|
53
|
+
start: new Date(2019, 5, 22),
|
|
54
|
+
end: new Date(2022, 7, 22),
|
|
55
|
+
current: new Date(2019, 6, 22),
|
|
56
|
+
res: 7,
|
|
57
|
+
};
|
|
58
|
+
expect(getMonths(t.start, t.end, t.current).length).toEqual(t.res);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe("Extracts correct years", () => {
|
|
63
|
+
test("Same year (1)", () => {
|
|
64
|
+
const t = {
|
|
65
|
+
start: new Date(2019, 2, 22),
|
|
66
|
+
end: new Date(2019, 9, 22),
|
|
67
|
+
res: 1,
|
|
68
|
+
};
|
|
69
|
+
expect(getYears(t.start, t.end).length).toEqual(t.res);
|
|
70
|
+
});
|
|
71
|
+
test("Multiple years (11)", () => {
|
|
72
|
+
const t = {
|
|
73
|
+
start: new Date(2019, 2, 22),
|
|
74
|
+
end: new Date(2029, 9, 22),
|
|
75
|
+
res: 11,
|
|
76
|
+
};
|
|
77
|
+
expect(getYears(t.start, t.end).length).toEqual(t.res);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { isSameYear } from "date-fns";
|
|
2
|
+
import { getInitialYear } from "..";
|
|
3
|
+
|
|
4
|
+
describe("Returns initial year for monthpicker", () => {
|
|
5
|
+
test("Default selected should be valid (true)", () => {
|
|
6
|
+
expect(
|
|
7
|
+
isSameYear(
|
|
8
|
+
new Date("Oct 4 2021"),
|
|
9
|
+
getInitialYear({
|
|
10
|
+
defaultMonth: new Date("Aug 4 2021"),
|
|
11
|
+
})
|
|
12
|
+
)
|
|
13
|
+
).toBeTruthy();
|
|
14
|
+
});
|
|
15
|
+
test("Default selected should not be valid (false)", () => {
|
|
16
|
+
expect(
|
|
17
|
+
isSameYear(
|
|
18
|
+
new Date("Oct 4 2021"),
|
|
19
|
+
getInitialYear({
|
|
20
|
+
defaultMonth: new Date("Aug 4 2020"),
|
|
21
|
+
})
|
|
22
|
+
)
|
|
23
|
+
).toBeFalsy();
|
|
24
|
+
});
|
|
25
|
+
test("Default selected should not be valid (false)", () => {
|
|
26
|
+
expect(
|
|
27
|
+
isSameYear(
|
|
28
|
+
new Date("Oct 4 2021"),
|
|
29
|
+
getInitialYear({
|
|
30
|
+
defaultMonth: new Date("Aug 4 2020"),
|
|
31
|
+
/* toDate: new Date("Oct 4 2021"), */
|
|
32
|
+
/* fromDate: new Date("Oct 4 2021"), */
|
|
33
|
+
})
|
|
34
|
+
)
|
|
35
|
+
).toBeFalsy();
|
|
36
|
+
});
|
|
37
|
+
test("Default selected should be valid with fromDate", () => {
|
|
38
|
+
expect(
|
|
39
|
+
isSameYear(
|
|
40
|
+
new Date("Oct 4 2021"),
|
|
41
|
+
getInitialYear({
|
|
42
|
+
defaultMonth: new Date("Aug 4 2021"),
|
|
43
|
+
/* toDate: new Date("Oct 4 2021"), */
|
|
44
|
+
fromDate: new Date("Oct 4 2021"),
|
|
45
|
+
})
|
|
46
|
+
)
|
|
47
|
+
).toBeTruthy();
|
|
48
|
+
});
|
|
49
|
+
test("Default selected should be valid with toDate", () => {
|
|
50
|
+
expect(
|
|
51
|
+
isSameYear(
|
|
52
|
+
new Date("Oct 4 2021"),
|
|
53
|
+
getInitialYear({
|
|
54
|
+
defaultMonth: new Date("Aug 4 2021"),
|
|
55
|
+
toDate: new Date("Jun 4 2021"),
|
|
56
|
+
})
|
|
57
|
+
)
|
|
58
|
+
).toBeTruthy();
|
|
59
|
+
});
|
|
60
|
+
test("Default selected should be moved to toDate", () => {
|
|
61
|
+
expect(
|
|
62
|
+
isSameYear(
|
|
63
|
+
new Date("Oct 4 2020"),
|
|
64
|
+
getInitialYear({
|
|
65
|
+
defaultMonth: new Date("Aug 4 2021"),
|
|
66
|
+
toDate: new Date("Jun 4 2020"),
|
|
67
|
+
})
|
|
68
|
+
)
|
|
69
|
+
).toBeTruthy();
|
|
70
|
+
});
|
|
71
|
+
test("Default selected should be moved to fromDate", () => {
|
|
72
|
+
expect(
|
|
73
|
+
isSameYear(
|
|
74
|
+
new Date("Oct 4 2022"),
|
|
75
|
+
getInitialYear({
|
|
76
|
+
defaultMonth: new Date("Aug 4 2021"),
|
|
77
|
+
fromDate: new Date("Jun 4 2022"),
|
|
78
|
+
})
|
|
79
|
+
)
|
|
80
|
+
).toBeTruthy();
|
|
81
|
+
});
|
|
82
|
+
test("Default selected should be moved to fromDate, not toDate", () => {
|
|
83
|
+
expect(
|
|
84
|
+
isSameYear(
|
|
85
|
+
new Date("Oct 4 2022"),
|
|
86
|
+
getInitialYear({
|
|
87
|
+
defaultMonth: new Date("Aug 4 2021"),
|
|
88
|
+
toDate: new Date("Oct 4 2023"),
|
|
89
|
+
fromDate: new Date("Jun 4 2022"),
|
|
90
|
+
})
|
|
91
|
+
)
|
|
92
|
+
).toBeTruthy();
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { isMatch } from "..";
|
|
2
|
+
|
|
3
|
+
const disabled = [
|
|
4
|
+
new Date("Sep 8 2022"),
|
|
5
|
+
[new Date("Aug 4 2018"), new Date("Dec 1 2011")],
|
|
6
|
+
{ from: new Date("Sep 4 2023"), to: new Date("Sep 15 2023") },
|
|
7
|
+
];
|
|
8
|
+
|
|
9
|
+
describe("Returns if date is disabled", () => {
|
|
10
|
+
test("Date should be disabled type Date (true)", () => {
|
|
11
|
+
expect(isMatch(new Date("Sep 4 2022"), disabled)).toBeTruthy();
|
|
12
|
+
});
|
|
13
|
+
test("Date should be disabled type Date[] (true)", () => {
|
|
14
|
+
expect(isMatch(new Date("Dec 4 2011"), disabled)).toBeTruthy();
|
|
15
|
+
});
|
|
16
|
+
test("Date should be disabled type Range (true)", () => {
|
|
17
|
+
expect(isMatch(new Date("Sep 5 2023"), disabled)).toBeTruthy();
|
|
18
|
+
});
|
|
19
|
+
test("Date should be disabled type Range (true)", () => {
|
|
20
|
+
expect(isMatch(new Date("Sep 3 2023"), disabled)).toBeTruthy();
|
|
21
|
+
});
|
|
22
|
+
test("Date should be disabled type After (true)", () => {
|
|
23
|
+
expect(
|
|
24
|
+
isMatch(new Date("Aug 5 2018"), [{ after: new Date("Aug 2 2018") }])
|
|
25
|
+
).toBeTruthy();
|
|
26
|
+
});
|
|
27
|
+
test("Date should be disabled type After (true)", () => {
|
|
28
|
+
expect(
|
|
29
|
+
isMatch(new Date("Oct 5 2018"), [{ after: new Date("Aug 2 2018") }])
|
|
30
|
+
).toBeTruthy();
|
|
31
|
+
});
|
|
32
|
+
test("Date should be disabled type Before (true)", () => {
|
|
33
|
+
expect(
|
|
34
|
+
isMatch(new Date("Aug 1 2018"), [{ before: new Date("Aug 2 2018") }])
|
|
35
|
+
).toBeTruthy();
|
|
36
|
+
});
|
|
37
|
+
test("Date should be disabled type Before (true)", () => {
|
|
38
|
+
expect(
|
|
39
|
+
isMatch(new Date("Jul 1 2018"), [{ before: new Date("Aug 2 2018") }])
|
|
40
|
+
).toBeTruthy();
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { parseDate } from "..";
|
|
2
|
+
import { isValidDate } from "..";
|
|
3
|
+
import nb from "date-fns/locale/nb";
|
|
4
|
+
import { getMonth, getYear } from "date-fns";
|
|
5
|
+
|
|
6
|
+
const check = (inp: string) =>
|
|
7
|
+
expect(isValidDate(parseDate(inp, new Date(), nb, "date")));
|
|
8
|
+
|
|
9
|
+
const parse = (inp: string) => parseDate(inp, new Date(), nb, "date");
|
|
10
|
+
|
|
11
|
+
describe("Parse date-inputs", () => {
|
|
12
|
+
test("No spaces", () => {
|
|
13
|
+
check("150522").toBeTruthy();
|
|
14
|
+
check("11052022").toBeTruthy();
|
|
15
|
+
check("15052022").toBeTruthy();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test(". divider", () => {
|
|
19
|
+
check("1.5.22").toBeTruthy();
|
|
20
|
+
check("1.5.2022").toBeTruthy();
|
|
21
|
+
check("11.05.22").toBeTruthy();
|
|
22
|
+
check("11.05.2022").toBeTruthy();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("/ divider", () => {
|
|
26
|
+
check("1/5/22").toBeTruthy();
|
|
27
|
+
check("10/5/22").toBeTruthy();
|
|
28
|
+
check("1/5/2022").toBeTruthy();
|
|
29
|
+
check("10/5/2022").toBeTruthy();
|
|
30
|
+
check("1/05/22").toBeTruthy();
|
|
31
|
+
check("10/05/22").toBeTruthy();
|
|
32
|
+
check("1/05/2022").toBeTruthy();
|
|
33
|
+
check("10/05/2022").toBeTruthy();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("- divider", () => {
|
|
37
|
+
check("1-5-22").toBeTruthy();
|
|
38
|
+
check("10-5-22").toBeTruthy();
|
|
39
|
+
check("1-5-2022").toBeTruthy();
|
|
40
|
+
check("10-5-2022").toBeTruthy();
|
|
41
|
+
check("1-05-22").toBeTruthy();
|
|
42
|
+
check("10-05-22").toBeTruthy();
|
|
43
|
+
check("1-05-2022").toBeTruthy();
|
|
44
|
+
check("10-05-2022").toBeTruthy();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("Parse date-inputs to correct year", () => {
|
|
49
|
+
test("No spaces", () => {
|
|
50
|
+
expect(getYear(parse("150522"))).toEqual(2022);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test(". divider", () => {
|
|
54
|
+
expect(getYear(parse("1.05.22"))).toEqual(2022);
|
|
55
|
+
expect(getYear(parse("11.05.22"))).toEqual(2022);
|
|
56
|
+
expect(getYear(parse("1.5.22"))).toEqual(2022);
|
|
57
|
+
expect(getYear(parse("11.5.22"))).toEqual(2022);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("/ divider", () => {
|
|
61
|
+
expect(getYear(parse("1/5/22"))).toEqual(2022);
|
|
62
|
+
expect(getYear(parse("10/5/22"))).toEqual(2022);
|
|
63
|
+
expect(getYear(parse("1/05/22"))).toEqual(2022);
|
|
64
|
+
expect(getYear(parse("10/05/22"))).toEqual(2022);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("- divider", () => {
|
|
68
|
+
expect(getYear(parse("1-5-22"))).toEqual(2022);
|
|
69
|
+
expect(getYear(parse("10-5-22"))).toEqual(2022);
|
|
70
|
+
expect(getYear(parse("1-05-22"))).toEqual(2022);
|
|
71
|
+
expect(getYear(parse("10-05-22"))).toEqual(2022);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("Parse date-inputs to correct month", () => {
|
|
76
|
+
test("No spaces", () => {
|
|
77
|
+
expect(getMonth(parse("150522"))).toEqual(4);
|
|
78
|
+
expect(getMonth(parse("11052022"))).toEqual(4);
|
|
79
|
+
expect(getMonth(parse("15052022"))).toEqual(4);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { isThisMonth, setYear } from "date-fns";
|
|
2
|
+
|
|
3
|
+
export const dateIsInCurrentMonth = (
|
|
4
|
+
date: Date,
|
|
5
|
+
dateToCompare: Date
|
|
6
|
+
): boolean => {
|
|
7
|
+
return isThisMonth(setYear(date, Number(dateToCompare.getFullYear())));
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/** @private */
|
|
11
|
+
export function isValidDate(day: Date): boolean {
|
|
12
|
+
return !isNaN(day.getTime());
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const hasNextYear = (year: Date, years: Date[], val: any): boolean => {
|
|
16
|
+
return years.some((x) => year.getFullYear() + val === x.getFullYear());
|
|
17
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { isSameDay } from "date-fns";
|
|
2
|
+
import { isDateRange } from "react-day-picker";
|
|
3
|
+
|
|
4
|
+
// TODO: ((date: Date) => boolean)
|
|
5
|
+
export const disableDate = (
|
|
6
|
+
disabledSelection: Date | Array<any>,
|
|
7
|
+
date: Date
|
|
8
|
+
): boolean => {
|
|
9
|
+
let result: boolean = false;
|
|
10
|
+
if (disabledSelection instanceof Date) {
|
|
11
|
+
return isSameDay(disabledSelection, date);
|
|
12
|
+
} else if (disabledSelection instanceof Array) {
|
|
13
|
+
for (let i = 0; i < disabledSelection.length; i++) {
|
|
14
|
+
const selection = disabledSelection[i];
|
|
15
|
+
if (isDateRange(selection)) {
|
|
16
|
+
if (selection.from && selection.to) {
|
|
17
|
+
result = date >= selection.from && date <= selection.to;
|
|
18
|
+
}
|
|
19
|
+
} else if (selection instanceof Date) {
|
|
20
|
+
result = isSameDay(selection, date);
|
|
21
|
+
}
|
|
22
|
+
if (result) break;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { format } from "date-fns";
|
|
2
|
+
import {
|
|
3
|
+
INPUT_DATE_STRING_FORMAT_DATE,
|
|
4
|
+
INPUT_DATE_STRING_FORMAT_MONTH,
|
|
5
|
+
} from "./parse-date";
|
|
6
|
+
|
|
7
|
+
export const formatDateForInput = (
|
|
8
|
+
date: Date,
|
|
9
|
+
locale: Locale,
|
|
10
|
+
type: "date" | "month"
|
|
11
|
+
) => {
|
|
12
|
+
const INPUT_DATE_STRING_FORMAT =
|
|
13
|
+
type === "date"
|
|
14
|
+
? INPUT_DATE_STRING_FORMAT_DATE
|
|
15
|
+
: INPUT_DATE_STRING_FORMAT_MONTH;
|
|
16
|
+
return format(date, INPUT_DATE_STRING_FORMAT, { locale });
|
|
17
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isSameYear,
|
|
3
|
+
setMonth,
|
|
4
|
+
setYear,
|
|
5
|
+
startOfMonth,
|
|
6
|
+
startOfYear,
|
|
7
|
+
} from "date-fns";
|
|
8
|
+
|
|
9
|
+
export const getMonths = (start: Date, end: Date, current: Date): Date[] => {
|
|
10
|
+
const dropdownMonths: Date[] = [];
|
|
11
|
+
|
|
12
|
+
if (isSameYear(start, end)) {
|
|
13
|
+
const date = startOfMonth(start);
|
|
14
|
+
for (let month = start.getMonth(); month <= end.getMonth(); month++) {
|
|
15
|
+
dropdownMonths.push(setMonth(date, month));
|
|
16
|
+
}
|
|
17
|
+
} else if (isSameYear(current, end)) {
|
|
18
|
+
const date = startOfMonth(new Date());
|
|
19
|
+
for (let month = 0; month <= end.getMonth(); month++) {
|
|
20
|
+
dropdownMonths.push(setMonth(date, month));
|
|
21
|
+
}
|
|
22
|
+
} else if (isSameYear(current, start)) {
|
|
23
|
+
const date = startOfMonth(start);
|
|
24
|
+
for (let month = date.getMonth(); month <= 11; month++) {
|
|
25
|
+
dropdownMonths.push(setMonth(date, month));
|
|
26
|
+
}
|
|
27
|
+
} else {
|
|
28
|
+
const date = startOfMonth(new Date());
|
|
29
|
+
for (let month = 0; month <= 11; month++) {
|
|
30
|
+
dropdownMonths.push(setMonth(date, month));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return dropdownMonths;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const getYears = (start: Date, end: Date): Date[] => {
|
|
37
|
+
const years: Date[] = [];
|
|
38
|
+
const fromYear = start.getFullYear();
|
|
39
|
+
const toYear = end.getFullYear();
|
|
40
|
+
for (let year = fromYear; year <= toYear; year++) {
|
|
41
|
+
years.push(setYear(startOfYear(new Date()), year));
|
|
42
|
+
}
|
|
43
|
+
return years;
|
|
44
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { startOfYear } from "date-fns";
|
|
2
|
+
import { DayPickerContextValue } from "react-day-picker";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
export function getInitialYear(context: Partial<DayPickerContextValue>): Date {
|
|
8
|
+
const { month, defaultMonth, toDate, fromDate } = context;
|
|
9
|
+
|
|
10
|
+
let initialMonth = month || defaultMonth || new Date();
|
|
11
|
+
|
|
12
|
+
const isAfter = toDate && toDate.getFullYear() < initialMonth.getFullYear();
|
|
13
|
+
|
|
14
|
+
const isBefore =
|
|
15
|
+
fromDate && fromDate.getFullYear() > initialMonth.getFullYear();
|
|
16
|
+
|
|
17
|
+
if (isAfter) {
|
|
18
|
+
initialMonth = toDate;
|
|
19
|
+
}
|
|
20
|
+
if (isBefore) {
|
|
21
|
+
initialMonth = fromDate;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return startOfYear(initialMonth);
|
|
25
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export { formatDateForInput } from "./format-date";
|
|
2
|
+
export { getMonths, getYears } from "./get-dates";
|
|
3
|
+
export {
|
|
4
|
+
labelMonthDropdown,
|
|
5
|
+
labelYearDropdown,
|
|
6
|
+
labels,
|
|
7
|
+
labelNextYear,
|
|
8
|
+
labelPrevYear,
|
|
9
|
+
} from "./labels";
|
|
10
|
+
export {
|
|
11
|
+
INPUT_DATE_STRING_FORMAT_DATE,
|
|
12
|
+
INPUT_DATE_STRING_FORMAT_MONTH,
|
|
13
|
+
parseDate,
|
|
14
|
+
} from "./parse-date";
|
|
15
|
+
export { getLocaleFromString } from "./locale";
|
|
16
|
+
export { disableDate } from "./dates-disabled";
|
|
17
|
+
|
|
18
|
+
export { dateIsInCurrentMonth, isValidDate, hasNextYear } from "./check-dates";
|
|
19
|
+
export { getInitialYear } from "./get-initial-year";
|
|
20
|
+
export { isMatch, isDateInRange, Matcher } from "./is-match";
|
|
21
|
+
export { nextEnabled } from "./navigation";
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isDate,
|
|
3
|
+
isSameDay,
|
|
4
|
+
differenceInCalendarDays,
|
|
5
|
+
isSameMonth,
|
|
6
|
+
} from "date-fns";
|
|
7
|
+
import {
|
|
8
|
+
DateAfter,
|
|
9
|
+
DateBefore,
|
|
10
|
+
DateRange,
|
|
11
|
+
isDateAfterType,
|
|
12
|
+
isDateBeforeType,
|
|
13
|
+
isDateRange,
|
|
14
|
+
} from "react-day-picker";
|
|
15
|
+
|
|
16
|
+
export type Matcher =
|
|
17
|
+
| ((date: Date) => boolean)
|
|
18
|
+
| Date
|
|
19
|
+
| Date[]
|
|
20
|
+
| DateRange
|
|
21
|
+
| DateBefore
|
|
22
|
+
| DateAfter;
|
|
23
|
+
|
|
24
|
+
function isDateType(value: unknown): value is Date {
|
|
25
|
+
return isDate(value);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isArrayOfDates(value: unknown): value is Date[] {
|
|
29
|
+
return Array.isArray(value) && value.every(isDate);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function isMatch(day: Date, matchers: Matcher[]): boolean {
|
|
33
|
+
return matchers.some((matcher: Matcher) => {
|
|
34
|
+
if (isDateType(matcher)) {
|
|
35
|
+
return isSameMonth(day, matcher);
|
|
36
|
+
}
|
|
37
|
+
if (isArrayOfDates(matcher)) {
|
|
38
|
+
return matcher.some((matcherDay) => {
|
|
39
|
+
return isSameMonth(matcherDay, day);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
if (isDateRange(matcher)) {
|
|
43
|
+
return isDateInRange(day, matcher);
|
|
44
|
+
}
|
|
45
|
+
if (isDateAfterType(matcher)) {
|
|
46
|
+
return (
|
|
47
|
+
isSameMonth(day, matcher.after) ||
|
|
48
|
+
differenceInCalendarDays(day, matcher.after) > 0
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
if (isDateBeforeType(matcher)) {
|
|
52
|
+
return (
|
|
53
|
+
isSameMonth(day, matcher.before) ||
|
|
54
|
+
differenceInCalendarDays(matcher.before, day) > 0
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
if (typeof matcher === "function") {
|
|
58
|
+
return matcher(day);
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function isDateInRange(date: Date, range: DateRange): boolean {
|
|
65
|
+
let { from, to } = range;
|
|
66
|
+
if (!from) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
if (!to && isSameDay(from, date)) {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
if (!to) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
const isToBeforeFrom = differenceInCalendarDays(to, from) < 0;
|
|
76
|
+
if (to && isToBeforeFrom) {
|
|
77
|
+
[from, to] = [to, from];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (isSameMonth(from, date) || isSameMonth(to, date)) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
differenceInCalendarDays(date, from) >= 0 &&
|
|
86
|
+
differenceInCalendarDays(to, date) >= 0
|
|
87
|
+
);
|
|
88
|
+
}
|