@oslokommune/punkt-react 19.5.0 → 19.6.0

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.
@@ -6,6 +6,7 @@
6
6
  */
7
7
  import type { IPktStringsProps } from '../shared-strings';
8
8
  import type { IFormFieldErrorProps, IFormFieldLabelProps, ITagOptionalRequiredProps } from './form-field';
9
+ import type { TPktInputSize } from './size';
9
10
  /** Checkbox label placement — shared runtime source for types, specs, and Preview. */
10
11
  export declare const CHECKBOX_LABEL_POSITIONS: readonly ['right', 'left'];
11
12
  export type TCheckboxLabelPosition = (typeof CHECKBOX_LABEL_POSITIONS)[number];
@@ -28,12 +29,13 @@ export interface IPktCheckboxBase extends IFormFieldLabelProps, Pick<IFormFieldE
28
29
  isSwitch?: boolean;
29
30
  hideLabel?: boolean;
30
31
  labelPosition?: TCheckboxLabelPosition;
32
+ inputSize?: TPktInputSize;
31
33
  }
32
34
  /**
33
35
  * Documented Checkbox props (subset of `IPktCheckboxBase`).
34
36
  * Used for typed component specs in documentation.
35
37
  */
36
- export type ICheckboxSpecProps = Pick<IPktCheckboxBase, 'label' | 'checkHelptext' | 'name' | 'form' | 'value' | 'id' | 'defaultChecked' | 'checked' | 'indeterminate' | 'hasTile' | 'disabled' | 'hasError' | 'isSwitch' | 'labelPosition' | 'hideLabel' | 'requiredTag' | 'requiredText' | 'optionalTag' | 'optionalText' | 'tagText'>;
38
+ export type ICheckboxSpecProps = Pick<IPktCheckboxBase, 'label' | 'checkHelptext' | 'name' | 'form' | 'value' | 'id' | 'defaultChecked' | 'checked' | 'indeterminate' | 'hasTile' | 'inputSize' | 'disabled' | 'hasError' | 'isSwitch' | 'labelPosition' | 'hideLabel' | 'requiredTag' | 'requiredText' | 'optionalTag' | 'optionalText' | 'tagText'>;
37
39
  /**
38
40
  * Default values for pkt-checkbox. Single source of truth for both frameworks.
39
41
  * `checked` is intentionally absent — React must keep it `undefined` to stay uncontrolled.
@@ -19,4 +19,5 @@ export type TSize7 = 'xxsmall' | 'xsmall' | 'small' | 'medium' | 'large' | 'xlar
19
19
  /**
20
20
  * Sizes used in our input components
21
21
  */
22
- export type TPktInputSize = 'xsmall' | 'small' | 'medium';
22
+ export declare const InputSize: readonly ['xsmall', 'small', 'medium'];
23
+ export type TPktInputSize = (typeof InputSize)[number];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslokommune/punkt-react",
3
- "version": "19.5.0",
3
+ "version": "19.6.0",
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.1.0",
44
+ "@oslokommune/punkt-css": "^19.6.0",
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": "b3cd6677098953e95888488fa523fcfed4dc714b"
104
+ "gitHead": "52793889285e2b8365c81315dce280062c38a543"
105
105
  }
@@ -105,3 +105,27 @@ describe('PktCheckbox', () => {
105
105
  })
106
106
  })
107
107
  })
108
+
109
+ describe('PktCheckbox inputSize', () => {
110
+ const root = (inputSize?: 'small' | 'medium' | 'xsmall') =>
111
+ render(<PktCheckbox id="s" label="Valg" inputSize={inputSize} />).container.querySelector('.pkt-input-check')
112
+
113
+ it('adds no size class without inputSize, so the size is inherited', () => {
114
+ expect(root()?.className).toBe('pkt-input-check')
115
+ })
116
+
117
+ it('adds the size class for inputSize', () => {
118
+ expect(root('small')).toHaveClass('pkt-input-check--small')
119
+ expect(root('xsmall')).toHaveClass('pkt-input-check--xsmall')
120
+ })
121
+
122
+ it('adds the medium class when medium is set explicitly', () => {
123
+ expect(root('medium')).toHaveClass('pkt-input-check--medium')
124
+ })
125
+
126
+ it('does not pass inputSize on to the native input', () => {
127
+ render(<PktCheckbox id="n" label="Valg" inputSize="small" />)
128
+ expect(document.getElementById('n')).not.toHaveAttribute('inputsize')
129
+ expect(document.getElementById('n')).not.toHaveAttribute('inputSize')
130
+ })
131
+ })
@@ -34,6 +34,7 @@ export const PktCheckbox = forwardRef(
34
34
  isSwitch = CHECKBOX_DEFAULTS.isSwitch,
35
35
  hideLabel = CHECKBOX_DEFAULTS.hideLabel,
36
36
  labelPosition = CHECKBOX_DEFAULTS.labelPosition,
37
+ inputSize,
37
38
  defaultChecked,
38
39
  checked,
39
40
  indeterminate,
@@ -66,7 +67,9 @@ export const PktCheckbox = forwardRef(
66
67
  }
67
68
  }, [ref])
