@navikt/ds-react 2.8.13 → 2.8.15
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/cjs/date/datepicker/caption/DropdownCaption.js +6 -2
- package/cjs/date/hooks/useDatepicker.js +14 -6
- package/cjs/date/hooks/useMonthPicker.js +27 -17
- package/cjs/date/hooks/useRangeDatepicker.js +3 -1
- package/cjs/date/monthpicker/MonthCaption.js +18 -15
- package/cjs/date/utils/check-dates.js +1 -5
- package/cjs/date/utils/get-dates.js +9 -1
- package/cjs/date/utils/index.js +1 -2
- package/esm/date/datepicker/caption/DropdownCaption.js +6 -2
- package/esm/date/datepicker/caption/DropdownCaption.js.map +1 -1
- package/esm/date/hooks/useDatepicker.js +15 -7
- package/esm/date/hooks/useDatepicker.js.map +1 -1
- package/esm/date/hooks/useEscape.d.ts +1 -2
- package/esm/date/hooks/useEscape.js.map +1 -1
- package/esm/date/hooks/useMonthPicker.js +28 -18
- package/esm/date/hooks/useMonthPicker.js.map +1 -1
- package/esm/date/hooks/useOutsideClickHandler.d.ts +1 -2
- package/esm/date/hooks/useOutsideClickHandler.js.map +1 -1
- package/esm/date/hooks/useRangeDatepicker.js +3 -1
- package/esm/date/hooks/useRangeDatepicker.js.map +1 -1
- package/esm/date/monthpicker/MonthCaption.js +19 -16
- package/esm/date/monthpicker/MonthCaption.js.map +1 -1
- package/esm/date/utils/check-dates.d.ts +0 -1
- package/esm/date/utils/check-dates.js +0 -3
- package/esm/date/utils/check-dates.js.map +1 -1
- package/esm/date/utils/get-dates.d.ts +1 -1
- package/esm/date/utils/get-dates.js +9 -1
- package/esm/date/utils/get-dates.js.map +1 -1
- package/esm/date/utils/index.d.ts +1 -1
- package/esm/date/utils/index.js +1 -1
- package/esm/date/utils/index.js.map +1 -1
- package/package.json +5 -4
- package/src/date/datepicker/caption/DropdownCaption.tsx +9 -3
- package/src/date/datepicker/datepicker.stories.tsx +2 -3
- package/src/date/hooks/useDatepicker.tsx +22 -7
- package/src/date/hooks/useEscape.tsx +2 -2
- package/src/date/hooks/useMonthPicker.tsx +36 -21
- package/src/date/hooks/useOutsideClickHandler.tsx +2 -2
- package/src/date/hooks/useRangeDatepicker.tsx +7 -1
- package/src/date/monthpicker/MonthCaption.tsx +21 -17
- package/src/date/monthpicker/monthpicker.stories.tsx +3 -1
- package/src/date/utils/__tests__/check-dates.test.ts +1 -27
- package/src/date/utils/__tests__/get-dates.test.ts +44 -2
- package/src/date/utils/check-dates.ts +0 -4
- package/src/date/utils/get-dates.ts +17 -1
- package/src/date/utils/index.ts +1 -1
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Left, Right } from "@navikt/ds-icons";
|
|
2
|
-
import isSameYear from "date-fns/isSameYear";
|
|
3
2
|
import setYear from "date-fns/setYear";
|
|
4
3
|
import startOfMonth from "date-fns/startOfMonth";
|
|
5
4
|
import startOfYear from "date-fns/startOfYear";
|
|
@@ -7,7 +6,8 @@ import React from "react";
|
|
|
7
6
|
import { useDayPicker } from "react-day-picker";
|
|
8
7
|
import { Button, Select } from "../..";
|
|
9
8
|
import { useSharedMonthContext } from "../context";
|
|
10
|
-
import {
|
|
9
|
+
import { labelNextYear, labelPrevYear } from "../utils";
|
|
10
|
+
import { isAfter, isBefore } from "date-fns";
|
|
11
11
|
|
|
12
12
|
export const MonthCaption = () => {
|
|
13
13
|
const {
|
|
@@ -27,34 +27,38 @@ export const MonthCaption = () => {
|
|
|
27
27
|
for (let year = fromYear; year <= toYear; year++) {
|
|
28
28
|
years.push(setYear(startOfYear(new Date()), year));
|
|
29
29
|
}
|
|
30
|
+
|
|
31
|
+
if (!years.map((x) => x.getFullYear()).includes(year.getFullYear())) {
|
|
32
|
+
years.push(setYear(startOfYear(new Date()), year.getFullYear()));
|
|
33
|
+
}
|
|
34
|
+
years.sort((a, b) => a.getFullYear() - b.getFullYear());
|
|
30
35
|
}
|
|
31
36
|
|
|
32
37
|
const handleYearChange = (e) =>
|
|
33
38
|
toYear(setYear(startOfMonth(new Date()), Number(e.target.value)));
|
|
34
39
|
|
|
35
40
|
const handleButtonClick = (val: number) => {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
41
|
+
const newYear = Number(year.getFullYear() + val);
|
|
42
|
+
toYear(setYear(year, newYear));
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const hasPrevYear = () => {
|
|
46
|
+
return fromDate
|
|
47
|
+
? isBefore(year?.getFullYear() - 1, fromDate?.getFullYear())
|
|
48
|
+
: true;
|
|
45
49
|
};
|
|
46
50
|
|
|
47
|
-
const
|
|
48
|
-
return
|
|
49
|
-
|
|
50
|
-
|
|
51
|
+
const hasNextYear = () => {
|
|
52
|
+
return toDate
|
|
53
|
+
? isAfter(year?.getFullYear() + 1, toDate?.getFullYear())
|
|
54
|
+
: true;
|
|
51
55
|
};
|
|
52
56
|
|
|
53
57
|
return (
|
|
54
58
|
<div className="navds-date__caption">
|
|
55
59
|
<Button
|
|
56
60
|
className="navds-date__caption-button"
|
|
57
|
-
disabled={
|
|
61
|
+
disabled={hasPrevYear()}
|
|
58
62
|
onClick={() => handleButtonClick(-1)}
|
|
59
63
|
aria-label={labelPrevYear(locale?.code)}
|
|
60
64
|
icon={<Left aria-hidden />}
|
|
@@ -83,7 +87,7 @@ export const MonthCaption = () => {
|
|
|
83
87
|
)}
|
|
84
88
|
<Button
|
|
85
89
|
className="navds-date__caption-button"
|
|
86
|
-
disabled={
|
|
90
|
+
disabled={hasNextYear()}
|
|
87
91
|
onClick={() => handleButtonClick(1)}
|
|
88
92
|
aria-label={labelNextYear(locale?.code)}
|
|
89
93
|
icon={<Right aria-hidden />}
|
|
@@ -77,11 +77,13 @@ export const UseMonthpicker = () => {
|
|
|
77
77
|
const { inputProps, monthpickerProps } = UNSAFE_useMonthpicker({
|
|
78
78
|
disabled: [new Date("Apr 1 2022")],
|
|
79
79
|
onMonthChange: console.log,
|
|
80
|
+
fromDate: new Date("Jan 1 2022"),
|
|
81
|
+
toDate: new Date("Sep 27 2025"),
|
|
80
82
|
});
|
|
81
83
|
|
|
82
84
|
return (
|
|
83
85
|
<div style={{ height: "20rem" }}>
|
|
84
|
-
<MonthPicker {...monthpickerProps}>
|
|
86
|
+
<MonthPicker {...monthpickerProps} dropdownCaption>
|
|
85
87
|
<MonthPicker.Input
|
|
86
88
|
{...inputProps}
|
|
87
89
|
label="Velg måned"
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { dateIsInCurrentMonth
|
|
1
|
+
import { dateIsInCurrentMonth } from "..";
|
|
2
2
|
|
|
3
3
|
const selectedDate = new Date("Feb 1 1994");
|
|
4
4
|
|
|
@@ -19,29 +19,3 @@ describe("Returns if date is in current month", () => {
|
|
|
19
19
|
).toBeFalsy();
|
|
20
20
|
});
|
|
21
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 2024"), 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
|
-
});
|
|
@@ -57,6 +57,25 @@ describe("Extracts correct months", () => {
|
|
|
57
57
|
};
|
|
58
58
|
expect(getMonths(t.start, t.end, t.current).length).toEqual(t.res);
|
|
59
59
|
});
|
|
60
|
+
test("End part of 2022, including starting month (12)", () => {
|
|
61
|
+
const t = {
|
|
62
|
+
start: new Date(2019, 5, 22),
|
|
63
|
+
end: new Date(2022, 7, 22),
|
|
64
|
+
current: new Date(2022, 7, 22),
|
|
65
|
+
res: 8,
|
|
66
|
+
};
|
|
67
|
+
expect(getMonths(t.start, t.end, t.current).length).toEqual(t.res);
|
|
68
|
+
});
|
|
69
|
+
test("Adds out of range month to list (13)", () => {
|
|
70
|
+
const t = {
|
|
71
|
+
start: new Date(2019, 5, 22),
|
|
72
|
+
end: new Date(2022, 7, 22),
|
|
73
|
+
current: new Date(2022, 9, 22),
|
|
74
|
+
res: 9,
|
|
75
|
+
};
|
|
76
|
+
console.log(getMonths(t.start, t.end, t.current));
|
|
77
|
+
expect(getMonths(t.start, t.end, t.current).length).toEqual(t.res);
|
|
78
|
+
});
|
|
60
79
|
});
|
|
61
80
|
|
|
62
81
|
describe("Extracts correct years", () => {
|
|
@@ -64,16 +83,39 @@ describe("Extracts correct years", () => {
|
|
|
64
83
|
const t = {
|
|
65
84
|
start: new Date(2019, 2, 22),
|
|
66
85
|
end: new Date(2019, 9, 22),
|
|
86
|
+
current: 2019,
|
|
67
87
|
res: 1,
|
|
68
88
|
};
|
|
69
|
-
expect(getYears(t.start, t.end).length).toEqual(t.res);
|
|
89
|
+
expect(getYears(t.start, t.end, t.current).length).toEqual(t.res);
|
|
70
90
|
});
|
|
91
|
+
|
|
71
92
|
test("Multiple years (11)", () => {
|
|
72
93
|
const t = {
|
|
73
94
|
start: new Date(2019, 2, 22),
|
|
74
95
|
end: new Date(2029, 9, 22),
|
|
96
|
+
current: 2022,
|
|
75
97
|
res: 11,
|
|
76
98
|
};
|
|
77
|
-
expect(getYears(t.start, t.end).length).toEqual(t.res);
|
|
99
|
+
expect(getYears(t.start, t.end, t.current).length).toEqual(t.res);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("Displaymonth outside range: after (11)", () => {
|
|
103
|
+
const t = {
|
|
104
|
+
start: new Date(2019, 2, 22),
|
|
105
|
+
end: new Date(2029, 9, 22),
|
|
106
|
+
current: 2040,
|
|
107
|
+
res: 12,
|
|
108
|
+
};
|
|
109
|
+
expect(getYears(t.start, t.end, t.current).length).toEqual(t.res);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("Displaymonth outside range: before (11)", () => {
|
|
113
|
+
const t = {
|
|
114
|
+
start: new Date(2019, 2, 22),
|
|
115
|
+
end: new Date(2029, 9, 22),
|
|
116
|
+
current: 2001,
|
|
117
|
+
res: 12,
|
|
118
|
+
};
|
|
119
|
+
expect(getYears(t.start, t.end, t.current).length).toEqual(t.res);
|
|
78
120
|
});
|
|
79
121
|
});
|
|
@@ -12,7 +12,3 @@ export const dateIsInCurrentMonth = (
|
|
|
12
12
|
export function isValidDate(day: Date): boolean {
|
|
13
13
|
return day && !isNaN(day?.getTime()) && day.getFullYear() > 999;
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
export const hasNextYear = (year: Date, years: Date[], val: any): boolean => {
|
|
17
|
-
return years.some((x) => year.getFullYear() + val === x.getFullYear());
|
|
18
|
-
};
|
|
@@ -28,15 +28,31 @@ export const getMonths = (start: Date, end: Date, current: Date): Date[] => {
|
|
|
28
28
|
dropdownMonths.push(setMonth(date, month));
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
+
|
|
32
|
+
if (!dropdownMonths.map((d) => d.getMonth()).includes(current.getMonth())) {
|
|
33
|
+
dropdownMonths.push(current);
|
|
34
|
+
}
|
|
35
|
+
dropdownMonths.sort((a, b) => a.getMonth() - b.getMonth());
|
|
36
|
+
|
|
31
37
|
return dropdownMonths;
|
|
32
38
|
};
|
|
33
39
|
|
|
34
|
-
export const getYears = (
|
|
40
|
+
export const getYears = (
|
|
41
|
+
start: Date,
|
|
42
|
+
end: Date,
|
|
43
|
+
currentYear: number
|
|
44
|
+
): Date[] => {
|
|
35
45
|
const years: Date[] = [];
|
|
36
46
|
const fromYear = start.getFullYear();
|
|
37
47
|
const toYear = end.getFullYear();
|
|
38
48
|
for (let year = fromYear; year <= toYear; year++) {
|
|
39
49
|
years.push(setYear(startOfYear(new Date()), year));
|
|
40
50
|
}
|
|
51
|
+
|
|
52
|
+
if (fromYear > currentYear || toYear < currentYear) {
|
|
53
|
+
years.push(setYear(startOfYear(new Date()), currentYear));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
years.sort((a, b) => a.getFullYear() - b.getFullYear());
|
|
41
57
|
return years;
|
|
42
58
|
};
|
package/src/date/utils/index.ts
CHANGED
|
@@ -15,7 +15,7 @@ export {
|
|
|
15
15
|
export { getLocaleFromString } from "./locale";
|
|
16
16
|
export { disableDate } from "./dates-disabled";
|
|
17
17
|
|
|
18
|
-
export { dateIsInCurrentMonth, isValidDate
|
|
18
|
+
export { dateIsInCurrentMonth, isValidDate } from "./check-dates";
|
|
19
19
|
export { getInitialYear } from "./get-initial-year";
|
|
20
20
|
export { isMatch, isDateInRange, type Matcher } from "./is-match";
|
|
21
21
|
export { nextEnabled } from "./navigation";
|