@oslokommune/punkt-react 18.8.0 → 18.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 +35 -0
- package/LICENSE +1 -1
- package/dist/components/combobox/types.d.ts +2 -0
- package/dist/components/formAttribute.test.d.ts +1 -0
- package/dist/components/timepicker/useTimepickerState.d.ts +1 -0
- package/dist/punkt-react.cjs +1 -1
- package/dist/punkt-react.js +1511 -1492
- package/dist/shared-types/fileupload.d.ts +2 -1
- package/package.json +6 -6
- package/src/components/combobox/Combobox.tsx +1 -0
- package/src/components/combobox/types.ts +2 -0
- package/src/components/combobox/useComboboxState.ts +8 -5
- package/src/components/fileupload/DropZone.tsx +7 -1
- package/src/components/fileupload/FileUpload.tsx +1 -0
- package/src/components/formAttribute.test.tsx +96 -0
- package/src/components/timepicker/Timepicker.tsx +1 -0
- package/src/components/timepicker/useTimepickerState.ts +14 -14
|
@@ -61,6 +61,7 @@ export interface IFilesChangedDetail {
|
|
|
61
61
|
export interface IPktFileUploadBase extends IFormFieldCommonProps<'forms' | 'fileupload'> {
|
|
62
62
|
/** Field name. Used for native input (`form`) or hidden ID inputs (`custom`). */
|
|
63
63
|
name: string;
|
|
64
|
+
form?: string;
|
|
64
65
|
required?: boolean;
|
|
65
66
|
value?: IFileItem[];
|
|
66
67
|
defaultValue?: IFileItem[];
|
|
@@ -93,7 +94,7 @@ export interface IPktFileUploadBase extends IFormFieldCommonProps<'forms' | 'fil
|
|
|
93
94
|
truncateTail?: number;
|
|
94
95
|
}
|
|
95
96
|
/** Documented props for typed component specs — subset of base + docs-only demo props. */
|
|
96
|
-
export type IFileUploadSpecProps = Pick<IPktFileUploadBase, TFormFieldCommonSpecKeys | 'name' | 'required' | 'value' | 'defaultValue' | 'onFilesChanged' | 'uploadStrategy' | 'multiple' | 'itemRenderer' | 'allowedFormats' | 'formatErrorMessage' | 'maxFileSize' | 'onFileValidation' | 'onFileValidate' | 'sizeErrorMessage' | 'addCommentsEnabled' | 'enableImagePreview' | 'extraOperations' | 'fullwidth' | 'onFileUploadRequested' | 'onTransferCancelled' | 'renameFilesEnabled' | 'transfers' | 'truncateTail'> & {
|
|
97
|
+
export type IFileUploadSpecProps = Pick<IPktFileUploadBase, TFormFieldCommonSpecKeys | 'name' | 'form' | 'required' | 'value' | 'defaultValue' | 'onFilesChanged' | 'uploadStrategy' | 'multiple' | 'itemRenderer' | 'allowedFormats' | 'formatErrorMessage' | 'maxFileSize' | 'onFileValidation' | 'onFileValidate' | 'sizeErrorMessage' | 'addCommentsEnabled' | 'enableImagePreview' | 'extraOperations' | 'fullwidth' | 'onFileUploadRequested' | 'onTransferCancelled' | 'renameFilesEnabled' | 'transfers' | 'truncateTail'> & {
|
|
97
98
|
previewUploadIntervalMs?: number;
|
|
98
99
|
previewSimulateUploadError?: boolean;
|
|
99
100
|
previewUploadStatus?: TPreviewUploadStatus;
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oslokommune/punkt-react",
|
|
3
|
-
"version": "18.
|
|
4
|
-
"description": "React komponentbibliotek til Punkt,
|
|
3
|
+
"version": "18.9.1",
|
|
4
|
+
"description": "React komponentbibliotek til Punkt, designsystem for Oslo kommune",
|
|
5
5
|
"homepage": "https://punkt.oslo.kommune.no",
|
|
6
|
-
"author": "Team Designsystem, Oslo
|
|
6
|
+
"author": "Team Designsystem, Oslo kommune",
|
|
7
7
|
"main": "dist/punkt-react.cjs",
|
|
8
8
|
"module": "dist/punkt-react.js",
|
|
9
9
|
"files": [
|
|
@@ -39,8 +39,8 @@
|
|
|
39
39
|
"@eslint/compat": "^2.1.0",
|
|
40
40
|
"@eslint/eslintrc": "^3.3.5",
|
|
41
41
|
"@eslint/js": "^10.0.1",
|
|
42
|
-
"@oslokommune/punkt-assets": "^18.
|
|
43
|
-
"@oslokommune/punkt-css": "^18.
|
|
42
|
+
"@oslokommune/punkt-assets": "^18.9.1",
|
|
43
|
+
"@oslokommune/punkt-css": "^18.9.1",
|
|
44
44
|
"@testing-library/jest-dom": "^6.9.1",
|
|
45
45
|
"@testing-library/react": "^16.3.2",
|
|
46
46
|
"@testing-library/user-event": "^14.6.1",
|
|
@@ -100,5 +100,5 @@
|
|
|
100
100
|
"url": "https://github.com/oslokommune/punkt/issues"
|
|
101
101
|
},
|
|
102
102
|
"license": "MIT",
|
|
103
|
-
"gitHead": "
|
|
103
|
+
"gitHead": "66da3e7fd37f5a1058dac1dbf681c5ce5e2bc08b"
|
|
104
104
|
}
|
|
@@ -33,6 +33,7 @@ export interface IPktCombobox extends IPktStringsProps<'combobox' | 'listbox' |
|
|
|
33
33
|
|
|
34
34
|
// Form
|
|
35
35
|
name?: string
|
|
36
|
+
form?: string
|
|
36
37
|
id?: string
|
|
37
38
|
disabled?: boolean
|
|
38
39
|
required?: boolean
|
|
@@ -122,6 +123,7 @@ export interface IComboboxState {
|
|
|
122
123
|
inline: boolean
|
|
123
124
|
inputSize: 'small' | 'medium' | 'xsmall'
|
|
124
125
|
name?: string
|
|
126
|
+
form?: string
|
|
125
127
|
className?: string
|
|
126
128
|
|
|
127
129
|
// Derived state
|
|
@@ -34,6 +34,7 @@ import {
|
|
|
34
34
|
import type { IPktCombobox, IComboboxState } from './types'
|
|
35
35
|
import { formatString } from 'shared-strings'
|
|
36
36
|
import { usePktStrings } from '../../hooks/usePktStrings'
|
|
37
|
+
import { useOwnerForm } from '../../hooks/useOwnerForm'
|
|
37
38
|
|
|
38
39
|
// Options sync helper (React-specific: immutable, returns new array)
|
|
39
40
|
function syncOptionsWithValues(options: IPktComboboxOption[], values: string[]): IPktComboboxOption[] {
|
|
@@ -80,6 +81,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
80
81
|
counter,
|
|
81
82
|
inputSize = 'medium',
|
|
82
83
|
name,
|
|
84
|
+
form,
|
|
83
85
|
className,
|
|
84
86
|
onChange,
|
|
85
87
|
onValueChange,
|
|
@@ -98,6 +100,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
98
100
|
const triggerRef = useRef<HTMLDivElement>(null)
|
|
99
101
|
const listboxRef = useRef<HTMLDivElement>(null)
|
|
100
102
|
const wrapperRef = useRef<HTMLDivElement>(null)
|
|
103
|
+
const ownerForm = useOwnerForm(wrapperRef, form)
|
|
101
104
|
|
|
102
105
|
// Focus the appropriate trigger: text input for editable, combobox div for select-only
|
|
103
106
|
const focusTrigger = useCallback(() => {
|
|
@@ -1036,8 +1039,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
1036
1039
|
|
|
1037
1040
|
// Form reset: listen for the parent <form>'s reset event and restore initial values
|
|
1038
1041
|
useEffect(() => {
|
|
1039
|
-
|
|
1040
|
-
if (!form) return
|
|
1042
|
+
if (!ownerForm) return
|
|
1041
1043
|
|
|
1042
1044
|
const handleReset = () => {
|
|
1043
1045
|
// Use setTimeout so the reset event finishes before we update state
|
|
@@ -1055,9 +1057,9 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
1055
1057
|
}, 0)
|
|
1056
1058
|
}
|
|
1057
1059
|
|
|
1058
|
-
|
|
1059
|
-
return () =>
|
|
1060
|
-
}, [multiple])
|
|
1060
|
+
ownerForm.addEventListener('reset', handleReset)
|
|
1061
|
+
return () => ownerForm.removeEventListener('reset', handleReset)
|
|
1062
|
+
}, [multiple, ownerForm])
|
|
1061
1063
|
|
|
1062
1064
|
// Expose value getter/setter on ref for RHF register() compatibility.
|
|
1063
1065
|
// RHF reads ref.value via getFieldValue(field._f) — we read directly from the
|
|
@@ -1152,6 +1154,7 @@ export function useComboboxState(props: IPktCombobox, ref: React.ForwardedRef<HT
|
|
|
1152
1154
|
inline,
|
|
1153
1155
|
inputSize,
|
|
1154
1156
|
name,
|
|
1157
|
+
form,
|
|
1155
1158
|
className,
|
|
1156
1159
|
|
|
1157
1160
|
// Derived
|
|
@@ -216,7 +216,13 @@ export const DropZone = forwardRef<HTMLInputElement, IDropZoneProps>(
|
|
|
216
216
|
{uploadStrategy === 'custom' && (
|
|
217
217
|
<>
|
|
218
218
|
{value?.map((fileItem) => (
|
|
219
|
-
<input
|
|
219
|
+
<input
|
|
220
|
+
key={fileItem.fileId}
|
|
221
|
+
type="hidden"
|
|
222
|
+
name={name}
|
|
223
|
+
form={inputProps.form}
|
|
224
|
+
value={fileItem.fileId}
|
|
225
|
+
/>
|
|
220
226
|
))}
|
|
221
227
|
</>
|
|
222
228
|
)}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { render } from '@testing-library/react'
|
|
2
|
+
import type { ReactNode } from 'react'
|
|
3
|
+
import { PktTextinput } from './textinput/Textinput'
|
|
4
|
+
import { PktTextarea } from './textarea/Textarea'
|
|
5
|
+
import { PktCheckbox } from './checkbox/Checkbox'
|
|
6
|
+
import { PktRadioButton } from './radio/RadioButton'
|
|
7
|
+
import { PktSelect } from './select/Select'
|
|
8
|
+
import { PktCombobox } from './combobox/Combobox'
|
|
9
|
+
import { PktDatepicker } from './datepicker/Datepicker'
|
|
10
|
+
import { PktTimepicker } from './timepicker/Timepicker'
|
|
11
|
+
import { PktFileUpload } from './fileupload/FileUpload'
|
|
12
|
+
|
|
13
|
+
const fields = (form?: string): ReactNode => (
|
|
14
|
+
<>
|
|
15
|
+
<PktTextinput id="ti" name="ti" label="Tekst" form={form} defaultValue="a" />
|
|
16
|
+
<PktTextarea id="ta" name="ta" label="Tekstområde" form={form} defaultValue="b" />
|
|
17
|
+
<PktCheckbox id="cb" name="cb" label="Avkryssing" form={form} value="on" defaultChecked />
|
|
18
|
+
<PktRadioButton id="rb" name="rb" label="Radio" form={form} value="r1" defaultChecked />
|
|
19
|
+
<PktSelect id="se" name="se" label="Nedtrekk" form={form} defaultValue="s1">
|
|
20
|
+
<option value="s1">s1</option>
|
|
21
|
+
</PktSelect>
|
|
22
|
+
<PktCombobox
|
|
23
|
+
id="co"
|
|
24
|
+
name="co"
|
|
25
|
+
label="Kombiboks"
|
|
26
|
+
form={form}
|
|
27
|
+
value="c1"
|
|
28
|
+
options={[{ value: 'c1', label: 'c1' }]}
|
|
29
|
+
/>
|
|
30
|
+
<PktDatepicker id="dp" name="dp" label="Dato" form={form} value="2026-01-01" />
|
|
31
|
+
<PktTimepicker id="tp" name="tp" label="Tid" form={form} value="13:45" />
|
|
32
|
+
<PktFileUpload
|
|
33
|
+
id="fu"
|
|
34
|
+
name="fu"
|
|
35
|
+
label="Fil"
|
|
36
|
+
form={form}
|
|
37
|
+
uploadStrategy="custom"
|
|
38
|
+
transfers={[]}
|
|
39
|
+
onFileUploadRequested={() => {}}
|
|
40
|
+
value={[
|
|
41
|
+
{
|
|
42
|
+
fileId: 'f1',
|
|
43
|
+
file: new File(['a'], 'a.txt'),
|
|
44
|
+
attributes: { targetFilename: 'a.txt' },
|
|
45
|
+
},
|
|
46
|
+
]}
|
|
47
|
+
/>
|
|
48
|
+
</>
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
const expected = {
|
|
52
|
+
ti: 'a',
|
|
53
|
+
ta: 'b',
|
|
54
|
+
cb: 'on',
|
|
55
|
+
rb: 'r1',
|
|
56
|
+
se: 's1',
|
|
57
|
+
co: 'c1',
|
|
58
|
+
dp: '2026-01-01',
|
|
59
|
+
tp: '13:45',
|
|
60
|
+
fu: 'f1',
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const submitted = (container: HTMLElement) => {
|
|
64
|
+
const data = new FormData(container.querySelector('#skjema') as HTMLFormElement)
|
|
65
|
+
return Object.fromEntries(Object.keys(expected).map((name) => [name, data.get(name)]))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
describe('form attribute', () => {
|
|
69
|
+
test('submits every field nested inside the form', () => {
|
|
70
|
+
const { container } = render(<form id="skjema">{fields()}</form>)
|
|
71
|
+
|
|
72
|
+
expect(submitted(container)).toEqual(expected)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('submits every field linked to the form with the form attribute', () => {
|
|
76
|
+
const { container } = render(
|
|
77
|
+
<>
|
|
78
|
+
<form id="skjema" />
|
|
79
|
+
{fields('skjema')}
|
|
80
|
+
</>,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
expect(submitted(container)).toEqual(expected)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('leaves fields out of a form they are not linked to', () => {
|
|
87
|
+
const { container } = render(
|
|
88
|
+
<>
|
|
89
|
+
<form id="skjema" />
|
|
90
|
+
{fields()}
|
|
91
|
+
</>,
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
expect(new FormData(container.querySelector('#skjema') as HTMLFormElement).entries().next().done).toBe(true)
|
|
95
|
+
})
|
|
96
|
+
})
|
|
@@ -15,6 +15,7 @@ import { isValidTimeString, timeToMinutes } from 'shared-utils/timepicker/time-u
|
|
|
15
15
|
import type { IPktTimepicker } from './types'
|
|
16
16
|
import { TPktInputSize } from 'shared-types/size'
|
|
17
17
|
import { usePktStrings } from '../../hooks/usePktStrings'
|
|
18
|
+
import { useOwnerForm } from '../../hooks/useOwnerForm'
|
|
18
19
|
import { formatString, type TPktStrings } from 'shared-strings'
|
|
19
20
|
|
|
20
21
|
/**
|
|
@@ -117,6 +118,7 @@ export function useTimepickerState(props: IPktTimepicker, ref: React.ForwardedRe
|
|
|
117
118
|
stepArrows = false,
|
|
118
119
|
fullwidth = false,
|
|
119
120
|
name,
|
|
121
|
+
form,
|
|
120
122
|
disabled = false,
|
|
121
123
|
required = false,
|
|
122
124
|
helptext,
|
|
@@ -196,6 +198,7 @@ export function useTimepickerState(props: IPktTimepicker, ref: React.ForwardedRe
|
|
|
196
198
|
const popupRef = useRef<HTMLDivElement>(null)
|
|
197
199
|
const containerRef = useRef<HTMLDivElement | null>(null)
|
|
198
200
|
const changeInputRef = useRef<HTMLInputElement>(null)
|
|
201
|
+
const ownerForm = useOwnerForm(containerRef, form)
|
|
199
202
|
|
|
200
203
|
const hoursDigitCountRef = useRef(0)
|
|
201
204
|
const hoursFirstDigitRef = useRef(-1)
|
|
@@ -284,15 +287,13 @@ export function useTimepickerState(props: IPktTimepicker, ref: React.ForwardedRe
|
|
|
284
287
|
}, [syncValueFromDisplay, required, min, max, step, catalog.validation])
|
|
285
288
|
|
|
286
289
|
useEffect(() => {
|
|
287
|
-
|
|
288
|
-
const form = root?.closest('form')
|
|
289
|
-
if (!form) return
|
|
290
|
+
if (!ownerForm) return
|
|
290
291
|
|
|
291
292
|
const isSubmitActivator = (target: EventTarget | null) => {
|
|
292
293
|
const el = target as HTMLElement | null
|
|
293
|
-
if (!el || !
|
|
294
|
+
if (!el || !ownerForm.contains(el)) return false
|
|
294
295
|
const control = el.closest('button, input[type="submit"], input[type="image"]')
|
|
295
|
-
if (!control || !
|
|
296
|
+
if (!control || !ownerForm.contains(control)) return false
|
|
296
297
|
if (control instanceof HTMLButtonElement) {
|
|
297
298
|
return control.type !== 'button' && control.type !== 'reset'
|
|
298
299
|
}
|
|
@@ -304,9 +305,9 @@ export function useTimepickerState(props: IPktTimepicker, ref: React.ForwardedRe
|
|
|
304
305
|
applyNativeConstraintAnchorMessage()
|
|
305
306
|
}
|
|
306
307
|
|
|
307
|
-
|
|
308
|
-
return () =>
|
|
309
|
-
}, [applyNativeConstraintAnchorMessage])
|
|
308
|
+
ownerForm.addEventListener('pointerdown', onPointerDownCapture, true)
|
|
309
|
+
return () => ownerForm.removeEventListener('pointerdown', onPointerDownCapture, true)
|
|
310
|
+
}, [applyNativeConstraintAnchorMessage, ownerForm])
|
|
310
311
|
|
|
311
312
|
useEffect(() => {
|
|
312
313
|
if (changeInputRef.current) {
|
|
@@ -749,9 +750,7 @@ export function useTimepickerState(props: IPktTimepicker, ref: React.ForwardedRe
|
|
|
749
750
|
)
|
|
750
751
|
|
|
751
752
|
useEffect(() => {
|
|
752
|
-
|
|
753
|
-
const form = root?.closest('form')
|
|
754
|
-
if (!form) return
|
|
753
|
+
if (!ownerForm) return
|
|
755
754
|
|
|
756
755
|
const handleReset = () => {
|
|
757
756
|
window.setTimeout(() => {
|
|
@@ -770,9 +769,9 @@ export function useTimepickerState(props: IPktTimepicker, ref: React.ForwardedRe
|
|
|
770
769
|
}, 0)
|
|
771
770
|
}
|
|
772
771
|
|
|
773
|
-
|
|
774
|
-
return () =>
|
|
775
|
-
}, [isControlled, setHours, setMinutes])
|
|
772
|
+
ownerForm.addEventListener('reset', handleReset)
|
|
773
|
+
return () => ownerForm.removeEventListener('reset', handleReset)
|
|
774
|
+
}, [isControlled, setHours, setMinutes, ownerForm])
|
|
776
775
|
|
|
777
776
|
const outerClasses = [
|
|
778
777
|
'pkt-timepicker',
|
|
@@ -820,6 +819,7 @@ export function useTimepickerState(props: IPktTimepicker, ref: React.ForwardedRe
|
|
|
820
819
|
popupId,
|
|
821
820
|
hiddenInputId,
|
|
822
821
|
name: name ?? id,
|
|
822
|
+
form,
|
|
823
823
|
isOpen,
|
|
824
824
|
hourOptions,
|
|
825
825
|
minuteOptions,
|