@oslokommune/punkt-react 13.13.2 → 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.13.2",
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": "7707f0d8e3d999702c6b5bf2d4986d8b75307ffb"
109
+ "gitHead": "4fdac01524382592f47b2a77735477a5a0aa6d1e"
110
110
  }
@@ -0,0 +1,112 @@
1
+ import { render, screen } from '@testing-library/react'
2
+
3
+ import { PktLink } from './Link'
4
+
5
+ describe('PktLink', () => {
6
+ it('should render with children element', () => {
7
+ render(
8
+ <PktLink href="/test">
9
+ Test <strong>Link</strong>
10
+ </PktLink>,
11
+ )
12
+ const link = screen.getByRole('link')
13
+ expect(link.innerHTML).toEqual('Test <strong>Link</strong>')
14
+ })
15
+
16
+ it('should apply href attribute', () => {
17
+ render(<PktLink href="/test-url">Test Link</PktLink>)
18
+ const link = screen.getByText('Test Link')
19
+ expect(link).toHaveAttribute('href', '/test-url')
20
+ })
21
+
22
+ it('should apply base pkt-link class', () => {
23
+ render(<PktLink href="/test">Test Link</PktLink>)
24
+ const link = screen.getByText('Test Link')
25
+ expect(link).toHaveClass('pkt-link')
26
+ })
27
+
28
+ it('should apply custom className in addition to base class', () => {
29
+ render(
30
+ <PktLink href="/test" className="custom-class">
31
+ Test Link
32
+ </PktLink>,
33
+ )
34
+ const link = screen.getByText('Test Link')
35
+ expect(link).toHaveClass('pkt-link')
36
+ expect(link).toHaveClass('custom-class')
37
+ })
38
+
39
+ it.each`
40
+ description | iconName | iconPosition | expectedClasses | notExpectedClasses
41
+ ${'icon-left class when iconName and no iconPosition'} | ${'arrow'} | ${undefined} | ${['pkt-link--icon-left']} | ${['pkt-link--icon-right']}
42
+ ${'icon-left class when iconPosition is "left"'} | ${'arrow'} | ${'left'} | ${['pkt-link--icon-left']} | ${['pkt-link--icon-right']}
43
+ ${'icon-right class when iconPosition is "right"'} | ${'arrow'} | ${'right'} | ${['pkt-link--icon-right']} | ${['pkt-link--icon-left']}
44
+ ${'no icon classes when iconName is not provided'} | ${undefined} | ${undefined} | ${[]} | ${['pkt-link--icon-left', 'pkt-link--icon-right']}
45
+ ${'no icon classes when iconName is not provided'} | ${undefined} | ${'left'} | ${[]} | ${['pkt-link--icon-left', 'pkt-link--icon-right']}
46
+ `('should apply $description', ({ iconName, iconPosition, expectedClasses, notExpectedClasses }) => {
47
+ render(
48
+ <PktLink href="/test" iconName={iconName} iconPosition={iconPosition}>
49
+ Test Link
50
+ </PktLink>,
51
+ )
52
+ const link = screen.getByText('Test Link')
53
+ expectedClasses.forEach((className: string) => {
54
+ expect(link).toHaveClass(className)
55
+ })
56
+ notExpectedClasses.forEach((className: string) => {
57
+ expect(link).not.toHaveClass(className)
58
+ })
59
+ const icon = document.querySelector('.pkt-link__icon')
60
+ if (iconName) {
61
+ expect(icon).toBeInTheDocument();
62
+ } else {
63
+ expect(icon).not.toBeInTheDocument();
64
+ }
65
+ })
66
+
67
+ it.each`
68
+ description | external | hasExternalClass | relAttribute
69
+ ${'external class and rel when external is true'} | ${true} | ${true} | ${'noopener noreferrer'}
70
+ ${'no external class or rel when external is false'} | ${false} | ${false} | ${null}
71
+ ${'no external class or rel when external is undefined'} | ${undefined} | ${false} | ${null}
72
+ `('should apply $description', ({ external, hasExternalClass, relAttribute }) => {
73
+ render(
74
+ <PktLink href="/test" external={external}>
75
+ Test Link
76
+ </PktLink>,
77
+ )
78
+ const link = screen.getByText('Test Link')
79
+
80
+ if (hasExternalClass) {
81
+ expect(link).toHaveClass('pkt-link--external')
82
+ } else {
83
+ expect(link).not.toHaveClass('pkt-link--external')
84
+ }
85
+
86
+ if (relAttribute) {
87
+ expect(link).toHaveAttribute('rel', relAttribute)
88
+ } else {
89
+ expect(link).not.toHaveAttribute('rel')
90
+ }
91
+ })
92
+
93
+ it('should apply target attribute', () => {
94
+ render(
95
+ <PktLink href="/test" target="_blank">
96
+ Test Link
97
+ </PktLink>,
98
+ )
99
+ const link = screen.getByText('Test Link')
100
+ expect(link).toHaveAttribute('target', '_blank')
101
+ })
102
+
103
+ it('should spread additional props to anchor element', () => {
104
+ render(
105
+ <PktLink href="/test" data-testid="custom-link" aria-label="Custom label">
106
+ Test Link
107
+ </PktLink>,
108
+ )
109
+ const link = screen.getByTestId('custom-link')
110
+ expect(link).toHaveAttribute('aria-label', 'Custom label')
111
+ })
112
+ })
@@ -1,41 +1,48 @@
1
1
  'use client'
