@oslokommune/punkt-react 19.8.0 → 19.9.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.
- package/CHANGELOG.md +27 -0
- package/dist/components/checkboxgroup/CheckboxGroup.d.ts +17 -0
- package/dist/components/checkboxgroup/CheckboxGroup.forms.test.d.ts +0 -0
- package/dist/components/checkboxgroup/CheckboxGroup.test.d.ts +0 -0
- package/dist/components/checkboxgroup/CheckboxGroupContext.d.ts +12 -0
- package/dist/components/index.d.ts +3 -1
- package/dist/components/inputwrapper/InputWrapper.d.ts +4 -1
- package/dist/components/interfaces.d.ts +2 -0
- package/dist/components/radio/RadioButton.d.ts +1 -1
- package/dist/components/radiogroup/RadioGroup.d.ts +17 -0
- package/dist/components/radiogroup/RadioGroup.forms.test.d.ts +0 -0
- package/dist/components/radiogroup/RadioGroup.test.d.ts +0 -0
- package/dist/components/radiogroup/RadioGroupContext.d.ts +13 -0
- package/dist/punkt-react.cjs +1 -1
- package/dist/punkt-react.js +2416 -2164
- package/dist/shared-strings/nb.d.ts +1 -0
- package/dist/shared-types/index.d.ts +4 -2
- package/dist/shared-types/option-group.d.ts +50 -0
- package/package.json +3 -3
- package/src/components/checkbox/Checkbox.test.tsx +77 -0
- package/src/components/checkbox/Checkbox.tsx +40 -6
- package/src/components/checkboxgroup/CheckboxGroup.forms.test.tsx +149 -0
- package/src/components/checkboxgroup/CheckboxGroup.test.tsx +495 -0
- package/src/components/checkboxgroup/CheckboxGroup.tsx +162 -0
- package/src/components/checkboxgroup/CheckboxGroupContext.ts +15 -0
- package/src/components/index.ts +3 -7
- package/src/components/inputwrapper/InputWrapper.tsx +21 -2
- package/src/components/interfaces.ts +2 -0
- package/src/components/radio/RadioButton.test.tsx +70 -0
- package/src/components/radio/RadioButton.tsx +52 -9
- package/src/components/radiogroup/RadioGroup.forms.test.tsx +145 -0
- package/src/components/radiogroup/RadioGroup.test.tsx +472 -0
- package/src/components/radiogroup/RadioGroup.tsx +123 -0
- package/src/components/radiogroup/RadioGroupContext.ts +16 -0
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import '@testing-library/jest-dom'
|
|
2
|
+
|
|
3
|
+
import { act, fireEvent, render, screen } from '@testing-library/react'
|
|
4
|
+
import { axe, toHaveNoViolations } from 'jest-axe'
|
|
5
|
+
import { createRef, useState } from 'react'
|
|
6
|
+
import { vi } from 'vitest'
|
|
7
|
+
|
|
8
|
+
import { PktCheckbox } from '../checkbox/Checkbox'
|
|
9
|
+
import { PktTextinput } from '../textinput/Textinput'
|
|
10
|
+
import { type IPktCheckboxGroup, PktCheckboxGroup, type TPktCheckboxGroupElement } from './CheckboxGroup'
|
|
11
|
+
|
|
12
|
+
expect.extend(toHaveNoViolations)
|
|
13
|
+
|
|
14
|
+
const Options = () => (
|
|
15
|
+
<>
|
|
16
|
+
<PktCheckbox id="enkelt" value="enkelt" label="Enkeltrom" />
|
|
17
|
+
<PktCheckbox id="dobbelt" value="dobbelt" label="Dobbeltrom" />
|
|
18
|
+
<PktCheckbox id="varsler" value="varsler" label="Send meg varsler" isSwitch />
|
|
19
|
+
</>
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
const checkbox = (id: string) => document.getElementById(id) as HTMLInputElement
|
|
23
|
+
|
|
24
|
+
const ControlledGroup = (props: Partial<IPktCheckboxGroup> & { initial?: string[] }) => {
|
|
25
|
+
const { initial = [], ...rest } = props
|
|
26
|
+
const [value, setValue] = useState(initial)
|
|
27
|
+
return (
|
|
28
|
+
<>
|
|
29
|
+
<PktCheckboxGroup label="Romtype" value={value} onValueChange={setValue} {...rest}>
|
|
30
|
+
<Options />
|
|
31
|
+
</PktCheckboxGroup>
|
|
32
|
+
<output data-testid="value">{value.join(',')}</output>
|
|
33
|
+
</>
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
describe('PktCheckboxGroup', () => {
|
|
38
|
+
describe('rendering', () => {
|
|
39
|
+
it('renders the checkboxes inside the fieldset options of an input wrapper', () => {
|
|
40
|
+
const { container } = render(
|
|
41
|
+
<PktCheckboxGroup id="rom" label="Romtype">
|
|
42
|
+
<Options />
|
|
43
|
+
</PktCheckboxGroup>,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
const options = container.querySelector('fieldset .pkt-inputwrapper__options')
|
|
47
|
+
expect(container.querySelector('legend')).toHaveTextContent('Romtype')
|
|
48
|
+
expect(options).toContainElement(checkbox('enkelt'))
|
|
49
|
+
expect(options).toContainElement(checkbox('varsler'))
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('allows switches in the group', () => {
|
|
53
|
+
render(
|
|
54
|
+
<PktCheckboxGroup label="Romtype">
|
|
55
|
+
<Options />
|
|
56
|
+
</PktCheckboxGroup>,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
expect(checkbox('varsler')).toHaveAttribute('role', 'switch')
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
describe('the group element is the form field', () => {
|
|
64
|
+
const mount = (props: Partial<IPktCheckboxGroup> = {}) => {
|
|
65
|
+
const ref = createRef<TPktCheckboxGroupElement>()
|
|
66
|
+
render(
|
|
67
|
+
<PktCheckboxGroup ref={ref} name="rom" label="Romtype" {...props}>
|
|
68
|
+
<Options />
|
|
69
|
+
</PktCheckboxGroup>,
|
|
70
|
+
)
|
|
71
|
+
return ref.current!
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
it('has the group name', () => {
|
|
75
|
+
expect(mount().name).toBe('rom')
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('reads the checked values in DOM order', () => {
|
|
79
|
+
const field = mount()
|
|
80
|
+
|
|
81
|
+
fireEvent.click(checkbox('varsler'))
|
|
82
|
+
fireEvent.click(checkbox('enkelt'))
|
|
83
|
+
|
|
84
|
+
expect(field.value).toEqual(['enkelt', 'varsler'])
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('checks the listed checkboxes when the value is written, and validates again', () => {
|
|
88
|
+
const onChange = vi.fn()
|
|
89
|
+
const field = mount({ onChange, required: true })
|
|
90
|
+
expect(checkbox('enkelt').validity.customError).toBe(true)
|
|
91
|
+
|
|
92
|
+
field.value = ['dobbelt']
|
|
93
|
+
|
|
94
|
+
expect(checkbox('dobbelt')).toBeChecked()
|
|
95
|
+
expect(checkbox('enkelt')).not.toBeChecked()
|
|
96
|
+
expect(checkbox('enkelt').validity.valid).toBe(true)
|
|
97
|
+
expect(onChange).not.toHaveBeenCalled()
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it('moves focus to the first checked checkbox, or the first one', () => {
|
|
101
|
+
const field = mount()
|
|
102
|
+
|
|
103
|
+
field.focus()
|
|
104
|
+
expect(checkbox('enkelt')).toHaveFocus()
|
|
105
|
+
|
|
106
|
+
fireEvent.click(checkbox('dobbelt'))
|
|
107
|
+
field.focus()
|
|
108
|
+
expect(checkbox('dobbelt')).toHaveFocus()
|
|
109
|
+
})
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
describe('name and defaults', () => {
|
|
113
|
+
it('gives checkboxes without a name the group name', () => {
|
|
114
|
+
render(
|
|
115
|
+
<PktCheckboxGroup name="rom" label="Romtype">
|
|
116
|
+
<Options />
|
|
117
|
+
</PktCheckboxGroup>,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
expect(checkbox('enkelt')).toHaveAttribute('name', 'rom')
|
|
121
|
+
expect(checkbox('varsler')).toHaveAttribute('name', 'rom')
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('keeps a name set on the checkbox', () => {
|
|
125
|
+
render(
|
|
126
|
+
<PktCheckboxGroup name="rom" label="Romtype">
|
|
127
|
+
<PktCheckbox id="enkelt" name="eget" value="enkelt" label="Enkeltrom" />
|
|
128
|
+
</PktCheckboxGroup>,
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
expect(checkbox('enkelt')).toHaveAttribute('name', 'eget')
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it('turns on tile and error state for every checkbox', () => {
|
|
135
|
+
render(
|
|
136
|
+
<PktCheckboxGroup label="Romtype" hasTile hasError>
|
|
137
|
+
<Options />
|
|
138
|
+
</PktCheckboxGroup>,
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
expect(checkbox('dobbelt').closest('.pkt-input-check__input')).toHaveClass('pkt-input-check__input--tile')
|
|
142
|
+
expect(checkbox('dobbelt')).toHaveClass('pkt-input-check__input-checkbox--error')
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
it('does not pass required on to the checkboxes', () => {
|
|
146
|
+
render(
|
|
147
|
+
<PktCheckboxGroup label="Romtype" required>
|
|
148
|
+
<Options />
|
|
149
|
+
</PktCheckboxGroup>,
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
expect(checkbox('enkelt')).not.toBeRequired()
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
it('renders a checkbox outside a group without reading any group', () => {
|
|
156
|
+
render(<PktCheckbox id="alene" label="Alene" />)
|
|
157
|
+
|
|
158
|
+
expect(checkbox('alene')).not.toHaveAttribute('name')
|
|
159
|
+
expect(checkbox('alene').validity.valid).toBe(true)
|
|
160
|
+
})
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
describe('value', () => {
|
|
164
|
+
it('checks the checkboxes in a controlled value', () => {
|
|
165
|
+
render(<ControlledGroup initial={['enkelt', 'varsler']} />)
|
|
166
|
+
|
|
167
|
+
expect(checkbox('enkelt')).toBeChecked()
|
|
168
|
+
expect(checkbox('dobbelt')).not.toBeChecked()
|
|
169
|
+
expect(checkbox('varsler')).toBeChecked()
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it('gives the checked values in DOM order, not in the order they were checked', () => {
|
|
173
|
+
render(<ControlledGroup />)
|
|
174
|
+
|
|
175
|
+
fireEvent.click(checkbox('varsler'))
|
|
176
|
+
fireEvent.click(checkbox('enkelt'))
|
|
177
|
+
|
|
178
|
+
expect(screen.getByTestId('value')).toHaveTextContent('enkelt,varsler')
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
it('keeps the controlled value when the parent does not update it', () => {
|
|
182
|
+
render(
|
|
183
|
+
<PktCheckboxGroup label="Romtype" value={['enkelt']} onValueChange={() => {}}>
|
|
184
|
+
<Options />
|
|
185
|
+
</PktCheckboxGroup>,
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
fireEvent.click(checkbox('dobbelt'))
|
|
189
|
+
|
|
190
|
+
expect(checkbox('dobbelt')).not.toBeChecked()
|
|
191
|
+
expect(checkbox('enkelt')).toBeChecked()
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
it('checks the checkboxes in defaultValue and lets the user change them', () => {
|
|
195
|
+
const onValueChange = vi.fn()
|
|
196
|
+
render(
|
|
197
|
+
<PktCheckboxGroup label="Romtype" defaultValue={['dobbelt']} onValueChange={onValueChange}>
|
|
198
|
+
<Options />
|
|
199
|
+
</PktCheckboxGroup>,
|
|
200
|
+
)
|
|
201
|
+
expect(checkbox('dobbelt')).toBeChecked()
|
|
202
|
+
|
|
203
|
+
fireEvent.click(checkbox('enkelt'))
|
|
204
|
+
|
|
205
|
+
expect(onValueChange).toHaveBeenLastCalledWith(['enkelt', 'dobbelt'])
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
it('leaves out disabled checkboxes', () => {
|
|
209
|
+
const onValueChange = vi.fn()
|
|
210
|
+
render(
|
|
211
|
+
<PktCheckboxGroup label="Romtype" onValueChange={onValueChange}>
|
|
212
|
+
<PktCheckbox id="enkelt" value="enkelt" label="Enkeltrom" />
|
|
213
|
+
<PktCheckbox id="dobbelt" value="dobbelt" label="Dobbeltrom" defaultChecked disabled />
|
|
214
|
+
</PktCheckboxGroup>,
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
fireEvent.click(checkbox('enkelt'))
|
|
218
|
+
|
|
219
|
+
expect(onValueChange).toHaveBeenCalledWith(['enkelt'])
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
it('uses on as the value of a checkbox without a value', () => {
|
|
223
|
+
const onValueChange = vi.fn()
|
|
224
|
+
render(
|
|
225
|
+
<PktCheckboxGroup label="Vilkår" onValueChange={onValueChange}>
|
|
226
|
+
<PktCheckbox id="godta" label="Jeg godtar" />
|
|
227
|
+
</PktCheckboxGroup>,
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
fireEvent.click(checkbox('godta'))
|
|
231
|
+
|
|
232
|
+
expect(onValueChange).toHaveBeenCalledWith(['on'])
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
it('warns when a checkbox sets checked in a controlled group', () => {
|
|
236
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
237
|
+
render(
|
|
238
|
+
<PktCheckboxGroup label="Romtype" value={[]} onValueChange={() => {}}>
|
|
239
|
+
<PktCheckbox id="enkelt" value="enkelt" label="Enkeltrom" checked />
|
|
240
|
+
</PktCheckboxGroup>,
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
expect(warn).toHaveBeenCalledWith(expect.stringContaining('PktCheckbox (enkelt)'))
|
|
244
|
+
expect(checkbox('enkelt')).not.toBeChecked()
|
|
245
|
+
warn.mockRestore()
|
|
246
|
+
})
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
describe('events', () => {
|
|
250
|
+
it('calls onChange with the group as target and onValueChange with all checked values', () => {
|
|
251
|
+
const fromEvents: string[][] = []
|
|
252
|
+
const onValueChange = vi.fn()
|
|
253
|
+
render(
|
|
254
|
+
<PktCheckboxGroup
|
|
255
|
+
name="rom"
|
|
256
|
+
label="Romtype"
|
|
257
|
+
onChange={(event) => {
|
|
258
|
+
expect(event.target.name).toBe('rom')
|
|
259
|
+
fromEvents.push(event.target.value)
|
|
260
|
+
}}
|
|
261
|
+
onValueChange={onValueChange}
|
|
262
|
+
>
|
|
263
|
+
<Options />
|
|
264
|
+
</PktCheckboxGroup>,
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
fireEvent.click(checkbox('dobbelt'))
|
|
268
|
+
fireEvent.click(checkbox('enkelt'))
|
|
269
|
+
fireEvent.click(checkbox('dobbelt'))
|
|
270
|
+
|
|
271
|
+
const expected = [['dobbelt'], ['enkelt', 'dobbelt'], ['enkelt']]
|
|
272
|
+
expect(fromEvents).toEqual(expected)
|
|
273
|
+
expect(onValueChange.mock.calls.map(([value]) => value)).toEqual(expected)
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
it('calls onFocus when focus enters and onBlur when it leaves the group, not between checkboxes', () => {
|
|
277
|
+
const onFocus = vi.fn()
|
|
278
|
+
const onBlur = vi.fn()
|
|
279
|
+
render(
|
|
280
|
+
<>
|
|
281
|
+
<PktCheckboxGroup name="rom" label="Romtype" onFocus={onFocus} onBlur={onBlur}>
|
|
282
|
+
<Options />
|
|
283
|
+
</PktCheckboxGroup>
|
|
284
|
+
<button>Utenfor</button>
|
|
285
|
+
</>,
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
fireEvent.focus(checkbox('enkelt'))
|
|
289
|
+
fireEvent.blur(checkbox('enkelt'), { relatedTarget: checkbox('dobbelt') })
|
|
290
|
+
fireEvent.focus(checkbox('dobbelt'), { relatedTarget: checkbox('enkelt') })
|
|
291
|
+
fireEvent.blur(checkbox('dobbelt'), { relatedTarget: screen.getByRole('button') })
|
|
292
|
+
|
|
293
|
+
expect(onFocus).toHaveBeenCalledTimes(1)
|
|
294
|
+
expect(onBlur).toHaveBeenCalledTimes(1)
|
|
295
|
+
expect(onBlur.mock.calls[0][0].target.value).toEqual([])
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
it('still calls onChange on the checkbox itself', () => {
|
|
299
|
+
const onCheckboxChange = vi.fn()
|
|
300
|
+
render(
|
|
301
|
+
<PktCheckboxGroup label="Romtype" onValueChange={() => {}}>
|
|
302
|
+
<PktCheckbox id="enkelt" value="enkelt" label="Enkeltrom" onChange={onCheckboxChange} />
|
|
303
|
+
</PktCheckboxGroup>,
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
fireEvent.click(checkbox('enkelt'))
|
|
307
|
+
|
|
308
|
+
expect(onCheckboxChange).toHaveBeenCalledTimes(1)
|
|
309
|
+
})
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
describe('validation', () => {
|
|
313
|
+
it('makes every checkbox invalid with a custom error until one is checked', () => {
|
|
314
|
+
render(
|
|
315
|
+
<PktCheckboxGroup label="Romtype" required>
|
|
316
|
+
<Options />
|
|
317
|
+
</PktCheckboxGroup>,
|
|
318
|
+
)
|
|
319
|
+
|
|
320
|
+
for (const id of ['enkelt', 'dobbelt', 'varsler']) {
|
|
321
|
+
expect(checkbox(id).validity.customError).toBe(true)
|
|
322
|
+
expect(checkbox(id).validity.valueMissing).toBe(false)
|
|
323
|
+
expect(checkbox(id).validationMessage).toBe('Velg minst ett alternativ')
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
fireEvent.click(checkbox('dobbelt'))
|
|
327
|
+
for (const id of ['enkelt', 'dobbelt', 'varsler']) {
|
|
328
|
+
expect(checkbox(id).validity.valid).toBe(true)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
fireEvent.click(checkbox('dobbelt'))
|
|
332
|
+
expect(checkbox('enkelt').validity.customError).toBe(true)
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
it('follows a controlled value', () => {
|
|
336
|
+
const { rerender } = render(
|
|
337
|
+
<PktCheckboxGroup label="Romtype" required value={[]} onValueChange={() => {}}>
|
|
338
|
+
<Options />
|
|
339
|
+
</PktCheckboxGroup>,
|
|
340
|
+
)
|
|
341
|
+
expect(checkbox('enkelt').validity.customError).toBe(true)
|
|
342
|
+
|
|
343
|
+
rerender(
|
|
344
|
+
<PktCheckboxGroup label="Romtype" required value={['enkelt']} onValueChange={() => {}}>
|
|
345
|
+
<Options />
|
|
346
|
+
</PktCheckboxGroup>,
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
expect(checkbox('dobbelt').validity.valid).toBe(true)
|
|
350
|
+
})
|
|
351
|
+
|
|
352
|
+
it('clears the custom error when the group is no longer required', () => {
|
|
353
|
+
const { rerender } = render(
|
|
354
|
+
<PktCheckboxGroup label="Romtype" required>
|
|
355
|
+
<Options />
|
|
356
|
+
</PktCheckboxGroup>,
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
rerender(
|
|
360
|
+
<PktCheckboxGroup label="Romtype">
|
|
361
|
+
<Options />
|
|
362
|
+
</PktCheckboxGroup>,
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
expect(checkbox('enkelt').validity.valid).toBe(true)
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
it('validates again after a form reset', async () => {
|
|
369
|
+
vi.useFakeTimers()
|
|
370
|
+
render(
|
|
371
|
+
<form data-testid="skjema">
|
|
372
|
+
<PktCheckboxGroup label="Romtype" required>
|
|
373
|
+
<Options />
|
|
374
|
+
</PktCheckboxGroup>
|
|
375
|
+
</form>,
|
|
376
|
+
)
|
|
377
|
+
fireEvent.click(checkbox('enkelt'))
|
|
378
|
+
expect(checkbox('dobbelt').validity.valid).toBe(true)
|
|
379
|
+
|
|
380
|
+
act(() => {
|
|
381
|
+
;(screen.getByTestId('skjema') as HTMLFormElement).reset()
|
|
382
|
+
vi.runAllTimers()
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
expect(checkbox('enkelt')).not.toBeChecked()
|
|
386
|
+
expect(checkbox('dobbelt').validity.customError).toBe(true)
|
|
387
|
+
vi.useRealTimers()
|
|
388
|
+
})
|
|
389
|
+
|
|
390
|
+
it('keeps the rule of a required checkbox in a required group', () => {
|
|
391
|
+
render(
|
|
392
|
+
<PktCheckboxGroup label="Vilkår" required>
|
|
393
|
+
<PktCheckbox id="vilkar" value="vilkar" label="Vilkår" required />
|
|
394
|
+
<PktCheckbox id="nyhetsbrev" value="nyhetsbrev" label="Nyhetsbrev" />
|
|
395
|
+
</PktCheckboxGroup>,
|
|
396
|
+
)
|
|
397
|
+
|
|
398
|
+
fireEvent.click(checkbox('nyhetsbrev'))
|
|
399
|
+
|
|
400
|
+
expect(checkbox('nyhetsbrev').validity.valid).toBe(true)
|
|
401
|
+
expect(checkbox('vilkar').validity.valueMissing).toBe(true)
|
|
402
|
+
})
|
|
403
|
+
|
|
404
|
+
it('leaves the checkboxes valid when the group is not required', () => {
|
|
405
|
+
render(
|
|
406
|
+
<PktCheckboxGroup label="Romtype">
|
|
407
|
+
<Options />
|
|
408
|
+
</PktCheckboxGroup>,
|
|
409
|
+
)
|
|
410
|
+
|
|
411
|
+
expect(checkbox('enkelt').validity.valid).toBe(true)
|
|
412
|
+
})
|
|
413
|
+
})
|
|
414
|
+
|
|
415
|
+
describe('disabled', () => {
|
|
416
|
+
it('disables the fieldset and every checkbox', () => {
|
|
417
|
+
const { container } = render(
|
|
418
|
+
<PktCheckboxGroup label="Romtype" disabled>
|
|
419
|
+
<Options />
|
|
420
|
+
</PktCheckboxGroup>,
|
|
421
|
+
)
|
|
422
|
+
|
|
423
|
+
expect(container.querySelector('fieldset')).toBeDisabled()
|
|
424
|
+
expect(checkbox('enkelt')).toBeDisabled()
|
|
425
|
+
})
|
|
426
|
+
})
|
|
427
|
+
|
|
428
|
+
describe('a text field for an other option', () => {
|
|
429
|
+
const OtherForm = (props: Partial<IPktCheckboxGroup>) => {
|
|
430
|
+
const [value, setValue] = useState<string[]>([])
|
|
431
|
+
return (
|
|
432
|
+
<form data-testid="skjema">
|
|
433
|
+
<PktCheckboxGroup name="varsling" label="Varsling" value={value} onValueChange={setValue} {...props}>
|
|
434
|
+
<PktCheckbox id="epost" value="epost" label="E-post" />
|
|
435
|
+
<PktCheckbox id="annet" value="annet" label="Annet" />
|
|
436
|
+
{value.includes('annet') && <PktTextinput id="annet-tekst" name="varsling-annet" label="Hva annet?" />}
|
|
437
|
+
</PktCheckboxGroup>
|
|
438
|
+
<button>Utenfor</button>
|
|
439
|
+
</form>
|
|
440
|
+
)
|
|
441
|
+
}
|
|
442
|
+
const field = () => document.getElementById('annet-tekst') as HTMLInputElement | null
|
|
443
|
+
|
|
444
|
+
it('shows the text field inside the group while the other option is checked', () => {
|
|
445
|
+
render(<OtherForm />)
|
|
446
|
+
|
|
447
|
+
fireEvent.click(checkbox('annet'))
|
|
448
|
+
expect(field()?.closest('.pkt-inputwrapper__options')).toBeInTheDocument()
|
|
449
|
+
|
|
450
|
+
fireEvent.click(checkbox('annet'))
|
|
451
|
+
expect(field()).toBeNull()
|
|
452
|
+
})
|
|
453
|
+
|
|
454
|
+
it('keeps the text field out of the group value, handlers and validation', () => {
|
|
455
|
+
const onChange = vi.fn()
|
|
456
|
+
const onBlur = vi.fn()
|
|
457
|
+
render(<OtherForm onChange={onChange} onBlur={onBlur} required />)
|
|
458
|
+
fireEvent.click(checkbox('annet'))
|
|
459
|
+
|
|
460
|
+
fireEvent.blur(checkbox('annet'), { relatedTarget: field() })
|
|
461
|
+
fireEvent.change(field()!, { target: { value: 'Brevdue' } })
|
|
462
|
+
|
|
463
|
+
expect(onChange).toHaveBeenCalledTimes(1)
|
|
464
|
+
expect(onBlur).not.toHaveBeenCalled()
|
|
465
|
+
expect(field()!.validity.customError).toBe(false)
|
|
466
|
+
expect(Array.from(new FormData(screen.getByTestId('skjema')).entries())).toEqual([
|
|
467
|
+
['varsling', 'annet'],
|
|
468
|
+
['varsling-annet', 'Brevdue'],
|
|
469
|
+
])
|
|
470
|
+
})
|
|
471
|
+
|
|
472
|
+
it('calls onBlur when focus leaves the group from the text field', () => {
|
|
473
|
+
const onBlur = vi.fn()
|
|
474
|
+
render(<OtherForm onBlur={onBlur} />)
|
|
475
|
+
fireEvent.click(checkbox('annet'))
|
|
476
|
+
|
|
477
|
+
fireEvent.blur(field()!, { relatedTarget: screen.getByRole('button', { name: 'Utenfor' }) })
|
|
478
|
+
|
|
479
|
+
expect(onBlur).toHaveBeenCalledTimes(1)
|
|
480
|
+
expect(onBlur.mock.calls[0][0].target.name).toBe('varsling')
|
|
481
|
+
})
|
|
482
|
+
})
|
|
483
|
+
|
|
484
|
+
describe('accessibility', () => {
|
|
485
|
+
it('has no axe violations', async () => {
|
|
486
|
+
const { container } = render(
|
|
487
|
+
<PktCheckboxGroup id="rom" label="Romtype" helptext="Velg alle som passer" required>
|
|
488
|
+
<Options />
|
|
489
|
+
</PktCheckboxGroup>,
|
|
490
|
+
)
|
|
491
|
+
|
|
492
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
493
|
+
})
|
|
494
|
+
})
|
|
495
|
+
})
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type ChangeEvent,
|
|
5
|
+
type FocusEvent,
|
|
6
|
+
type ForwardedRef,
|
|
7
|
+
forwardRef,
|
|
8
|
+
type ReactNode,
|
|
9
|
+
useEffect,
|
|
10
|
+
useMemo,
|
|
11
|
+
useRef,
|
|
12
|
+
} from 'react'
|
|
13
|
+
import { OPTION_GROUP_DEFAULTS, type IPktCheckboxGroupBase } from 'shared-types'
|
|
14
|
+
import { useGeneratedId } from '../../hooks/useGeneratedId'
|
|
15
|
+
import { isActiveOption, type TPktOptionGroupElement, useOptionGroupField } from '../../hooks/useOptionGroupField'
|
|
16
|
+
import { usePktStrings } from '../../hooks/usePktStrings'
|
|
17
|
+
import { PktInputWrapper } from '../inputwrapper/InputWrapper'
|
|
18
|
+
import { CheckboxGroupContext, type ICheckboxGroupContext } from './CheckboxGroupContext'
|
|
19
|
+
|
|
20
|
+
export type TPktCheckboxGroupElement = TPktOptionGroupElement<string[]>
|
|
21
|
+
|
|
22
|
+
export interface IPktCheckboxGroup extends Omit<
|
|
23
|
+
IPktCheckboxGroupBase,
|
|
24
|
+
'label' | 'helptext' | 'helptextDropdown' | 'errorMessage'
|
|
25
|
+
> {
|
|
26
|
+
label: string
|
|
27
|
+
helptext?: string | ReactNode
|
|
28
|
+
helptextDropdown?: string | ReactNode
|
|
29
|
+
errorMessage?: string | ReactNode
|
|
30
|
+
className?: string
|
|
31
|
+
children?: ReactNode
|
|
32
|
+
onChange?: (event: ChangeEvent<TPktCheckboxGroupElement>) => void
|
|
33
|
+
onValueChange?: (value: string[]) => void
|
|
34
|
+
onFocus?: (event: FocusEvent<TPktCheckboxGroupElement>) => void
|
|
35
|
+
onBlur?: (event: FocusEvent<TPktCheckboxGroupElement>) => void
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const PktCheckboxGroup = forwardRef(
|
|
39
|
+
(
|
|
40
|
+
{
|
|
41
|
+
id: idProp,
|
|
42
|
+
name,
|
|
43
|
+
label,
|
|
44
|
+
helptext,
|
|
45
|
+
helptextDropdown,
|
|
46
|
+
helptextDropdownButton,
|
|
47
|
+
value,
|
|
48
|
+
defaultValue,
|
|
49
|
+
layout = OPTION_GROUP_DEFAULTS.layout,
|
|
50
|
+
inputSize = OPTION_GROUP_DEFAULTS.inputSize,
|
|
51
|
+
hasTile = OPTION_GROUP_DEFAULTS.hasTile,
|
|
52
|
+
required = OPTION_GROUP_DEFAULTS.required,
|
|
53
|
+
disabled = OPTION_GROUP_DEFAULTS.disabled,
|
|
54
|
+
hasError = OPTION_GROUP_DEFAULTS.hasError,
|
|
55
|
+
errorMessage,
|
|
56
|
+
useWrapper = OPTION_GROUP_DEFAULTS.useWrapper,
|
|
57
|
+
optionalTag = OPTION_GROUP_DEFAULTS.optionalTag,
|
|
58
|
+
optionalText,
|
|
59
|
+
requiredTag = OPTION_GROUP_DEFAULTS.requiredTag,
|
|
60
|
+
requiredText,
|
|
61
|
+
tagText,
|
|
62
|
+
strings,
|
|
63
|
+
className,
|
|
64
|
+
children,
|
|
65
|
+
onChange,
|
|
66
|
+
onValueChange,
|
|
67
|
+
onFocus,
|
|
68
|
+
onBlur,
|
|
69
|
+
}: IPktCheckboxGroup,
|
|
70
|
+
ref: ForwardedRef<TPktCheckboxGroupElement>,
|
|
71
|
+
) => {
|
|
72
|
+
const id = useGeneratedId(idProp)
|
|
73
|
+
const groupName = name || id
|
|
74
|
+
const s = usePktStrings(['validation'], strings)
|
|
75
|
+
const valueMissingMessage = s.validation.checkboxGroupValueMissing
|
|
76
|
+
const validityAppliedRef = useRef(false)
|
|
77
|
+
|
|
78
|
+
const field = useOptionGroupField<string[]>({
|
|
79
|
+
type: 'checkbox',
|
|
80
|
+
name: groupName,
|
|
81
|
+
forwardedRef: ref,
|
|
82
|
+
onChange,
|
|
83
|
+
onValueChange,
|
|
84
|
+
onFocus,
|
|
85
|
+
onBlur,
|
|
86
|
+
onValueSet: () => syncValidity(),
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
const syncValidity = () => {
|
|
90
|
+
if (!required && !validityAppliedRef.current) return
|
|
91
|
+
validityAppliedRef.current = required
|
|
92
|
+
const inputs = field.options()
|
|
93
|
+
const missing = required && !inputs.some((input) => input.checked && isActiveOption(input))
|
|
94
|
+
inputs.forEach((input) => input.setCustomValidity(missing ? valueMissingMessage : ''))
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
syncValidity()
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
const form = field.options()[0]?.form
|
|
103
|
+
if (!form) return
|
|
104
|
+
let timeout: ReturnType<typeof setTimeout> | undefined
|
|
105
|
+
const handleReset = () => {
|
|
106
|
+
timeout = setTimeout(syncValidity)
|
|
107
|
+
}
|
|
108
|
+
form.addEventListener('reset', handleReset)
|
|
109
|
+
return () => {
|
|
110
|
+
clearTimeout(timeout)
|
|
111
|
+
form.removeEventListener('reset', handleReset)
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
const context = useMemo<ICheckboxGroupContext>(
|
|
116
|
+
() => ({
|
|
117
|
+
name: groupName,
|
|
118
|
+
hasTile,
|
|
119
|
+
hasError,
|
|
120
|
+
disabled,
|
|
121
|
+
value,
|
|
122
|
+
defaultValue,
|
|
123
|
+
onOptionChange: field.onOptionChange,
|
|
124
|
+
}),
|
|
125
|
+
[groupName, hasTile, hasError, disabled, value, defaultValue, field],
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<CheckboxGroupContext.Provider value={context}>
|
|
130
|
+
<PktInputWrapper
|
|
131
|
+
ref={field.mergedRef}
|
|
132
|
+
onFocus={field.onRootFocus}
|
|
133
|
+
onBlur={field.onRootBlur}
|
|
134
|
+
id={idProp}
|
|
135
|
+
forId={id}
|
|
136
|
+
className={className}
|
|
137
|
+
label={label}
|
|
138
|
+
helptext={helptext}
|
|
139
|
+
helptextDropdown={helptextDropdown}
|
|
140
|
+
helptextDropdownButton={helptextDropdownButton}
|
|
141
|
+
hasError={hasError}
|
|
142
|
+
errorMessage={errorMessage}
|
|
143
|
+
disabled={disabled}
|
|
144
|
+
optionalTag={optionalTag}
|
|
145
|
+
optionalText={optionalText}
|
|
146
|
+
requiredTag={requiredTag}
|
|
147
|
+
requiredText={requiredText}
|
|
148
|
+
tagText={tagText}
|
|
149
|
+
useWrapper={useWrapper}
|
|
150
|
+
hasFieldset
|
|
151
|
+
layout={layout}
|
|
152
|
+
size={inputSize}
|
|
153
|
+
strings={strings}
|
|
154
|
+
>
|
|
155
|
+
{children}
|
|
156
|
+
</PktInputWrapper>
|
|
157
|
+
</CheckboxGroupContext.Provider>
|
|
158
|
+
)
|
|
159
|
+
},
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
PktCheckboxGroup.displayName = 'PktCheckboxGroup'
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ChangeEvent, createContext, useContext } from 'react'
|
|
2
|
+
|
|
3
|
+
export interface ICheckboxGroupContext {
|
|
4
|
+
name: string
|
|
5
|
+
hasTile: boolean
|
|
6
|
+
hasError: boolean
|
|
7
|
+
disabled: boolean
|
|
8
|
+
value: string[] | undefined
|
|
9
|
+
defaultValue: string[] | undefined
|
|
10
|
+
onOptionChange: (event: ChangeEvent<HTMLInputElement>) => void
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const CheckboxGroupContext = createContext<ICheckboxGroupContext | null>(null)
|
|
14
|
+
|
|
15
|
+
export const useCheckboxGroupContext = () => useContext(CheckboxGroupContext)
|
package/src/components/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ export { PktButton } from './button/Button'
|
|
|
7
7
|
export { PktCalendar } from './calendar/Calendar'
|
|
8
8
|
export { PktCard } from './card/Card'
|
|
9
9
|
export { PktCheckbox } from './checkbox/Checkbox'
|
|
10
|
+
export { PktCheckboxGroup } from './checkboxgroup/CheckboxGroup'
|
|
10
11
|
export { PktCombobox } from './combobox/Combobox'
|
|
11
12
|
export { PktConsent } from './consent/Consent'
|
|
12
13
|
export { PktDatepicker } from './datepicker/Datepicker'
|
|
@@ -32,6 +33,7 @@ export { PktMessagebox } from './messagebox/Messagebox'
|
|
|
32
33
|
export { PktModal } from './modal/Modal'
|
|
33
34
|
export { PktProgressbar } from './progressbar/Progressbar'
|
|
34
35
|
export { PktRadioButton } from './radio/RadioButton'
|
|
36
|
+
export { PktRadioGroup } from './radiogroup/RadioGroup'
|
|
35
37
|
export { PktSearchInput } from './searchinput/SearchInput'
|
|
36
38
|
export type { PktSearchInputSuggestion } from './searchinput/SearchInput'
|
|
37
39
|
export { PktSelect } from './select/Select'
|
|
@@ -54,11 +56,5 @@ export * from './interfaces'
|
|
|
54
56
|
export * from './types'
|
|
55
57
|
export { PktStringsProvider } from './strings/PktStringsProvider'
|
|
56
58
|
export type { IPktStringsProvider } from './strings/PktStringsProvider'
|
|
57
|
-
export {
|
|
58
|
-
getPktStrings,
|
|
59
|
-
nb as pktStringsNb,
|
|
60
|
-
resetPktStrings,
|
|
61
|
-
setPktStrings,
|
|
62
|
-
subscribePktStrings,
|
|
63
|
-
} from 'shared-strings'
|
|
59
|
+
export { getPktStrings, nb as pktStringsNb, resetPktStrings, setPktStrings, subscribePktStrings } from 'shared-strings'
|
|
64
60
|
export type { TPktStrings, TPktStringsNamespace, TPktStringsOverride, IPktStringsProps } from 'shared-strings'
|