@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,472 @@
|
|
|
1
|
+
import '@testing-library/jest-dom'
|
|
2
|
+
|
|
3
|
+
import { 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 { PktRadioButton } from '../radio/RadioButton'
|
|
9
|
+
import { PktTextinput } from '../textinput/Textinput'
|
|
10
|
+
import { type IPktRadioGroup, PktRadioGroup, type TPktRadioGroupElement } from './RadioGroup'
|
|
11
|
+
|
|
12
|
+
expect.extend(toHaveNoViolations)
|
|
13
|
+
|
|
14
|
+
const Options = () => (
|
|
15
|
+
<>
|
|
16
|
+
<PktRadioButton id="buss" value="buss" label="Buss" />
|
|
17
|
+
<PktRadioButton id="tog" value="tog" label="Tog" />
|
|
18
|
+
</>
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
const radio = (id: string) => document.getElementById(id) as HTMLInputElement
|
|
22
|
+
|
|
23
|
+
const ControlledGroup = (props: Partial<IPktRadioGroup> & { initial?: string }) => {
|
|
24
|
+
const { initial = '', ...rest } = props
|
|
25
|
+
const [value, setValue] = useState(initial)
|
|
26
|
+
return (
|
|
27
|
+
<>
|
|
28
|
+
<PktRadioGroup label="Reisemåte" value={value} onValueChange={setValue} {...rest}>
|
|
29
|
+
<Options />
|
|
30
|
+
</PktRadioGroup>
|
|
31
|
+
<output data-testid="value">{value}</output>
|
|
32
|
+
</>
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe('PktRadioGroup', () => {
|
|
37
|
+
describe('rendering', () => {
|
|
38
|
+
it('renders the radios inside the fieldset options of an input wrapper', () => {
|
|
39
|
+
const { container } = render(
|
|
40
|
+
<PktRadioGroup id="reise" label="Reisemåte">
|
|
41
|
+
<Options />
|
|
42
|
+
</PktRadioGroup>,
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
const fieldset = container.querySelector('fieldset.pkt-inputwrapper__fieldset')
|
|
46
|
+
expect(fieldset?.querySelector('legend')).toHaveTextContent('Reisemåte')
|
|
47
|
+
const options = fieldset?.querySelector('.pkt-inputwrapper__options')
|
|
48
|
+
expect(options).toContainElement(radio('buss'))
|
|
49
|
+
expect(options).toContainElement(radio('tog'))
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('puts the id on the root element and uses it for the wrapper ids', () => {
|
|
53
|
+
const { container } = render(
|
|
54
|
+
<PktRadioGroup id="reise" label="Reisemåte" helptext="Velg én">
|
|
55
|
+
<Options />
|
|
56
|
+
</PktRadioGroup>,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
expect(container.firstElementChild).toHaveAttribute('id', 'reise')
|
|
60
|
+
expect(container.querySelector('legend')).toHaveAttribute('id', 'reise-label')
|
|
61
|
+
expect(container.querySelector('fieldset')).toHaveAttribute('aria-describedby', 'reise-helptext')
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('passes layout and size to the input wrapper', () => {
|
|
65
|
+
const { container } = render(
|
|
66
|
+
<PktRadioGroup label="Reisemåte" layout="horizontal" inputSize="small">
|
|
67
|
+
<Options />
|
|
68
|
+
</PktRadioGroup>,
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
expect(container.firstElementChild).toHaveClass('pkt-inputwrapper--small')
|
|
72
|
+
expect(container.querySelector('.pkt-inputwrapper__options')).toHaveClass('pkt-inputwrapper__options--horizontal')
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('forwards the ref to the root element', () => {
|
|
76
|
+
const ref = createRef<TPktRadioGroupElement>()
|
|
77
|
+
const { container } = render(
|
|
78
|
+
<PktRadioGroup ref={ref} label="Reisemåte">
|
|
79
|
+
<Options />
|
|
80
|
+
</PktRadioGroup>,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
expect(ref.current).toBe(container.firstElementChild)
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
describe('the group element is the form field', () => {
|
|
88
|
+
const mount = (props: Partial<IPktRadioGroup> = {}) => {
|
|
89
|
+
const ref = createRef<TPktRadioGroupElement>()
|
|
90
|
+
render(
|
|
91
|
+
<PktRadioGroup ref={ref} name="reise" label="Reisemåte" {...props}>
|
|
92
|
+
<Options />
|
|
93
|
+
</PktRadioGroup>,
|
|
94
|
+
)
|
|
95
|
+
return ref.current!
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
it('has the group name', () => {
|
|
99
|
+
expect(mount().name).toBe('reise')
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('reads the value of the checked radio', () => {
|
|
103
|
+
const field = mount({ defaultValue: 'tog' })
|
|
104
|
+
|
|
105
|
+
expect(field.value).toBe('tog')
|
|
106
|
+
fireEvent.click(radio('buss'))
|
|
107
|
+
expect(field.value).toBe('buss')
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('checks the matching radio when the value is written, without calling the handlers', () => {
|
|
111
|
+
const onChange = vi.fn()
|
|
112
|
+
const field = mount({ onChange })
|
|
113
|
+
|
|
114
|
+
field.value = 'tog'
|
|
115
|
+
|
|
116
|
+
expect(radio('tog')).toBeChecked()
|
|
117
|
+
expect(field.value).toBe('tog')
|
|
118
|
+
field.value = ''
|
|
119
|
+
expect(radio('tog')).not.toBeChecked()
|
|
120
|
+
expect(onChange).not.toHaveBeenCalled()
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it('moves focus to the checked radio, or the first one', () => {
|
|
124
|
+
const field = mount()
|
|
125
|
+
|
|
126
|
+
field.focus()
|
|
127
|
+
expect(radio('buss')).toHaveFocus()
|
|
128
|
+
|
|
129
|
+
fireEvent.click(radio('tog'))
|
|
130
|
+
field.focus()
|
|
131
|
+
expect(radio('tog')).toHaveFocus()
|
|
132
|
+
})
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
describe('name', () => {
|
|
136
|
+
it('gives radios without a name the group name', () => {
|
|
137
|
+
render(
|
|
138
|
+
<PktRadioGroup name="reise" label="Reisemåte">
|
|
139
|
+
<Options />
|
|
140
|
+
</PktRadioGroup>,
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
expect(radio('buss')).toHaveAttribute('name', 'reise')
|
|
144
|
+
expect(radio('tog')).toHaveAttribute('name', 'reise')
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('falls back to the group id, then to a generated name', () => {
|
|
148
|
+
const { unmount } = render(
|
|
149
|
+
<PktRadioGroup id="reise" label="Reisemåte">
|
|
150
|
+
<Options />
|
|
151
|
+
</PktRadioGroup>,
|
|
152
|
+
)
|
|
153
|
+
expect(radio('buss')).toHaveAttribute('name', 'reise')
|
|
154
|
+
unmount()
|
|
155
|
+
|
|
156
|
+
render(
|
|
157
|
+
<PktRadioGroup label="Reisemåte">
|
|
158
|
+
<Options />
|
|
159
|
+
</PktRadioGroup>,
|
|
160
|
+
)
|
|
161
|
+
const name = radio('buss').getAttribute('name')
|
|
162
|
+
expect(name).toBeTruthy()
|
|
163
|
+
expect(radio('tog')).toHaveAttribute('name', name!)
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('keeps a name set on the radio', () => {
|
|
167
|
+
render(
|
|
168
|
+
<PktRadioGroup name="reise" label="Reisemåte">
|
|
169
|
+
<PktRadioButton id="buss" name="eget" value="buss" label="Buss" />
|
|
170
|
+
</PktRadioGroup>,
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
expect(radio('buss')).toHaveAttribute('name', 'eget')
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
it('renders a radio outside a group without reading any group', () => {
|
|
177
|
+
render(<PktRadioButton id="alene" label="Alene" />)
|
|
178
|
+
|
|
179
|
+
expect(radio('alene')).not.toHaveAttribute('name')
|
|
180
|
+
expect(radio('alene')).not.toHaveAttribute('required')
|
|
181
|
+
})
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
describe('defaults for the radios', () => {
|
|
185
|
+
it('turns on tile and error state for every radio', () => {
|
|
186
|
+
render(
|
|
187
|
+
<PktRadioGroup label="Reisemåte" hasTile hasError>
|
|
188
|
+
<Options />
|
|
189
|
+
</PktRadioGroup>,
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
for (const id of ['buss', 'tog']) {
|
|
193
|
+
expect(radio(id).closest('.pkt-input-check__input')).toHaveClass('pkt-input-check__input--tile')
|
|
194
|
+
expect(radio(id)).toHaveClass('pkt-input-check__input-checkbox--error')
|
|
195
|
+
}
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
it('lets a radio turn on tile when the group does not', () => {
|
|
199
|
+
render(
|
|
200
|
+
<PktRadioGroup label="Reisemåte">
|
|
201
|
+
<PktRadioButton id="buss" value="buss" label="Buss" hasTile />
|
|
202
|
+
<PktRadioButton id="tog" value="tog" label="Tog" />
|
|
203
|
+
</PktRadioGroup>,
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
expect(radio('buss').closest('.pkt-input-check__input')).toHaveClass('pkt-input-check__input--tile')
|
|
207
|
+
expect(radio('tog').closest('.pkt-input-check__input')).not.toHaveClass('pkt-input-check__input--tile')
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
it('passes required on to every radio', () => {
|
|
211
|
+
render(
|
|
212
|
+
<PktRadioGroup label="Reisemåte" required>
|
|
213
|
+
<Options />
|
|
214
|
+
</PktRadioGroup>,
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
expect(radio('buss')).toBeRequired()
|
|
218
|
+
expect(radio('tog')).toBeRequired()
|
|
219
|
+
})
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
describe('value', () => {
|
|
223
|
+
it('checks the radio matching a controlled value', () => {
|
|
224
|
+
render(<ControlledGroup initial="tog" />)
|
|
225
|
+
|
|
226
|
+
expect(radio('tog')).toBeChecked()
|
|
227
|
+
expect(radio('buss')).not.toBeChecked()
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
it('follows the controlled value when the user selects', () => {
|
|
231
|
+
render(<ControlledGroup />)
|
|
232
|
+
|
|
233
|
+
fireEvent.click(radio('buss'))
|
|
234
|
+
|
|
235
|
+
expect(screen.getByTestId('value')).toHaveTextContent('buss')
|
|
236
|
+
expect(radio('buss')).toBeChecked()
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
it('keeps the controlled value when the parent does not update it', () => {
|
|
240
|
+
render(
|
|
241
|
+
<PktRadioGroup label="Reisemåte" value="tog" onValueChange={() => {}}>
|
|
242
|
+
<Options />
|
|
243
|
+
</PktRadioGroup>,
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
fireEvent.click(radio('buss'))
|
|
247
|
+
|
|
248
|
+
expect(radio('tog')).toBeChecked()
|
|
249
|
+
expect(radio('buss')).not.toBeChecked()
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
it('unchecks every radio when the controlled value is an empty string', () => {
|
|
253
|
+
render(<ControlledGroup initial="" />)
|
|
254
|
+
|
|
255
|
+
expect(radio('buss')).not.toBeChecked()
|
|
256
|
+
expect(radio('tog')).not.toBeChecked()
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
it('checks the radio matching defaultValue and lets the user change it', () => {
|
|
260
|
+
render(
|
|
261
|
+
<PktRadioGroup label="Reisemåte" defaultValue="tog">
|
|
262
|
+
<Options />
|
|
263
|
+
</PktRadioGroup>,
|
|
264
|
+
)
|
|
265
|
+
expect(radio('tog')).toBeChecked()
|
|
266
|
+
|
|
267
|
+
fireEvent.click(radio('buss'))
|
|
268
|
+
|
|
269
|
+
expect(radio('buss')).toBeChecked()
|
|
270
|
+
expect(radio('tog')).not.toBeChecked()
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
it('lets defaultChecked on the radio win over defaultValue', () => {
|
|
274
|
+
render(
|
|
275
|
+
<PktRadioGroup label="Reisemåte" defaultValue="tog">
|
|
276
|
+
<PktRadioButton id="buss" value="buss" label="Buss" defaultChecked />
|
|
277
|
+
<PktRadioButton id="tog" value="tog" label="Tog" />
|
|
278
|
+
</PktRadioGroup>,
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
expect(radio('buss')).toBeChecked()
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
it('uses on as the value of a radio without a value', () => {
|
|
285
|
+
const onValueChange = vi.fn()
|
|
286
|
+
render(
|
|
287
|
+
<PktRadioGroup label="Svar" onValueChange={onValueChange}>
|
|
288
|
+
<PktRadioButton id="ja" label="Ja" />
|
|
289
|
+
</PktRadioGroup>,
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
fireEvent.click(radio('ja'))
|
|
293
|
+
|
|
294
|
+
expect(onValueChange).toHaveBeenCalledWith('on')
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
it('warns when a radio sets checked in a controlled group', () => {
|
|
298
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
299
|
+
render(
|
|
300
|
+
<PktRadioGroup label="Reisemåte" value="tog" onValueChange={() => {}}>
|
|
301
|
+
<PktRadioButton id="buss" value="buss" label="Buss" checked />
|
|
302
|
+
<PktRadioButton id="tog" value="tog" label="Tog" />
|
|
303
|
+
</PktRadioGroup>,
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
expect(warn).toHaveBeenCalledWith(expect.stringContaining('PktRadioButton (buss)'))
|
|
307
|
+
expect(radio('buss')).not.toBeChecked()
|
|
308
|
+
warn.mockRestore()
|
|
309
|
+
})
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
describe('events', () => {
|
|
313
|
+
it('calls onChange with the group as target and onValueChange with the value', () => {
|
|
314
|
+
const changes: Array<{ name: string; value: string; type: string }> = []
|
|
315
|
+
const onValueChange = vi.fn()
|
|
316
|
+
const { container } = render(
|
|
317
|
+
<PktRadioGroup
|
|
318
|
+
name="reise"
|
|
319
|
+
label="Reisemåte"
|
|
320
|
+
onChange={(event) => changes.push({ name: event.target.name, value: event.target.value, type: event.type })}
|
|
321
|
+
onValueChange={onValueChange}
|
|
322
|
+
>
|
|
323
|
+
<Options />
|
|
324
|
+
</PktRadioGroup>,
|
|
325
|
+
)
|
|
326
|
+
|
|
327
|
+
fireEvent.click(radio('tog'))
|
|
328
|
+
|
|
329
|
+
expect(changes).toEqual([{ name: 'reise', value: 'tog', type: 'change' }])
|
|
330
|
+
expect(onValueChange).toHaveBeenCalledWith('tog')
|
|
331
|
+
expect(container.firstElementChild).toHaveProperty('value', 'tog')
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
it('calls onFocus when focus enters and onBlur when it leaves the group, not between radios', () => {
|
|
335
|
+
const onFocus = vi.fn()
|
|
336
|
+
const onBlur = vi.fn()
|
|
337
|
+
render(
|
|
338
|
+
<>
|
|
339
|
+
<PktRadioGroup name="reise" label="Reisemåte" onFocus={onFocus} onBlur={onBlur}>
|
|
340
|
+
<Options />
|
|
341
|
+
</PktRadioGroup>
|
|
342
|
+
<button>Utenfor</button>
|
|
343
|
+
</>,
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
fireEvent.focus(radio('buss'))
|
|
347
|
+
fireEvent.blur(radio('buss'), { relatedTarget: radio('tog') })
|
|
348
|
+
fireEvent.focus(radio('tog'), { relatedTarget: radio('buss') })
|
|
349
|
+
fireEvent.blur(radio('tog'), { relatedTarget: screen.getByRole('button') })
|
|
350
|
+
|
|
351
|
+
expect(onFocus).toHaveBeenCalledTimes(1)
|
|
352
|
+
expect(onBlur).toHaveBeenCalledTimes(1)
|
|
353
|
+
expect(onBlur.mock.calls[0][0].target.name).toBe('reise')
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
it('still calls onChange on the radio itself', () => {
|
|
357
|
+
const onRadioChange = vi.fn()
|
|
358
|
+
const onGroupChange = vi.fn()
|
|
359
|
+
render(
|
|
360
|
+
<PktRadioGroup label="Reisemåte" onChange={onGroupChange}>
|
|
361
|
+
<PktRadioButton id="buss" value="buss" label="Buss" onChange={onRadioChange} />
|
|
362
|
+
</PktRadioGroup>,
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
fireEvent.click(radio('buss'))
|
|
366
|
+
|
|
367
|
+
expect(onRadioChange).toHaveBeenCalledTimes(1)
|
|
368
|
+
expect(onGroupChange).toHaveBeenCalledTimes(1)
|
|
369
|
+
})
|
|
370
|
+
|
|
371
|
+
it('does not call the handlers when the value is changed from code', () => {
|
|
372
|
+
const onValueChange = vi.fn()
|
|
373
|
+
const { rerender } = render(
|
|
374
|
+
<PktRadioGroup label="Reisemåte" value="buss" onValueChange={onValueChange}>
|
|
375
|
+
<Options />
|
|
376
|
+
</PktRadioGroup>,
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
rerender(
|
|
380
|
+
<PktRadioGroup label="Reisemåte" value="tog" onValueChange={onValueChange}>
|
|
381
|
+
<Options />
|
|
382
|
+
</PktRadioGroup>,
|
|
383
|
+
)
|
|
384
|
+
|
|
385
|
+
expect(radio('tog')).toBeChecked()
|
|
386
|
+
expect(onValueChange).not.toHaveBeenCalled()
|
|
387
|
+
})
|
|
388
|
+
})
|
|
389
|
+
|
|
390
|
+
describe('disabled', () => {
|
|
391
|
+
it('disables the fieldset and marks every radio as disabled', () => {
|
|
392
|
+
const { container } = render(
|
|
393
|
+
<PktRadioGroup label="Reisemåte" disabled hasTile>
|
|
394
|
+
<Options />
|
|
395
|
+
</PktRadioGroup>,
|
|
396
|
+
)
|
|
397
|
+
|
|
398
|
+
expect(container.querySelector('fieldset')).toBeDisabled()
|
|
399
|
+
expect(container.firstElementChild).toHaveClass('pkt-inputwrapper--disabled')
|
|
400
|
+
expect(radio('buss')).toBeDisabled()
|
|
401
|
+
expect(radio('buss').closest('.pkt-input-check__input')).toHaveClass('pkt-input-check__input--tile-disabled')
|
|
402
|
+
})
|
|
403
|
+
})
|
|
404
|
+
|
|
405
|
+
describe('a text field for an other option', () => {
|
|
406
|
+
const OtherForm = (props: Partial<IPktRadioGroup>) => {
|
|
407
|
+
const [value, setValue] = useState('')
|
|
408
|
+
return (
|
|
409
|
+
<form data-testid="skjema">
|
|
410
|
+
<PktRadioGroup name="reise" label="Reisemåte" value={value} onValueChange={setValue} {...props}>
|
|
411
|
+
<PktRadioButton id="buss" value="buss" label="Buss" />
|
|
412
|
+
<PktRadioButton id="annet" value="annet" label="Annet" />
|
|
413
|
+
{value === 'annet' && <PktTextinput id="annet-tekst" name="reise-annet" label="Hva annet?" />}
|
|
414
|
+
</PktRadioGroup>
|
|
415
|
+
<button>Utenfor</button>
|
|
416
|
+
</form>
|
|
417
|
+
)
|
|
418
|
+
}
|
|
419
|
+
const field = () => document.getElementById('annet-tekst') as HTMLInputElement | null
|
|
420
|
+
|
|
421
|
+
it('shows the text field inside the group when the other option is chosen', () => {
|
|
422
|
+
render(<OtherForm />)
|
|
423
|
+
expect(field()).toBeNull()
|
|
424
|
+
|
|
425
|
+
fireEvent.click(radio('annet'))
|
|
426
|
+
|
|
427
|
+
expect(field()?.closest('.pkt-inputwrapper__options')).toBeInTheDocument()
|
|
428
|
+
fireEvent.click(radio('buss'))
|
|
429
|
+
expect(field()).toBeNull()
|
|
430
|
+
})
|
|
431
|
+
|
|
432
|
+
it('keeps the text field out of the group value and handlers', () => {
|
|
433
|
+
const onChange = vi.fn()
|
|
434
|
+
const onBlur = vi.fn()
|
|
435
|
+
render(<OtherForm onChange={onChange} onBlur={onBlur} />)
|
|
436
|
+
fireEvent.click(radio('annet'))
|
|
437
|
+
|
|
438
|
+
fireEvent.blur(radio('annet'), { relatedTarget: field() })
|
|
439
|
+
fireEvent.change(field()!, { target: { value: 'Ferge' } })
|
|
440
|
+
|
|
441
|
+
expect(onChange).toHaveBeenCalledTimes(1)
|
|
442
|
+
expect(onBlur).not.toHaveBeenCalled()
|
|
443
|
+
expect(Array.from(new FormData(screen.getByTestId('skjema')).entries())).toEqual([
|
|
444
|
+
['reise', 'annet'],
|
|
445
|
+
['reise-annet', 'Ferge'],
|
|
446
|
+
])
|
|
447
|
+
})
|
|
448
|
+
|
|
449
|
+
it('calls onBlur when focus leaves the group from the text field', () => {
|
|
450
|
+
const onBlur = vi.fn()
|
|
451
|
+
render(<OtherForm onBlur={onBlur} />)
|
|
452
|
+
fireEvent.click(radio('annet'))
|
|
453
|
+
|
|
454
|
+
fireEvent.blur(field()!, { relatedTarget: screen.getByRole('button', { name: 'Utenfor' }) })
|
|
455
|
+
|
|
456
|
+
expect(onBlur).toHaveBeenCalledTimes(1)
|
|
457
|
+
expect(onBlur.mock.calls[0][0].target.name).toBe('reise')
|
|
458
|
+
})
|
|
459
|
+
})
|
|
460
|
+
|
|
461
|
+
describe('accessibility', () => {
|
|
462
|
+
it('has no axe violations', async () => {
|
|
463
|
+
const { container } = render(
|
|
464
|
+
<PktRadioGroup id="reise" label="Reisemåte" helptext="Velg én" required>
|
|
465
|
+
<Options />
|
|
466
|
+
</PktRadioGroup>,
|
|
467
|
+
)
|
|
468
|
+
|
|
469
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
470
|
+
})
|
|
471
|
+
})
|
|
472
|
+
})
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { type ChangeEvent, type FocusEvent, type ForwardedRef, forwardRef, type ReactNode, useMemo } from 'react'
|
|
4
|
+
import { OPTION_GROUP_DEFAULTS, type IPktRadioGroupBase } from 'shared-types'
|
|
5
|
+
import { useGeneratedId } from '../../hooks/useGeneratedId'
|
|
6
|
+
import { type TPktOptionGroupElement, useOptionGroupField } from '../../hooks/useOptionGroupField'
|
|
7
|
+
import { PktInputWrapper } from '../inputwrapper/InputWrapper'
|
|
8
|
+
import { type IRadioGroupContext, RadioGroupContext } from './RadioGroupContext'
|
|
9
|
+
|
|
10
|
+
export type TPktRadioGroupElement = TPktOptionGroupElement<string>
|
|
11
|
+
|
|
12
|
+
export interface IPktRadioGroup extends Omit<
|
|
13
|
+
IPktRadioGroupBase,
|
|
14
|
+
'label' | 'helptext' | 'helptextDropdown' | 'errorMessage'
|
|
15
|
+
> {
|
|
16
|
+
label: string
|
|
17
|
+
helptext?: string | ReactNode
|
|
18
|
+
helptextDropdown?: string | ReactNode
|
|
19
|
+
errorMessage?: string | ReactNode
|
|
20
|
+
className?: string
|
|
21
|
+
children?: ReactNode
|
|
22
|
+
onChange?: (event: ChangeEvent<TPktRadioGroupElement>) => void
|
|
23
|
+
onValueChange?: (value: string) => void
|
|
24
|
+
onFocus?: (event: FocusEvent<TPktRadioGroupElement>) => void
|
|
25
|
+
onBlur?: (event: FocusEvent<TPktRadioGroupElement>) => void
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const PktRadioGroup = forwardRef(
|
|
29
|
+
(
|
|
30
|
+
{
|
|
31
|
+
id: idProp,
|
|
32
|
+
name,
|
|
33
|
+
label,
|
|
34
|
+
helptext,
|
|
35
|
+
helptextDropdown,
|
|
36
|
+
helptextDropdownButton,
|
|
37
|
+
value,
|
|
38
|
+
defaultValue,
|
|
39
|
+
layout = OPTION_GROUP_DEFAULTS.layout,
|
|
40
|
+
inputSize = OPTION_GROUP_DEFAULTS.inputSize,
|
|
41
|
+
hasTile = OPTION_GROUP_DEFAULTS.hasTile,
|
|
42
|
+
required = OPTION_GROUP_DEFAULTS.required,
|
|
43
|
+
disabled = OPTION_GROUP_DEFAULTS.disabled,
|
|
44
|
+
hasError = OPTION_GROUP_DEFAULTS.hasError,
|
|
45
|
+
errorMessage,
|
|
46
|
+
useWrapper = OPTION_GROUP_DEFAULTS.useWrapper,
|
|
47
|
+
optionalTag = OPTION_GROUP_DEFAULTS.optionalTag,
|
|
48
|
+
optionalText,
|
|
49
|
+
requiredTag = OPTION_GROUP_DEFAULTS.requiredTag,
|
|
50
|
+
requiredText,
|
|
51
|
+
tagText,
|
|
52
|
+
strings,
|
|
53
|
+
className,
|
|
54
|
+
children,
|
|
55
|
+
onChange,
|
|
56
|
+
onValueChange,
|
|
57
|
+
onFocus,
|
|
58
|
+
onBlur,
|
|
59
|
+
}: IPktRadioGroup,
|
|
60
|
+
ref: ForwardedRef<TPktRadioGroupElement>,
|
|
61
|
+
) => {
|
|
62
|
+
const id = useGeneratedId(idProp)
|
|
63
|
+
const groupName = name || id
|
|
64
|
+
|
|
65
|
+
const field = useOptionGroupField<string>({
|
|
66
|
+
type: 'radio',
|
|
67
|
+
name: groupName,
|
|
68
|
+
forwardedRef: ref,
|
|
69
|
+
onChange,
|
|
70
|
+
onValueChange,
|
|
71
|
+
onFocus,
|
|
72
|
+
onBlur,
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
const context = useMemo<IRadioGroupContext>(
|
|
76
|
+
() => ({
|
|
77
|
+
name: groupName,
|
|
78
|
+
hasTile,
|
|
79
|
+
hasError,
|
|
80
|
+
disabled,
|
|
81
|
+
required,
|
|
82
|
+
value,
|
|
83
|
+
defaultValue,
|
|
84
|
+
onOptionChange: field.onOptionChange,
|
|
85
|
+
}),
|
|
86
|
+
[groupName, hasTile, hasError, disabled, required, value, defaultValue, field],
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<RadioGroupContext.Provider value={context}>
|
|
91
|
+
<PktInputWrapper
|
|
92
|
+
ref={field.mergedRef}
|
|
93
|
+
onFocus={field.onRootFocus}
|
|
94
|
+
onBlur={field.onRootBlur}
|
|
95
|
+
id={idProp}
|
|
96
|
+
forId={id}
|
|
97
|
+
className={className}
|
|
98
|
+
label={label}
|
|
99
|
+
helptext={helptext}
|
|
100
|
+
helptextDropdown={helptextDropdown}
|
|
101
|
+
helptextDropdownButton={helptextDropdownButton}
|
|
102
|
+
hasError={hasError}
|
|
103
|
+
errorMessage={errorMessage}
|
|
104
|
+
disabled={disabled}
|
|
105
|
+
optionalTag={optionalTag}
|
|
106
|
+
optionalText={optionalText}
|
|
107
|
+
requiredTag={requiredTag}
|
|
108
|
+
requiredText={requiredText}
|
|
109
|
+
tagText={tagText}
|
|
110
|
+
useWrapper={useWrapper}
|
|
111
|
+
hasFieldset
|
|
112
|
+
layout={layout}
|
|
113
|
+
size={inputSize}
|
|
114
|
+
strings={strings}
|
|
115
|
+
>
|
|
116
|
+
{children}
|
|
117
|
+
</PktInputWrapper>
|
|
118
|
+
</RadioGroupContext.Provider>
|
|
119
|
+
)
|
|
120
|
+
},
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
PktRadioGroup.displayName = 'PktRadioGroup'
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type ChangeEvent, createContext, useContext } from 'react'
|
|
2
|
+
|
|
3
|
+
export interface IRadioGroupContext {
|
|
4
|
+
name: string
|
|
5
|
+
hasTile: boolean
|
|
6
|
+
hasError: boolean
|
|
7
|
+
disabled: boolean
|
|
8
|
+
required: boolean
|
|
9
|
+
value: string | undefined
|
|
10
|
+
defaultValue: string | undefined
|
|
11
|
+
onOptionChange: (event: ChangeEvent<HTMLInputElement>) => void
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const RadioGroupContext = createContext<IRadioGroupContext | null>(null)
|
|
15
|
+
|
|
16
|
+
export const useRadioGroupContext = () => useContext(RadioGroupContext)
|