@oslokommune/punkt-react 19.3.0 → 19.4.0
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 +13 -0
- package/dist/components/card/Card.d.ts +2 -1
- package/dist/components/consent/Consent.d.ts +6 -38
- package/dist/components/datepicker/types.d.ts +1 -1
- package/dist/components/fileupload/extensions/Comments.d.ts +2 -1
- package/dist/components/fileupload/extensions/Remove.d.ts +2 -1
- package/dist/components/fileupload/extensions/Rename.d.ts +2 -1
- package/dist/components/fileupload/hooks/index.d.ts +1 -0
- package/dist/components/fileupload/hooks/useImageObjectUrl.d.ts +1 -0
- package/dist/components/fileupload/hooks.d.ts +1 -0
- package/dist/components/fileupload/types.d.ts +2 -0
- package/dist/components/header-menu/HeaderMenu.d.ts +3 -2
- package/dist/components/messagebox/Messagebox.d.ts +2 -1
- package/dist/components/modal/Modal.d.ts +2 -1
- package/dist/punkt-react.cjs +1 -1
- package/dist/punkt-react.js +1986 -1955
- package/dist/shared-strings/nb.d.ts +34 -0
- package/dist/shared-types/consent.d.ts +69 -0
- package/dist/shared-types/index.d.ts +2 -0
- package/package.json +2 -2
- package/src/components/alert/Alert.test.tsx +5 -5
- package/src/components/alert/Alert.tsx +2 -4
- package/src/components/breadcrumbs/Breadcrumbs.tsx +15 -12
- package/src/components/calendar/Calendar.core.test.tsx +22 -16
- package/src/components/calendar/CalendarNav.tsx +11 -6
- package/src/components/calendar/useCalendarState.ts +42 -55
- package/src/components/card/Card.tsx +17 -5
- package/src/components/combobox/ComboboxTags.tsx +3 -3
- package/src/components/combobox/Listbox.tsx +7 -5
- package/src/components/combobox/useComboboxState.ts +22 -18
- package/src/components/consent/Consent.test.tsx +6 -0
- package/src/components/consent/Consent.tsx +30 -55
- package/src/components/datepicker/DatepickerInputs.tsx +3 -9
- package/src/components/datepicker/types.ts +1 -1
- package/src/components/datepicker/useDatepickerState.ts +22 -17
- package/src/components/fileupload/DropZone.tsx +10 -10
- package/src/components/fileupload/FileUpload.tsx +9 -14
- package/src/components/fileupload/Subcomponents.tsx +78 -84
- package/src/components/fileupload/extensions/Comments.tsx +56 -50
- package/src/components/fileupload/extensions/Remove.tsx +8 -3
- package/src/components/fileupload/extensions/Rename.tsx +12 -8
- package/src/components/fileupload/hooks/index.ts +1 -0
- package/src/components/fileupload/hooks/useImageObjectUrl.ts +15 -0
- package/src/components/fileupload/hooks/useOperationState.ts +9 -16
- package/src/components/fileupload/hooks.ts +1 -0
- package/src/components/fileupload/types.ts +7 -1
- package/src/components/formAttribute.test.tsx +1 -0
- package/src/components/header/HeaderGlobal.tsx +8 -2
- package/src/components/header/HeaderService.test.tsx +5 -2
- package/src/components/header/HeaderService.tsx +5 -5
- package/src/components/header-menu/HeaderMenu.tsx +8 -3
- package/src/components/headerUserMenu/UserMenu.tsx +3 -2
- package/src/components/inputwrapper/InputWrapper.tsx +20 -27
- package/src/components/loader/Loader.tsx +12 -9
- package/src/components/messagebox/Messagebox.tsx +6 -2
- package/src/components/modal/Modal.tsx +13 -9
- package/src/components/searchinput/SearchInput.tsx +10 -6
- package/src/components/tabs/Tabs.test.tsx +1 -1
- package/src/components/tabs/Tabs.tsx +30 -35
- package/src/components/tag/Tag.tsx +7 -6
- package/src/components/textarea/Textarea.test.tsx +9 -0
- package/src/components/textarea/Textarea.tsx +6 -10
- package/src/components/textinput/Textinput.tsx +3 -9
- package/src/components/timepicker/useTimepickerState.ts +25 -21
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
|
|
3
|
+
import { formatString } from 'shared-strings'
|
|
3
4
|
import type { IComboboxState } from './types'
|
|
4
5
|
import { PktIcon } from '../icon/Icon'
|
|
5
6
|
import { usePktStrings } from '../../hooks/usePktStrings'
|
|
7
|
+
import { withStableKeys } from '../../utils/stableKeys'
|
|
6
8
|
|
|
7
9
|
interface IListboxProps {
|
|
8
10
|
state: IComboboxState
|
|
@@ -76,7 +78,7 @@ export const Listbox = ({ state }: IListboxProps) => {
|
|
|
76
78
|
)}
|
|
77
79
|
{showMaxBanner && (
|
|
78
80
|
<div className="pkt-listbox__banner pkt-listbox__banner--maximum-reached">
|
|
79
|
-
{
|
|
81
|
+
{formatString(s.listbox.maxReached, { selected: selectedCount, max: maxlength })}
|
|
80
82
|
</div>
|
|
81
83
|
)}
|
|
82
84
|
|
|
@@ -98,20 +100,20 @@ export const Listbox = ({ state }: IListboxProps) => {
|
|
|
98
100
|
onKeyDown={handleNewOptionKeydown}
|
|
99
101
|
>
|
|
100
102
|
<PktIcon className="pkt-listbox__banner-icon" name="plus-sign" />
|
|
101
|
-
|
|
103
|
+
{formatString(s.listbox.addValue, { value: addValueText })}
|
|
102
104
|
</div>
|
|
103
105
|
)}
|
|
104
106
|
|
|
105
107
|
{options.length === 0 && filteredOptions.length === 0 && !showUserMessage && (
|
|
106
108
|
<div className="pkt-listbox__banner pkt-listbox__banner--empty">
|
|
107
109
|
<PktIcon className="pkt-listbox__banner-icon" name="exclamation-mark-circle" />
|
|
108
|
-
|
|
110
|
+
{s.listbox.empty}
|
|
109
111
|
</div>
|
|
110
112
|
)}
|
|
111
113
|
</div>
|
|
112
114
|
|
|
113
115
|
<ul className="pkt-listbox__options" role="presentation">
|
|
114
|
-
{filteredOptions.map((option, index) => {
|
|
116
|
+
{withStableKeys(filteredOptions, (option) => option.value).map(({ item: option, key }, index) => {
|
|
115
117
|
const optionClasses = [
|
|
116
118
|
'pkt-listbox__option',
|
|
117
119
|
!multiple && option.selected && 'pkt-listbox__option--selected',
|
|
@@ -124,7 +126,7 @@ export const Listbox = ({ state }: IListboxProps) => {
|
|
|
124
126
|
|
|
125
127
|
return (
|
|
126
128
|
<li
|
|
127
|
-
key={
|
|
129
|
+
key={key}
|
|
128
130
|
className={optionClasses}
|
|
129
131
|
onClick={() => handleOptionClick(option.value)}
|
|
130
132
|
onKeyDown={(e) => handleOptionKeydown(e, option.value)}
|
|
@@ -175,12 +175,11 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
175
175
|
// Store initial default values for form reset
|
|
176
176
|
const initialValuesRef = useRef(internalValues)
|
|
177
177
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}, [value, isControlled, multiple])
|
|
178
|
+
const [prevValue, setPrevValue] = useState(value)
|
|
179
|
+
if (isControlled && value !== prevValue) {
|
|
180
|
+
setPrevValue(value)
|
|
181
|
+
setInternalValues(parseValueToArray(value as string | string[] | null | undefined, multiple))
|
|
182
|
+
}
|
|
184
183
|
|
|
185
184
|
const values = isControlled
|
|
186
185
|
? parseValueToArray(value as string | string[] | null | undefined, multiple)
|
|
@@ -201,7 +200,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
201
200
|
const [isOpen, setIsOpen] = useState(false)
|
|
202
201
|
const [searchValue, setSearchValue] = useState('')
|
|
203
202
|
const [inputValue, setInputValue] = useState('')
|
|
204
|
-
const [userInfoMessageKey,
|
|
203
|
+
const [userInfoMessageKey, setUserInfoMessageKey] = useState<TComboboxMessageKey>('')
|
|
205
204
|
const [messageValue, setMessageValue] = useState<string | null>(null)
|
|
206
205
|
const [addValueText, setAddValueText] = useState<string | null>(null)
|
|
207
206
|
const [inputFocus, setInputFocus] = useState(false)
|
|
@@ -282,7 +281,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
282
281
|
if (!optValue) return
|
|
283
282
|
if (values.includes(optValue)) return
|
|
284
283
|
if (multiple && isMaxSelectionReached(values.length, maxlength ?? null)) {
|
|
285
|
-
|
|
284
|
+
setUserInfoMessageKey('maxSelected')
|
|
286
285
|
return
|
|
287
286
|
}
|
|
288
287
|
const newValues = multiple ? [...values, optValue] : [optValue]
|
|
@@ -352,7 +351,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
352
351
|
}
|
|
353
352
|
if (!multiple) {
|
|
354
353
|
setEditingSingleValue(false)
|
|
355
|
-
|
|
354
|
+
setUserInfoMessageKey('')
|
|
356
355
|
}
|
|
357
356
|
}
|
|
358
357
|
},
|
|
@@ -364,7 +363,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
364
363
|
(val: string | null) => {
|
|
365
364
|
if (disabled) return
|
|
366
365
|
|
|
367
|
-
|
|
366
|
+
setUserInfoMessageKey('')
|
|
368
367
|
setAddValueText(null)
|
|
369
368
|
|
|
370
369
|
const valueFromOptions = findOptionByValue(options, val)?.value || null
|
|
@@ -458,7 +457,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
458
457
|
setSearchValue(newSearchValue)
|
|
459
458
|
resetInput(shouldResetInput, resultingSingleValue)
|
|
460
459
|
// After resetInput, which clears any stale message in single mode.
|
|
461
|
-
|
|
460
|
+
setUserInfoMessageKey(newUserInfoMessage)
|
|
462
461
|
setMessageValue(newMessageValue)
|
|
463
462
|
|
|
464
463
|
if (!shouldOptionsBeOpen) {
|
|
@@ -489,7 +488,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
489
488
|
const closeAndProcessInput = useCallback(() => {
|
|
490
489
|
setInputFocus(false)
|
|
491
490
|
setAddValueText(null)
|
|
492
|
-
|
|
491
|
+
setUserInfoMessageKey('')
|
|
493
492
|
setSearchValue('')
|
|
494
493
|
|
|
495
494
|
if (inputRef.current && inputRef.current.type !== 'hidden') {
|
|
@@ -552,7 +551,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
552
551
|
|
|
553
552
|
if (!search) {
|
|
554
553
|
setAddValueText(null)
|
|
555
|
-
|
|
554
|
+
setUserInfoMessageKey('')
|
|
556
555
|
if (!multiple && values[0]) {
|
|
557
556
|
removeSelected(values[0])
|
|
558
557
|
}
|
|
@@ -561,7 +560,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
561
560
|
|
|
562
561
|
const result = getSearchInfoMessage(currentInputValue, values, options, allowUserInput)
|
|
563
562
|
setAddValueText(result.addValueText)
|
|
564
|
-
|
|
563
|
+
setUserInfoMessageKey(result.userInfoMessage)
|
|
565
564
|
},
|
|
566
565
|
[values, options, allowUserInput, multiple, removeSelected],
|
|
567
566
|
)
|
|
@@ -645,7 +644,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
645
644
|
// Just reset the input and keep the "already selected" message.
|
|
646
645
|
if (input && values.includes(input)) {
|
|
647
646
|
resetInput(true)
|
|
648
|
-
|
|
647
|
+
setUserInfoMessageKey('alreadySelected')
|
|
649
648
|
break
|
|
650
649
|
}
|
|
651
650
|
toggleValue(input)
|
|
@@ -1041,16 +1040,18 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
1041
1040
|
// Form reset: listen for the parent <form>'s reset event and restore initial values
|
|
1042
1041
|
useEffect(() => {
|
|
1043
1042
|
if (!ownerForm) return
|
|
1043
|
+
let resetTimeout: number | undefined
|
|
1044
1044
|
|
|
1045
1045
|
const handleReset = () => {
|
|
1046
1046
|
// Use setTimeout so the reset event finishes before we update state
|
|
1047
|
-
window.
|
|
1047
|
+
window.clearTimeout(resetTimeout)
|
|
1048
|
+
resetTimeout = window.setTimeout(() => {
|
|
1048
1049
|
setInternalValues(initialValuesRef.current)
|
|
1049
1050
|
setUserAddedOptions([])
|
|
1050
1051
|
setInputValue('')
|
|
1051
1052
|
setSearchValue('')
|
|
1052
1053
|
setAddValueText(null)
|
|
1053
|
-
|
|
1054
|
+
setUserInfoMessageKey('')
|
|
1054
1055
|
if (changeInputRef.current) {
|
|
1055
1056
|
const resetVal = multiple ? initialValuesRef.current.join(',') : initialValuesRef.current[0] || ''
|
|
1056
1057
|
changeInputRef.current.value = resetVal
|
|
@@ -1059,7 +1060,10 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
1059
1060
|
}
|
|
1060
1061
|
|
|
1061
1062
|
ownerForm.addEventListener('reset', handleReset)
|
|
1062
|
-
return () =>
|
|
1063
|
+
return () => {
|
|
1064
|
+
ownerForm.removeEventListener('reset', handleReset)
|
|
1065
|
+
window.clearTimeout(resetTimeout)
|
|
1066
|
+
}
|
|
1063
1067
|
}, [multiple, ownerForm])
|
|
1064
1068
|
|
|
1065
1069
|
// Expose value getter/setter on ref for RHF register() compatibility.
|
|
@@ -74,6 +74,12 @@ describe('PktConsent', () => {
|
|
|
74
74
|
expect(screen.getByRole('button', { name: 'Go to settings' })).toBeInTheDocument()
|
|
75
75
|
})
|
|
76
76
|
|
|
77
|
+
test.each(['sv', 'constructor'])('falls back to bokmål when i18nLanguage is "%s"', (language) => {
|
|
78
|
+
render(<PktConsent i18nLanguage={language} />)
|
|
79
|
+
|
|
80
|
+
expect(screen.getByRole('button', { name: 'Innstillinger for informasjonskapsler' })).toBeInTheDocument()
|
|
81
|
+
})
|
|
82
|
+
|
|
77
83
|
test('renders a custom triggerText when provided', () => {
|
|
78
84
|
render(<PktConsent triggerText="Endre samtykke" />)
|
|
79
85
|
|
|
@@ -1,39 +1,22 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
|
|
3
3
|
import { forwardRef, HTMLAttributes, MouseEvent as ReactMouseEvent, Ref, useEffect, useRef } from 'react'
|
|
4
|
+
import {
|
|
5
|
+
CONSENT_DEFAULTS,
|
|
6
|
+
type IConsentValue,
|
|
7
|
+
type ICookieBannerApi,
|
|
8
|
+
type ICookieConsentEvent,
|
|
9
|
+
type ICookieEvents,
|
|
10
|
+
type IPktConsentBase,
|
|
11
|
+
type TConsentDetailsMap,
|
|
12
|
+
type TCookieEventHandler,
|
|
13
|
+
type TPktConsentTriggerType,
|
|
14
|
+
} from 'shared-types'
|
|
4
15
|
|
|
5
16
|
import { PktButton } from '../button/Button'
|
|
6
17
|
import { consentStrings } from './strings'
|
|
7
18
|
|
|
8
|
-
|
|
9
|
-
name: string
|
|
10
|
-
consent: boolean
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
interface ConsentValue {
|
|
14
|
-
items: ConsentItem[]
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
interface CookieConsentEvent {
|
|
18
|
-
value: string | ConsentValue
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
type ConsentDetailsMap = Record<string, boolean>
|
|
22
|
-
|
|
23
|
-
type CookieEventHandler = (consent: CookieConsentEvent) => void
|
|
24
|
-
|
|
25
|
-
interface CookieEvents {
|
|
26
|
-
on(eventName: string, handler: CookieEventHandler): void
|
|
27
|
-
off(eventName: string, handler: CookieEventHandler): void
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
interface CookieBannerApi {
|
|
31
|
-
cookieConsent: {
|
|
32
|
-
validateConsentCookie(): Promise<boolean>
|
|
33
|
-
getConsentCookie(): string | ConsentValue
|
|
34
|
-
}
|
|
35
|
-
openCookieModal(): void
|
|
36
|
-
}
|
|
19
|
+
export type { TPktConsentTriggerType }
|
|
37
20
|
|
|
38
21
|
declare global {
|
|
39
22
|
interface Window {
|
|
@@ -43,8 +26,8 @@ declare global {
|
|
|
43
26
|
cookieBanner_cookieExpiryDays?: string | null
|
|
44
27
|
cookieBanner_googleAnalyticsId?: string | null
|
|
45
28
|
cookieBanner_hotjarId?: string | null
|
|
46
|
-
cookieBanner?:
|
|
47
|
-
__cookieEvents?:
|
|
29
|
+
cookieBanner?: ICookieBannerApi
|
|
30
|
+
__cookieEvents?: ICookieEvents
|
|
48
31
|
}
|
|
49
32
|
}
|
|
50
33
|
|
|
@@ -85,39 +68,28 @@ const parseJsonOrPassThrough = <T,>(value: T | string): T => {
|
|
|
85
68
|
}
|
|
86
69
|
}
|
|
87
70
|
|
|
88
|
-
const toConsentDetails = (consent:
|
|
89
|
-
const parsed = parseJsonOrPassThrough<
|
|
90
|
-
return parsed.items.reduce<
|
|
71
|
+
const toConsentDetails = (consent: ICookieConsentEvent): TConsentDetailsMap => {
|
|
72
|
+
const parsed = parseJsonOrPassThrough<IConsentValue>(consent.value)
|
|
73
|
+
return parsed.items.reduce<TConsentDetailsMap>((acc, item) => {
|
|
91
74
|
acc[item.name] = item.consent
|
|
92
75
|
return acc
|
|
93
76
|
}, {})
|
|
94
77
|
}
|
|
95
78
|
|
|
96
|
-
export
|
|
97
|
-
|
|
98
|
-
export interface IPktConsent extends HTMLAttributes<HTMLElement> {
|
|
99
|
-
devMode?: boolean
|
|
100
|
-
cookieDomain?: string | null
|
|
101
|
-
cookieSecure?: string | null
|
|
102
|
-
cookieExpiryDays?: string | null
|
|
103
|
-
hotjarId?: string | null
|
|
104
|
-
googleAnalyticsId?: string | null
|
|
105
|
-
i18nLanguage?: string
|
|
106
|
-
triggerType?: TPktConsentTriggerType | null
|
|
107
|
-
triggerText?: string | null
|
|
108
|
-
onToggleConsent?: (e: CustomEvent<ConsentDetailsMap>) => void
|
|
79
|
+
export interface IPktConsent extends IPktConsentBase, HTMLAttributes<HTMLElement> {
|
|
80
|
+
onToggleConsent?: (e: CustomEvent<TConsentDetailsMap>) => void
|
|
109
81
|
}
|
|
110
82
|
|
|
111
83
|
export const PktConsent = forwardRef<HTMLElement, IPktConsent>(function PktConsent(
|
|
112
84
|
{
|
|
113
|
-
devMode =
|
|
85
|
+
devMode = CONSENT_DEFAULTS.devMode,
|
|
114
86
|
cookieDomain = null,
|
|
115
87
|
cookieSecure = null,
|
|
116
88
|
cookieExpiryDays = null,
|
|
117
89
|
hotjarId = null,
|
|
118
90
|
googleAnalyticsId = null,
|
|
119
|
-
i18nLanguage =
|
|
120
|
-
triggerType =
|
|
91
|
+
i18nLanguage = CONSENT_DEFAULTS.i18nLanguage,
|
|
92
|
+
triggerType = CONSENT_DEFAULTS.triggerType,
|
|
121
93
|
triggerText,
|
|
122
94
|
onToggleConsent,
|
|
123
95
|
...rest
|
|
@@ -146,12 +118,13 @@ export const PktConsent = forwardRef<HTMLElement, IPktConsent>(function PktConse
|
|
|
146
118
|
|
|
147
119
|
const triggerInitRef = useRef<() => void>(() => {})
|
|
148
120
|
|
|
149
|
-
const language = (
|
|
121
|
+
const language = (Object.hasOwn(consentStrings.i18n, i18nLanguage) ? i18nLanguage : 'nb') as keyof typeof consentStrings.i18n
|
|
150
122
|
const resolvedTriggerText = triggerText ?? consentStrings.i18n[language].contentPresentation.buttons.settings
|
|
151
123
|
|
|
152
124
|
useEffect(() => {
|
|
153
125
|
let cancelled = false
|
|
154
|
-
let cookieEventHandler:
|
|
126
|
+
let cookieEventHandler: TCookieEventHandler | undefined
|
|
127
|
+
let emitTimeout: number | undefined
|
|
155
128
|
|
|
156
129
|
const config = configRef.current
|
|
157
130
|
// cookieBanner_* identifiers are defined by the external cookie banner script
|
|
@@ -164,11 +137,11 @@ export const PktConsent = forwardRef<HTMLElement, IPktConsent>(function PktConse
|
|
|
164
137
|
if (config.devMode) window.cookieBanner_devMode = config.devMode
|
|
165
138
|
/* eslint-enable camelcase */
|
|
166
139
|
|
|
167
|
-
const emitConsents = (consent:
|
|
140
|
+
const emitConsents = (consent: ICookieConsentEvent) => {
|
|
168
141
|
const handler = onToggleRef.current
|
|
169
142
|
if (!handler) return
|
|
170
143
|
handler(
|
|
171
|
-
new CustomEvent<
|
|
144
|
+
new CustomEvent<TConsentDetailsMap>('toggle-consent', {
|
|
172
145
|
detail: toConsentDetails(consent),
|
|
173
146
|
bubbles: true,
|
|
174
147
|
cancelable: false,
|
|
@@ -187,7 +160,8 @@ export const PktConsent = forwardRef<HTMLElement, IPktConsent>(function PktConse
|
|
|
187
160
|
if (cancelled || !hasConsent) return
|
|
188
161
|
|
|
189
162
|
const cookie = banner.cookieConsent.getConsentCookie()
|
|
190
|
-
window.
|
|
163
|
+
window.clearTimeout(emitTimeout)
|
|
164
|
+
emitTimeout = window.setTimeout(() => {
|
|
191
165
|
if (!cancelled) emitConsents({ value: cookie })
|
|
192
166
|
}, 0)
|
|
193
167
|
|
|
@@ -205,6 +179,7 @@ export const PktConsent = forwardRef<HTMLElement, IPktConsent>(function PktConse
|
|
|
205
179
|
|
|
206
180
|
return () => {
|
|
207
181
|
cancelled = true
|
|
182
|
+
window.clearTimeout(emitTimeout)
|
|
208
183
|
if (cookieEventHandler) {
|
|
209
184
|
window.__cookieEvents?.off('CookieManager.setCookie', cookieEventHandler)
|
|
210
185
|
}
|
|
@@ -30,7 +30,7 @@ export const DatepickerInputs = ({ state }: { state: IDatepickerState }) => {
|
|
|
30
30
|
datepickerInputsClasses,
|
|
31
31
|
isInputDisabled,
|
|
32
32
|
inputRef,
|
|
33
|
-
|
|
33
|
+
inputToRef,
|
|
34
34
|
btnRef,
|
|
35
35
|
toggleCalendar,
|
|
36
36
|
handleFocus,
|
|
@@ -58,13 +58,7 @@ export const DatepickerInputs = ({ state }: { state: IDatepickerState }) => {
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
const renderCalendarButton = () => (
|
|
61
|
-
<button
|
|
62
|
-
ref={btnRef}
|
|
63
|
-
className={buttonClasses}
|
|
64
|
-
type="button"
|
|
65
|
-
onClick={toggleCalendar}
|
|
66
|
-
disabled={disabled}
|
|
67
|
-
>
|
|
61
|
+
<button ref={btnRef} className={buttonClasses} type="button" onClick={toggleCalendar} disabled={disabled}>
|
|
68
62
|
<PktIcon name="calendar" />
|
|
69
63
|
<span className="pkt-btn__text">{strings.datepicker.openPicker}</span>
|
|
70
64
|
</button>
|
|
@@ -143,7 +137,7 @@ export const DatepickerInputs = ({ state }: { state: IDatepickerState }) => {
|
|
|
143
137
|
</div>
|
|
144
138
|
{!showRangeLabels && <div className="pkt-input-separator">–</div>}
|
|
145
139
|
<input
|
|
146
|
-
ref={
|
|
140
|
+
ref={inputToRef}
|
|
147
141
|
className={inputClasses}
|
|
148
142
|
type={inputType}
|
|
149
143
|
id={`${id}-to`}
|
|
@@ -115,7 +115,7 @@ export interface IDatepickerState {
|
|
|
115
115
|
|
|
116
116
|
// Refs
|
|
117
117
|
inputRef: React.RefObject<HTMLInputElement | null>
|
|
118
|
-
|
|
118
|
+
inputToRef: React.RefObject<HTMLInputElement | null>
|
|
119
119
|
changeInputRef: React.RefObject<HTMLInputElement | null>
|
|
120
120
|
btnRef: React.RefObject<HTMLButtonElement | null>
|
|
121
121
|
wrapperRef: React.RefObject<HTMLDivElement | null>
|
|
@@ -79,24 +79,24 @@ export function useDatepickerState(props: IPktDatepicker, ref: React.ForwardedRe
|
|
|
79
79
|
// Store initial default values for form reset
|
|
80
80
|
const initialValuesRef = useRef(internalValue)
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
82
|
+
const [prevValue, setPrevValue] = useState(value)
|
|
83
|
+
if (isControlled && value !== prevValue) {
|
|
84
|
+
setPrevValue(value)
|
|
85
|
+
setInternalValue(normalizeValue(value))
|
|
86
|
+
}
|
|
87
87
|
|
|
88
88
|
// Calendar state
|
|
89
89
|
const [calendarOpen, setCalendarOpen] = useState(calendarOpenProp ?? false)
|
|
90
90
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
91
|
+
const [prevCalendarOpenProp, setPrevCalendarOpenProp] = useState(calendarOpenProp)
|
|
92
|
+
if (calendarOpenProp !== prevCalendarOpenProp) {
|
|
93
|
+
setPrevCalendarOpenProp(calendarOpenProp)
|
|
94
|
+
if (calendarOpenProp !== undefined) setCalendarOpen(calendarOpenProp)
|
|
95
|
+
}
|
|
96
96
|
|
|
97
97
|
// Refs
|
|
98
98
|
const inputRef = useRef<HTMLInputElement>(null)
|
|
99
|
-
const
|
|
99
|
+
const inputToRef = useRef<HTMLInputElement>(null)
|
|
100
100
|
const changeInputRef = useRef<HTMLInputElement>(null)
|
|
101
101
|
const btnRef = useRef<HTMLButtonElement>(null)
|
|
102
102
|
const wrapperRef = useRef<HTMLDivElement>(null)
|
|
@@ -383,7 +383,7 @@ export function useDatepickerState(props: IPktDatepicker, ref: React.ForwardedRe
|
|
|
383
383
|
e.nativeEvent,
|
|
384
384
|
() => toggleCalendar(),
|
|
385
385
|
() => submitFormOrBlur(inputRef.current),
|
|
386
|
-
() =>
|
|
386
|
+
() => inputToRef.current?.focus(),
|
|
387
387
|
() => inputRef.current?.blur(),
|
|
388
388
|
)
|
|
389
389
|
},
|
|
@@ -395,9 +395,9 @@ export function useDatepickerState(props: IPktDatepicker, ref: React.ForwardedRe
|
|
|
395
395
|
handleDatepickerKeydown(
|
|
396
396
|
e.nativeEvent,
|
|
397
397
|
() => toggleCalendar(),
|
|
398
|
-
() => submitFormOrBlur(
|
|
398
|
+
() => submitFormOrBlur(inputToRef.current),
|
|
399
399
|
undefined,
|
|
400
|
-
() =>
|
|
400
|
+
() => inputToRef.current?.blur(),
|
|
401
401
|
)
|
|
402
402
|
},
|
|
403
403
|
[toggleCalendar, submitFormOrBlur],
|
|
@@ -421,9 +421,11 @@ export function useDatepickerState(props: IPktDatepicker, ref: React.ForwardedRe
|
|
|
421
421
|
useEffect(() => {
|
|
422
422
|
const form = wrapperRef.current?.closest('form')
|
|
423
423
|
if (!form) return
|
|
424
|
+
let resetTimeout: number | undefined
|
|
424
425
|
|
|
425
426
|
const handleReset = () => {
|
|
426
|
-
window.
|
|
427
|
+
window.clearTimeout(resetTimeout)
|
|
428
|
+
resetTimeout = window.setTimeout(() => {
|
|
427
429
|
setInternalValue(initialValuesRef.current)
|
|
428
430
|
setCalendarOpen(false)
|
|
429
431
|
if (changeInputRef.current) {
|
|
@@ -433,7 +435,10 @@ export function useDatepickerState(props: IPktDatepicker, ref: React.ForwardedRe
|
|
|
433
435
|
}
|
|
434
436
|
|
|
435
437
|
form.addEventListener('reset', handleReset)
|
|
436
|
-
return () =>
|
|
438
|
+
return () => {
|
|
439
|
+
form.removeEventListener('reset', handleReset)
|
|
440
|
+
window.clearTimeout(resetTimeout)
|
|
441
|
+
}
|
|
437
442
|
}, [])
|
|
438
443
|
|
|
439
444
|
// Expose value getter/setter on ref for RHF register() compatibility.
|
|
@@ -515,7 +520,7 @@ export function useDatepickerState(props: IPktDatepicker, ref: React.ForwardedRe
|
|
|
515
520
|
isInputDisabled,
|
|
516
521
|
hasCounter,
|
|
517
522
|
inputRef,
|
|
518
|
-
|
|
523
|
+
inputToRef,
|
|
519
524
|
changeInputRef,
|
|
520
525
|
btnRef,
|
|
521
526
|
wrapperRef,
|
|
@@ -12,7 +12,6 @@ import {
|
|
|
12
12
|
useState,
|
|
13
13
|
} from 'react'
|
|
14
14
|
|
|
15
|
-
|
|
16
15
|
import { PktIcon } from '../icon/Icon'
|
|
17
16
|
import { FileItem, TFileItemList, TUploadStrategy } from './types'
|
|
18
17
|
import { usePktStrings } from '../../hooks/usePktStrings'
|
|
@@ -27,7 +26,8 @@ import type { IPktStringsProps } from 'shared-strings'
|
|
|
27
26
|
* - optionally rendering hidden inputs with file IDs when `uploadStrategy="custom"`
|
|
28
27
|
*/
|
|
29
28
|
interface IDropZoneProps
|
|
30
|
-
extends
|
|
29
|
+
extends
|
|
30
|
+
Omit<InputHTMLAttributes<HTMLInputElement>, 'checked' | 'size' | 'type' | 'value' | 'onChange' | 'width'>,
|
|
31
31
|
IPktStringsProps<'fileupload'> {
|
|
32
32
|
/** Called with `FileItem`s created from dropped/selected files. */
|
|
33
33
|
onFilesAdded?: (files: TFileItemList) => void
|
|
@@ -211,18 +211,18 @@ export const DropZone = forwardRef<HTMLInputElement, IDropZoneProps>(
|
|
|
211
211
|
accept={resolvedAccept}
|
|
212
212
|
disabled={disabled}
|
|
213
213
|
name={(uploadStrategy === 'form' && name) || undefined} // Ikke sett name hvis uploadStrategy er 'custom' - ignorerer ved POST
|
|
214
|
-
aria-label={
|
|
214
|
+
aria-label={
|
|
215
|
+
hasVisibleLabel
|
|
216
|
+
? undefined
|
|
217
|
+
: multiple
|
|
218
|
+
? s.fileupload.openFileDialogLabelMultiple
|
|
219
|
+
: s.fileupload.openFileDialogLabelSingle
|
|
220
|
+
}
|
|
215
221
|
/>
|
|
216
222
|
{uploadStrategy === 'custom' && (
|
|
217
223
|
<>
|
|
218
224
|
{value?.map((fileItem) => (
|
|
219
|
-
<input
|
|
220
|
-
key={fileItem.fileId}
|
|
221
|
-
type="hidden"
|
|
222
|
-
name={name}
|
|
223
|
-
form={inputProps.form}
|
|
224
|
-
value={fileItem.fileId}
|
|
225
|
-
/>
|
|
225
|
+
<input key={fileItem.fileId} type="hidden" name={name} form={inputProps.form} value={fileItem.fileId} />
|
|
226
226
|
))}
|
|
227
227
|
</>
|
|
228
228
|
)}
|
|
@@ -224,10 +224,7 @@ export const PktFileUpload: FC<IPktFileUpload> = forwardRef<HTMLInputElement, IP
|
|
|
224
224
|
uploadStrategy,
|
|
225
225
|
required,
|
|
226
226
|
currentFileCount: value.length,
|
|
227
|
-
onMissingFiles: useCallback(
|
|
228
|
-
() => setValidationError(fileuploadStrings.requiredMissing),
|
|
229
|
-
[fileuploadStrings],
|
|
230
|
-
),
|
|
227
|
+
onMissingFiles: useCallback(() => setValidationError(fileuploadStrings.requiredMissing), [fileuploadStrings]),
|
|
231
228
|
})
|
|
232
229
|
|
|
233
230
|
const itemRenderer = typeof itemRendererProp === 'string' ? ItemRenderers[itemRendererProp] : itemRendererProp
|
|
@@ -310,23 +307,19 @@ export const PktFileUpload: FC<IPktFileUpload> = forwardRef<HTMLInputElement, IP
|
|
|
310
307
|
|
|
311
308
|
const queueItemOperations = useMemo<TQueueItemOperation[]>(() => {
|
|
312
309
|
const operations: TQueueItemOperation[] = []
|
|
313
|
-
if (effectiveRenameEnabled) operations.push(renameFileOperation)
|
|
314
|
-
if (effectiveCommentsEnabled) operations.push(addCommentOperation)
|
|
310
|
+
if (effectiveRenameEnabled) operations.push(renameFileOperation(fileuploadStrings))
|
|
311
|
+
if (effectiveCommentsEnabled) operations.push(addCommentOperation(fileuploadStrings))
|
|
315
312
|
operations.push(...extraOperations)
|
|
316
|
-
operations.push(removeFileOperation(onFileRemoved))
|
|
313
|
+
operations.push(removeFileOperation(onFileRemoved, fileuploadStrings))
|
|
317
314
|
return operations
|
|
318
|
-
}, [effectiveRenameEnabled, effectiveCommentsEnabled, extraOperations, onFileRemoved])
|
|
315
|
+
}, [effectiveRenameEnabled, effectiveCommentsEnabled, extraOperations, onFileRemoved, fileuploadStrings])
|
|
319
316
|
|
|
320
317
|
const { totalCount, uploadedCount, failedCount } = useMemo(
|
|
321
318
|
() => countFileTransferStates(value, transfers),
|
|
322
319
|
[value, transfers],
|
|
323
320
|
)
|
|
324
321
|
|
|
325
|
-
const totalLabel = plural(
|
|
326
|
-
totalCount,
|
|
327
|
-
fileuploadStrings.fileLabelOne,
|
|
328
|
-
fileuploadStrings.fileLabelOther,
|
|
329
|
-
)
|
|
322
|
+
const totalLabel = plural(totalCount, fileuploadStrings.fileLabelOne, fileuploadStrings.fileLabelOther)
|
|
330
323
|
|
|
331
324
|
const fileUploadContent = (
|
|
332
325
|
<div
|
|
@@ -338,7 +331,9 @@ export const PktFileUpload: FC<IPktFileUpload> = forwardRef<HTMLInputElement, IP
|
|
|
338
331
|
aria-disabled={disabled}
|
|
339
332
|
{...(disabled ? { inert: true } : {})}
|
|
340
333
|
>
|
|
341
|
-
<PktFileUploadContext.Provider
|
|
334
|
+
<PktFileUploadContext.Provider
|
|
335
|
+
value={{ name: props.name || '', multiple: !!multiple, id, strings: fileuploadStrings }}
|
|
336
|
+
>
|
|
342
337
|
<div className="pkt-sr-only" aria-live="polite" aria-atomic="true">
|
|
343
338
|
{addedAnnouncement ||
|
|
344
339
|
(totalCount > 0 && uploadedCount > 0
|