@oslokommune/punkt-react 13.9.1 → 13.10.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslokommune/punkt-react",
3
- "version": "13.9.1",
3
+ "version": "13.10.1",
4
4
  "description": "React komponentbibliotek til Punkt, et designsystem laget av Oslo Origo",
5
5
  "homepage": "https://punkt.oslo.kommune.no",
6
6
  "author": "Team Designsystem, Oslo Origo",
@@ -40,6 +40,7 @@
40
40
  "@lit-labs/ssr-dom-shim": "^1.2.1",
41
41
  "@lit/react": "^1.0.7",
42
42
  "@oslokommune/punkt-elements": "^13.9.1",
43
+ "classnames": "^2.5.1",
43
44
  "prettier": "^3.3.3",
44
45
  "react-element-to-jsx-string": "^15.0.0",
45
46
  "react-hook-form": "^7.53.0",
@@ -59,7 +60,6 @@
59
60
  "@types/react-dom": "^18.3.0",
60
61
  "@types/react-syntax-highlighter": "^15.5.13",
61
62
  "@vitejs/plugin-react": "^4.3.1",
62
- "classnames": "^2.5.1",
63
63
  "eslint": "^9.37.0",
64
64
  "eslint-config-prettier": "^9.1.0",
65
65
  "eslint-plugin-prettier": "^5.2.1",
@@ -106,5 +106,5 @@
106
106
  "url": "https://github.com/oslokommune/punkt/issues"
107
107
  },
108
108
  "license": "MIT",
109
- "gitHead": "44dcfeeeecbe7f3d5404f3d77f2de46689ccb80e"
109
+ "gitHead": "9390b12e766784c827247d15cde4ab8899a3c518"
110
110
  }
@@ -1,5 +1,4 @@
1
- import { createRef } from 'react'
2
- import { render, fireEvent } from '@testing-library/react'
1
+ import { render, fireEvent, waitFor } from '@testing-library/react'
3
2
  import { axe, toHaveNoViolations } from 'jest-axe'
4
3
  import { PktAlert } from './Alert'
5
4
 
@@ -12,15 +11,6 @@ expect.extend(toHaveNoViolations)
12
11
  afterEach(cleanup)
13
12
 
