@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
|
@@ -14,6 +14,7 @@ export declare const nb: {
|
|
|
14
14
|
/** Valideringsmeldinger. Nøkkelnavnene følger `ValidityState` der det finnes et tilsvarende flagg. */
|
|
15
15
|
validation: {
|
|
16
16
|
valueMissing: string;
|
|
17
|
+
checkboxGroupValueMissing: string;
|
|
17
18
|
typeMismatch: string;
|
|
18
19
|
patternMismatch: string;
|
|
19
20
|
tooShort: string;
|
|
@@ -9,10 +9,12 @@ export { HEADER_MENU_LOCALES, DEFAULT_HEADER_FOOTER_URL, DEFAULT_HEADER_LOGO_LIN
|
|
|
9
9
|
export type { THeaderMenuLocale, IHeaderMenuLink, IHeaderMenuIconLink, IHeaderMenuButton, IHeaderMenuServices, IHeaderMenuSection, IMegaMenu, IHeaderFooter, IHeaderFooterLocaleData, THeaderFooterApi, IPktHeaderMenu, IPktHeaderGlobal, } from './header-footer';
|
|
10
10
|
export type { IPktFooterLink, IPktFooterColumn, IPktFooterSocialLink, IPktFooterBase, IPktFooterProps, IPktFooterSimpleProps, IFooterSimpleSpecProps, IFooterSpecProps, } from './footer';
|
|
11
11
|
export { DEFAULT_FOOTER_PERSONVERN_URL, DEFAULT_FOOTER_TILGJENGELIGHET_URL } from './footer';
|
|
12
|
-
export { CARD_SKINS, CARD_PADDINGS, CARD_IMAGE_SHAPES, CARD_IMAGE_ASPECTS, CARD_TAG_POSITIONS, CARD_TAG_SKIN_OPTIONS, CARD_DEFAULTS } from './card';
|
|
12
|
+
export { CARD_SKINS, CARD_PADDINGS, CARD_IMAGE_SHAPES, CARD_IMAGE_ASPECTS, CARD_TAG_POSITIONS, CARD_TAG_SKIN_OPTIONS, CARD_DEFAULTS, } from './card';
|
|
13
13
|
export type { TCardSkin, TCardPadding, TCardImageShape, TCardImageAspect, TCardTagPosition, ICardTag, ICardImage, IPktCardBase, ICardSpecProps, } from './card';
|
|
14
14
|
export { CHECKBOX_LABEL_POSITIONS, CHECKBOX_DEFAULTS } from './checkbox';
|
|
15
15
|
export type { TCheckboxLabelPosition, IPktCheckboxBase, ICheckboxSpecProps } from './checkbox';
|
|
16
|
+
export { OPTION_GROUP_DEFAULTS } from './option-group';
|
|
17
|
+
export type { IPktOptionGroupBase, IPktRadioGroupBase, IPktCheckboxGroupBase, IRadioGroupSpecProps, ICheckboxGroupSpecProps, } from './option-group';
|
|
16
18
|
export { CONSENT_TRIGGER_TYPES, CONSENT_TRIGGER_TYPE_OPTIONS, CONSENT_DEFAULTS } from './consent';
|
|
17
19
|
export type { TPktConsentTriggerType, IConsentItem, IConsentValue, ICookieConsentEvent, TConsentDetailsMap, TCookieEventHandler, ICookieEvents, ICookieBannerApi, IPktConsentBase, IConsentSpecProps, } from './consent';
|
|
18
20
|
export { ICON_DEFAULT_PATH, ICON_SIZE_CLASSES, ICON_DEFAULTS } from './icon';
|
|
@@ -48,5 +50,5 @@ export { COMBOBOX_TAG_SKINS, COMBOBOX_DISPLAY_VALUES, COMBOBOX_TAG_PLACEMENTS, C
|
|
|
48
50
|
export type { IPktComboboxOption, TPktComboboxTagSkin, TPktComboboxDisplayValue, TPktComboboxTagPlacement, IPktComboboxBase, IComboboxSpecProps, } from './combobox';
|
|
49
51
|
export type { IPktSelectOption, TSelectOption } from './select';
|
|
50
52
|
export type { TPktSearchInputAppearance, TPktSearchInputMethod, IPktSearchInputSuggestion, IPktSearchInput, } from './searchinput';
|
|
51
|
-
export { UPLOAD_STRATEGIES, FILE_UPLOAD_ITEM_RENDERERS, PREVIEW_UPLOAD_STATUS
|
|
53
|
+
export { UPLOAD_STRATEGIES, FILE_UPLOAD_ITEM_RENDERERS, PREVIEW_UPLOAD_STATUS } from './fileupload';
|
|
52
54
|
export type { TUploadStrategy as TFileUploadStrategy, TFileUploadItemRenderer, TPreviewUploadStatus, TFilesChangedReason, TFileValidator, TTransferProgress, IFileItem, IFileTransfer, IFileValidateDetail, ITransferCancelledDetail, IFilesChangedDetail, IFileUploadSpecProps, IPktFileUploadBase, } from './fileupload';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* option-group.ts
|
|
3
|
+
*
|
|
4
|
+
* Type definitions for pkt-radio-group and pkt-checkbox-group.
|
|
5
|
+
* Used by React, Elements, and typed component specs.
|
|
6
|
+
*/
|
|
7
|
+
import type { IFormFieldCommonProps } from './form-field';
|
|
8
|
+
import type { TLayout } from './layout';
|
|
9
|
+
import type { TPktInputSize } from './size';
|
|
10
|
+
/**
|
|
11
|
+
* Props shared by the radio and checkbox groups. The group renders the input wrapper with a
|
|
12
|
+
* fieldset, so the wrapper props are passed straight through.
|
|
13
|
+
*/
|
|
14
|
+
export interface IPktOptionGroupBase extends IFormFieldCommonProps<'forms' | 'validation'> {
|
|
15
|
+
id?: string;
|
|
16
|
+
name?: string;
|
|
17
|
+
layout?: TLayout;
|
|
18
|
+
inputSize?: TPktInputSize;
|
|
19
|
+
hasTile?: boolean;
|
|
20
|
+
required?: boolean;
|
|
21
|
+
useWrapper?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/** `value` follows `RadioNodeList.value`: the value of the checked radio, `''` when none is. */
|
|
24
|
+
export interface IPktRadioGroupBase extends IPktOptionGroupBase {
|
|
25
|
+
value?: string;
|
|
26
|
+
defaultValue?: string;
|
|
27
|
+
}
|
|
28
|
+
/** `value` follows `FormData.getAll(name)`: checked values in DOM order, disabled ones left out. */
|
|
29
|
+
export interface IPktCheckboxGroupBase extends IPktOptionGroupBase {
|
|
30
|
+
value?: string[];
|
|
31
|
+
defaultValue?: string[];
|
|
32
|
+
}
|
|
33
|
+
type TOptionGroupSpecKeys = 'id' | 'name' | 'label' | 'helptext' | 'helptextDropdown' | 'helptextDropdownButton' | 'value' | 'defaultValue' | 'layout' | 'inputSize' | 'hasTile' | 'required' | 'disabled' | 'hasError' | 'errorMessage' | 'useWrapper' | 'requiredTag' | 'requiredText' | 'optionalTag' | 'optionalText' | 'tagText';
|
|
34
|
+
/** Documented RadioGroup props, used for typed component specs. */
|
|
35
|
+
export type IRadioGroupSpecProps = Pick<IPktRadioGroupBase, TOptionGroupSpecKeys>;
|
|
36
|
+
/** Documented CheckboxGroup props, used for typed component specs. */
|
|
37
|
+
export type ICheckboxGroupSpecProps = Pick<IPktCheckboxGroupBase, TOptionGroupSpecKeys>;
|
|
38
|
+
/** Default values for both groups. Single source of truth for both frameworks. */
|
|
39
|
+
export declare const OPTION_GROUP_DEFAULTS: {
|
|
40
|
+
readonly layout: 'vertical';
|
|
41
|
+
readonly inputSize: 'medium';
|
|
42
|
+
readonly hasTile: false;
|
|
43
|
+
readonly required: false;
|
|
44
|
+
readonly disabled: false;
|
|
45
|
+
readonly hasError: false;
|
|
46
|
+
readonly useWrapper: true;
|
|
47
|
+
readonly requiredTag: false;
|
|
48
|
+
readonly optionalTag: false;
|
|
49
|
+
};
|
|
50
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oslokommune/punkt-react",
|
|
3
|
-
"version": "19.
|
|
3
|
+
"version": "19.9.1",
|
|
4
4
|
"description": "React komponentbibliotek til Punkt, designsystem for Oslo kommune",
|
|
5
5
|
"homepage": "https://punkt.oslo.kommune.no",
|
|
6
6
|
"author": "Team Designsystem, Oslo kommune",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"@eslint/eslintrc": "^3.3.5",
|
|
42
42
|
"@eslint/js": "^10.0.1",
|
|
43
43
|
"@oslokommune/punkt-assets": "^19.0.4",
|
|
44
|
-
"@oslokommune/punkt-css": "^19.
|
|
44
|
+
"@oslokommune/punkt-css": "^19.9.1",
|
|
45
45
|
"@testing-library/jest-dom": "^6.9.1",
|
|
46
46
|
"@testing-library/react": "^16.3.2",
|
|
47
47
|
"@testing-library/user-event": "^14.6.1",
|
|
@@ -101,5 +101,5 @@
|
|
|
101
101
|
"url": "https://github.com/oslokommune/punkt/issues"
|
|
102
102
|
},
|
|
103
103
|
"license": "MIT",
|
|
104
|
-
"gitHead": "
|
|
104
|
+
"gitHead": "c6f5dcb88eab44820e4f1610fe79a2756d098e2b"
|
|
105
105
|
}
|
|
@@ -4,6 +4,7 @@ import { fireEvent, render, screen } from '@testing-library/react'
|
|
|
4
4
|
import { axe, toHaveNoViolations } from 'jest-axe'
|
|
5
5
|
import { vi } from 'vitest'
|
|
6
6
|
|
|
7
|
+
import { checkboxContexts } from '../../utils/option-contexts'
|
|
7
8
|
import { PktCheckbox } from './Checkbox'
|
|
8
9
|
|
|
9
10
|
expect.extend(toHaveNoViolations)
|
|
@@ -129,3 +130,79 @@ describe('PktCheckbox inputSize', () => {
|
|
|
129
130
|
expect(document.getElementById('n')).not.toHaveAttribute('inputSize')
|
|
130
131
|
})
|
|
131
132
|
})
|
|
133
|
+
|
|
134
|
+
describe.each(checkboxContexts)('PktCheckbox $name', ({ wrap }) => {
|
|
135
|
+
const input = () => document.getElementById('valg') as HTMLInputElement
|
|
136
|
+
|
|
137
|
+
it('renders the input with its label and value', () => {
|
|
138
|
+
render(wrap(<PktCheckbox id="valg" value="a" label="Valg" />))
|
|
139
|
+
|
|
140
|
+
expect(input()).toHaveAttribute('type', 'checkbox')
|
|
141
|
+
expect(input()).toHaveAttribute('value', 'a')
|
|
142
|
+
expect(input()).toHaveAccessibleName('Valg')
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
it('is checked when checked is set', () => {
|
|
146
|
+
render(wrap(<PktCheckbox id="valg" value="a" label="Valg" checked onChange={() => {}} />))
|
|
147
|
+
|
|
148
|
+
expect(input()).toBeChecked()
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it('starts checked with defaultChecked and lets the user uncheck it', () => {
|
|
152
|
+
render(wrap(<PktCheckbox id="valg" value="a" label="Valg" defaultChecked />))
|
|
153
|
+
expect(input()).toBeChecked()
|
|
154
|
+
|
|
155
|
+
fireEvent.click(input())
|
|
156
|
+
|
|
157
|
+
expect(input()).not.toBeChecked()
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it('calls its own onChange when the user toggles it', () => {
|
|
161
|
+
const onChange = vi.fn()
|
|
162
|
+
render(wrap(<PktCheckbox id="valg" value="a" label="Valg" onChange={onChange} />))
|
|
163
|
+
|
|
164
|
+
fireEvent.click(input())
|
|
165
|
+
|
|
166
|
+
expect(onChange).toHaveBeenCalledTimes(1)
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
it('keeps its own name', () => {
|
|
170
|
+
render(wrap(<PktCheckbox id="valg" name="eget" value="a" label="Valg" />))
|
|
171
|
+
|
|
172
|
+
expect(input()).toHaveAttribute('name', 'eget')
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
it('adds the tile, error and size classes', () => {
|
|
176
|
+
const { container } = render(
|
|
177
|
+
wrap(<PktCheckbox id="valg" value="a" label="Valg" hasTile hasError inputSize="small" />),
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
expect(container.querySelector('.pkt-input-check')).toHaveClass('pkt-input-check--small')
|
|
181
|
+
expect(container.querySelector('.pkt-input-check__input')).toHaveClass('pkt-input-check__input--tile')
|
|
182
|
+
expect(input()).toHaveClass('pkt-input-check__input-checkbox--error')
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
it('renders as a switch', () => {
|
|
186
|
+
render(wrap(<PktCheckbox id="valg" value="a" label="Valg" isSwitch />))
|
|
187
|
+
|
|
188
|
+
expect(input()).toHaveAttribute('role', 'switch')
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
it('is invalid while unchecked when required is set', () => {
|
|
192
|
+
render(wrap(<PktCheckbox id="valg" value="a" label="Valg" required />))
|
|
193
|
+
|
|
194
|
+
expect(input().validity.valueMissing).toBe(true)
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
it('is disabled when disabled is set', () => {
|
|
198
|
+
render(wrap(<PktCheckbox id="valg" value="a" label="Valg" disabled />))
|
|
199
|
+
|
|
200
|
+
expect(input()).toBeDisabled()
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
it('has no axe violations', async () => {
|
|
204
|
+
const { container } = render(wrap(<PktCheckbox id="valg" value="a" label="Valg" />))
|
|
205
|
+
|
|
206
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
207
|
+
})
|
|
208
|
+
})
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
ChangeEvent,
|
|
2
3
|
ChangeEventHandler,
|
|
3
4
|
ForwardedRef,
|
|
4
5
|
forwardRef,
|
|
@@ -11,6 +12,7 @@ import {
|
|
|
11
12
|
import { usePktStrings } from '../../hooks/usePktStrings'
|
|
12
13
|
import { useGeneratedId } from '../../hooks/useGeneratedId'
|
|
13
14
|
import { CHECKBOX_DEFAULTS, type IPktCheckboxBase } from 'shared-types'
|
|
15
|
+
import { useCheckboxGroupContext } from '../checkboxgroup/CheckboxGroupContext'
|
|
14
16
|
|
|
15
17
|
export interface IPktCheckbox
|
|
16
18
|
extends
|
|
@@ -44,6 +46,9 @@ export const PktCheckbox = forwardRef(
|
|
|
44
46
|
requiredText,
|
|
45
47
|
strings,
|
|
46
48
|
tagText,
|
|
49
|
+
name,
|
|
50
|
+
value,
|
|
51
|
+
onChange,
|
|
47
52
|
'aria-describedby': ariaDescribedbyProp,
|
|
48
53
|
...props
|
|
49
54
|
}: IPktCheckbox,
|
|
@@ -52,6 +57,30 @@ export const PktCheckbox = forwardRef(
|
|
|
52
57
|
const s = usePktStrings(['forms'], strings)
|
|
53
58
|
const id = useGeneratedId(idProp)
|
|
54
59
|
const internalRef = useRef<HTMLInputElement>(null)
|
|
60
|
+
const group = useCheckboxGroupContext()
|
|
61
|
+
const optionValue = value ?? 'on'
|
|
62
|
+
const groupValue = group?.value
|
|
63
|
+
const groupControlled = groupValue !== undefined
|
|
64
|
+
const resolvedChecked = groupControlled ? groupValue.includes(optionValue) : checked
|
|
65
|
+
const resolvedDefaultChecked =
|
|
66
|
+
defaultChecked ?? (group?.defaultValue !== undefined ? group.defaultValue.includes(optionValue) : undefined)
|
|
67
|
+
const tile = hasTile || !!group?.hasTile
|
|
68
|
+
const error = hasError || !!group?.hasError
|
|
69
|
+
const isDisabled = disabled || !!group?.disabled
|
|
70
|
+
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
if (groupControlled && checked !== undefined) {
|
|
73
|
+
// eslint-disable-next-line no-console
|
|
74
|
+
console.warn(
|
|
75
|
+
`PktCheckbox (${id}): checked ignoreres når PktCheckboxGroup har value. Styr valget med value på gruppen.`,
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
}, [groupControlled, checked, id])
|
|
79
|
+
|
|
80
|
+
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
|
|
81
|
+
onChange?.(event)
|
|
82
|
+
group?.onOptionChange(event)
|
|
83
|
+
}
|
|
55
84
|
|
|
56
85
|
useEffect(() => {
|
|
57
86
|
if (internalRef.current && indeterminate !== undefined) {
|
|
@@ -74,7 +103,7 @@ export const PktCheckbox = forwardRef(
|
|
|
74
103
|
const labelClasses = [
|
|
75
104
|
'pkt-input-check__input-label',
|
|
76
105
|
labelPosition === 'left' ? 'pkt-input-check__input-label--left' : '',
|
|
77
|
-
|
|
106
|
+
isDisabled ? 'pkt-input-check__input-label--disabled' : '',
|
|
78
107
|
`pkt-input-check__input-label--${labelPosition}`,
|
|
79
108
|
hideLabel ? 'pkt-sr-only' : '',
|
|
80
109
|
]
|
|
@@ -83,8 +112,8 @@ export const PktCheckbox = forwardRef(
|
|
|
83
112
|
|
|
84
113
|
const inputTileClasses = [
|
|
85
114
|
'pkt-input-check__input',
|
|
86
|
-
|
|
87
|
-
|
|
115
|
+
tile ? 'pkt-input-check__input--tile' : '',
|
|
116
|
+
isDisabled && tile ? 'pkt-input-check__input--tile-disabled' : '',
|
|
88
117
|
]
|
|
89
118
|
.filter(Boolean)
|
|
90
119
|
.join(' ')
|
|
@@ -128,13 +157,18 @@ export const PktCheckbox = forwardRef(
|
|
|
128
157
|
<input
|
|
129
158
|
role={isSwitch ? 'switch' : 'checkbox'}
|
|
130
159
|
ref={internalRef}
|
|
131
|
-
className={`pkt-input-check__input-checkbox ${
|
|
160
|
+
className={`pkt-input-check__input-checkbox ${error ? 'pkt-input-check__input-checkbox--error' : ''}`}
|
|
132
161
|
type="checkbox"
|
|
133
162
|
id={id}
|
|
134
|
-
|
|
163
|
+
name={name ?? group?.name}
|
|
164
|
+
value={value}
|
|
165
|
+
disabled={isDisabled}
|
|
135
166
|
aria-labelledby={label ? labelId : undefined}
|
|
136
167
|
aria-describedby={describedBy}
|
|
137
|
-
{...(
|
|
168
|
+
{...(resolvedChecked !== undefined
|
|
169
|
+
? { checked: resolvedChecked }
|
|
170
|
+
: { defaultChecked: resolvedDefaultChecked })}
|
|
171
|
+
onChange={group ? handleChange : onChange}
|
|
138
172
|
{...props}
|
|
139
173
|
/>
|
|
140
174
|
{labelPosition === 'right' && renderLabel()}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import '@testing-library/jest-dom'
|
|
2
|
+
|
|
3
|
+
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
4
|
+
import { type ReactNode } from 'react'
|
|
5
|
+
import { Controller, useForm, type UseFormReturn } from 'react-hook-form'
|
|
6
|
+
import { vi } from 'vitest'
|
|
7
|
+
|
|
8
|
+
import { PktCheckbox } from '../checkbox/Checkbox'
|
|
9
|
+
import { PktCheckboxGroup } from './CheckboxGroup'
|
|
10
|
+
|
|
11
|
+
type TValues = { rom: string[] }
|
|
12
|
+
|
|
13
|
+
const checkbox = (id: string) => document.getElementById(id) as HTMLInputElement
|
|
14
|
+
|
|
15
|
+
const options = (extra: object = {}) => (
|
|
16
|
+
<>
|
|
17
|
+
<PktCheckbox id="enkelt" value="enkelt" label="Enkeltrom" {...extra} />
|
|
18
|
+
<PktCheckbox id="dobbelt" value="dobbelt" label="Dobbeltrom" {...extra} />
|
|
19
|
+
<PktCheckbox id="varsler" value="varsler" label="Varsler" isSwitch {...extra} />
|
|
20
|
+
</>
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
const renderForm = (fields: (form: UseFormReturn<TValues>) => ReactNode) => {
|
|
24
|
+
const onSubmit = vi.fn()
|
|
25
|
+
let form!: UseFormReturn<TValues>
|
|
26
|
+
const Form = () => {
|
|
27
|
+
form = useForm<TValues>({ defaultValues: { rom: [] } })
|
|
28
|
+
return (
|
|
29
|
+
<form onSubmit={form.handleSubmit(onSubmit)}>
|
|
30
|
+
{fields(form)}
|
|
31
|
+
<button type="submit">Send</button>
|
|
32
|
+
</form>
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
render(<Form />)
|
|
36
|
+
return { form: () => form, onSubmit }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const patterns: Array<[string, (form: UseFormReturn<TValues>) => ReactNode]> = [
|
|
40
|
+
[
|
|
41
|
+
'register on the group',
|
|
42
|
+
(form) => (
|
|
43
|
+
<PktCheckboxGroup label="Romtype" {...form.register('rom', { required: true })}>
|
|
44
|
+
{options()}
|
|
45
|
+
</PktCheckboxGroup>
|
|
46
|
+
),
|
|
47
|
+
],
|
|
48
|
+
[
|
|
49
|
+
'Controller with the field spread on the group',
|
|
50
|
+
(form) => (
|
|
51
|
+
<Controller
|
|
52
|
+
name="rom"
|
|
53
|
+
control={form.control}
|
|
54
|
+
rules={{ required: true }}
|
|
55
|
+
render={({ field }) => (
|
|
56
|
+
<PktCheckboxGroup label="Romtype" {...field}>
|
|
57
|
+
{options()}
|
|
58
|
+
</PktCheckboxGroup>
|
|
59
|
+
)}
|
|
60
|
+
/>
|
|
61
|
+
),
|
|
62
|
+
],
|
|
63
|
+
[
|
|
64
|
+
'Controller with onValueChange',
|
|
65
|
+
(form) => (
|
|
66
|
+
<Controller
|
|
67
|
+
name="rom"
|
|
68
|
+
control={form.control}
|
|
69
|
+
rules={{ required: true }}
|
|
70
|
+
render={({ field }) => (
|
|
71
|
+
<PktCheckboxGroup
|
|
72
|
+
label="Romtype"
|
|
73
|
+
name={field.name}
|
|
74
|
+
value={field.value}
|
|
75
|
+
onValueChange={field.onChange}
|
|
76
|
+
onBlur={field.onBlur}
|
|
77
|
+
ref={field.ref}
|
|
78
|
+
>
|
|
79
|
+
{options()}
|
|
80
|
+
</PktCheckboxGroup>
|
|
81
|
+
)}
|
|
82
|
+
/>
|
|
83
|
+
),
|
|
84
|
+
],
|
|
85
|
+
[
|
|
86
|
+
'register on each checkbox',
|
|
87
|
+
(form) => <PktCheckboxGroup label="Romtype">{options(form.register('rom', { required: true }))}</PktCheckboxGroup>,
|
|
88
|
+
],
|
|
89
|
+
]
|
|
90
|
+
|
|
91
|
+
describe.each(patterns)('PktCheckboxGroup with React Hook Form: %s', (_, fields) => {
|
|
92
|
+
it('tracks the checked values in DOM order', () => {
|
|
93
|
+
const { form } = renderForm(fields)
|
|
94
|
+
|
|
95
|
+
fireEvent.click(checkbox('varsler'))
|
|
96
|
+
fireEvent.click(checkbox('enkelt'))
|
|
97
|
+
|
|
98
|
+
expect(form().getValues('rom')).toEqual(['enkelt', 'varsler'])
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('shows values set with setValue and clears them with reset', async () => {
|
|
102
|
+
const { form } = renderForm(fields)
|
|
103
|
+
|
|
104
|
+
await act(async () => form().setValue('rom', ['dobbelt']))
|
|
105
|
+
expect(checkbox('dobbelt')).toBeChecked()
|
|
106
|
+
expect(checkbox('enkelt')).not.toBeChecked()
|
|
107
|
+
expect(form().getValues('rom')).toEqual(['dobbelt'])
|
|
108
|
+
|
|
109
|
+
await act(async () => form().reset({ rom: [] }))
|
|
110
|
+
expect(checkbox('dobbelt')).not.toBeChecked()
|
|
111
|
+
expect(form().getValues('rom')).toEqual([])
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it('blocks submit while required and empty, and submits the checked values', async () => {
|
|
115
|
+
const { onSubmit } = renderForm(fields)
|
|
116
|
+
|
|
117
|
+
fireEvent.click(screen.getByRole('button', { name: 'Send' }))
|
|
118
|
+
await waitFor(() => expect(checkbox('enkelt')).toHaveFocus())
|
|
119
|
+
expect(onSubmit).not.toHaveBeenCalled()
|
|
120
|
+
|
|
121
|
+
fireEvent.click(checkbox('dobbelt'))
|
|
122
|
+
fireEvent.click(screen.getByRole('button', { name: 'Send' }))
|
|
123
|
+
await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1))
|
|
124
|
+
expect(onSubmit.mock.calls[0][0]).toEqual({ rom: ['dobbelt'] })
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
describe('PktCheckboxGroup with a handler that reads event.target', () => {
|
|
129
|
+
it('gives the group name and all checked values, as form libraries such as Formik expect', () => {
|
|
130
|
+
const values: Record<string, unknown> = {}
|
|
131
|
+
render(
|
|
132
|
+
<PktCheckboxGroup
|
|
133
|
+
name="rom"
|
|
134
|
+
label="Romtype"
|
|
135
|
+
onChange={(event) => {
|
|
136
|
+
const { name, value, type } = event.target as unknown as { name: string; value: string[]; type?: string }
|
|
137
|
+
values[name] = type === 'checkbox' ? undefined : value
|
|
138
|
+
}}
|
|
139
|
+
>
|
|
140
|
+
{options()}
|
|
141
|
+
</PktCheckboxGroup>,
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
fireEvent.click(checkbox('dobbelt'))
|
|
145
|
+
fireEvent.click(checkbox('enkelt'))
|
|
146
|
+
|
|
147
|
+
expect(values).toEqual({ rom: ['enkelt', 'dobbelt'] })
|
|
148
|
+
})
|
|
149
|
+
})
|