68
69
 
69
- const classes = [className, 'pkt-input-check'].filter(Boolean).join(' ')
70
+ const classes = [className, 'pkt-input-check', inputSize ? `pkt-input-check--${inputSize}` : '']
71
+ .filter(Boolean)
72
+ .join(' ')
70
73
 
71
74
  const labelClasses = [
72
75
  'pkt-input-check__input-label',
@@ -475,9 +475,12 @@ describe('PktCombobox', () => {
475
475
  test('renders label as screen-reader-only when useWrapper is false', () => {
476
476
  const { container } = createComboboxTest({ useWrapper: false })
477
477
 
478
- const labelEl = container.querySelector('label')
479
- expect(labelEl).toBeInTheDocument()
480
- expect(labelEl).toHaveClass('pkt-sr-only')
478
+ const legend = container.querySelector('fieldset > legend')
479
+ expect(legend).toBeInTheDocument()
480
+ expect(legend).toHaveClass('pkt-sr-only')
481
+
482
+ const combobox = container.querySelector('[role="combobox"]')
483
+ expect(combobox?.getAttribute('aria-labelledby')).toBe(legend?.id)
481
484
  })
482
485
  })
483
486
  })
@@ -0,0 +1,92 @@
1
+ import '@testing-library/jest-dom'
2
+
3
+ import { render } from '@testing-library/react'
4
+
5
+ import { PktInputWrapper } from './InputWrapper'
6
+
7
+ const renderFieldset = (props: Partial<Parameters<typeof PktInputWrapper>[0]> = {}) => {
8
+ const { container } = render(
9
+ <PktInputWrapper forId="g" label="Gruppe" hasFieldset {...props}>
10
+ <input type="radio" name="g" aria-label="Valg" />
11
+ </PktInputWrapper>,
12
+ )
13
+ return container.querySelector('fieldset') as HTMLFieldSetElement
14
+ }
15
+
16
+ const describedByIds = (fieldset: HTMLFieldSetElement) =>
17
+ (fieldset.getAttribute('aria-describedby') ?? '').split(' ').filter(Boolean)
18
+
19
+ describe('PktInputWrapper', () => {
20
+ describe('fieldset name and description', () => {
21
+ test('links the error message to the fieldset', () => {
22
+ const fieldset = renderFieldset({ hasError: true, errorMessage: 'Velg ett alternativ' })
23
+
24
+ expect(describedByIds(fieldset)).toContain('g-error')
25
+ expect(document.getElementById('g-error')).toBeInTheDocument()
26
+ })
27
+
28
+ test('links both helptext and error message to the fieldset', () => {
29
+ const fieldset = renderFieldset({ helptext: 'Velg ett', hasError: true, errorMessage: 'Mangler' })
30
+
31
+ expect(describedByIds(fieldset)).toEqual(['g-helptext', 'g-error'])
32
+ })
33
+
34
+ test('does not link an error message when hasError is off', () => {
35
+ const fieldset = renderFieldset({ errorMessage: 'Mangler' })
36
+
37
+ expect(fieldset).not.toHaveAttribute('aria-describedby')
38
+ })
39
+
40
+ test('only references ids that are rendered', () => {
41
+ const fieldset = renderFieldset({ helptext: 'Velg ett', hasError: true, errorMessage: 'Mangler' })
42
+
43
+ describedByIds(fieldset).forEach((id) => {
44
+ expect(document.getElementById(id)).toBeInTheDocument()
45
+ })
46
+ })
47
+
48
+ test('names the fieldset with a hidden legend when useWrapper is off', () => {
49
+ const fieldset = renderFieldset({ useWrapper: false })
50
+
51
+ const legend = fieldset.querySelector(':scope > legend')
52
+ expect(legend).toHaveClass('pkt-sr-only')
53
+ expect(legend?.textContent?.trim()).toBe('Gruppe')
54
+ expect(fieldset.querySelector('label')).toBeNull()
55
+ })
56
+ })
57
+ })
58
+
59
+ describe('PktInputWrapper options container', () => {
60
+ const renderWrapper = (props: Partial<Parameters<typeof PktInputWrapper>[0]> = {}) =>
61
+ render(
62
+ <PktInputWrapper forId="g" label="Gruppe" {...props}>
63
+ <span className="valg">Valg</span>
64
+ </PktInputWrapper>,
65
+ ).container
66
+
67
+ test('wraps the options of a fieldset in a vertical options container', () => {
68
+ const options = renderWrapper({ hasFieldset: true }).querySelector('.pkt-inputwrapper__options')
69
+
70
+ expect(options).not.toHaveClass('pkt-inputwrapper__options--horizontal')
71
+ expect(options?.querySelector('.valg')).toBeInTheDocument()
72
+ })
73
+
74
+ test('lays the options out horizontally with layout="horizontal"', () => {
75
+ const container = renderWrapper({ hasFieldset: true, layout: 'horizontal' })
76
+
77
+ expect(container.querySelector('.pkt-inputwrapper__options')).toHaveClass('pkt-inputwrapper__options--horizontal')
78
+ })
79
+
80
+ test('uses the options container for horizontal layout without a fieldset', () => {
81
+ const container = renderWrapper({ layout: 'horizontal' })
82
+
83
+ expect(container.querySelector('.pkt-inputwrapper__options--horizontal .valg')).toBeInTheDocument()
84
+ })
85
+
86
+ test('keeps the markup unchanged without a fieldset and horizontal layout', () => {
87
+ const container = renderWrapper()
88
+
89
+ expect(container.querySelector('.pkt-inputwrapper__options')).toBeNull()
90
+ expect(container.querySelector('.pkt-inputwrapper__fieldset > .valg')).toBeInTheDocument()
91
+ })
92
+ })
@@ -7,6 +7,7 @@ import { usePktStrings } from '../../hooks/usePktStrings'
7
7
  import { formatString, type IPktStringsProps } from 'shared-strings'
