@oslokommune/punkt-react 13.14.0 → 13.15.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslokommune/punkt-react",
3
- "version": "13.14.0",
3
+ "version": "13.15.0",
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",
@@ -106,5 +106,5 @@
106
106
  "url": "https://github.com/oslokommune/punkt/issues"
107
107
  },
108
108
  "license": "MIT",
109
- "gitHead": "fe689e7956b40608a560d8c8cd6ee32b5f408f27"
109
+ "gitHead": "4fdac01524382592f47b2a77735477a5a0aa6d1e"
110
110
  }
@@ -1,35 +1,41 @@
1
1
  import '@testing-library/jest-dom'
2
+
3
+ import { fireEvent, render as originalRender, RenderOptions, RenderResult } from '@testing-library/react'
2
4
  import { axe, toHaveNoViolations } from 'jest-axe'
3
- import { fireEvent, render, screen } from '@testing-library/react'
4
- import React, { createRef } from 'react'
5
- import userEvent from '@testing-library/user-event'
5
+ import { createRef, ReactNode } from 'react'
6
6
 
7
7
  import { PktTag } from './Tag'
8
8
 
9
9
  expect.extend(toHaveNoViolations)
10
10
 
11
+ const render = async (
12
+ ui: ReactNode,
13
+ options?: Omit<RenderOptions, 'queries'> | undefined,
14
+ ): Promise<RenderResult> => {
15
+ const renderResult = originalRender(ui, options)
16
+ await window.customElements.whenDefined('pkt-icon')
17
+ return Promise.resolve(renderResult)
18
+ }
19
+
11
20
  describe('PktTag', () => {
12
21
  describe('basic rendering', () => {
13
22
  it('renders correctly without any props', async () => {
14
- const { getByText } = render(<PktTag>Hello</PktTag>)
15
- await window.customElements.whenDefined('pkt-tag')
23
+ const { getByText } = await render(<PktTag>Hello</PktTag>)
16
24
  expect(getByText('Hello')).toBeInTheDocument()
17
25
  })
18
26
 
19
27
  it('renders an icon when iconName prop is provided', async () => {
20
- render(<PktTag iconName="user">Tag with Icon</PktTag>)
21
- await window.customElements.whenDefined('pkt-tag')
28
+ await render(<PktTag iconName="user">Tag with Icon</PktTag>)
22
29
  const iconElement = document.querySelector('.pkt-tag__icon.pkt-icon[name="user"]')
23
30
  expect(iconElement).toBeInTheDocument()
24
31
  })
25
32
 
26
33
  it('renders with the specified skin and size', async () => {
27
- const { container } = render(
34
+ const { container } = await render(
28
35
  <PktTag skin="red" size="small">
29
36
  Tag
30
37
  </PktTag>,
31
38
  )
32
- await window.customElements.whenDefined('pkt-tag')
33
39
 
34
40
  const spanElement = container.querySelector('span.pkt-tag')
35
41
 
@@ -38,40 +44,48 @@ describe('PktTag', () => {
38
44
  })
39
45
 
40
46
  it('forwardRef works correctly', async () => {
41
- const ref = createRef<HTMLElement>()
42
- const { unmount } = render(
47
+ const ref = createRef<HTMLButtonElement>()
48
+ const { unmount } = await render(
43
49
  <PktTag closeTag={true} ref={ref}>
44
50
  Click me
45
51
  </PktTag>,
46
52
  )
47
- await window.customElements.whenDefined('pkt-tag')
48
53
  expect(ref.current).toBeInstanceOf(HTMLElement)
49
54
  unmount()
50
55
  expect(ref.current).toBeNull()
51
56
  })
52
57
 
53
58
  it('closes the tag when the close button is clicked', async () => {
54
- const { container, getByText } = render(<PktTag closeTag={true}>Closeable Tag</PktTag>)
55
- await window.customElements.whenDefined('pkt-tag')
59
+ const { container, getByText } = await render(<PktTag closeTag={true}>Closeable Tag</PktTag>)
56
60
 
57
61
  const buttonElement = container.querySelector('.pkt-tag.pkt-btn')
58
62
  expect(buttonElement).not.toHaveClass('pkt-hide')
59
63
 
60
- await fireEvent.click(getByText('Closeable Tag'))
64
+ fireEvent.click(getByText('Closeable Tag'))
61
65
 
62
66
  expect(buttonElement).toBeInTheDocument()
63
67
  expect(buttonElement).toHaveClass('pkt-hide')
64
68
  })
69
+
70
+ it('calls onClose when tag is clicked', async () => {
71
+ const onClose = jest.fn()
72
+ const { getByText } = await render(
73
+ <PktTag closeTag={true} onClose={onClose}>
74
+ Closeable Tag
75
+ </PktTag>,
76
+ )
77
+ fireEvent.click(getByText('Closeable Tag'))
78
+ expect(onClose).toHaveBeenCalled()
79
+ })
65
80
  })
66
81
 
67
82
  describe('accessibility', () => {
68
83
  it('renders with no wcag errors with axe', async () => {
69
- const { container } = render(
84
+ const { container } = await render(
70
85
  <PktTag skin="red" size="small" iconName="user" closeTag={true}>
71
86
  Tag
72
87
  </PktTag>,
73
88
  )
74
- await window.customElements.whenDefined('pkt-tag')
75
89
  const results = await axe(container)
76
90
  expect(results).toHaveNoViolations()
77
91
  })
@@ -1,38 +1,122 @@
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 { PktTag as PktElTag } from '@oslokommune/punkt-elements'
7
- import type { PktElType, PktElConstructor } from '@/interfaces/IPktElements'
3
+ import classNames from 'classnames'
4
+ import {
5
+ ForwardedRef,
6
+ forwardRef,
7
+ HTMLAttributes,
8
+ ReactNode,
9
+ RefObject,
10
+ useCallback,
11
+ useMemo,
12
+ useRef,
13
+ useState,
14
+ } from 'react'
8
15
 
9
- export interface IPktTag extends PktElType {
16
+ import { PktIcon } from '..'
17
+
18
+ interface BasePktTagProps {
10
19
  skin?: 'blue' | 'blue-light' | 'blue-dark' | 'green' | 'red' | 'beige' | 'yellow' | 'grey' | 'gray'
11
20
  textStyle?: 'normal-text' | 'thin-text'
12
21
  size?: 'small' | 'medium' | 'large'
13
22
  closeTag?: boolean
14
23
  iconName?: string
15
24
  ariaLabel?: string
16
- onClose?: (e: CustomEvent) => void
25
+ onClose?: () => void
26
+ children?: ReactNode
17
27
  }
18
28
 
19
- const LitComponent: FC<IPktTag> = createComponent({
20
- tagName: 'pkt-tag',
21
- elementClass: PktElTag as PktElConstructor<HTMLElement>,
22
- react: React,
23
- displayName: 'PktTag',
24
- events: {
25
- onClose: 'close' as EventName<CustomEvent>,
26
- },
27
- })
28
-
29
- export const PktTag: FC<IPktTag> = forwardRef(
30
- ({ children, ...props }: IPktTag, ref: ForwardedRef<HTMLElement>): ReactElement => {
31
- return (
32
- <LitComponent ref={ref} {...props}>
33
- <div className="pkt-contents">{children}</div>
34
- </LitComponent>
35
- )
29
+ interface ButtonPktTagProps extends BasePktTagProps, HTMLAttributes<HTMLButtonElement> {
30
+ closeTag: true
31
+ type?: 'button' | 'submit' | 'reset'
32
+ }
33
+
34
+ interface SpanPktTagProps extends BasePktTagProps, HTMLAttributes<HTMLSpanElement> {
35
+ closeTag?: false | undefined
36
+ onClose?: undefined
37
+ type?: undefined
38
+ }
39
+
40
+ export type IPktTag = ButtonPktTagProps | SpanPktTagProps;
41
+
42
+ export const PktTag = forwardRef<HTMLSpanElement | HTMLButtonElement, ButtonPktTagProps | SpanPktTagProps>(
43
+ (
44
+ {
45
+ children,
46
+ skin,
47
+ textStyle,
48
+ size,
49
+ closeTag,
50
+ className,
51
+ iconName,
52
+ ariaLabel,
53
+ onClose,
54
+ type,
55
+ 'aria-description': ariaDescriptionPropValue,
56
+ ...props
57
+ }: ButtonPktTagProps | SpanPktTagProps,
58
+ forwardedRef: ForwardedRef<HTMLElement>,
59
+ ) => {
60
+ const [isClosed, setClosed] = useState<boolean>(false)
61
+
62
+ const close = useCallback(() => {
63
+ setClosed(true)
64
+ if (onClose) {
65
+ onClose()
66
+ }
67
+ }, [setClosed])
68
+
69
+ const spanRef: RefObject<HTMLButtonElement | HTMLSpanElement> = useRef<HTMLButtonElement | HTMLSpanElement>(null)
70
+
71
+ const ariaDescription = useMemo(() => {
72
+ if (closeTag && !ariaLabel) {
73
+ const label = spanRef.current?.textContent?.trim()
74
+ return (label && `Klikk for å fjerne ${label}`) || ariaDescriptionPropValue
75
+ }
76
+ }, [closeTag, ariaLabel])
77
+
78
+ const classes = {
79
+ 'pkt-tag': true,
80
+ [`pkt-tag--${size}`]: !!size,
81
+ [`pkt-tag--${skin}`]: !!skin,
82
+ [`pkt-tag--${textStyle}`]: !!textStyle,
83
+ }
84
+
85
+ const btnClasses = {
86
+ 'pkt-tag': true,
87
+ 'pkt-btn': true,
88
+ 'pkt-btn--tertiary': true,
89
+ [`pkt-tag--${textStyle}`]: !!textStyle,
90
+ [`pkt-tag--${size}`]: !!size,
91
+ [`pkt-tag--${skin}`]: !!skin,
92
+ 'pkt-btn--icons-right-and-left': closeTag && !!iconName,
93
+ 'pkt-hide': isClosed,
94
+ }
95
+
96
+ if (closeTag) {
97
+ return (
98
+ <button
99
+ {...props}
100
+ className={classNames(btnClasses, className)}
101
+ type={type}
102
+ onClick={close}
103
+ aria-label={ariaLabel}
104
+ aria-description={ariaDescription}
105
+ ref={forwardedRef as ForwardedRef<HTMLButtonElement>}
106
+ >
107
+ {iconName && <PktIcon className={'pkt-tag__icon'} name={iconName} />}
108
+ <span ref={spanRef}>{children}</span>
109
+ <PktIcon className={'pkt-tag__close-btn'} name={'close'} />
110
+ </button>
111
+ )
112
+ } else {
113
+ return (
114
+ <span {...props} className={classNames(classes, className)} ref={forwardedRef as ForwardedRef<HTMLSpanElement>}>
115
+ {iconName && <PktIcon className={'pkt-tag__icon'} name={iconName} />}
116
+ <span ref={spanRef}>{children}</span>
117
+ </span>
118
+ )
119
+ }
36
120
  },
37
121
  )
38
122