@oslokommune/punkt-react 19.4.0 → 19.5.2

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.
@@ -0,0 +1,39 @@
1
+ /**
2
+ * icon.ts
3
+ *
4
+ * Type definitions for pkt-icon.
5
+ * Used by React, Elements, and typed component specs.
6
+ */
7
+ import type { Booleanish } from './booleanish';
8
+ /** Default CDN path for icons. Overridden globally by `window.pktIconPath`. */
9
+ export declare const ICON_DEFAULT_PATH = "https://punkt-cdn.oslo.kommune.no/latest/icons/";
10
+ /** Icon size modifier classes — shared runtime source for types, specs, and Preview. */
11
+ export declare const ICON_SIZE_CLASSES: readonly ['pkt-icon--small', 'pkt-icon--medium', 'pkt-icon--large'];
12
+ export type TIconSizeClass = (typeof ICON_SIZE_CLASSES)[number];
13
+ /**
14
+ * Icon props shared by React and Elements.
15
+ *
16
+ * @remarks Elements narrows `name` to `PktIconName` and takes `aria-hidden`/`aria-label`
17
+ * as string attributes.
18
+ */
19
+ export interface IPktIconBase {
20
+ name?: string;
21
+ path?: string;
22
+ ariaHidden?: Booleanish;
23
+ ariaLabel?: string;
24
+ }
25
+ /**
26
+ * Documented Icon props. `className` is not a component prop but the size class
27
+ * (`className` in React, `class` in Elements), documented so Preview can offer it.
28
+ */
29
+ export type IIconSpecProps = Pick<IPktIconBase, 'name' | 'path' | 'ariaHidden' | 'ariaLabel'> & {
30
+ className?: TIconSizeClass;
31
+ };
32
+ /**
33
+ * Default values for pkt-icon. Single source of truth for both frameworks.
34
+ * `path` is only the fallback — `window.pktIconPath` wins when set.
35
+ */
36
+ export declare const ICON_DEFAULTS: {
37
+ readonly name: '';
38
+ readonly path: "https://punkt-cdn.oslo.kommune.no/latest/icons/";
39
+ };
@@ -15,6 +15,8 @@ export { CHECKBOX_LABEL_POSITIONS, CHECKBOX_DEFAULTS } from './checkbox';
15
15
  export type { TCheckboxLabelPosition, IPktCheckboxBase, ICheckboxSpecProps } from './checkbox';
16
16
  export { CONSENT_TRIGGER_TYPES, CONSENT_TRIGGER_TYPE_OPTIONS, CONSENT_DEFAULTS } from './consent';
17
17
  export type { TPktConsentTriggerType, IConsentItem, IConsentValue, ICookieConsentEvent, TConsentDetailsMap, TCookieEventHandler, ICookieEvents, ICookieBannerApi, IPktConsentBase, IConsentSpecProps, } from './consent';
18
+ export { ICON_DEFAULT_PATH, ICON_SIZE_CLASSES, ICON_DEFAULTS } from './icon';
19
+ export type { TIconSizeClass, IPktIconBase, IIconSpecProps } from './icon';
18
20
  export { LAYOUT_DIRECTIONS } from './layout';
19
21
  export type { TLayout, TVerticalPosition, THorizontalPosition, TPosition, TPositionWithCenter, } from './layout';
20
22
  export type { THTMLButtonType, TSelectionMode } from './form';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslokommune/punkt-react",
3
- "version": "19.4.0",
3
+ "version": "19.5.2",
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",
@@ -101,5 +101,5 @@
101
101
  "url": "https://github.com/oslokommune/punkt/issues"
102
102
  },
103
103
  "license": "MIT",
104
- "gitHead": "74057ff529f28056e96199ce2b2def5aee6ef9bf"
104
+ "gitHead": "519d9c748c2bef3bdb12d5979e115c05a2509f44"
105
105
  }
@@ -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
  })
@@ -2,13 +2,12 @@
2
2
 
3
3
  import classNames from 'classnames'
4
4
  import { forwardRef, HTMLAttributes, MutableRefObject, useEffect, useLayoutEffect, useRef } from 'react'
