@oslokommune/punkt-react 18.7.0 → 18.7.2
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 +809 -787
- package/dist/shared-strings/nb.d.ts +1 -0
- package/package.json +8 -10
- 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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oslokommune/punkt-react",
|
|
3
|
-
"version": "18.7.
|
|
3
|
+
"version": "18.7.2",
|
|
4
4
|
"description": "React komponentbibliotek til Punkt, et designsystem laget av Oslo Origo",
|
|
5
5
|
"homepage": "https://punkt.oslo.kommune.no",
|
|
6
6
|
"author": "Team Designsystem, Oslo Origo",
|
|
@@ -33,19 +33,14 @@
|
|
|
33
33
|
"test": "vitest run",
|
|
34
34
|
"test:watch": "vitest"
|
|
35
35
|
},
|
|
36
|
-
"dependencies": {
|
|
37
|
-
"classnames": "^2.5.1",
|
|
38
|
-
"prettier": "^3.8.4",
|
|
39
|
-
"react-hook-form": "^7.79.0"
|
|
40
|
-
},
|
|
41
36
|
"devDependencies": {
|
|
42
37
|
"@babel/plugin-transform-private-property-in-object": "^7.27.1",
|
|
43
38
|
"@eslint-react/eslint-plugin": "^5.9.0",
|
|
44
39
|
"@eslint/compat": "^2.1.0",
|
|
45
40
|
"@eslint/eslintrc": "^3.3.5",
|
|
46
41
|
"@eslint/js": "^10.0.1",
|
|
47
|
-
"@oslokommune/punkt-assets": "^18.
|
|
48
|
-
"@oslokommune/punkt-css": "^18.
|
|
42
|
+
"@oslokommune/punkt-assets": "^18.7.1",
|
|
43
|
+
"@oslokommune/punkt-css": "^18.7.1",
|
|
49
44
|
"@testing-library/jest-dom": "^6.9.1",
|
|
50
45
|
"@testing-library/react": "^16.3.2",
|
|
51
46
|
"@testing-library/user-event": "^14.6.1",
|
|
@@ -54,6 +49,7 @@
|
|
|
54
49
|
"@types/react": "^19.2.17",
|
|
55
50
|
"@types/react-dom": "^19.2.3",
|
|
56
51
|
"@vitejs/plugin-react": "^6.0.2",
|
|
52
|
+
"classnames": "^2.5.1",
|
|
57
53
|
"eslint": "^10.5.0",
|
|
58
54
|
"eslint-config-prettier": "^10.1.8",
|
|
59
55
|
"eslint-plugin-prettier": "^5.5.6",
|
|
@@ -62,7 +58,9 @@
|
|
|
62
58
|
"globals": "^17.6.0",
|
|
63
59
|
"jest-axe": "^10.0.0",
|
|
64
60
|
"jsdom": "^29.1.1",
|
|
65
|
-
"
|
|
61
|
+
"prettier": "^3.8.4",
|
|
62
|
+
"react-hook-form": "^7.79.0",
|
|
63
|
+
"react-router-dom": "^7.18.3",
|
|
66
64
|
"sass": "^1.101.0",
|
|
67
65
|
"typescript": "^7.0.2",
|
|
68
66
|
"typescript-eslint": "^8.61.1",
|
|
@@ -102,5 +100,5 @@
|
|
|
102
100
|
"url": "https://github.com/oslokommune/punkt/issues"
|
|
103
101
|
},
|
|
104
102
|
"license": "MIT",
|
|
105
|
-
"gitHead": "
|
|
103
|
+
"gitHead": "f590a9e5e76fbb4ebf2cf93322937f023ae3160d"
|
|
106
104
|
}
|
|
@@ -72,12 +72,7 @@ describe('PktCombobox', () => {
|
|
|
72
72
|
options: getDefaultOptions(),
|
|
73
73
|
})
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
// is aria-hidden and non-focusable, but axe flags it anyway.
|
|
77
|
-
// Selection state is conveyed by aria-selected on the option element.
|
|
78
|
-
const results = await axe(container, {
|
|
79
|
-
rules: { 'nested-interactive': { enabled: false } },
|
|
80
|
-
})
|
|
75
|
+
const results = await axe(container)
|
|
81
76
|
expect(results).toHaveNoViolations()
|
|
82
77
|
})
|
|
83
78
|
|
|
@@ -173,7 +168,13 @@ describe('PktCombobox', () => {
|
|
|
173
168
|
})
|
|
174
169
|
|
|
175
170
|
const textInput = container.querySelector('input[type="text"]')
|
|
176
|
-
expect(textInput
|
|
171
|
+
expect(textInput).not.toHaveAttribute('aria-activedescendant')
|
|
172
|
+
|
|
173
|
+
fireEvent.click(textInput!)
|
|
174
|
+
|
|
175
|
+
const active = textInput?.getAttribute('aria-activedescendant')
|
|
176
|
+
expect(active).toBeTruthy()
|
|
177
|
+
expect(container.querySelector(`[id="${active}"]`)).not.toBeNull()
|
|
177
178
|
})
|
|
178
179
|
|
|
179
180
|
test('text input aria-expanded reflects dropdown state', () => {
|
|
@@ -270,6 +270,44 @@ describe('PktCombobox', () => {
|
|
|
270
270
|
})
|
|
271
271
|
})
|
|
272
272
|
|
|
273
|
+
describe('Focus after committing a value', () => {
|
|
274
|
+
const commit = async (container: HTMLElement, typed: string) => {
|
|
275
|
+
const input = container.querySelector('input[type="text"]') as HTMLInputElement
|
|
276
|
+
await act(async () => {
|
|
277
|
+
input.focus()
|
|
278
|
+
fireEvent.change(input, { target: { value: typed } })
|
|
279
|
+
fireEvent.keyDown(input, { key: 'Enter' })
|
|
280
|
+
await new Promise((resolve) => setTimeout(resolve, 0))
|
|
281
|
+
})
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
test('moves focus out of the text input after adding a user value', async () => {
|
|
285
|
+
const { container } = createComboboxTest({
|
|
286
|
+
allowUserInput: true,
|
|
287
|
+
options: getDefaultOptions(),
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
await commit(container, 'NewFruit')
|
|
291
|
+
|
|
292
|
+
const input = container.querySelector('input[type="text"]')
|
|
293
|
+
expect(document.activeElement).not.toBe(input)
|
|
294
|
+
expect(document.activeElement).toBe(container.querySelector('.pkt-combobox__input'))
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
test('moves focus out of the text input after selecting a typeahead match', async () => {
|
|
298
|
+
const { container } = createComboboxTest({
|
|
299
|
+
typeahead: true,
|
|
300
|
+
options: getDefaultOptions(),
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
await commit(container, 'apple')
|
|
304
|
+
|
|
305
|
+
const input = container.querySelector('input[type="text"]')
|
|
306
|
+
expect(document.activeElement).not.toBe(input)
|
|
307
|
+
expect(document.activeElement).toBe(container.querySelector('.pkt-combobox__input'))
|
|
308
|
+
})
|
|
309
|
+
})
|
|
310
|
+
|
|
273
311
|
describe('Focus-out behavior', () => {
|
|
274
312
|
test('closes dropdown when clicking outside combobox', () => {
|
|
275
313
|
const { container } = createComboboxTest({
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import '@testing-library/jest-dom'
|
|
2
|
+
|
|
3
|
+
import { act, fireEvent, render } from '@testing-library/react'
|
|
4
|
+
|
|
5
|
+
import type { IPktComboboxOption } from 'shared-types/combobox'
|
|
6
|
+
import { PktCombobox } from './Combobox'
|
|
7
|
+
import type { IPktCombobox } from './types'
|
|
8
|
+
|
|
9
|
+
const bydeler: IPktComboboxOption[] = [
|
|
10
|
+
{ value: 'bfr', label: 'Frogner' },
|
|
11
|
+
{ value: 'bga', label: 'Grünerløkka' },
|
|
12
|
+
{ value: 'bsa', label: 'Sagene' },
|
|
13
|
+
{ value: 'bgo', label: 'Gamle Oslo' },
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
const createCombobox = (props: Partial<IPktCombobox> = {}) =>
|
|
17
|
+
render(<PktCombobox label="L" id="test" {...(props as IPktCombobox)} />)
|
|
18
|
+
|
|
19
|
+
/** Values the listbox renders as selected. */
|
|
20
|
+
const selectedInList = (container: HTMLElement) =>
|
|
21
|
+
Array.from(container.querySelectorAll('[role="option"]'))
|
|
22
|
+
.filter((el) => el.getAttribute('aria-selected') === 'true')
|
|
23
|
+
.map((el) => el.getAttribute('data-value'))
|
|
24
|
+
|
|
25
|
+
const tagCount = (container: HTMLElement) => container.querySelectorAll('.pkt-tag').length
|
|
26
|
+
|
|
27
|
+
const clickOption = async (container: HTMLElement, value: string) => {
|
|
28
|
+
const option = container.querySelector(`[data-value="${value}"]`) as HTMLElement
|
|
29
|
+
await act(async () => {
|
|
30
|
+
fireEvent.click(option)
|
|
31
|
+
await new Promise((resolve) => setTimeout(resolve, 0))
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
describe('PktCombobox preselection', () => {
|
|
36
|
+
describe('Preselected via the value prop', () => {
|
|
37
|
+
test('marks the preselected options in both the tag list and the listbox', () => {
|
|
38
|
+
const { container } = createCombobox({
|
|
39
|
+
multiple: true,
|
|
40
|
+
tagPlacement: 'outside',
|
|
41
|
+
defaultValue: ['bfr', 'bga', 'bsa'],
|
|
42
|
+
options: bydeler,
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
expect(selectedInList(container)).toEqual(['bfr', 'bga', 'bsa'])
|
|
46
|
+
expect(tagCount(container)).toBe(3)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('keeps the existing selection in the listbox when another option is picked', async () => {
|
|
50
|
+
const { container } = createCombobox({
|
|
51
|
+
multiple: true,
|
|
52
|
+
tagPlacement: 'outside',
|
|
53
|
+
defaultValue: ['bfr', 'bga', 'bsa'],
|
|
54
|
+
options: bydeler,
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
await clickOption(container, 'bgo')
|
|
58
|
+
|
|
59
|
+
expect(selectedInList(container)).toEqual(['bfr', 'bga', 'bsa', 'bgo'])
|
|
60
|
+
expect(tagCount(container)).toBe(4)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test('keeps the listbox in sync when an option is deselected', async () => {
|
|
64
|
+
const { container } = createCombobox({
|
|
65
|
+
multiple: true,
|
|
66
|
+
defaultValue: ['bfr', 'bga', 'bsa'],
|
|
67
|
+
options: bydeler,
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
await clickOption(container, 'bga')
|
|
71
|
+
|
|
72
|
+
expect(selectedInList(container)).toEqual(['bfr', 'bsa'])
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
describe('Preselected via options.selected', () => {
|
|
77
|
+
test('seeds the value from options marked selected', () => {
|
|
78
|
+
const { container } = createCombobox({
|
|
79
|
+
multiple: true,
|
|
80
|
+
typeahead: true,
|
|
81
|
+
options: [
|
|
82
|
+
{ value: 'HODEPINE', selected: true },
|
|
83
|
+
{ value: 'KVALME' },
|
|
84
|
+
{ value: 'INGEN', selected: true },
|
|
85
|
+
],
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
expect(selectedInList(container)).toEqual(['HODEPINE', 'INGEN'])
|
|
89
|
+
expect(tagCount(container)).toBe(2)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
test('adds to the preselected values rather than replacing them', async () => {
|
|
93
|
+
const { container } = createCombobox({
|
|
94
|
+
multiple: true,
|
|
95
|
+
typeahead: true,
|
|
96
|
+
options: [
|
|
97
|
+
{ value: 'HODEPINE', selected: true },
|
|
98
|
+
{ value: 'KVALME' },
|
|
99
|
+
{ value: 'INGEN', selected: true },
|
|
100
|
+
],
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
await clickOption(container, 'KVALME')
|
|
104
|
+
|
|
105
|
+
expect(selectedInList(container)).toEqual(['HODEPINE', 'KVALME', 'INGEN'])
|
|
106
|
+
expect(tagCount(container)).toBe(3)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('takes only the first preselected option in single-select mode', () => {
|
|
110
|
+
const { container } = createCombobox({
|
|
111
|
+
options: [
|
|
112
|
+
{ value: 'HODEPINE', selected: true },
|
|
113
|
+
{ value: 'KVALME' },
|
|
114
|
+
],
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
expect(selectedInList(container)).toEqual(['HODEPINE'])
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
describe('Preselected via defaultValue', () => {
|
|
122
|
+
test('seeds the value from defaultValue', () => {
|
|
123
|
+
const { container } = createCombobox({
|
|
124
|
+
multiple: true,
|
|
125
|
+
defaultValue: ['bfr', 'bga'],
|
|
126
|
+
options: bydeler,
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
expect(selectedInList(container)).toEqual(['bfr', 'bga'])
|
|
130
|
+
expect(tagCount(container)).toBe(2)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('lets value take precedence over defaultValue', () => {
|
|
134
|
+
const { container } = createCombobox({
|
|
135
|
+
multiple: true,
|
|
136
|
+
value: ['bsa'],
|
|
137
|
+
defaultValue: ['bfr', 'bga'],
|
|
138
|
+
options: bydeler,
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
expect(selectedInList(container)).toEqual(['bsa'])
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
test('does not reapply defaultValue after the selection is cleared', async () => {
|
|
145
|
+
const { container } = createCombobox({
|
|
146
|
+
multiple: true,
|
|
147
|
+
defaultValue: ['bfr'],
|
|
148
|
+
options: bydeler,
|
|
149
|
+
})
|
|
150
|
+
expect(selectedInList(container)).toEqual(['bfr'])
|
|
151
|
+
|
|
152
|
+
await clickOption(container, 'bfr')
|
|
153
|
+
|
|
154
|
+
expect(selectedInList(container)).toEqual([])
|
|
155
|
+
})
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
describe('Form reset', () => {
|
|
159
|
+
const renderInForm = (props: Partial<IPktCombobox>) =>
|
|
160
|
+
render(
|
|
161
|
+
<form>
|
|
162
|
+
<PktCombobox label="L" id="test" name="test" {...(props as IPktCombobox)} />
|
|
163
|
+
</form>,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
const addUserValue = async (container: HTMLElement, text: string) => {
|
|
167
|
+
const input = container.querySelector('input[type="text"]') as HTMLInputElement
|
|
168
|
+
await act(async () => {
|
|
169
|
+
input.focus()
|
|
170
|
+
fireEvent.change(input, { target: { value: text } })
|
|
171
|
+
fireEvent.keyDown(input, { key: 'Enter' })
|
|
172
|
+
await new Promise((resolve) => setTimeout(resolve, 0))
|
|
173
|
+
})
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const reset = async (container: HTMLElement) => {
|
|
177
|
+
await act(async () => {
|
|
178
|
+
;(container.querySelector('form') as HTMLFormElement).reset()
|
|
179
|
+
await new Promise((resolve) => setTimeout(resolve, 0))
|
|
180
|
+
})
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
test('clears a selected option', async () => {
|
|
184
|
+
const { container } = renderInForm({ multiple: true, options: bydeler })
|
|
185
|
+
await clickOption(container, 'bfr')
|
|
186
|
+
expect(tagCount(container)).toBe(1)
|
|
187
|
+
|
|
188
|
+
await reset(container)
|
|
189
|
+
|
|
190
|
+
expect(tagCount(container)).toBe(0)
|
|
191
|
+
expect(selectedInList(container)).toEqual([])
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
test('clears a user-added value', async () => {
|
|
195
|
+
const { container } = renderInForm({
|
|
196
|
+
multiple: true,
|
|
197
|
+
allowUserInput: true,
|
|
198
|
+
options: bydeler,
|
|
199
|
+
})
|
|
200
|
+
await addUserValue(container, 'egendefinert')
|
|
201
|
+
expect(tagCount(container)).toBe(1)
|
|
202
|
+
|
|
203
|
+
await reset(container)
|
|
204
|
+
|
|
205
|
+
expect(tagCount(container)).toBe(0)
|
|
206
|
+
})
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
describe('Selection state after picking an option', () => {
|
|
210
|
+
test('marks the option in the listbox on the first click', async () => {
|
|
211
|
+
const { container } = createCombobox({
|
|
212
|
+
multiple: true,
|
|
213
|
+
maxlength: 3,
|
|
214
|
+
includeSearch: true,
|
|
215
|
+
options: bydeler,
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
await clickOption(container, 'bfr')
|
|
219
|
+
|
|
220
|
+
expect(selectedInList(container)).toEqual(['bfr'])
|
|
221
|
+
expect(tagCount(container)).toBe(1)
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
test('keeps value and listbox in sync across repeated toggles', async () => {
|
|
225
|
+
const { container } = createCombobox({ multiple: true, options: bydeler })
|
|
226
|
+
|
|
227
|
+
for (const value of ['bfr', 'bga', 'bfr', 'bsa']) {
|
|
228
|
+
await clickOption(container, value)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
expect(selectedInList(container)).toEqual(['bga', 'bsa'])
|
|
232
|
+
expect(tagCount(container)).toBe(2)
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
test('keeps selection when all options are preselected', async () => {
|
|
236
|
+
const { container } = createCombobox({
|
|
237
|
+
multiple: true,
|
|
238
|
+
defaultValue: ['bfr', 'bga', 'bsa', 'bgo'],
|
|
239
|
+
options: bydeler,
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
await clickOption(container, 'bfr')
|
|
243
|
+
|
|
244
|
+
expect(selectedInList(container)).toEqual(['bga', 'bsa', 'bgo'])
|
|
245
|
+
})
|
|
246
|
+
})
|
|
247
|
+
})
|
|
@@ -27,6 +27,8 @@ export const ComboboxInput = ({ state }: IComboboxInputProps) => {
|
|
|
27
27
|
hasError,
|
|
28
28
|
label,
|
|
29
29
|
inputFocus,
|
|
30
|
+
skipInputTab,
|
|
31
|
+
restoreInputTab,
|
|
30
32
|
inputRef,
|
|
31
33
|
triggerRef,
|
|
32
34
|
handleInputChange,
|
|
@@ -63,7 +65,7 @@ export const ComboboxInput = ({ state }: IComboboxInputProps) => {
|
|
|
63
65
|
|
|
64
66
|
// Compute aria-activedescendant for select-only mode
|
|
65
67
|
const activeDescendant =
|
|
66
|
-
values[0] && findOptionByValue(options, values[0])
|
|
68
|
+
isOpen && values[0] && findOptionByValue(options, values[0])
|
|
67
69
|
? `${listboxId}-${findOptionIndex(options, values[0])}`
|
|
68
70
|
: undefined
|
|
69
71
|
|
|
@@ -89,7 +91,16 @@ export const ComboboxInput = ({ state }: IComboboxInputProps) => {
|
|
|
89
91
|
}
|
|
90
92
|
|
|
91
93
|
return (
|
|
92
|
-
<div
|
|
94
|
+
<div
|
|
95
|
+
ref={triggerRef}
|
|
96
|
+
className={inputClasses}
|
|
97
|
+
onClick={handleInputClick}
|
|
98
|
+
onBlur={(e) => {
|
|
99
|
+
// React's onBlur bubbles (focusout), so only react to the wrapper's own blur.
|
|
100
|
+
if (e.target === e.currentTarget) restoreInputTab()
|
|
101
|
+
}}
|
|
102
|
+
{...selectOnlyProps}
|
|
103
|
+
>
|
|
93
104
|
{showPlaceholder ? (
|
|
94
105
|
<span className="pkt-combobox__placeholder" onClick={handlePlaceholderClick}>
|
|
95
106
|
{placeholder}
|
|
@@ -113,6 +124,7 @@ export const ComboboxInput = ({ state }: IComboboxInputProps) => {
|
|
|
113
124
|
onKeyDown={handleInputKeydown}
|
|
114
125
|
onFocus={handleInputFocus}
|
|
115
126
|
onBlur={handleInputBlur}
|
|
127
|
+
tabIndex={skipInputTab ? -1 : undefined}
|
|
116
128
|
placeholder={!values.length || (multiple && tagPlacement === 'outside') ? placeholder : undefined}
|
|
117
129
|
autoComplete="off"
|
|
118
130
|
role="combobox"
|
|
@@ -132,7 +144,6 @@ export const ComboboxInput = ({ state }: IComboboxInputProps) => {
|
|
|
132
144
|
id={inputId}
|
|
133
145
|
name={`${name || id}-input`}
|
|
134
146
|
value={values.join(',')}
|
|
135
|
-
readOnly
|
|
136
147
|
/>
|
|
137
148
|
)}
|
|
138
149
|
|
|
@@ -142,10 +142,11 @@ export const Listbox = ({ state }: IListboxProps) => {
|
|
|
142
142
|
<input
|
|
143
143
|
className="pkt-input-check__input-checkbox"
|
|
144
144
|
type="checkbox"
|
|
145
|
+
inert
|
|
145
146
|
tabIndex={-1}
|
|
146
147
|
value={option.value}
|
|
147
148
|
checked={option.selected || false}
|
|
148
|
-
|
|
149
|
+
onChange={() => {}}
|
|
149
150
|
aria-hidden="true"
|
|
150
151
|
disabled={isOptionDisabled}
|
|
151
152
|
/>
|
|
@@ -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,
|