@oslokommune/punkt-react 19.3.0 → 19.3.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.
Files changed (60) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/components/card/Card.d.ts +2 -1
  3. package/dist/components/datepicker/types.d.ts +1 -1
  4. package/dist/components/fileupload/extensions/Comments.d.ts +2 -1
  5. package/dist/components/fileupload/extensions/Remove.d.ts +2 -1
  6. package/dist/components/fileupload/extensions/Rename.d.ts +2 -1
  7. package/dist/components/fileupload/hooks/index.d.ts +1 -0
  8. package/dist/components/fileupload/hooks/useImageObjectUrl.d.ts +1 -0
  9. package/dist/components/fileupload/hooks.d.ts +1 -0
  10. package/dist/components/fileupload/types.d.ts +2 -0
  11. package/dist/components/header-menu/HeaderMenu.d.ts +3 -2
  12. package/dist/components/messagebox/Messagebox.d.ts +2 -1
  13. package/dist/components/modal/Modal.d.ts +2 -1
  14. package/dist/punkt-react.cjs +1 -1
  15. package/dist/punkt-react.js +1981 -1954
  16. package/dist/shared-strings/nb.d.ts +34 -0
  17. package/package.json +2 -2
  18. package/src/components/alert/Alert.test.tsx +5 -5
  19. package/src/components/alert/Alert.tsx +2 -4
  20. package/src/components/breadcrumbs/Breadcrumbs.tsx +15 -12
  21. package/src/components/calendar/Calendar.core.test.tsx +22 -16
  22. package/src/components/calendar/CalendarNav.tsx +11 -6
  23. package/src/components/calendar/useCalendarState.ts +42 -55
  24. package/src/components/card/Card.tsx +17 -5
  25. package/src/components/combobox/ComboboxTags.tsx +3 -3
  26. package/src/components/combobox/Listbox.tsx +7 -5
  27. package/src/components/combobox/useComboboxState.ts +22 -18
  28. package/src/components/consent/Consent.tsx +4 -1
  29. package/src/components/datepicker/DatepickerInputs.tsx +3 -9
  30. package/src/components/datepicker/types.ts +1 -1
  31. package/src/components/datepicker/useDatepickerState.ts +22 -17
  32. package/src/components/fileupload/DropZone.tsx +10 -10
  33. package/src/components/fileupload/FileUpload.tsx +9 -14
  34. package/src/components/fileupload/Subcomponents.tsx +78 -84
  35. package/src/components/fileupload/extensions/Comments.tsx +56 -50
  36. package/src/components/fileupload/extensions/Remove.tsx +8 -3
  37. package/src/components/fileupload/extensions/Rename.tsx +12 -8
  38. package/src/components/fileupload/hooks/index.ts +1 -0
  39. package/src/components/fileupload/hooks/useImageObjectUrl.ts +15 -0
  40. package/src/components/fileupload/hooks/useOperationState.ts +9 -16
  41. package/src/components/fileupload/hooks.ts +1 -0
  42. package/src/components/fileupload/types.ts +7 -1
  43. package/src/components/formAttribute.test.tsx +1 -0
  44. package/src/components/header/HeaderGlobal.tsx +8 -2
  45. package/src/components/header/HeaderService.test.tsx +5 -2
  46. package/src/components/header/HeaderService.tsx +5 -5
  47. package/src/components/header-menu/HeaderMenu.tsx +8 -3
  48. package/src/components/headerUserMenu/UserMenu.tsx +3 -2
  49. package/src/components/inputwrapper/InputWrapper.tsx +20 -27
  50. package/src/components/loader/Loader.tsx +12 -9
  51. package/src/components/messagebox/Messagebox.tsx +6 -2
  52. package/src/components/modal/Modal.tsx +13 -9
  53. package/src/components/searchinput/SearchInput.tsx +10 -6
  54. package/src/components/tabs/Tabs.test.tsx +1 -1
  55. package/src/components/tabs/Tabs.tsx +30 -35
  56. package/src/components/tag/Tag.tsx +7 -6
  57. package/src/components/textarea/Textarea.test.tsx +9 -0
  58. package/src/components/textarea/Textarea.tsx +6 -10
  59. package/src/components/textinput/Textinput.tsx +3 -9
  60. package/src/components/timepicker/useTimepickerState.ts +25 -21