5
- import type { Booleanish } from 'shared-types'
6
-
7
- const defaultPath = 'https://punkt-cdn.oslo.kommune.no/latest/icons/'
5
+ import type { PktIconName } from '@oslokommune/punkt-assets/dist/icons/icon'
6
+ import { ICON_DEFAULTS, type Booleanish, type IPktIconBase } from 'shared-types'
8
7
 
9
8
  if (typeof window !== 'undefined') {
10
9
  window.pktFetch = window.pktFetch === undefined ? (fetch as unknown as typeof window.pktFetch) : window.pktFetch
11
- window.pktIconPath = window.pktIconPath || defaultPath
10
+ window.pktIconPath = window.pktIconPath || ICON_DEFAULTS.path
12
11
  }
13
12
 
14
13
  const errorSvg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"></svg>'
@@ -72,11 +71,8 @@ const downloadIconOrGetFromCache = async (name: string, path: string | undefined
72
71
  return errorSvg
73
72
  }
74
73
 
75
- interface IPktIcon extends HTMLAttributes<HTMLElement> {
76
- name?: string
77
- path?: string
78
- ariaHidden?: Booleanish
79
- ariaLabel?: string
74
+ interface IPktIcon extends Omit<IPktIconBase, 'name'>, HTMLAttributes<HTMLElement> {
75
+ name?: PktIconName
80
76
  }
81
77
 
82
78
  const toBoolean = (v: Booleanish | undefined): boolean | undefined => {
@@ -86,7 +82,7 @@ const toBoolean = (v: Booleanish | undefined): boolean | undefined => {
86
82
 
87
83
  export const PktIcon = forwardRef<HTMLSpanElement, IPktIcon>(function PktIcon(
88
84
  {
89
- name = '',
85
+ name = ICON_DEFAULTS.name,
90
86
  path,
91
87
  className,
92
88
  ariaHidden,
@@ -97,7 +93,7 @@ export const PktIcon = forwardRef<HTMLSpanElement, IPktIcon>(function PktIcon(
97
93
  },
98
94
  forwardedRef,
99
95
  ) {
100
- const resolvedPath = path ?? (typeof window !== 'undefined' ? window.pktIconPath : defaultPath)
96
+ const resolvedPath = path ?? (typeof window !== 'undefined' ? window.pktIconPath : ICON_DEFAULTS.path)
101
97
  const elementRef = useRef<HTMLSpanElement | null>(null)
102
98
 
103
99
  const explicitAriaHidden = toBoolean(ariaHidden) ?? toBoolean(ariaHiddenAttr)
@@ -0,0 +1,57 @@
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
+ })
@@ -79,7 +79,15 @@ export const PktInputWrapper = forwardRef(
79
79
  const generatedId = useMemo(() => uuidish(), [])
80
80
  const forId = forIdProp || generatedId
81
81
 
82
- const describedBy = ariaDescribedby || (helptext ? `${forId}-helptext` : undefined)
82
+ const describedBy =
83
+ [
84
+ ...new Set(
85
+ [ariaDescribedby || (helptext ? `${forId}-helptext` : ''), hasError && errorMessage ? `${forId}-error` : '']
86
+ .join(' ')
87
+ .split(' ')
88
+ .filter(Boolean),
89
+ ),
90
+ ].join(' ') || undefined
83
91
  const showCounter = !!counter
84
92
  const counterTop = showCounter && counterPosition === 'top'
85
93
  const counterBottom = showCounter && counterPosition === 'bottom'
@@ -133,6 +141,14 @@ export const PktInputWrapper = forwardRef(
133
141
  </>
134
142
  )
135
143
 
144
+ if (!useWrapper && hasFieldset) {
145
+ return (
146
+ <legend className="pkt-sr-only" id={`${forId}-label`}>
147
+ {label}
148
+ </legend>
149
+ )
150
+ }
151
+
136
152
  if (!useWrapper) {
137
153
  return (
138
154
  <label htmlFor={forId} className="pkt-sr-only" id={`${forId}-label`}>