14
13
  describe('PktAlert', () => {
15
- test('ref works correctly', async () => {
16
- const ref = createRef<HTMLElement>()
17
- const { unmount } = render(<PktAlert ref={ref}>Alert text</PktAlert>)
18
- await window.customElements.whenDefined('pkt-alert')
19
- expect(ref.current).toBeInstanceOf(HTMLElement)
20
- unmount()
21
- expect(ref.current).toBeNull()
22
- })
23
-
24
14
  test('calls onClose when close button is clicked', async () => {
25
15
  const onClose = jest.fn()
26
16
  const { getByLabelText } = render(
@@ -28,14 +18,14 @@ describe('PktAlert', () => {
28
18
  Hello World
29
19
  </PktAlert>,
30
20
  )
31
- await window.customElements.whenDefined('pkt-alert')
32
21
  fireEvent.click(getByLabelText('close'))
33
- expect(onClose).toHaveBeenCalledTimes(1)
22
+ await waitFor(() => {
23
+ expect(onClose).toHaveBeenCalledTimes(1)
24
+ })
34
25
  })
35
26
 
36
27
  test('renders with default role', async () => {
37
28
  const { getByRole } = render(<PktAlert>Alert text</PktAlert>)
38
- await window.customElements.whenDefined('pkt-alert')
39
29
  const alert = getByRole('status')
40
30
  expect(alert).toBeInTheDocument()
41
31
  expect(alert).toHaveAttribute('role', 'status')
@@ -43,16 +33,54 @@ describe('PktAlert', () => {
43
33
 
44
34
  test('renders with alert as role prop', async () => {
45
35
  const { getByRole } = render(<PktAlert role="alert">Alert text</PktAlert>)
46
- await window.customElements.whenDefined('pkt-alert')
47
36
  const alert = getByRole('alert')
48
37
  expect(alert).toBeInTheDocument()
49
38
  expect(alert).toHaveAttribute('role', 'alert')
50
39
  })
51
40
 
41
+ test.each`
42
+ camelCase | kebabCase | rendered
43
+ ${'assertive'} | ${undefined} | ${'assertive'}
44
+ ${undefined} | ${'assertive'} | ${'assertive'}
45
+ ${'polite'} | ${undefined} | ${'polite'}
46
+ ${undefined} | ${undefined} | ${'polite'}
47
+ ${'off'} | ${'assertive'} | ${'off'}
48
+ `(
49
+ 'should support both ariaLive ($camelCase) and aria-live ($kebabCase) attributes ',
50
+ async ({ camelCase, kebabCase, rendered }) => {
51
+ const { container } = render(
52
+ <PktAlert ariaLive={camelCase} aria-live={kebabCase}>
53
+ Alert text
54
+ </PktAlert>,
55
+ )
56
+ const elementThatHasAriaLive = container.querySelector('*[aria-live]')
57
+ expect(elementThatHasAriaLive).toHaveAttribute('aria-live', rendered)
58
+ },
59
+ )
60
+
61
+ test.each`
62
+ title | expected
63
+ ${'Simple title'} | ${'Simple title'}
64
+ ${''} | ${undefined}
65
+ ${null} | ${undefined}
66
+ ${(
67
+ <span>
68
+ Complex <strong>title</strong>
69
+ </span>
70
+ )} | ${'<span>Complex <strong>title</strong></span>'}
71
+ `('support complex title %$', async ({title, expected}) => {
72
+ const { container } = render(
73
+ <PktAlert title={title}>
74
+ Alert text
75
+ </PktAlert>,
76
+ )
77
+ const titleElement = container.querySelector('.pkt-alert__title');
78
+ expect(titleElement?.innerHTML).toEqual(expected)
79
+ })
80
+
52
81
  describe('accessibility', () => {
53
82
  test('renders with no wcag errors with axe', async () => {
54
83
  const { container } = render(<PktAlert title="Error" skin="error"></PktAlert>)
55
- await window.customElements.whenDefined('pkt-alert')
56
84
  const results = await axe(container)
57
85
 
58
86
  expect(results).toHaveNoViolations()
@@ -1,40 +1,91 @@
1
1
  'use client'
2
2
 
3
- import { FC, ForwardedRef, forwardRef, ReactElement } from 'react'
4
- import * as React from 'react'
5
- import { createComponent, EventName } from '@lit/react'
6
- import { PktAlert as PktElAlert } from '@oslokommune/punkt-elements'
7
- import type { PktElType, PktElConstructor } from '@/interfaces/IPktElements'
3
+ import { ReactNode, useState } from 'react'
4
+ import { FC, HTMLAttributes, ReactElement, useCallback } from 'react'
5
+ import classNames from 'classnames'
6
+ import { PktButton, PktIcon } from '..'
7
+ import { TAlertRole, TAriaLive } from '@/types/aria'
8
8
 
9
- export interface IPktAlert extends PktElType {
9
+ export interface IPktAlert extends Omit<HTMLAttributes<HTMLDivElement>, 'title'> {
10
10
  skin?: 'error' | 'success' | 'warning' | 'info'
11
11
  closeAlert?: boolean
12
- title?: string
12
+ title?: ReactNode
13
13
  date?: string
14
- ariaLive?: 'off' | 'polite' | 'assertive'
14
+ ariaLive?: TAriaLive
15
15
  compact?: boolean
16
- role?: string
16
+ role?: TAlertRole
17
17
  onClose?: (e: CustomEvent) => void
18
18
  }
19
19
 
20
- const LitComponent: FC<IPktAlert> = createComponent({
21
- tagName: 'pkt-alert',
22
- elementClass: PktElAlert as PktElConstructor<HTMLElement>,
23
- react: React,
24
- displayName: 'PktAlert',
25
- events: {
26
- onClose: 'close' as EventName<CustomEvent>,
27
- },
28
- })
29
-
30
- export const PktAlert: FC<IPktAlert> = forwardRef(
31
- ({ children, ...props }: IPktAlert, ref: ForwardedRef<HTMLElement>): ReactElement => {
32
- return (
33
- <LitComponent ref={ref} {...props}>
34
- <div className="pkt-contents">{children}</div>
35
- </LitComponent>
36
- )
37
- },
38
- )
20
+ export type TAlertSkin = 'error' | 'success' | 'warning' | 'info'
21
+
22
+ export const PktAlert: FC<IPktAlert> = ({
23
+ children,
24
+ closeAlert,
25
+ compact,
26
+ title,
27
+ date,
28
+ ariaLive: ariaLiveCamel,
29
+ 'aria-live': ariaLiveKebab = 'polite' as TAriaLive,
30
+ role = 'status',
31
+ skin = 'info' as TAlertSkin,
32
+ ...props
33
+ }: IPktAlert): ReactElement => {
34
+ const [isClosed, setIsClosed] = useState(false)
35
+
36
+ const classes = {
37
+ 'pkt-alert': true,
38
+ 'pkt-alert--compact': compact,
39
+ [`pkt-alert--${skin}`]: skin,
40
+ 'pkt-hide': isClosed,
41
+ }
42
+ const gridClasses = {
43
+ 'pkt-alert__grid': true,
44
+ 'pkt-alert__noTitle': !title,
45
+ 'pkt-alert__noDate': !date,
46
+ }
47
+
48
+ const onClose = useCallback(() => {
49
+ setIsClosed(true)
50
+ props.onClose &&
51
+ props.onClose(new CustomEvent('close', { detail: { origin: event }, bubbles: true, composed: true }))
52
+ }, [props.onClose, setIsClosed])
53
+
54
+ const ariaLive = ariaLiveCamel || ariaLiveKebab
55
+
56
+ return (
57
+ <div {...props} aria-live={ariaLive} role={role} className={classNames(classes)}>
58
+ <div className={classNames(gridClasses)}>
59
+ <PktIcon
60
+ className="pkt-alert__icon"
61
+ aria-hidden="true"
62
+ name={skin === 'info' ? 'alert-information' : `alert-${skin}`}
63
+ ></PktIcon>
64
+
65
+ {closeAlert && (
66
+ <div className="pkt-alert__close">
67
+ <PktButton
68
+ tabIndex={0}
69
+ aria-label="close"
70
+ size={compact ? 'small' : 'medium'}
71
+ type="button"
72
+ skin="tertiary"
73
+ iconName="close"
74
+ variant="icon-only"
75
+ onClick={onClose}
76
+ >
77
+ <span className="sr-only">Lukk</span>
78
+ </PktButton>
79
+ </div>
80
+ )}
81
+ {title && <div className="pkt-alert__title">{title}</div>}
82
+
83
+ <div className="pkt-alert__text">{children}</div>
84
+
85
+ {date && <div className="pkt-alert__date">Sist oppdatert: {date}</div>}
86
+ </div>
87
+ </div>
88
+ )
89
+ }
39
90
 
40
91
  PktAlert.displayName = 'PktAlert'
@@ -1,6 +1,6 @@
1
1
  import '@testing-library/jest-dom'
2
2
 
3
- import { fireEvent, render } from '@testing-library/react'
3
+ import { fireEvent, render, screen } from '@testing-library/react'
4
4
  import { axe, toHaveNoViolations } from 'jest-axe'
5
5
  import React from 'react'
6
6
 
@@ -32,21 +32,13 @@ describe('PktSelect', () => {
32
32
  })
33
33
 
34
34
  test('renders error message when hasError prop is true', async () => {
35
- const { getByText, getByRole } = render(
35
+ render(
36
36
  <PktSelect label="Input Label" id="inputId" hasError errorMessage="Input error" />,
37
37
  )
38
-
39
- await window.customElements.whenDefined('pkt-alert')
40
- const errorMessage = getByText('Input error')
41
- expect(errorMessage).toBeInTheDocument()
42
-
43
- const alertContainer = errorMessage.closest('pkt-alert')
44
- expect(alertContainer).toHaveAttribute('id', 'inputId-input-error')
38
+ const alertContainer = screen.getByRole('alert')
39
+ const errorMessageId = 'inputId-input-error' // når https://github.com/oslokommune/punkt/issues/3092 er fiksa: screen.getByRole('combobox').getAttribute('aria-errormessage')
40
+ expect(alertContainer).toHaveAttribute('id', errorMessageId)
45
41
  expect(alertContainer).toHaveTextContent('Input error')
46
-
47
- const alert = getByRole('alert')
48
- expect(alert).toBeInTheDocument()
49
- expect(alert).toHaveAttribute('role', 'alert')
50
42
  })
51
43
 
52
44
  describe('PktSelect', () => {
@@ -1,6 +1,6 @@
1
1
  import '@testing-library/jest-dom'
2
2
 
3
- import { fireEvent, render } from '@testing-library/react'
3
+ import { fireEvent, render, screen } from '@testing-library/react'
4
4
  import { axe, toHaveNoViolations } from 'jest-axe'
5
5
  import React from 'react'
6
6
 
@@ -19,14 +19,11 @@ describe('PktTextarea', () => {
19
19
  })
20
20
 
21
21
  test('renders error message when hasError prop is true', async () => {
22
- const { getByText } = render(<PktTextarea label="Input Label" id="inputId" hasError errorMessage="Input error" />)
23
-
24
- await window.customElements.whenDefined('pkt-alert')
25
- const errorMessage = getByText('Input error')
26
- expect(errorMessage).toBeInTheDocument()
27
- const alertContainer = errorMessage.closest('pkt-alert')
28
-
29
- expect(alertContainer).toHaveAttribute('id', 'inputId-input-error')
22
+ render(<PktTextarea label="Input Label" id="inputId" hasError errorMessage="Input error" />)
23
+ const errorMessageId = 'inputId-input-error' // når https://github.com/oslokommune/punkt/issues/3093 er fiksa: screen.getByRole('textbox').getAttribute('aria-errormessage')
24
+ const alertContainer = screen.getByRole('alert')
25
+ expect(alertContainer).toHaveAttribute('id', errorMessageId)
26
+ expect(alertContainer).toHaveTextContent('Input error')
30
27
  })
31
28
 
32
29
  describe('PktTextarea', () => {
@@ -1,6 +1,6 @@
1
1
  import '@testing-library/jest-dom'
2
2
 
3
- import { fireEvent, render } from '@testing-library/react'
3
+ import { fireEvent, render, screen } from '@testing-library/react'
4
4
  import { axe, toHaveNoViolations } from 'jest-axe'
5
5
 
6
6
  import { PktTextinput } from './Textinput'
@@ -21,13 +21,13 @@ describe('PktTextinput', () => {
21
21
  })
22
22
 
23
23
  test('renders error message when hasError prop is true', async () => {
24
- const { getByText } = render(<PktTextinput label={label} id={inputId} hasError errorMessage="Input error" />)
24
+ render(<PktTextinput label={label} id={inputId} hasError errorMessage="Input error" />)
25
+
26
+ const errorMessageId = screen.getByRole('textbox').getAttribute('aria-errormessage')
27
+ const alertContainer = screen.getByRole('alert')
28
+ expect(alertContainer).toHaveAttribute('id', errorMessageId)
29
+ expect(alertContainer).toHaveTextContent('Input error')
25
30
 
26
- const errorMessage = getByText('Input error')
27
- expect(errorMessage).toBeInTheDocument()
28
- const alertContainer = errorMessage.closest('pkt-alert')
29
- expect(alertContainer).not.toBeNull()
30
- expect(alertContainer).toHaveAttribute('id', 'inputId-error')
31
31
  })
32
32
 
33
33
  describe('PktTextinput', () => {