@oslokommune/punkt-react 18.7.1 → 18.8.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 +48 -0
- package/dist/components/combobox/Combobox.preselection.test.d.ts +0 -0
- package/dist/components/combobox/types.d.ts +2 -0
- package/dist/punkt-react.cjs +1 -1
- package/dist/punkt-react.js +1043 -1023
- package/dist/shared-strings/nb.d.ts +1 -0
- package/package.json +3 -3
- package/src/components/checkbox/Checkbox.tsx +26 -13
- package/src/components/combobox/Combobox.accessibility.test.tsx +8 -7
- package/src/components/combobox/Combobox.interaction.test.tsx +38 -0
- package/src/components/combobox/Combobox.preselection.test.tsx +247 -0
- package/src/components/combobox/ComboboxInput.tsx +14 -3
- package/src/components/combobox/Listbox.tsx +2 -1
- package/src/components/combobox/types.ts +2 -0
- package/src/components/combobox/useComboboxState.ts +51 -17
- package/src/components/radio/RadioButton.tsx +22 -19
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
import type { IPktComboboxOption, TPktComboboxDisplayValue } from 'shared-types/combobox'
|
|
15
15
|
import {
|
|
16
16
|
findOptionByValue,
|
|
17
|
+
getOptionDisplayText,
|
|
17
18
|
filterOptionsBySearch,
|
|
18
19
|
buildFulltext,
|
|
19
20
|
isMaxSelectionReached,
|
|
@@ -31,6 +32,7 @@ import {
|
|
|
31
32
|
} from 'shared-utils/combobox'
|
|
32
33
|
|
|
33
34
|
import type { IPktCombobox, IComboboxState } from './types'
|
|
35
|
+
import { formatString } from 'shared-strings'
|
|
34
36
|
import { usePktStrings } from '../../hooks/usePktStrings'
|
|
35
37
|
|
|
36
38
|
// Options sync helper (React-specific: immutable, returns new array)
|
|
@@ -106,6 +108,15 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
106
108
|
}
|
|
107
109
|
}, [hasTextInput])
|
|
108
110
|
|
|
111
|
+
const focusAfterCommit = useCallback(() => {
|
|
112
|
+
// The input sits inside the wrapper, so Tab would step back into it.
|
|
113
|
+
// Written to the DOM directly so it lands before focus moves.
|
|
114
|
+
setSkipInputTab(true)
|
|
115
|
+
const input = inputRef.current
|
|
116
|
+
if (input && input.type !== 'hidden') input.setAttribute('tabindex', '-1')
|
|
117
|
+
triggerRef.current?.focus()
|
|
118
|
+
}, [])
|
|
119
|
+
|
|
109
120
|
// Parse <option> children into options
|
|
110
121
|
const optionsFromChildren = useMemo(() => {
|
|
111
122
|
const result: IPktComboboxOption[] = []
|
|
@@ -148,7 +159,13 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
148
159
|
|
|
149
160
|
const [internalValues, setInternalValues] = useState<string[]>(() => {
|
|
150
161
|
const initial = value ?? defaultValue
|
|
151
|
-
|
|
162
|
+
const fromValue = parseValueToArray(initial as string | string[] | null | undefined, multiple)
|
|
163
|
+
const preselected = baseOptions
|
|
164
|
+
.filter((option) => option.selected && !fromValue.includes(option.value))
|
|
165
|
+
.map((option) => option.value)
|
|
166
|
+
if (!preselected.length) return fromValue
|
|
167
|
+
if (multiple) return [...fromValue, ...preselected]
|
|
168
|
+
return fromValue.length ? fromValue : [preselected[0]]
|
|
152
169
|
})
|
|
153
170
|
|
|
154
171
|
// Store initial default values for form reset
|
|
@@ -181,14 +198,19 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
181
198
|
const [searchValue, setSearchValue] = useState('')
|
|
182
199
|
const [inputValue, setInputValue] = useState('')
|
|
183
200
|
const [userInfoMessageKey, setUserInfoMessage] = useState<TComboboxMessageKey>('')
|
|
201
|
+
const [messageValue, setMessageValue] = useState<string | null>(null)
|
|
184
202
|
const [addValueText, setAddValueText] = useState<string | null>(null)
|
|
185
203
|
const [inputFocus, setInputFocus] = useState(false)
|
|
204
|
+
const [skipInputTab, setSkipInputTab] = useState(false)
|
|
205
|
+
// Set when focus returns to the input on close, so handleInputFocus does not reopen the list.
|
|
206
|
+
const suppressNextOpenRef = useRef(false)
|
|
186
207
|
const [editingSingleValue, setEditingSingleValue] = useState(false)
|
|
187
208
|
const [focusedTagIndex, setFocusedTagIndex] = useState<number>(-1)
|
|
188
|
-
const suppressNextOpenRef = useRef(false)
|
|
189
209
|
|
|
190
210
|
const catalog = usePktStrings(['combobox'], strings)
|
|
191
|
-
const userInfoMessage = userInfoMessageKey
|
|
211
|
+
const userInfoMessage = userInfoMessageKey
|
|
212
|
+
? formatString(catalog.combobox[userInfoMessageKey], { value: messageValue ?? '' })
|
|
213
|
+
: ''
|
|
192
214
|
|
|
193
215
|
// Derived state
|
|
194
216
|
const maxIsReached = isMaxSelectionReached(values.length, maxlength ?? null)
|
|
@@ -353,6 +375,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
353
375
|
let shouldOptionsBeOpen = false
|
|
354
376
|
let shouldResetInput = true
|
|
355
377
|
let newUserInfoMessage: TComboboxMessageKey = ''
|
|
378
|
+
let newMessageValue: string | null = null
|
|
356
379
|
let newSearchValue = ''
|
|
357
380
|
// Track the resulting single value for resetInput (avoids stale closure reads)
|
|
358
381
|
let resultingSingleValue: string | undefined
|
|
@@ -397,6 +420,14 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
397
420
|
setSelected(valueFromOptions)
|
|
398
421
|
shouldOptionsBeOpen = false
|
|
399
422
|
resultingSingleValue = valueFromOptions || ''
|
|
423
|
+
newUserInfoMessage = 'valueSelected'
|
|
424
|
+
// The two modes resolve displayValueAs through different helpers.
|
|
425
|
+
newMessageValue = hasTextInput
|
|
426
|
+
? getSingleValueForInput(valueFromOptions || '', options, displayValueAs)
|
|
427
|
+
: getOptionDisplayText(
|
|
428
|
+
findOptionByValue(options, valueFromOptions),
|
|
429
|
+
displayValueAs as TPktComboboxDisplayValue,
|
|
430
|
+
)
|
|
400
431
|
}
|
|
401
432
|
// Multi with room
|
|
402
433
|
else if (isMultiple && !isMaxItemsReached) {
|
|
@@ -420,19 +451,15 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
420
451
|
}
|
|
421
452
|
|
|
422
453
|
setIsOpen(shouldOptionsBeOpen)
|
|
423
|
-
setUserInfoMessage(newUserInfoMessage)
|
|
424
454
|
setSearchValue(newSearchValue)
|
|
425
455
|
resetInput(shouldResetInput, resultingSingleValue)
|
|
456
|
+
// After resetInput, which clears any stale message in single mode.
|
|
457
|
+
setUserInfoMessage(newUserInfoMessage)
|
|
458
|
+
setMessageValue(newMessageValue)
|
|
426
459
|
|
|
427
460
|
if (!shouldOptionsBeOpen) {
|
|
428
|
-
if (isSingle && hasTextInput) {
|
|
429
|
-
// Suppress the next handleInputFocus from reopening the dropdown,
|
|
430
|
-
// then move focus back to the text input so screen readers
|
|
431
|
-
// announce the selected value instead of the browser window.
|
|
432
|
-
suppressNextOpenRef.current = true
|
|
433
|
-
}
|
|
434
461
|
window.setTimeout(() => {
|
|
435
|
-
|
|
462
|
+
focusAfterCommit()
|
|
436
463
|
}, 0)
|
|
437
464
|
}
|
|
438
465
|
},
|
|
@@ -448,7 +475,9 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
448
475
|
removeAllSelected,
|
|
449
476
|
setSelected,
|
|
450
477
|
resetInput,
|
|
451
|
-
|
|
478
|
+
focusAfterCommit,
|
|
479
|
+
hasTextInput,
|
|
480
|
+
displayValueAs,
|
|
452
481
|
],
|
|
453
482
|
)
|
|
454
483
|
|
|
@@ -654,8 +683,8 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
654
683
|
const handleInputFocus = useCallback(() => {
|
|
655
684
|
if (disabled) return
|
|
656
685
|
|
|
657
|
-
|
|
658
|
-
|
|
686
|
+
setSkipInputTab(false)
|
|
687
|
+
|
|
659
688
|
if (suppressNextOpenRef.current) {
|
|
660
689
|
suppressNextOpenRef.current = false
|
|
661
690
|
setInputFocus(true)
|
|
@@ -830,6 +859,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
830
859
|
case 'Escape':
|
|
831
860
|
e.preventDefault()
|
|
832
861
|
setIsOpen(false)
|
|
862
|
+
if (hasTextInput) suppressNextOpenRef.current = true
|
|
833
863
|
focusTrigger()
|
|
834
864
|
break
|
|
835
865
|
case 'Tab':
|
|
@@ -838,7 +868,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
838
868
|
break
|
|
839
869
|
}
|
|
840
870
|
},
|
|
841
|
-
[toggleValue, includeSearch, focusTrigger, closeAndProcessInput],
|
|
871
|
+
[toggleValue, includeSearch, focusTrigger, closeAndProcessInput, hasTextInput],
|
|
842
872
|
)
|
|
843
873
|
|
|
844
874
|
const handleTagRemove = useCallback(
|
|
@@ -941,11 +971,12 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
941
971
|
case 'Escape':
|
|
942
972
|
e.preventDefault()
|
|
943
973
|
setIsOpen(false)
|
|
974
|
+
if (hasTextInput) suppressNextOpenRef.current = true
|
|
944
975
|
focusTrigger()
|
|
945
976
|
break
|
|
946
977
|
}
|
|
947
978
|
},
|
|
948
|
-
[focusTrigger, closeAndProcessInput],
|
|
979
|
+
[focusTrigger, closeAndProcessInput, hasTextInput],
|
|
949
980
|
)
|
|
950
981
|
|
|
951
982
|
const handleNewOptionClick = useCallback(
|
|
@@ -978,11 +1009,12 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
978
1009
|
case 'Escape':
|
|
979
1010
|
e.preventDefault()
|
|
980
1011
|
setIsOpen(false)
|
|
1012
|
+
if (hasTextInput) suppressNextOpenRef.current = true
|
|
981
1013
|
focusTrigger()
|
|
982
1014
|
break
|
|
983
1015
|
}
|
|
984
1016
|
},
|
|
985
|
-
[toggleValue, includeSearch, focusTrigger],
|
|
1017
|
+
[toggleValue, includeSearch, focusTrigger, hasTextInput],
|
|
986
1018
|
)
|
|
987
1019
|
|
|
988
1020
|
// Outside click handler
|
|
@@ -1083,6 +1115,8 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
1083
1115
|
// UI state
|
|
1084
1116
|
isOpen,
|
|
1085
1117
|
userInfoMessage,
|
|
1118
|
+
skipInputTab,
|
|
1119
|
+
restoreInputTab: () => setSkipInputTab(false),
|
|
1086
1120
|
strings,
|
|
1087
1121
|
addValueText,
|
|
1088
1122
|
maxIsReached,
|
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ChangeEventHandler,
|
|
3
|
-
ForwardedRef,
|
|
4
|
-
forwardRef,
|
|
5
|
-
type InputHTMLAttributes,
|
|
6
|
-
ReactElement,
|
|
7
|
-
ReactNode,
|
|
8
|
-
} from 'react'
|
|
1
|
+
import { ChangeEventHandler, ForwardedRef, forwardRef, type InputHTMLAttributes, ReactElement, ReactNode } from 'react'
|
|
9
2
|
import { usePktStrings } from '../../hooks/usePktStrings'
|
|
3
|
+
import { useGeneratedId } from '../../hooks/useGeneratedId'
|
|
10
4
|
import type { IPktStringsProps } from 'shared-strings'
|
|
11
5
|
|
|
12
6
|
export interface IPktRadioButton extends InputHTMLAttributes<HTMLInputElement>, IPktStringsProps<'forms'> {
|
|
@@ -33,7 +27,7 @@ export interface IPktRadioButton extends InputHTMLAttributes<HTMLInputElement>,
|
|
|
33
27
|
export const PktRadioButton = forwardRef(
|
|
34
28
|
(
|
|
35
29
|
{
|
|
36
|
-
id,
|
|
30
|
+
id: idProp,
|
|
37
31
|
name,
|
|
38
32
|
label,
|
|
39
33
|
className,
|
|
@@ -49,11 +43,13 @@ export const PktRadioButton = forwardRef(
|
|
|
49
43
|
requiredText,
|
|
50
44
|
strings,
|
|
51
45
|
tagText,
|
|
46
|
+
'aria-describedby': ariaDescribedbyProp,
|
|
52
47
|
...props
|
|
53
48
|
}: IPktRadioButton,
|
|
54
49
|
ref: ForwardedRef<HTMLInputElement>,
|
|
55
50
|
): ReactElement => {
|
|
56
51
|
const s = usePktStrings(['forms'], strings)
|
|
52
|
+
const id = useGeneratedId(idProp)
|
|
57
53
|
const classes = [className, 'pkt-input-check'].filter(Boolean).join(' ')
|
|
58
54
|
|
|
59
55
|
const inputTileClasses = [
|
|
@@ -82,19 +78,18 @@ export const PktRadioButton = forwardRef(
|
|
|
82
78
|
<>
|
|
83
79
|
{tagText && <span className={tagClasses + ` pkt-tag--gray`}>{tagText}</span>}
|
|
84
80
|
{optionalTag && (
|
|
85
|
-
<span className={tagClasses + ` pkt-tag--blue-light`}>
|
|
86
|
-
{optionalText ?? s.forms.optional}
|
|
87
|
-
</span>
|
|
88
|
-
)}
|
|
89
|
-
{requiredTag && (
|
|
90
|
-
<span className={tagClasses + ` pkt-tag--beige`}>
|
|
91
|
-
{requiredText ?? s.forms.required}
|
|
92
|
-
</span>
|
|
81
|
+
<span className={tagClasses + ` pkt-tag--blue-light`}>{optionalText ?? s.forms.optional}</span>
|
|
93
82
|
)}
|
|
83
|
+
{requiredTag && <span className={tagClasses + ` pkt-tag--beige`}>{requiredText ?? s.forms.required}</span>}
|
|
94
84
|
</>
|
|
95
85
|
)
|
|
96
86
|
}
|
|
97
87
|
|
|
88
|
+
const labelId = `${id}-check-label`
|
|
89
|
+
const helptextId = `${id}-check-helptext`
|
|
90
|
+
const describedBy =
|
|
91
|
+
[checkHelptext ? helptextId : undefined, ariaDescribedbyProp].filter(Boolean).join(' ') || undefined
|
|
92
|
+
|
|
98
93
|
return (
|
|
99
94
|
<div className={classes}>
|
|
100
95
|
<div className={inputTileClasses}>
|
|
@@ -105,12 +100,20 @@ export const PktRadioButton = forwardRef(
|
|
|
105
100
|
name={name}
|
|
106
101
|
disabled={disabled}
|
|
107
102
|
className={inputCheckBoxClasses}
|
|
103
|
+
aria-labelledby={label ? labelId : undefined}
|
|
104
|
+
aria-describedby={describedBy}
|
|
108
105
|
{...(checked !== undefined ? { checked } : { defaultChecked })}
|
|
109
106
|
{...props}
|
|
110
107
|
/>
|
|
111
108
|
<label className={labelClasses} htmlFor={id}>
|
|
112
|
-
|
|
113
|
-
|
|
109
|
+
<span id={labelId}>
|
|
110
|
+
{label} {renderTags()}
|
|
111
|
+
</span>
|
|
112
|
+
{checkHelptext && (
|
|
113
|
+
<div id={helptextId} className="pkt-input-check__input-helptext">
|
|
114
|
+
{checkHelptext}
|
|
115
|
+
</div>
|
|
116
|
+
)}
|
|
114
117
|
</label>
|
|
115
118
|
</div>
|
|
116
119
|
</div>
|