@oslokommune/punkt-react 19.15.5 → 19.15.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslokommune/punkt-react",
3
- "version": "19.15.5",
3
+ "version": "19.15.6",
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": "0baab53f4a1bd33c07a73d24eacbd66087c98738"
104
+ "gitHead": "573e87674ae6282cb3c4b0d4954a944aa3027899"
105
105
  }
@@ -1,9 +1,9 @@
1
1
  'use client'
2
2
 
3
3
  import classNames from 'classnames'
4
- import { forwardRef, HTMLAttributes, ReactNode, useMemo, useState } from 'react'
5
- import { uuidish } from 'shared-utils/utils'
4
+ import { forwardRef, HTMLAttributes, ReactNode, useState } from 'react'
6
5
 
6
+ import { useGeneratedId } from '../../hooks/useGeneratedId'
7
7
  import { PktIcon } from '../icon/Icon'
8
8
 
9
9
  const defaultDropdownButton = "Les mer <span class='pkt-sr-only'>om feltet</span>"
@@ -40,8 +40,7 @@ export const PktHelptext = forwardRef<HTMLDivElement, IPktHelptextProps>(functio
40
40
  ) {
41
41
  const [isOpen, setIsOpen] = useState(false)
42
42
 
43
- const generatedId = useMemo(() => uuidish(), [])
44
- const forId = forIdProp || generatedId
43
+ const forId = useGeneratedId(forIdProp)
45
44
 
46
45
  const toggle = () => {
47
46
  const next = !isOpen
@@ -0,0 +1,32 @@
1
+ import { act } from '@testing-library/react'
2
+ import { hydrateRoot, type Root } from 'react-dom/client'
3
+ import { renderToString } from 'react-dom/server'
4
+ import { vi } from 'vitest'
5
+
6
+ import { serverRenderCases } from '../utils/serverRenderCases'
7
+
8
+ describe('hydration', () => {
9
+ test.each(serverRenderCases)('%s hydrates without mismatches', async (_name, element) => {
10
+ const container = document.createElement('div')
11
+ container.innerHTML = renderToString(element())
12
+ document.body.append(container)
13
+
14
+ const errors: string[] = []
15
+ const consoleError = vi.spyOn(console, 'error').mockImplementation((...args: unknown[]) => {
16
+ errors.push(args.map(String).join(' '))
17
+ })
18
+
19
+ let root: Root | undefined
20
+ await act(async () => {
21
+ root = hydrateRoot(container, element(), {
22
+ onRecoverableError: (error) => errors.push(String(error)),
23
+ })
24
+ })
25
+
26
+ consoleError.mockRestore()
27
+ act(() => root?.unmount())
28
+ container.remove()
29
+
30
+ expect(errors).toEqual([])
31
+ })
32
+ })
@@ -1,12 +1,12 @@
1
1
  'use client'
2
2
 
3
- import { type FocusEventHandler, ForwardedRef, forwardRef, type ReactNode, RefAttributes, useMemo } from 'react'
3
+ import { type FocusEventHandler, ForwardedRef, forwardRef, type ReactNode, RefAttributes } from 'react'
4
4
 
5
5
  import { useFocusModality } from '../../hooks/useFocusModality'
6
+ import { useGeneratedId } from '../../hooks/useGeneratedId'
6
7
  import { usePktStrings } from '../../hooks/usePktStrings'
7
8
  import { formatString, type IPktStringsProps } from 'shared-strings'
8
9
  import { counterStatusId, isCounterExceeded } from 'shared-utils/forms/described-by'
9
- import { uuidish } from 'shared-utils/utils'
10
10
  import type { TLayout } from 'shared-types'
11
11
  import { PktAlert } from '../alert/Alert'
12
12
  import { PktHelptext } from '../helptext/Helptext'
@@ -85,8 +85,7 @@ export const PktInputWrapper = forwardRef(
85
85
  const focusModality = useFocusModality<HTMLDivElement>(ref)
86
86
  const s = usePktStrings(['forms', 'validation'], strings)
87
87
 
88
- const generatedId = useMemo(() => uuidish(), [])
89
- const forId = forIdProp || generatedId
88
+ const forId = useGeneratedId(forIdProp)
90
89
 
91
90
  const describedBy =
92
91
  [
@@ -34,9 +34,7 @@ describe('PktProgressbar', () => {
34
34
  })
35
35
 
36
36
  test('renders title correctly', () => {
37
- const { container, getByText } = render(
38
- <PktProgressbar id="progress3" valueCurrent={50} title="Progress" />,
39
- )
37
+ const { container, getByText } = render(<PktProgressbar id="progress3" valueCurrent={50} title="Progress" />)
40
38
 
41
39
  const progressbar = container.firstChild as HTMLElement
42
40
 
@@ -56,9 +54,7 @@ describe('PktProgressbar', () => {
56
54
  })
57
55
 
58
56
  test('renders status as percentage', () => {
59
- const { container } = render(
60
- <PktProgressbar id="progress5" valueCurrent={50} statusType="percentage" />,
61
- )
57
+ const { container } = render(<PktProgressbar id="progress5" valueCurrent={50} statusType="percentage" />)
62
58
  const status = container.querySelector('.pkt-progressbar__status')
63
59
  expect(status).toBeInTheDocument()
64
60
  expect(status).toHaveTextContent('50%')
@@ -75,12 +71,7 @@ describe('PktProgressbar', () => {
75
71
 
76
72
  test('renders status with center placement', () => {
77
73
  const { container } = render(
78
- <PktProgressbar
79
- id="progress7"
80
- valueCurrent={50}
81
- statusType="percentage"
82
- statusPlacement="center"
83
- />,
74
+ <PktProgressbar id="progress7" valueCurrent={50} statusType="percentage" statusPlacement="center" />,
84
75
  )
85
76
  const status = container.querySelector('.pkt-progressbar__status--center')
86
77
  expect(status).toBeInTheDocument()
@@ -88,21 +79,14 @@ describe('PktProgressbar', () => {
88
79
 
89
80
  test('renders status with following placement', () => {
90
81
  const { container } = render(
91
- <PktProgressbar
92
- id="progress8"
93
- valueCurrent={50}
94
- statusType="percentage"
95
- statusPlacement="following"
96
- />,
82
+ <PktProgressbar id="progress8" valueCurrent={50} statusType="percentage" statusPlacement="following" />,
97
83
  )
98
84
  const status = container.querySelector('.pkt-progressbar__status-placement--following')
99
85
  expect(status).toBeInTheDocument()
100
86
  })
101
87
 
102
88
  test('handles aria-label correctly', () => {
103
- const { container } = render(
104
- <PktProgressbar id="progress9" valueCurrent={50} ariaLabel="Progress bar" />,
105
- )
89
+ const { container } = render(<PktProgressbar id="progress9" valueCurrent={50} ariaLabel="Progress bar" />)
106
90
  const progressbar = container.firstChild as HTMLElement
107
91
  expect(progressbar).toHaveAttribute('aria-label', 'Progress bar')
108
92
  expect(progressbar).not.toHaveAttribute('aria-labelledby')
@@ -117,9 +101,7 @@ describe('PktProgressbar', () => {
117
101
  })
118
102
 
119
103
  test('handles id for title and aria-labelledby correctly', () => {
120
- const { container } = render(
121
- <PktProgressbar valueCurrent={50} title="Progress" id="progressId" />,
122
- )
104
+ const { container } = render(<PktProgressbar valueCurrent={50} title="Progress" id="progressId" />)
123
105
 
124
106
  const progressbar = container.firstChild as HTMLElement
125
107
 
@@ -132,15 +114,12 @@ describe('PktProgressbar', () => {
132
114
 
133
115
  const progressbar = container.firstChild as HTMLElement
134
116
 
135
- const id = progressbar?.getAttribute('id')
136
- expect(id).toHaveLength(36)
117
+ expect(progressbar.id).toMatch(/^[\w-]+$/)
137
118
  })
138
119
 
139
120
  test('sets ref correctly', () => {
140
121
  const ref = createRef<HTMLDivElement>()
141
- const { unmount } = render(
142
- <PktProgressbar ref={ref} id="progress-ref" valueCurrent={50} title="Ref bar" />,
143
- )
122
+ const { unmount } = render(<PktProgressbar ref={ref} id="progress-ref" valueCurrent={50} title="Ref bar" />)
144
123
 
145
124
  expect(ref.current).toBeInstanceOf(HTMLDivElement)
146
125
  unmount()
@@ -1,7 +1,7 @@
1
1
  'use client'
2
2
 
3
3
  import classNames from 'classnames'
4
- import { forwardRef, HTMLAttributes, Ref, useEffect, useMemo, useRef, useState } from 'react'
4
+ import { forwardRef, HTMLAttributes, Ref, useEffect, useRef, useState } from 'react'
5
5
  import type {
6
6
  TAriaLive,
7
7
  TProgressbarRole,
@@ -10,9 +10,15 @@ import type {
10
10
  TProgressbarStatusType,
11
11
  TProgressbarTitlePosition,
12
12
  } from 'shared-types'
13
- import { uuidish } from 'shared-utils/utils'
13
+ import { useGeneratedId } from '../../hooks/useGeneratedId'
14
14
 
15
- export type { TProgressbarRole, TProgressbarSkin, TProgressbarStatusPlacement, TProgressbarStatusType, TProgressbarTitlePosition }
15
+ export type {
16
+ TProgressbarRole,
17
+ TProgressbarSkin,
18
+ TProgressbarStatusPlacement,
19
+ TProgressbarStatusType,
20
+ TProgressbarTitlePosition,
21
+ }
16
22
 
17
23
  export interface IPktProgressbar extends Omit<HTMLAttributes<HTMLDivElement>, 'role' | 'title'> {
18
24
  ariaLabel?: string | null
@@ -54,8 +60,7 @@ export const PktProgressbar = forwardRef<HTMLDivElement, IPktProgressbar>(
54
60
  },
55
61
  ref,
56
62
  ) => {
57
- const generatedId = useMemo(() => uuidish(), [])
58
- const progressbarId = id || generatedId
63
+ const progressbarId = useGeneratedId(id)
59
64
 
60
65
  const labelRef = useRef<HTMLSpanElement>(null)
61
66
  const [labelWidth, setLabelWidth] = useState(0)
@@ -79,9 +84,7 @@ export const PktProgressbar = forwardRef<HTMLDivElement, IPktProgressbar>(
79
84
  ? `${valueCurrent} av ${valueMax - valueMin}`
80
85
  : ariaValueText || undefined
81
86
 
82
- const computedAriaLabelledby = !ariaLabel
83
- ? ariaLabelledby || `${progressbarId}-title`
84
- : undefined
87
+ const computedAriaLabelledby = !ariaLabel ? ariaLabelledby || `${progressbarId}-title` : undefined
85
88
 
86
89
  return (
87
90
  <div
@@ -101,10 +104,12 @@ export const PktProgressbar = forwardRef<HTMLDivElement, IPktProgressbar>(
101
104
  >
102
105
  <div
103
106
  className="pkt-progressbar__container"
104
- style={{
105
- '--pkt-progress-label-width': `${labelWidth}px`,
106
- '--pkt-progress-width': `${currentPercentage}%`,
107
- } as React.CSSProperties}
107
+ style={
108
+ {
109
+ '--pkt-progress-label-width': `${labelWidth}px`,
110
+ '--pkt-progress-width': `${currentPercentage}%`,
111
+ } as React.CSSProperties
112
+ }
108
113
  >
109
114
  {title && (
110
115
  <p
@@ -1,11 +1,11 @@
1
1
  'use client'
2
2
 
3
- import { ChangeEvent, forwardRef, HTMLProps, ReactNode, useMemo } from 'react'
4
- import { uuidish } from 'shared-utils/utils'
3
+ import { ChangeEvent, forwardRef, HTMLProps, ReactNode } from 'react'
5
4
 
6
5
  import type { IPktSearchInputSuggestion } from 'shared-types/searchinput'
7
6
 
8
7
  import { useFocusModality } from '../../hooks/useFocusModality'
8
+ import { useGeneratedId } from '../../hooks/useGeneratedId'
9
9
  import { PktButton } from '../button/Button'
10
10
  import { usePktStrings } from '../../hooks/usePktStrings'
11
11
  import type { IPktStringsProps } from 'shared-strings'
@@ -96,8 +96,7 @@ export const PktSearchInput = forwardRef<HTMLInputElement, ISearchInput | ISearc
96
96
  .filter(Boolean)
97
97
  .join(' ')
98
98
 
99
- const generatedId = useMemo(() => uuidish(), [])
100
- const id = idProp || generatedId
99
+ const id = useGeneratedId(idProp)
101
100
 
102
101
  const focusModality = useFocusModality()
103
102
 
@@ -1,46 +1,14 @@
1
1
  // @vitest-environment node
2
- import { type ElementType, Fragment } from 'react'
3
2
  import { renderToString } from 'react-dom/server'
4
- import { MemoryRouter } from 'react-router-dom'
5
3
 
6
- import * as components from './index'
7
-
8
- const requiredProps: Record<string, Record<string, unknown>> = {
9
- PktBreadcrumbs: {
10
- breadcrumbs: [
11
- { text: 'Start', href: '/' },
12
- { text: 'Side', href: '/side' },
13
- ],
14
- },
15
- PktTabItem: { index: 0 },
16
- }
17
-
18
- const parents: Record<string, ElementType> = {
19
- PktTabItem: components.PktTabs,
20
- }
21
-
22
- const isComponent = (value: unknown) =>
23
- typeof value === 'function' || (typeof value === 'object' && value !== null && '$$typeof' in value)
24
-
25
- const entries = Object.entries(components as Record<string, unknown>)
26
- .filter(([name, value]) => name.startsWith('Pkt') && isComponent(value))
27
- .map(([name, value]) => [name, value as ElementType] as [string, ElementType])
4
+ import { serverRenderCases } from '../utils/serverRenderCases'
28
5
 
29
6
  describe('server rendering', () => {
30
7
  test('runs without window', () => {
31
8
  expect(typeof window).toBe('undefined')
32
9
  })
33
10
 
34
- test.each(entries)('%s renders without throwing', (name, Component) => {
35
- const Parent = parents[name] ?? Fragment
36
- expect(() =>
37
- renderToString(
38
- <MemoryRouter>
39
- <Parent>
40
- <Component {...requiredProps[name]} />
41
- </Parent>
42
- </MemoryRouter>,
43
- ),
44
- ).not.toThrow()
11
+ test.each(serverRenderCases)('%s renders without throwing', (_name, element) => {
12
+ expect(() => renderToString(element())).not.toThrow()
45
13
  })
46
14
  })