@oslokommune/punkt-react 18.5.1 → 18.6.1
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/CHANGELOG.md +38 -0
- package/dist/components/calendar/types.d.ts +2 -16
- package/dist/punkt-react.cjs +1 -1
- package/dist/punkt-react.js +601 -593
- package/dist/shared-types/calendar.d.ts +67 -0
- package/dist/shared-types/index.d.ts +2 -1
- package/package.json +2 -2
- package/src/components/calendar/types.ts +2 -23
- package/src/components/calendar/useCalendarState.ts +8 -8
- package/src/components/datepicker/Datepicker.accessibility.test.tsx +9 -5
- package/src/components/datepicker/Datepicker.core.test.tsx +1 -1
- package/src/components/datepicker/Datepicker.selection.test.tsx +23 -0
- package/src/components/datepicker/DatepickerInputs.tsx +6 -7
- package/src/components/datepicker/useDatepickerState.ts +3 -1
|
@@ -5,6 +5,73 @@
|
|
|
5
5
|
* Used by both Elements and React calendar implementations
|
|
6
6
|
* and by the shared calendar helper functions.
|
|
7
7
|
*/
|
|
8
|
+
import type { IPktStringsProps } from '../shared-strings';
|
|
9
|
+
/** Calendar props shared by React and Elements. */
|
|
10
|
+
export interface IPktCalendarBase extends IPktStringsProps<'dates' | 'calendar'> {
|
|
11
|
+
id?: string;
|
|
12
|
+
currentmonth?: Date | string | null;
|
|
13
|
+
selected?: string[];
|
|
14
|
+
excludeweekdays?: string[];
|
|
15
|
+
excludedates?: Date[] | string[];
|
|
16
|
+
earliest?: string | null;
|
|
17
|
+
latest?: string | null;
|
|
18
|
+
weeknumbers?: boolean;
|
|
19
|
+
withcontrols?: boolean;
|
|
20
|
+
multiple?: boolean;
|
|
21
|
+
maxMultiple?: number;
|
|
22
|
+
range?: boolean;
|
|
23
|
+
today?: string | null;
|
|
24
|
+
}
|
|
25
|
+
export declare const CALENDAR_DEFAULTS: {
|
|
26
|
+
readonly multiple: false;
|
|
27
|
+
readonly maxMultiple: 0;
|
|
28
|
+
readonly range: false;
|
|
29
|
+
readonly earliest: any;
|
|
30
|
+
readonly latest: any;
|
|
31
|
+
readonly weeknumbers: false;
|
|
32
|
+
readonly withcontrols: false;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Documented Calendar props (subset of `IPktCalendarBase`), plus the
|
|
36
|
+
* React-only selection/close callbacks and the Elements-only deprecated
|
|
37
|
+
* per-string props that `strings` / `setPktStrings()` have superseded.
|
|
38
|
+
*/
|
|
39
|
+
export type ICalendarSpecProps = Pick<IPktCalendarBase, 'id' | 'currentmonth' | 'selected' | 'excludeweekdays' | 'excludedates' | 'earliest' | 'latest' | 'weeknumbers' | 'withcontrols' | 'multiple' | 'maxMultiple' | 'range' | 'today'> & {
|
|
40
|
+
/**
|
|
41
|
+
* React-only callback fired with the updated selection (ISO date strings).
|
|
42
|
+
* @remarks Elements-paritet: `date-selected`-eventet.
|
|
43
|
+
*/
|
|
44
|
+
onDateSelected?: (selected: string[]) => void;
|
|
45
|
+
/**
|
|
46
|
+
* React-only callback fired when the calendar requests to close (Escape or focus-out).
|
|
47
|
+
* @remarks Elements-paritet: `close`-eventet.
|
|
48
|
+
*/
|
|
49
|
+
onClose?: () => void;
|
|
50
|
+
/**
|
|
51
|
+
* @deprecated Elements-only. Bruk `strings={{ dates: { daysShort: [...] } }}` eller `setPktStrings()`.
|
|
52
|
+
*/
|
|
53
|
+
dayStrings?: string[];
|
|
54
|
+
/**
|
|
55
|
+
* @deprecated Elements-only. Bruk `strings={{ dates: { days: [...] } }}` eller `setPktStrings()`.
|
|
56
|
+
*/
|
|
57
|
+
dayStringsLong?: string[];
|
|
58
|
+
/**
|
|
59
|
+
* @deprecated Elements-only. Bruk `strings={{ dates: { months: [...] } }}` eller `setPktStrings()`.
|
|
60
|
+
*/
|
|
61
|
+
monthStrings?: string[];
|
|
62
|
+
/**
|
|
63
|
+
* @deprecated Elements-only. Bruk `strings={{ dates: { week: '...' } }}` eller `setPktStrings()`.
|
|
64
|
+
*/
|
|
65
|
+
weekString?: string;
|
|
66
|
+
/**
|
|
67
|
+
* @deprecated Elements-only. Bruk `strings={{ calendar: { prevMonth: '...' } }}` eller `setPktStrings()`.
|
|
68
|
+
*/
|
|
69
|
+
prevMonthString?: string;
|
|
70
|
+
/**
|
|
71
|
+
* @deprecated Elements-only. Bruk `strings={{ calendar: { nextMonth: '...' } }}` eller `setPktStrings()`.
|
|
72
|
+
*/
|
|
73
|
+
nextMonthString?: string;
|
|
74
|
+
};
|
|
8
75
|
export interface IDateConstraints {
|
|
9
76
|
earliest: string | null;
|
|
10
77
|
latest: string | null;
|
|
@@ -25,7 +25,8 @@ export type { IRenderLink, TRenderLink } from './render-link';
|
|
|
25
25
|
export type { IPktStringsProps } from '../shared-strings';
|
|
26
26
|
export type { IHelptextProps, ITagOptionalRequiredProps, IFormFieldLabelProps, IFormFieldErrorProps, IFormFieldCommonProps, TFormFieldCommonSpecKeys, } from './form-field';
|
|
27
27
|
export type { TMessageboxSkin, TCardSkin, TTagSkin } from './skin';
|
|
28
|
-
export
|
|
28
|
+
export { CALENDAR_DEFAULTS } from './calendar';
|
|
29
|
+
export type { IPktCalendarBase, ICalendarSpecProps, IDateConstraints, ICalendarGridDimensions, IGridCellPosition, TDateRangeMap, ISelectionState, ISelectionOptions, } from './calendar';
|
|
29
30
|
export type { IDatepickerStrings } from './datepicker';
|
|
30
31
|
export { defaultSingleStrings, defaultRangeStrings } from './datepicker';
|
|
31
32
|
export type { ITimepickerStrings } from './timepicker';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oslokommune/punkt-react",
|
|
3
|
-
"version": "18.
|
|
3
|
+
"version": "18.6.1",
|
|
4
4
|
"description": "React komponentbibliotek til Punkt, et designsystem laget av Oslo Origo",
|
|
5
5
|
"homepage": "https://punkt.oslo.kommune.no",
|
|
6
6
|
"author": "Team Designsystem, Oslo Origo",
|
|
@@ -102,5 +102,5 @@
|
|
|
102
102
|
"url": "https://github.com/oslokommune/punkt/issues"
|
|
103
103
|
},
|
|
104
104
|
"license": "MIT",
|
|
105
|
-
"gitHead": "
|
|
105
|
+
"gitHead": "8d641683796be69cb519cfe8ca021731d165e8f1"
|
|
106
106
|
}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import type { IDateConstraints, TDateRangeMap } from 'shared-types/calendar'
|
|
2
|
-
|
|
3
|
-
import type { IPktStringsProps } from 'shared-strings'
|
|
1
|
+
import type { IDateConstraints, IPktCalendarBase, TDateRangeMap } from 'shared-types/calendar'
|
|
4
2
|
|
|
5
3
|
/** Datonavn og navigasjonstekster slik kalender-underkomponentene forventer dem. */
|
|
6
4
|
export interface IPktCalendarStrings {
|
|
@@ -23,31 +21,12 @@ export interface PktCalendarHandle {
|
|
|
23
21
|
close: () => void
|
|
24
22
|
}
|
|
25
23
|
|
|
26
|
-
export interface IPktCalendar extends
|
|
27
|
-
// Selection
|
|
28
|
-
selected?: string[]
|
|
29
|
-
multiple?: boolean
|
|
30
|
-
maxMultiple?: number
|
|
31
|
-
range?: boolean
|
|
32
|
-
|
|
33
|
-
// Constraints
|
|
34
|
-
earliest?: string | null
|
|
35
|
-
latest?: string | null
|
|
36
|
-
excludedates?: Date[] | string[]
|
|
37
|
-
excludeweekdays?: string[]
|
|
38
|
-
|
|
39
|
-
// Display
|
|
40
|
-
weeknumbers?: boolean
|
|
41
|
-
withcontrols?: boolean
|
|
42
|
-
currentmonth?: Date | string | null
|
|
43
|
-
today?: string
|
|
44
|
-
|
|
24
|
+
export interface IPktCalendar extends IPktCalendarBase {
|
|
45
25
|
// Callbacks
|
|
46
26
|
onDateSelected?: (selected: string[]) => void
|
|
47
27
|
onClose?: () => void
|
|
48
28
|
|
|
49
29
|
// React standard
|
|
50
|
-
id?: string
|
|
51
30
|
className?: string
|
|
52
31
|
}
|
|
53
32
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'
|
|
2
2
|
import { formatISODate, newDate, parseISODateString, newDateFromDate, todayInTz } from 'shared-utils/date-utils'
|
|
3
|
-
import type
|
|
3
|
+
import { CALENDAR_DEFAULTS, type IDateConstraints, type TDateRangeMap } from 'shared-types/calendar'
|
|
4
4
|
import { isDateExcluded, isDayDisabled as checkDayDisabled } from 'shared-utils/calendar/date-validation'
|
|
5
5
|
import {
|
|
6
6
|
convertSelectedToDates,
|
|
@@ -23,15 +23,15 @@ import { usePktStrings } from '../../hooks/usePktStrings'
|
|
|
23
23
|
export function useCalendarState(props: IPktCalendar): ICalendarState {
|
|
24
24
|
const {
|
|
25
25
|
selected: selectedProp,
|
|
26
|
-
multiple =
|
|
27
|
-
maxMultiple =
|
|
28
|
-
range =
|
|
29
|
-
earliest =
|
|
30
|
-
latest =
|
|
26
|
+
multiple = CALENDAR_DEFAULTS.multiple,
|
|
27
|
+
maxMultiple = CALENDAR_DEFAULTS.maxMultiple,
|
|
28
|
+
range = CALENDAR_DEFAULTS.range,
|
|
29
|
+
earliest = CALENDAR_DEFAULTS.earliest,
|
|
30
|
+
latest = CALENDAR_DEFAULTS.latest,
|
|
31
31
|
excludedates: excludedatesProp,
|
|
32
32
|
excludeweekdays = [],
|
|
33
|
-
weeknumbers =
|
|
34
|
-
withcontrols =
|
|
33
|
+
weeknumbers = CALENDAR_DEFAULTS.weeknumbers,
|
|
34
|
+
withcontrols = CALENDAR_DEFAULTS.withcontrols,
|
|
35
35
|
currentmonth: currentmonthProp,
|
|
36
36
|
today: todayProp,
|
|
37
37
|
strings: stringsProp,
|
|
@@ -153,7 +153,7 @@ describe('PktDatepicker', () => {
|
|
|
153
153
|
|
|
154
154
|
const button = container.querySelector('button[type="button"]')
|
|
155
155
|
expect(button).toBeInTheDocument()
|
|
156
|
-
expect(button).
|
|
156
|
+
expect(button).toHaveAccessibleName('Åpne kalender')
|
|
157
157
|
})
|
|
158
158
|
|
|
159
159
|
test('input has aria-describedby when helptext is present', () => {
|
|
@@ -162,7 +162,9 @@ describe('PktDatepicker', () => {
|
|
|
162
162
|
})
|
|
163
163
|
|
|
164
164
|
const input = container.querySelector('input[type="date"]') as HTMLInputElement
|
|
165
|
-
|
|
165
|
+
const describedby = input.getAttribute('aria-describedby')
|
|
166
|
+
expect(describedby).toBe(`${datePickerId}-input-helptext`)
|
|
167
|
+
expect(container.querySelector(`#${describedby}`)).not.toBeNull()
|
|
166
168
|
})
|
|
167
169
|
|
|
168
170
|
test('input does not have aria-describedby when no helptext', () => {
|
|
@@ -189,7 +191,9 @@ describe('PktDatepicker', () => {
|
|
|
189
191
|
})
|
|
190
192
|
|
|
191
193
|
const input = container.querySelector('input[type="date"]') as HTMLInputElement
|
|
192
|
-
|
|
194
|
+
const errormessage = input.getAttribute('aria-errormessage')
|
|
195
|
+
expect(errormessage).toBe(`${datePickerId}-input-error`)
|
|
196
|
+
expect(container.querySelector(`#${errormessage}`)).not.toBeNull()
|
|
193
197
|
})
|
|
194
198
|
|
|
195
199
|
test('input does not have aria-invalid when no error', () => {
|
|
@@ -261,7 +265,7 @@ describe('PktDatepicker', () => {
|
|
|
261
265
|
})
|
|
262
266
|
|
|
263
267
|
const button = container.querySelector('button[type="button"]')
|
|
264
|
-
expect(button).
|
|
268
|
+
expect(button).toHaveAccessibleName('Open calendar')
|
|
265
269
|
})
|
|
266
270
|
|
|
267
271
|
test('uses custom strings for range labels', () => {
|
|
@@ -285,7 +289,7 @@ describe('PktDatepicker', () => {
|
|
|
285
289
|
})
|
|
286
290
|
|
|
287
291
|
const button = container.querySelector('button[type="button"]')
|
|
288
|
-
expect(button).
|
|
292
|
+
expect(button).toHaveAccessibleName('Åpne kalender')
|
|
289
293
|
|
|
290
294
|
const prefix = container.querySelector('.pkt-input-prefix')
|
|
291
295
|
expect(prefix).toHaveTextContent('Fra')
|
|
@@ -72,7 +72,7 @@ describe('PktDatepicker', () => {
|
|
|
72
72
|
|
|
73
73
|
const button = container.querySelector('button[type="button"]')
|
|
74
74
|
expect(button).toBeInTheDocument()
|
|
75
|
-
expect(button).
|
|
75
|
+
expect(button).toHaveAccessibleName('Åpne kalender')
|
|
76
76
|
})
|
|
77
77
|
})
|
|
78
78
|
|
|
@@ -147,6 +147,29 @@ describe('PktDatepicker', () => {
|
|
|
147
147
|
})
|
|
148
148
|
|
|
149
149
|
describe('Range selection', () => {
|
|
150
|
+
test('fires change on a complete range and on clearing, not on the first date', () => {
|
|
151
|
+
const fired: string[] = []
|
|
152
|
+
const { container } = createDatepickerTest({
|
|
153
|
+
range: true,
|
|
154
|
+
calendarOpen: true,
|
|
155
|
+
currentmonth: '2026-01',
|
|
156
|
+
onChange: (e: ChangeEvent<HTMLInputElement>) => fired.push(e.target.value),
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
const days = Array.from(container.querySelectorAll('button')).filter((b) =>
|
|
160
|
+
/^\d+$/.test((b.textContent ?? '').trim()),
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
fireEvent.click(days[14])
|
|
164
|
+
expect(fired).toEqual([])
|
|
165
|
+
|
|
166
|
+
fireEvent.click(days[19])
|
|
167
|
+
expect(fired).toEqual(['2026-01-15,2026-01-20'])
|
|
168
|
+
|
|
169
|
+
fireEvent.click(days[14])
|
|
170
|
+
expect(fired).toEqual(['2026-01-15,2026-01-20', ''])
|
|
171
|
+
})
|
|
172
|
+
|
|
150
173
|
test('renders two inputs for range mode', () => {
|
|
151
174
|
const { container } = createDatepickerTest({
|
|
152
175
|
range: true,
|
|
@@ -64,7 +64,6 @@ export const DatepickerInputs = ({ state }: { state: IDatepickerState }) => {
|
|
|
64
64
|
type="button"
|
|
65
65
|
onClick={toggleCalendar}
|
|
66
66
|
disabled={disabled}
|
|
67
|
-
aria-label={strings.datepicker.openPicker}
|
|
68
67
|
>
|
|
69
68
|
<PktIcon name="calendar" />
|
|
70
69
|
<span className="pkt-btn__text">{strings.datepicker.openPicker}</span>
|
|
@@ -93,9 +92,9 @@ export const DatepickerInputs = ({ state }: { state: IDatepickerState }) => {
|
|
|
93
92
|
placeholder={placeholder}
|
|
94
93
|
readOnly={readOnly || (isIOSDevice && !readOnly)}
|
|
95
94
|
disabled={isInputDisabled}
|
|
96
|
-
aria-describedby={helptext ? `${
|
|
95
|
+
aria-describedby={helptext ? `${inputId}-helptext` : undefined}
|
|
97
96
|
aria-invalid={hasError}
|
|
98
|
-
aria-errormessage={hasError ? `${
|
|
97
|
+
aria-errormessage={hasError ? `${inputId}-error` : undefined}
|
|
99
98
|
onClick={handleInputClick}
|
|
100
99
|
onTouchEnd={handleInputTouchEnd}
|
|
101
100
|
onKeyDown={handleMultipleKeydown}
|
|
@@ -129,9 +128,9 @@ export const DatepickerInputs = ({ state }: { state: IDatepickerState }) => {
|
|
|
129
128
|
disabled={disabled}
|
|
130
129
|
required={required}
|
|
131
130
|
aria-label={`${label} ${strings.generic.from}`}
|
|
132
|
-
aria-describedby={helptext ? `${
|
|
131
|
+
aria-describedby={helptext ? `${inputId}-helptext` : undefined}
|
|
133
132
|
aria-invalid={hasError}
|
|
134
|
-
aria-errormessage={hasError ? `${
|
|
133
|
+
aria-errormessage={hasError ? `${inputId}-error` : undefined}
|
|
135
134
|
onClick={handleInputClick}
|
|
136
135
|
onTouchEnd={handleInputTouchEnd}
|
|
137
136
|
onKeyDown={handleRangeFromKeydown}
|
|
@@ -187,9 +186,9 @@ export const DatepickerInputs = ({ state }: { state: IDatepickerState }) => {
|
|
|
187
186
|
readOnly={readOnly || (isIOSDevice && !readOnly)}
|
|
188
187
|
disabled={disabled}
|
|
189
188
|
required={required}
|
|
190
|
-
aria-describedby={helptext ? `${
|
|
189
|
+
aria-describedby={helptext ? `${inputId}-helptext` : undefined}
|
|
191
190
|
aria-invalid={hasError}
|
|
192
|
-
aria-errormessage={hasError ? `${
|
|
191
|
+
aria-errormessage={hasError ? `${inputId}-error` : undefined}
|
|
193
192
|
onClick={handleInputClick}
|
|
194
193
|
onTouchEnd={handleInputTouchEnd}
|
|
195
194
|
onKeyDown={handleSingleKeydown}
|
|
@@ -188,12 +188,14 @@ export function useDatepickerState(props: IPktDatepicker, ref: React.ForwardedRe
|
|
|
188
188
|
|
|
189
189
|
const dispatchChange = useCallback(
|
|
190
190
|
(newValues: string[]) => {
|
|
191
|
+
// Én dato er ingen fullført periode. Tom er en fullført endring.
|
|
192
|
+
if (range && newValues.length === 1) return
|
|
191
193
|
const input = changeInputRef.current
|
|
192
194
|
if (!input || !nativeInputValueSetter) return
|
|
193
195
|
nativeInputValueSetter.call(input, newValues.join(','))
|
|
194
196
|
input.dispatchEvent(new Event('input', { bubbles: true }))
|
|
195
197
|
},
|
|
196
|
-
[nativeInputValueSetter],
|
|
198
|
+
[nativeInputValueSetter, range],
|
|
197
199
|
)
|
|
198
200
|
|
|
199
201
|
// Calendar toggle
|