@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
|
@@ -135,11 +135,11 @@ export const PktHeaderService = forwardRef(
|
|
|
135
135
|
|
|
136
136
|
const [openMenu, setOpenMenu] = useState<'none' | 'slot' | 'search' | 'user'>(openedMenu || 'none')
|
|
137
137
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
138
|
+
const [prevOpenedMenu, setPrevOpenedMenu] = useState(openedMenu)
|
|
139
|
+
if (openedMenu !== prevOpenedMenu) {
|
|
140
|
+
setPrevOpenedMenu(openedMenu)
|
|
141
|
+
if (openedMenu !== undefined) setOpenMenu(openedMenu)
|
|
142
|
+
}
|
|
143
143
|
|
|
144
144
|
const [alignSlotRight, setAlignSlotRight] = useState(false)
|
|
145
145
|
const [alignSearchRight, setAlignSearchRight] = useState(false)
|
|
@@ -20,9 +20,11 @@ import {
|
|
|
20
20
|
type THeaderFooterApi,
|
|
21
21
|
type THeaderMenuLocale,
|
|
22
22
|
} from 'shared-types'
|
|
23
|
+
import type { IPktStringsProps } from 'shared-strings'
|
|
23
24
|
import { deriveSocialIcon, fetchHeaderFooterData, mapOdsIcon, selectLocaleData } from 'shared-utils/header-footer'
|
|
24
25
|
|
|
25
26
|
import { useElementWidth } from '../../hooks/useElementWidth'
|
|
27
|
+
import { usePktStrings } from '../../hooks/usePktStrings'
|
|
26
28
|
import { PktAccordion } from '../accordion/Accordion'
|
|
27
29
|
import { PktAccordionItem } from '../accordion/AccordionItem'
|
|
28
30
|
import { PktIcon } from '../icon/Icon'
|
|
@@ -36,7 +38,8 @@ export type {
|
|
|
36
38
|
IHeaderMenuServices,
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
export interface IPktHeaderMenu
|
|
41
|
+
export interface IPktHeaderMenu
|
|
42
|
+
extends Omit<HTMLAttributes<HTMLElement>, 'onError'>, ISharedPktHeaderMenu, IPktStringsProps<'header'> {
|
|
40
43
|
/** Forwarded to the host element. */
|
|
41
44
|
ref?: Ref<HTMLElement>
|
|
42
45
|
/** Fired when the payload has been fetched (or `data` was supplied). */
|
|
@@ -69,8 +72,10 @@ export const PktHeaderMenu = ({
|
|
|
69
72
|
onDataLoaded,
|
|
70
73
|
onDataError,
|
|
71
74
|
ref,
|
|
75
|
+
strings,
|
|
72
76
|
...rest
|
|
73
77
|
}: IPktHeaderMenu) => {
|
|
78
|
+
const headerStrings = usePktStrings(['header'], strings).header
|
|
74
79
|
const [fetchState, setFetchState] = useState<LoadState>('loading')
|
|
75
80
|
const [fetchedData, setFetchedData] = useState<THeaderFooterApi | undefined>(undefined)
|
|
76
81
|
const loadState: LoadState = data ? 'ready' : fetchState
|
|
@@ -129,7 +134,7 @@ export const PktHeaderMenu = ({
|
|
|
129
134
|
if (loadState === 'loading') {
|
|
130
135
|
return (
|
|
131
136
|
<nav {...rest} ref={setRootRef} className={hostClasses} aria-busy="true">
|
|
132
|
-
<p className="pkt-header-menu__loading">
|
|
137
|
+
<p className="pkt-header-menu__loading">{headerStrings.menuLoading}</p>
|
|
133
138
|
</nav>
|
|
134
139
|
)
|
|
135
140
|
}
|
|
@@ -137,7 +142,7 @@ export const PktHeaderMenu = ({
|
|
|
137
142
|
if (loadState === 'error' || !localeData) {
|
|
138
143
|
return (
|
|
139
144
|
<nav {...rest} ref={setRootRef} className={hostClasses} aria-hidden={!open}>
|
|
140
|
-
<p className="pkt-header-menu__error">
|
|
145
|
+
<p className="pkt-header-menu__error">{headerStrings.menuLoadError}</p>
|
|
141
146
|
</nav>
|
|
142
147
|
)
|
|
143
148
|
}
|
|
@@ -9,6 +9,7 @@ import { usePktStrings } from '../../hooks/usePktStrings'
|
|
|
9
9
|
import { Representing, User, UserMenuItem } from '../header/types'
|
|
10
10
|
import { PktIcon } from '../icon/Icon'
|
|
11
11
|
import { PktLink } from '../link/Link'
|
|
12
|
+
import { withStableKeys } from '../../utils/stableKeys'
|
|
12
13
|
|
|
13
14
|
// Internal type for rendering links/buttons (supports both href and onClick)
|
|
14
15
|
interface IInternalMenuItemBase {
|
|
@@ -74,9 +75,9 @@ const LinkOrButtonComponent = ({ item, className }: { item: TInternalMenuItem; c
|
|
|
74
75
|
const LinkSection = ({ links }: { links: TInternalMenuItem[] }) => {
|
|
75
76
|
return (
|
|
76
77
|
<ul className="pkt-user-menu__sublist">
|
|
77
|
-
{links.map((item,
|
|
78
|
+
{withStableKeys(links, (item) => `${'href' in item ? item.href : ''}|${item.title}`).map(({ item, key }) => {
|
|
78
79
|
return (
|
|
79
|
-
<li key={
|
|
80
|
+
<li key={key} className="pkt-user-menu__subitem">
|
|
80
81
|
<LinkOrButtonComponent item={item} />
|
|
81
82
|
</li>
|
|
82
83
|
)
|
|
@@ -98,38 +98,31 @@ export const PktInputWrapper = forwardRef(
|
|
|
98
98
|
const tagClasses = ['pkt-tag', 'pkt-tag--small', 'pkt-tag--thin-text'].join(' ')
|
|
99
99
|
|
|
100
100
|
const counterExceeded = isCounterExceeded(counterCurrent, counterMaxLength)
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
{
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
) : null
|
|
101
|
+
const counterContent = showCounter ? (
|
|
102
|
+
<>
|
|
103
|
+
<div className="pkt-input__counter" aria-hidden="true">
|
|
104
|
+
{counterCurrent}
|
|
105
|
+
{counterMaxLength ? `/${counterMaxLength}` : ''}
|
|
106
|
+
</div>
|
|
107
|
+
<div className="pkt-sr-only" id={counterStatusId(forId)} aria-live="polite">
|
|
108
|
+
{counterExceeded
|
|
109
|
+
? formatString(s.validation.counterExceeded, {
|
|
110
|
+
current: counterCurrent,
|
|
111
|
+
max: counterMaxLength ?? 0,
|
|
112
|
+
})
|
|
113
|
+
: ''}
|
|
114
|
+
</div>
|
|
115
|
+
</>
|
|
116
|
+
) : null
|
|
118
117
|
|
|
119
118
|
const renderTags = () => {
|
|
120
119
|
return (
|
|
121
120
|
<>
|
|
122
121
|
{tagText && <span className={tagClasses + ` pkt-tag--gray`}>{tagText}</span>}
|
|
123
122
|
{optionalTag && (
|
|
124
|
-
<span className={tagClasses + ` pkt-tag--blue-light`}>
|
|
125
|
-
{optionalText ?? s.forms.optional}
|
|
126
|
-
</span>
|
|
127
|
-
)}
|
|
128
|
-
{requiredTag && (
|
|
129
|
-
<span className={tagClasses + ` pkt-tag--beige`}>
|
|
130
|
-
{requiredText ?? s.forms.required}
|
|
131
|
-
</span>
|
|
123
|
+
<span className={tagClasses + ` pkt-tag--blue-light`}>{optionalText ?? s.forms.optional}</span>
|
|
132
124
|
)}
|
|
125
|
+
{requiredTag && <span className={tagClasses + ` pkt-tag--beige`}>{requiredText ?? s.forms.required}</span>}
|
|
133
126
|
</>
|
|
134
127
|
)
|
|
135
128
|
}
|
|
@@ -183,9 +176,9 @@ export const PktInputWrapper = forwardRef(
|
|
|
183
176
|
<>
|
|
184
177
|
{renderLabel()}
|
|
185
178
|
{renderHelptext()}
|
|
186
|
-
{counterTop &&
|
|
179
|
+
{counterTop && counterContent}
|
|
187
180
|
{children}
|
|
188
|
-
{counterBottom &&
|
|
181
|
+
{counterBottom && counterContent}
|
|
189
182
|
{hasError && errorMessage && (
|
|
190
183
|
<div className="pkt-inputwrapper__alert-wrapper">
|
|
191
184
|
<PktAlert skin="error" size={errorAlertSize} aria-live="assertive" role="alert" id={`${forId}-error`}>
|
|
@@ -12,8 +12,7 @@ declare global {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
if (typeof window !== 'undefined') {
|
|
15
|
-
window.pktAnimationPath =
|
|
16
|
-
window.pktAnimationPath || 'https://punkt-cdn.oslo.kommune.no/latest/animations/'
|
|
15
|
+
window.pktAnimationPath = window.pktAnimationPath || 'https://punkt-cdn.oslo.kommune.no/latest/animations/'
|
|
17
16
|
}
|
|
18
17
|
|
|
19
18
|
export type TPktLoaderVariant = 'blue' | 'rainbow' | 'shapes'
|
|
@@ -78,17 +77,21 @@ export const PktLoader = forwardRef<HTMLDivElement, IPktLoader>(function PktLoad
|
|
|
78
77
|
},
|
|
79
78
|
ref,
|
|
80
79
|
) {
|
|
81
|
-
const [
|
|
80
|
+
const [delayElapsed, setDelayElapsed] = useState(false)
|
|
81
|
+
const [prevDelay, setPrevDelay] = useState(delay)
|
|
82
|
+
if (delay !== prevDelay) {
|
|
83
|
+
setPrevDelay(delay)
|
|
84
|
+
setDelayElapsed(false)
|
|
85
|
+
}
|
|
82
86
|
|
|
83
87
|
useEffect(() => {
|
|
84
|
-
if (delay
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return () => clearTimeout(handle)
|
|
88
|
-
}
|
|
89
|
-
setShouldDisplayLoader(true)
|
|
88
|
+
if (delay <= 0) return
|
|
89
|
+
const handle = setTimeout(() => setDelayElapsed(true), delay)
|
|
90
|
+
return () => clearTimeout(handle)
|
|
90
91
|
}, [delay])
|
|
91
92
|
|
|
93
|
+
const shouldDisplayLoader = delay <= 0 || delayElapsed
|
|
94
|
+
|
|
92
95
|
const classes = classNames(
|
|
93
96
|
'pkt-loader',
|
|
94
97
|
inline ? 'pkt-loader--inline' : 'pkt-loader--box',
|
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
import classNames from 'classnames'
|
|
4
4
|
import { createRef, FC, HTMLAttributes, ReactNode, useCallback, useState } from 'react'
|
|
5
|
+
import type { IPktStringsProps } from 'shared-strings'
|
|
5
6
|
import type { TMessageboxSkin } from 'shared-types'
|
|
6
7
|
|
|
8
|
+
import { usePktStrings } from '../../hooks/usePktStrings'
|
|
7
9
|
import { PktIcon } from '../icon/Icon'
|
|
8
10
|
|
|
9
11
|
export type { TMessageboxSkin }
|
|
10
12
|
|
|
11
|
-
export interface IPktMessagebox extends Omit<HTMLAttributes<HTMLDivElement>, 'title'> {
|
|
13
|
+
export interface IPktMessagebox extends Omit<HTMLAttributes<HTMLDivElement>, 'title'>, IPktStringsProps<'generic'> {
|
|
12
14
|
skin?: TMessageboxSkin
|
|
13
15
|
title?: ReactNode
|
|
14
16
|
compact?: boolean
|
|
@@ -24,8 +26,10 @@ export const PktMessagebox: FC<IPktMessagebox> = ({
|
|
|
24
26
|
compact,
|
|
25
27
|
closable,
|
|
26
28
|
onClose,
|
|
29
|
+
strings,
|
|
27
30
|
...props
|
|
28
31
|
}: IPktMessagebox) => {
|
|
32
|
+
const s = usePktStrings(['generic'], strings)
|
|
29
33
|
const [closed, setClosed] = useState(false)
|
|
30
34
|
|
|
31
35
|
const classes = {
|
|
@@ -52,7 +56,7 @@ export const PktMessagebox: FC<IPktMessagebox> = ({
|
|
|
52
56
|
<button
|
|
53
57
|
onClick={close}
|
|
54
58
|
className={'pkt-btn pkt-btn--tertiary pkt-btn--small pkt-btn--icon-only'}
|
|
55
|
-
aria-label={
|
|
59
|
+
aria-label={s.generic.close}
|
|
56
60
|
>
|
|
57
61
|
<PktIcon name="close" className="pkt-link__icon" />
|
|
58
62
|
</button>
|
|
@@ -13,12 +13,14 @@ import {
|
|
|
13
13
|
useRef,
|
|
14
14
|
} from 'react'
|
|
15
15
|
|
|
16
|
+
import type { IPktStringsProps } from 'shared-strings'
|
|
16
17
|
import { createScrollLockToken, setScrollLock } from 'shared-utils/scroll-lock'
|
|
17
18
|
|
|
19
|
+
import { usePktStrings } from '../../hooks/usePktStrings'
|
|
18
20
|
import { PktButton } from '../button/Button'
|
|
19
21
|
|
|
20
22
|
export interface IPktModal
|
|
21
|
-
extends Omit<HTMLAttributes<HTMLDialogElement>, 'onClose' | 'open'> {
|
|
23
|
+
extends Omit<HTMLAttributes<HTMLDialogElement>, 'onClose' | 'open'>, IPktStringsProps<'generic'> {
|
|
22
24
|
open?: boolean
|
|
23
25
|
headingText?: string
|
|
24
26
|
hideCloseButton?: boolean
|
|
@@ -50,12 +52,14 @@ export const PktModal = forwardRef(
|
|
|
50
52
|
drawerPosition = 'right',
|
|
51
53
|
transparentBackdrop = false,
|
|
52
54
|
onClose,
|
|
55
|
+
strings,
|
|
53
56
|
...props
|
|
54
57
|
}: IPktModal,
|
|
55
58
|
ref: ForwardedRef<HTMLDialogElement>,
|
|
56
59
|
) => {
|
|
60
|
+
const s = usePktStrings(['generic'], strings)
|
|
57
61
|
const internalRef = useRef<HTMLDialogElement | null>(null)
|
|
58
|
-
const
|
|
62
|
+
const scrollLockTokenRef = useRef(createScrollLockToken())
|
|
59
63
|
|
|
60
64
|
// Merge consumer ref + internal ref so we always have a working DOM handle
|
|
61
65
|
// for our own logic, regardless of whether the consumer passes a callback ref
|
|
@@ -72,7 +76,7 @@ export const PktModal = forwardRef(
|
|
|
72
76
|
const close = useCallback(() => {
|
|
73
77
|
if (!internalRef.current?.open) return
|
|
74
78
|
internalRef.current.close()
|
|
75
|
-
setScrollLock(
|
|
79
|
+
setScrollLock(scrollLockTokenRef.current, false)
|
|
76
80
|
onClose?.()
|
|
77
81
|
}, [onClose])
|
|
78
82
|
|
|
@@ -82,18 +86,18 @@ export const PktModal = forwardRef(
|
|
|
82
86
|
if (!internalRef.current?.open) {
|
|
83
87
|
internalRef.current?.showModal()
|
|
84
88
|
}
|
|
85
|
-
setScrollLock(
|
|
89
|
+
setScrollLock(scrollLockTokenRef.current, true)
|
|
86
90
|
} else {
|
|
87
91
|
if (internalRef.current?.open) {
|
|
88
92
|
internalRef.current.close()
|
|
89
93
|
}
|
|
90
|
-
setScrollLock(
|
|
94
|
+
setScrollLock(scrollLockTokenRef.current, false)
|
|
91
95
|
}
|
|
92
96
|
}, [open])
|
|
93
97
|
|
|
94
98
|
// Rydd opp scroll-lock hvis modalen avmonteres mens den er åpen
|
|
95
99
|
useEffect(() => {
|
|
96
|
-
const token =
|
|
100
|
+
const token = scrollLockTokenRef.current
|
|
97
101
|
return () => {
|
|
98
102
|
setScrollLock(token, false)
|
|
99
103
|
}
|
|
@@ -109,7 +113,7 @@ export const PktModal = forwardRef(
|
|
|
109
113
|
)
|
|
110
114
|
|
|
111
115
|
const handleNativeClose = useCallback(() => {
|
|
112
|
-
setScrollLock(
|
|
116
|
+
setScrollLock(scrollLockTokenRef.current, false)
|
|
113
117
|
onClose?.()
|
|
114
118
|
}, [onClose])
|
|
115
119
|
|
|
@@ -158,13 +162,13 @@ export const PktModal = forwardRef(
|
|
|
158
162
|
>
|
|
159
163
|
<PktButton
|
|
160
164
|
onClick={close}
|
|
161
|
-
aria-label=
|
|
165
|
+
aria-label={s.generic.close}
|
|
162
166
|
iconName="close"
|
|
163
167
|
variant="icon-only"
|
|
164
168
|
size="medium"
|
|
165
169
|
skin={isCloseButtonSkinDefault ? 'tertiary' : 'primary'}
|
|
166
170
|
>
|
|
167
|
-
|
|
171
|
+
{s.generic.close}
|
|
168
172
|
</PktButton>
|
|
169
173
|
</div>
|
|
170
174
|
) : (
|
|
@@ -9,6 +9,7 @@ import { useFocusModality } from '../../hooks/useFocusModality'
|
|
|
9
9
|
import { PktButton } from '../button/Button'
|
|
10
10
|
import { usePktStrings } from '../../hooks/usePktStrings'
|
|
11
11
|
import type { IPktStringsProps } from 'shared-strings'
|
|
12
|
+
import { textKey, withStableKeys } from '../../utils/stableKeys'
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* React suggestion item: portable fields from {@link IPktSearchInputSuggestion}
|
|
@@ -106,7 +107,7 @@ export const PktSearchInput = forwardRef<HTMLInputElement, ISearchInput | ISearc
|
|
|
106
107
|
...focusModality,
|
|
107
108
|
}
|
|
108
109
|
|
|
109
|
-
const
|
|
110
|
+
const renderWrapper = (children: ReactNode) =>
|
|
110
111
|
action ? (
|
|
111
112
|
<form {...wrapperProps} action={action} method={method}>
|
|
112
113
|
{children}
|
|
@@ -128,7 +129,7 @@ export const PktSearchInput = forwardRef<HTMLInputElement, ISearchInput | ISearc
|
|
|
128
129
|
const fieldClass =
|
|
129
130
|
appearance === 'local' ? `pkt-input__container pkt-input__container--${inputSize}` : 'pkt-searchinput__field'
|
|
130
131
|
|
|
131
|
-
return
|
|
132
|
+
return renderWrapper(
|
|
132
133
|
<>
|
|
133
134
|
{label && (
|
|
134
135
|
<label htmlFor={label ? id : undefined} className={label ? 'pkt-inputwrapper__label' : ''}>
|
|
@@ -184,9 +185,12 @@ export const PktSearchInput = forwardRef<HTMLInputElement, ISearchInput | ISearc
|
|
|
184
185
|
|
|
185
186
|
{suggestions && (
|
|
186
187
|
<ul id={`${id}-suggestions`} className="pkt-searchinput__suggestions" aria-live="assertive">
|
|
187
|
-
{
|
|
188
|
+
{withStableKeys(
|
|
189
|
+
suggestions,
|
|
190
|
+
(suggestion) => `${suggestion.href ?? ''}|${textKey(suggestion.title)}|${textKey(suggestion.text)}`,
|
|
191
|
+
).map(({ item: suggestion, key }, index) =>
|
|
188
192
|
suggestion.href ? (
|
|
189
|
-
<li key={
|
|
193
|
+
<li key={key}>
|
|
190
194
|
<a
|
|
191
195
|
href={suggestion.href}
|
|
192
196
|
className={suggestionClassName(suggestion)}
|
|
@@ -196,11 +200,11 @@ export const PktSearchInput = forwardRef<HTMLInputElement, ISearchInput | ISearc
|
|
|
196
200
|
</a>
|
|
197
201
|
</li>
|
|
198
202
|
) : suggestion.nonInteractive ? (
|
|
199
|
-
<li key={
|
|
203
|
+
<li key={key}>
|
|
200
204
|
<div className="pkt-searchinput__suggestion">{suggestionContent(suggestion)}</div>
|
|
201
205
|
</li>
|
|
202
206
|
) : (
|
|
203
|
-
<li key={
|
|
207
|
+
<li key={key}>
|
|
204
208
|
<button
|
|
205
209
|
type="button"
|
|
206
210
|
className={suggestionClassName(suggestion)}
|
|
@@ -402,7 +402,7 @@ describe('PktTabItem children format', () => {
|
|
|
402
402
|
const { getByText } = render(
|
|
403
403
|
<PktTabs onTabSelected={handleTabSelected}>
|
|
404
404
|
{items.map((item, i) => (
|
|
405
|
-
<PktTabItem key={
|
|
405
|
+
<PktTabItem key={item} index={i}>
|
|
406
406
|
{item}
|
|
407
407
|
</PktTabItem>
|
|
408
408
|
))}
|
|
@@ -17,15 +17,12 @@ import type { TTagSkin } from 'shared-types'
|
|
|
17
17
|
|
|
18
18
|
import { PktIcon } from '../icon/Icon'
|
|
19
19
|
import { PktTabItem } from './TabItem'
|
|
20
|
+
import { withStableKeys } from '../../utils/stableKeys'
|
|
20
21
|
|
|
21
22
|
// Context for passing tab navigation logic to children
|
|
22
23
|
interface ITabsContext {
|
|
23
24
|
arrowNav: boolean
|
|
24
|
-
registerTabRef: (
|
|
25
|
-
index: number,
|
|
26
|
-
el: HTMLAnchorElement | HTMLButtonElement | null,
|
|
27
|
-
disabled?: boolean,
|
|
28
|
-
) => void
|
|
25
|
+
registerTabRef: (index: number, el: HTMLAnchorElement | HTMLButtonElement | null, disabled?: boolean) => void
|
|
29
26
|
handleKeyPress: (index: number, event: KeyboardEvent<HTMLAnchorElement | HTMLButtonElement>) => void
|
|
30
27
|
selectTab: (index: number) => void
|
|
31
28
|
}
|
|
@@ -77,8 +74,8 @@ export const PktTabs = forwardRef(
|
|
|
77
74
|
}: IPktTabs,
|
|
78
75
|
ref: Ref<HTMLDivElement>,
|
|
79
76
|
): JSX.Element => {
|
|
80
|
-
const
|
|
81
|
-
const
|
|
77
|
+
const tabElementsRef = useRef<Array<HTMLAnchorElement | HTMLButtonElement | null>>([])
|
|
78
|
+
const disabledMapRef = useRef<Record<number, boolean>>({})
|
|
82
79
|
|
|
83
80
|
const useArrowNav = arrowNav && !disableArrowNav
|
|
84
81
|
|
|
@@ -89,16 +86,16 @@ export const PktTabs = forwardRef(
|
|
|
89
86
|
const hasSeparator = !!(separatorIconName || separatorIconPath)
|
|
90
87
|
|
|
91
88
|
useEffect(() => {
|
|
92
|
-
|
|
93
|
-
Object.keys(
|
|
89
|
+
tabElementsRef.current = tabElementsRef.current.slice(0, tabCount)
|
|
90
|
+
Object.keys(disabledMapRef.current).forEach((key) => {
|
|
94
91
|
const index = Number(key)
|
|
95
|
-
if (index >= tabCount) delete
|
|
92
|
+
if (index >= tabCount) delete disabledMapRef.current[index]
|
|
96
93
|
})
|
|
97
94
|
}, [tabCount])
|
|
98
95
|
|
|
99
96
|
const isTabDisabled = (index: number): boolean => {
|
|
100
97
|
if (tabs) return !!tabs[index]?.disabled
|
|
101
|
-
return !!
|
|
98
|
+
return !!disabledMapRef.current[index]
|
|
102
99
|
}
|
|
103
100
|
|
|
104
101
|
const selectTab = (index: number): void => {
|
|
@@ -127,13 +124,13 @@ export const PktTabs = forwardRef(
|
|
|
127
124
|
if (event.key === 'ArrowLeft') {
|
|
128
125
|
event.preventDefault()
|
|
129
126
|
const previousEnabled = findEnabledIndex(index, -1)
|
|
130
|
-
if (previousEnabled !== null)
|
|
127
|
+
if (previousEnabled !== null) tabElementsRef.current[previousEnabled]?.focus()
|
|
131
128
|
}
|
|
132
129
|
|
|
133
130
|
if (event.key === 'ArrowRight') {
|
|
134
131
|
event.preventDefault()
|
|
135
132
|
const nextEnabled = findEnabledIndex(index, 1)
|
|
136
|
-
if (nextEnabled !== null)
|
|
133
|
+
if (nextEnabled !== null) tabElementsRef.current[nextEnabled]?.focus()
|
|
137
134
|
}
|
|
138
135
|
|
|
139
136
|
if (event.key === 'Enter' || event.key === ' ' || event.key === 'Spacebar' || event.key === 'ArrowDown') {
|
|
@@ -144,13 +141,9 @@ export const PktTabs = forwardRef(
|
|
|
144
141
|
}
|
|
145
142
|
}
|
|
146
143
|
|
|
147
|
-
const registerTabRef = (
|
|
148
|
-
index
|
|
149
|
-
|
|
150
|
-
disabled = false,
|
|
151
|
-
) => {
|
|
152
|
-
tabRefs.current[index] = el
|
|
153
|
-
disabledMap.current[index] = disabled
|
|
144
|
+
const registerTabRef = (index: number, el: HTMLAnchorElement | HTMLButtonElement | null, disabled = false) => {
|
|
145
|
+
tabElementsRef.current[index] = el
|
|
146
|
+
disabledMapRef.current[index] = disabled
|
|
154
147
|
}
|
|
155
148
|
|
|
156
149
|
const renderSeparator = (key: string) => (
|
|
@@ -170,21 +163,23 @@ export const PktTabs = forwardRef(
|
|
|
170
163
|
)
|
|
171
164
|
}
|
|
172
165
|
|
|
173
|
-
const tabItems = tabs
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
166
|
+
const tabItems = withStableKeys(tabs ?? [], (tab) => `${tab.href ?? ''}|${tab.text}`).map(
|
|
167
|
+
({ item: tab, key }, index) => (
|
|
168
|
+
<PktTabItem
|
|
169
|
+
key={key}
|
|
170
|
+
active={tab.disabled ? false : tab.active}
|
|
171
|
+
disabled={tab.disabled}
|
|
172
|
+
href={tab.href}
|
|
173
|
+
icon={tab.icon}
|
|
174
|
+
controls={tab.controls}
|
|
175
|
+
tag={tab.tag?.text}
|
|
176
|
+
tagSkin={tab.tag?.skin}
|
|
177
|
+
index={index}
|
|
178
|
+
>
|
|
179
|
+
{tab.text}
|
|
180
|
+
</PktTabItem>
|
|
181
|
+
),
|
|
182
|
+
)
|
|
188
183
|
|
|
189
184
|
const tabsClasses = ['pkt-tabs', hasSeparator && 'pkt-tabs--with-separator'].filter(Boolean).join(' ')
|
|
190
185
|
const renderedTabs = withSeparators((hasChildren ? childItems : tabItems || []) as ReactNode[])
|
|
@@ -63,19 +63,22 @@ export const PktTag = forwardRef<HTMLSpanElement | HTMLButtonElement, ButtonPktT
|
|
|
63
63
|
}: ButtonPktTagProps | SpanPktTagProps,
|
|
64
64
|
forwardedRef: ForwardedRef<HTMLElement>,
|
|
65
65
|
) => {
|
|
66
|
-
const [isClosed,
|
|
66
|
+
const [isClosed, setIsClosed] = useState<boolean>(false)
|
|
67
67
|
|
|
68
68
|
const close = useCallback(() => {
|
|
69
|
-
|
|
69
|
+
setIsClosed(true)
|
|
70
70
|
if (onClose) {
|
|
71
71
|
onClose()
|
|
72
72
|
}
|
|
73
73
|
}, [onClose])
|
|
74
74
|
|
|
75
|
-
const spanRef: RefObject<HTMLButtonElement | HTMLSpanElement | null> = useRef<HTMLButtonElement | HTMLSpanElement>(
|
|
75
|
+
const spanRef: RefObject<HTMLButtonElement | HTMLSpanElement | null> = useRef<HTMLButtonElement | HTMLSpanElement>(
|
|
76
|
+
null,
|
|
77
|
+
)
|
|
76
78
|
const [labelText, setLabelText] = useState('')
|
|
77
79
|
|
|
78
80
|
useEffect(() => {
|
|
81
|
+
// eslint-disable-next-line @eslint-react/set-state-in-effect
|
|
79
82
|
setLabelText(spanRef.current?.textContent?.trim() ?? '')
|
|
80
83
|
}, [children])
|
|
81
84
|
|
|
@@ -83,9 +86,7 @@ export const PktTag = forwardRef<HTMLSpanElement | HTMLButtonElement, ButtonPktT
|
|
|
83
86
|
|
|
84
87
|
const ariaDescription = useMemo(() => {
|
|
85
88
|
if (closeTag && !ariaLabel)
|
|
86
|
-
return (
|
|
87
|
-
(labelText && formatString(s.tag.remove, { label: labelText })) || ariaDescriptionPropValue
|
|
88
|
-
)
|
|
89
|
+
return (labelText && formatString(s.tag.remove, { label: labelText })) || ariaDescriptionPropValue
|
|
89
90
|
}, [closeTag, ariaLabel, labelText, ariaDescriptionPropValue, s])
|
|
90
91
|
|
|
91
92
|
const classes = {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import '@testing-library/jest-dom'
|
|
2
2
|
|
|
3
|
+
import { createRef } from 'react'
|
|
4
|
+
|
|
3
5
|
import { fireEvent, render, screen } from '@testing-library/react'
|
|
4
6
|
import { axe, toHaveNoViolations } from 'jest-axe'
|
|
5
7
|
|
|
@@ -47,6 +49,13 @@ describe('PktTextarea', () => {
|
|
|
47
49
|
})
|
|
48
50
|
})
|
|
49
51
|
|
|
52
|
+
test('forwards object refs to the textarea', () => {
|
|
53
|
+
const ref = createRef<HTMLTextAreaElement>()
|
|
54
|
+
render(<PktTextarea label="Input Label" id="inputId" ref={ref} defaultValue="abc" />)
|
|
55
|
+
expect(ref.current?.tagName).toBe('TEXTAREA')
|
|
56
|
+
expect(ref.current?.value).toBe('abc')
|
|
57
|
+
})
|
|
58
|
+
|
|
50
59
|
describe('PktTextarea', () => {
|
|
51
60
|
test('toggles helptext class', async () => {
|
|
52
61
|
const { getByText, container } = render(
|
|
@@ -7,7 +7,6 @@ import {
|
|
|
7
7
|
forwardRef,
|
|
8
8
|
ReactNode,
|
|
9
9
|
TextareaHTMLAttributes,
|
|
10
|
-
useEffect,
|
|
11
10
|
useState,
|
|
12
11
|
} from 'react'
|
|
13
12
|
|
|
@@ -103,11 +102,12 @@ export const PktTextarea = forwardRef(
|
|
|
103
102
|
const labelId = `${inputId}-label` // id til label elementet og sendes inn til aria-labelledby i input (genereres i InputWrapper)
|
|
104
103
|
const labelledBy = ariaLabelledby || labelId
|
|
105
104
|
|
|
106
|
-
const [
|
|
105
|
+
const [inputLength, setInputLength] = useState(0)
|
|
106
|
+
const counterCurrent = value !== undefined ? value?.length || 0 : inputLength
|
|
107
107
|
|
|
108
108
|
const handleChange = (e: ChangeEvent<HTMLTextAreaElement>) => {
|
|
109
109
|
if (showCounter) {
|
|
110
|
-
|
|
110
|
+
setInputLength(e.currentTarget?.value?.length || 0)
|
|
111
111
|
}
|
|
112
112
|
if (onChange) {
|
|
113
113
|
onChange(e)
|
|
@@ -115,18 +115,14 @@ export const PktTextarea = forwardRef(
|
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
const initRef = (domInput: HTMLTextAreaElement) => {
|
|
118
|
-
|
|
118
|
+
setInputLength(domInput?.value?.length || 0)
|
|
119
119
|
if (typeof ref === 'function') {
|
|
120
120
|
ref(domInput)
|
|
121
|
+
} else if (ref) {
|
|
122
|
+
ref.current = domInput
|
|
121
123
|
}
|
|
122
124
|
}
|
|
123
125
|
|
|
124
|
-
useEffect(() => {
|
|
125
|
-
if (value !== undefined) {
|
|
126
|
-
setCounterCurrent(value?.length || 0)
|
|
127
|
-
}
|
|
128
|
-
}, [value])
|
|
129
|
-
|
|
130
126
|
return (
|
|
131
127
|
<div className={outerClasses}>
|
|
132
128
|
<PktInputWrapper
|
|
@@ -7,7 +7,6 @@ import {
|
|
|
7
7
|
forwardRef,
|
|
8
8
|
InputHTMLAttributes,
|
|
9
9
|
ReactNode,
|
|
10
|
-
useEffect,
|
|
11
10
|
useState,
|
|
12
11
|
} from 'react'
|
|
13
12
|
|
|
@@ -119,7 +118,8 @@ export const PktTextinput = forwardRef(
|
|
|
119
118
|
const shouldShowSearchIcon = type === 'search' && !iconNameRight && !omitSearchIcon
|
|
120
119
|
const isoValue = type === 'date' && value ? value.slice(0, 10) : value
|
|
121
120
|
|
|
122
|
-
const [
|
|
121
|
+
const [inputLength, setInputLength] = useState(0)
|
|
122
|
+
const counterCurrent = value !== undefined ? value?.length || 0 : inputLength
|
|
123
123
|
|
|
124
124
|
const id = useGeneratedId(idProp)
|
|
125
125
|
const inputId = `${id}` // id til input element og sendes inn til htmlFor i InputWrapper
|
|
@@ -128,19 +128,13 @@ export const PktTextinput = forwardRef(
|
|
|
128
128
|
|
|
129
129
|
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
|
|
130
130
|
if (showCounter) {
|
|
131
|
-
|
|
131
|
+
setInputLength(event.currentTarget?.value?.length || 0)
|
|
132
132
|
}
|
|
133
133
|
if (onChange) {
|
|
134
134
|
onChange(event)
|
|
135
135
|
}
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
useEffect(() => {
|
|
139
|
-
if (value !== undefined) {
|
|
140
|
-
setCounterCurrent(value?.length || 0)
|
|
141
|
-
}
|
|
142
|
-
}, [value])
|
|
143
|
-
|
|
144
138
|
return (
|
|
145
139
|
<div className={outerClasses}>
|
|
146
140
|
<PktInputWrapper
|