@@ -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 scrollLockToken = useRef(createScrollLockToken())
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(scrollLockToken.current, false)
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(scrollLockToken.current, true)
89
+ setScrollLock(scrollLockTokenRef.current, true)
86
90
  } else {
87
91
  if (internalRef.current?.open) {
88
92
  internalRef.current.close()
89
93
  }
90
- setScrollLock(scrollLockToken.current, false)
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 = scrollLockToken.current
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(scrollLockToken.current, false)
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="close"
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
- Lukk
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 WrapperElement = (children: ReactNode) =>
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 WrapperElement(
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
- {suggestions.map((suggestion, index) =>
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={`search-suggestion-${index}`}>
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={`search-suggestion-${index}`}>
203
+ <li key={key}>
200
204
  <div className="pkt-searchinput__suggestion">{suggestionContent(suggestion)}</div>
201
205
  </li>
202
206
  ) : (
203
- <li key={`search-suggestion-${index}`}>
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={i} index={i}>
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 tabRefs = useRef<Array<HTMLAnchorElement | HTMLButtonElement | null>>([])
81
- const disabledMap = useRef<Record<number, boolean>>({})
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
- tabRefs.current = tabRefs.current.slice(0, tabCount)
93
- Object.keys(disabledMap.current).forEach((key) => {
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 disabledMap.current[index]
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 !!disabledMap.current[index]
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) tabRefs.current[previousEnabled]?.focus()
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) tabRefs.current[nextEnabled]?.focus()
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: number,
149
- el: HTMLAnchorElement | HTMLButtonElement | null,
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?.map((tab, index) => (
174
- <PktTabItem
175
- key={index}
176
- active={tab.disabled ? false : tab.active}
177
- disabled={tab.disabled}
178
- href={tab.href}
179
- icon={tab.icon}
180
- controls={tab.controls}
181
- tag={tab.tag?.text}
182
- tagSkin={tab.tag?.skin}
183
- index={index}
184
- >
185
- {tab.text}
186
- </PktTabItem>
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, setClosed] = useState<boolean>(false)
66
+ const [isClosed, setIsClosed] = useState<boolean>(false)
67
67
 
68
68
  const close = useCallback(() => {
69
- setClosed(true)
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>(null)
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 [counterCurrent, setCounterCurrent] = useState(0)
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
- setCounterCurrent(e.currentTarget?.value?.length || 0)
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
- setCounterCurrent(domInput?.value?.length || 0)
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 [counterCurrent, setCounterCurrent] = useState(value?.length || 0)
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
- setCounterCurrent(event.currentTarget?.value?.length || 0)
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
@@ -154,11 +154,11 @@ export function useTimepickerState(props: IPktTimepicker, ref: React.ForwardedRe
154
154
 
155
155
  const effectiveValue = isControlled ? (value ?? '') : internalValue
156
156
 
157
- const [hours, setHoursState] = useState(() => splitValid(value !== undefined ? value : defaultValue)[0])
158
- const [minutes, setMinutesState] = useState(() => splitValid(value !== undefined ? value : defaultValue)[1])
157
+ const [hoursState, setHoursState] = useState(() => splitValid(value !== undefined ? value : defaultValue)[0])
158
+ const [minutesState, setMinutesState] = useState(() => splitValid(value !== undefined ? value : defaultValue)[1])
159
159
 
160
- const hoursRef = useRef(hours)
161
- const minutesRef = useRef(minutes)
160
+ const hoursRef = useRef(hoursState)
161
+ const minutesRef = useRef(minutesState)
162
162
 
163
163
  const setHours = useCallback((h: string) => {
164
164
  hoursRef.current = h
@@ -171,17 +171,14 @@ export function useTimepickerState(props: IPktTimepicker, ref: React.ForwardedRe
171
171
  }, [])
172
172
 
173
173
  const [isOpen, setIsOpen] = useState(false)
174
- const [isInvalid, setIsInvalid] = useState(false)
175
174
 
176
- const prevControlledValueRef = useRef(value)
177
- useEffect(() => {
178
- if (!isControlled) return
179
- if (value === prevControlledValueRef.current) return
180
- prevControlledValueRef.current = value
175
+ const [prevControlledValue, setPrevControlledValue] = useState(value)
176
+ if (isControlled && value !== prevControlledValue) {
177
+ setPrevControlledValue(value)
181
178
  const [h, m] = splitValid(value)
182
179
  setHours(h)
183
180
  setMinutes(m)
184
- }, [value, isControlled, setHours, setMinutes])
181
+ }
185
182
 
186
183
  useEffect(() => {
187
184
  if (step !== null && step !== undefined && !isValidStep(step)) {
@@ -222,13 +219,15 @@ export function useTimepickerState(props: IPktTimepicker, ref: React.ForwardedRe
222
219
 
223
220
  // Sett native constraint validation på det synlige timer-inputfeltet,
224
221
  // slik at form.reportValidity() viser feilmelding på riktig element
222
+ const validation = useMemo(
223
+ () => validateTimeValue(effectiveValue, { required, min, max, step }, catalog.validation),
224
+ [effectiveValue, required, min, max, step, catalog.validation],
225
+ )
226
+ const isInvalid = touched && !validation.valid
227
+
225
228
  useEffect(() => {
226
- const anchor = hoursInputRef.current
227
- if (!anchor) return
228
- const validation = validateTimeValue(effectiveValue, { required, min, max, step }, catalog.validation)
229
- anchor.setCustomValidity(validation.valid ? '' : (validation.message ?? ''))
230
- setIsInvalid(touched && !validation.valid)
231
- }, [effectiveValue, required, min, max, step, touched, catalog.validation])
229
+ hoursInputRef.current?.setCustomValidity(validation.valid ? '' : (validation.message ?? ''))
230
+ }, [validation])
232
231
 
233
232
  const commitValue = useCallback(
234
233
  (newValue: string) => {
@@ -751,9 +750,11 @@ export function useTimepickerState(props: IPktTimepicker, ref: React.ForwardedRe
751
750
 
752
751
  useEffect(() => {
753
752
  if (!ownerForm) return
753
+ let resetTimeout: number | undefined
754
754
 
755
755
  const handleReset = () => {
756
- window.setTimeout(() => {
756
+ window.clearTimeout(resetTimeout)
757
+ resetTimeout = window.setTimeout(() => {
757
758
  const init = initialValuesRef.current
758
759
  const [h, m] = splitValid(init)
759
760
  setHours(h)
@@ -770,7 +771,10 @@ export function useTimepickerState(props: IPktTimepicker, ref: React.ForwardedRe
770
771
  }
771
772
 
772
773
  ownerForm.addEventListener('reset', handleReset)
773
- return () => ownerForm.removeEventListener('reset', handleReset)
774
+ return () => {
775
+ ownerForm.removeEventListener('reset', handleReset)
776
+ window.clearTimeout(resetTimeout)
777
+ }
774
778
  }, [isControlled, setHours, setMinutes, ownerForm])
775
779
 
776
780
  const outerClasses = [
@@ -812,8 +816,8 @@ export function useTimepickerState(props: IPktTimepicker, ref: React.ForwardedRe
812
816
  min,
813
817
  max,
814
818
  step,
815
- hours,
816
- minutes,
819
+ hours: hoursState,
820
+ minutes: minutesState,
817
821
  hoursId,
818
822
  minutesId,
819
823
  popupId,