2
2
 
3
- import React, {
4
- FC,
5
- ForwardedRef,
6
- forwardRef,
7
- ForwardRefExoticComponent,
8
- LegacyRef,
9
- LinkHTMLAttributes,
10
- ReactElement,
11
- } from 'react'
12
- import { createComponent } from '@lit/react'
13
- import { PktLink as PktElLink } from '@oslokommune/punkt-elements'
3
+ import classNames from 'classnames'
4
+ import { FC, LinkHTMLAttributes } from 'react'
5
+
6
+ import { PktIcon } from '..'
14
7
 
15
8
  interface IPktLink extends LinkHTMLAttributes<HTMLAnchorElement> {
16
9
  href?: string
17
10
  iconName?: string | undefined
11
+ className?: string | undefined
18
12
  iconPosition?: string | undefined
19
13
  external?: boolean
20
- target?: string | null
21
- ref?: LegacyRef<HTMLAnchorElement>
14
+ target?: string | undefined
22
15
  }
23
16
 
24
- const LitComponent = createComponent({
25
- tagName: 'pkt-link',
26
- elementClass: PktElLink,
27
- react: React,
28
- displayName: 'PktLink',
29
- }) as ForwardRefExoticComponent<IPktLink>
17
+ export const PktLink: FC<IPktLink> = ({
18
+ href,
19
+ iconName,
20
+ className,
21
+ iconPosition,
22
+ external,
23
+ target,
24
+ children,
25
+ ...props
26
+ }: IPktLink) => {
27
+ const classes = {
28
+ 'pkt-link': true,
29
+ 'pkt-link--icon-left': (!!iconName && iconPosition === 'left') || !!(iconName && !iconPosition),
30
+ 'pkt-link--icon-right': !!iconName && iconPosition === 'right',
31
+ 'pkt-link--external': external,
32
+ }
30
33
 
31
- export const PktLink: FC<IPktLink> = forwardRef(
32
- ({ children, ...props }: IPktLink, ref: LegacyRef<HTMLAnchorElement>): ReactElement => {
33
- return (
34
- <LitComponent {...props} ref={ref}>
35
- <span className="pkt-contents">{children}</span>
36
- </LitComponent>
37
- )
38
- },
39
- )
34
+ return (
35
+ <a
36
+ {...props}
37
+ className={classNames(classes, className)}
38
+ href={href}
39
+ target={target}
40
+ rel={external ? 'noopener noreferrer' : undefined}
41
+ >
42
+ {iconName && <PktIcon name={iconName} className="pkt-link__icon" />}
43
+ {children}
44
+ </a>
45
+ )
46
+ }
40
47
 
41
48
  PktLink.displayName = 'PktLink'
@@ -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