8
8
  import { counterStatusId, isCounterExceeded } from 'shared-utils/forms/described-by'
9
9
  import { uuidish } from 'shared-utils/utils'
10
+ import type { TLayout } from 'shared-types'
10
11
  import { PktAlert } from '../alert/Alert'
11
12
  import { PktHelptext } from '../helptext/Helptext'
12
13
 
@@ -36,6 +37,7 @@ export interface IPktInputWrapper extends RefAttributes<HTMLElement>, IPktString
36
37
  children?: ReactNode
37
38
  className?: string
38
39
  hasFieldset?: boolean
40
+ layout?: TLayout
39
41
  role?: string
40
42
  counterPosition?: 'top' | 'bottom'
41
43
  size?: 'small' | 'medium' | 'xsmall'
@@ -66,6 +68,7 @@ export const PktInputWrapper = forwardRef(
66
68
  children,
67
69
  className = '',
68
70
  hasFieldset = false,
71
+ layout = 'vertical',
69
72
  role,
70
73
  counterPosition = 'bottom',
71
74
  size = 'medium',
@@ -79,7 +82,15 @@ export const PktInputWrapper = forwardRef(
79
82
  const generatedId = useMemo(() => uuidish(), [])
80
83
  const forId = forIdProp || generatedId
81
84
 
82
- const describedBy = ariaDescribedby || (helptext ? `${forId}-helptext` : undefined)
85
+ const describedBy =
86
+ [
87
+ ...new Set(
88
+ [ariaDescribedby || (helptext ? `${forId}-helptext` : ''), hasError && errorMessage ? `${forId}-error` : '']
89
+ .join(' ')
90
+ .split(' ')
91
+ .filter(Boolean),
92
+ ),
93
+ ].join(' ') || undefined
83
94
  const showCounter = !!counter
84
95
  const counterTop = showCounter && counterPosition === 'top'
85
96
  const counterBottom = showCounter && counterPosition === 'bottom'
@@ -133,6 +144,14 @@ export const PktInputWrapper = forwardRef(
133
144
  </>
134
145
  )
135
146
 
147
+ if (!useWrapper && hasFieldset) {
148
+ return (
149
+ <legend className="pkt-sr-only" id={`${forId}-label`}>
150
+ {label}
151
+ </legend>
152
+ )
153
+ }
154
+
136
155
  if (!useWrapper) {
137
156
  return (
138
157
  <label htmlFor={forId} className="pkt-sr-only" id={`${forId}-label`}>
@@ -177,7 +196,19 @@ export const PktInputWrapper = forwardRef(
177
196
  {renderLabel()}
178
197
  {renderHelptext()}
179
198
  {counterTop && counterContent}
180
- {children}
199
+ {hasFieldset || layout === 'horizontal' ? (
200
+ <div
201
+ className={
202
+ layout === 'horizontal'
203
+ ? 'pkt-inputwrapper__options pkt-inputwrapper__options--horizontal'
204
+ : 'pkt-inputwrapper__options'
205
+ }
206
+ >
207
+ {children}
208
+ </div>
209
+ ) : (
210
+ children
211
+ )}
181
212
  {counterBottom && counterContent}
182
213
  {hasError && errorMessage && (
183
214
  <div className="pkt-inputwrapper__alert-wrapper">
@@ -66,3 +66,29 @@ describe('PktRadiobutton', () => {
66
66
  })
67
67
  })
68
68
  })
69
+
70
+ describe('PktRadioButton inputSize', () => {
71
+ const root = (inputSize?: 'small' | 'medium' | 'xsmall') =>
72
+ render(<PktRadioButton id="s" name="g" label="Valg" inputSize={inputSize} />).container.querySelector(
73
+ '.pkt-input-check',
74
+ )
75
+
76
+ it('adds no size class without inputSize, so the size is inherited', () => {
77
+ expect(root()?.className).toBe('pkt-input-check')
78
+ })
79
+
80
+ it('adds the size class for inputSize', () => {
81
+ expect(root('small')).toHaveClass('pkt-input-check--small')
82
+ expect(root('xsmall')).toHaveClass('pkt-input-check--xsmall')
83
+ })
84
+
85
+ it('adds the medium class when medium is set explicitly', () => {
86
+ expect(root('medium')).toHaveClass('pkt-input-check--medium')
87
+ })
88
+
89
+ it('does not pass inputSize on to the native input', () => {
90
+ render(<PktRadioButton id="n" name="g" label="Valg" inputSize="small" />)
91
+ expect(document.getElementById('n')).not.toHaveAttribute('inputsize')
92
+ expect(document.getElementById('n')).not.toHaveAttribute('inputSize')
93
+ })
94
+ })
@@ -2,12 +2,14 @@ import { ChangeEventHandler, ForwardedRef, forwardRef, type InputHTMLAttributes,
2
2
  import { usePktStrings } from '../../hooks/usePktStrings'
3
3
  import { useGeneratedId } from '../../hooks/useGeneratedId'
4
4
  import type { IPktStringsProps } from 'shared-strings'
5
+ import type { TPktInputSize } from 'shared-types'
5
6
 
6
7
  export interface IPktRadioButton extends InputHTMLAttributes<HTMLInputElement>, IPktStringsProps<'forms'> {
7
8
  id: string
8
9
  name: string
9
10
  label: string
10
11
  hasTile?: boolean
12
+ inputSize?: TPktInputSize
11
13
  disabled?: boolean
12
14
  checkHelptext?: ReactNode | ReactNode[] | string
13
15
  defaultChecked?: boolean
@@ -32,6 +34,7 @@ export const PktRadioButton = forwardRef(
32
34
  label,
33
35
  className,
34
36
  hasTile = false,
37
+ inputSize,
35
38
  disabled = false,
36
39
  checkHelptext,
37
40
  hasError = false,
@@ -50,7 +53,9 @@ export const PktRadioButton = forwardRef(
50
53
  ): ReactElement => {
51
54
  const s = usePktStrings(['forms'], strings)
52
55
  const id = useGeneratedId(idProp)
53
- const classes = [className, 'pkt-input-check'].filter(Boolean).join(' ')
56
+ const classes = [className, 'pkt-input-check', inputSize ? `pkt-input-check--${inputSize}` : '']
57
+ .filter(Boolean)
58
+ .join(' ')
54
59
 
55
60
  const inputTileClasses = [
56
61
  'pkt-input-check__input',