@oslokommune/punkt-react 13.14.0 → 13.15.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/CHANGELOG.md +38 -0
- package/dist/index.d.ts +24 -10
- package/dist/punkt-react.es.js +1388 -1340
- package/dist/punkt-react.umd.js +118 -118
- package/package.json +3 -6
- package/src/components/tag/Tag.test.tsx +31 -17
- package/src/components/tag/Tag.tsx +108 -24
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oslokommune/punkt-react",
|
|
3
|
-
"version": "13.
|
|
3
|
+
"version": "13.15.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",
|
|
@@ -42,9 +42,7 @@
|
|
|
42
42
|
"@oslokommune/punkt-elements": "^13.13.2",
|
|
43
43
|
"classnames": "^2.5.1",
|
|
44
44
|
"prettier": "^3.3.3",
|
|
45
|
-
"react-
|
|
46
|
-
"react-hook-form": "^7.53.0",
|
|
47
|
-
"react-syntax-highlighter": "^15.6.6"
|
|
45
|
+
"react-hook-form": "^7.53.0"
|
|
48
46
|
},
|
|
49
47
|
"devDependencies": {
|
|
50
48
|
"@babel/plugin-transform-private-property-in-object": "^7.25.9",
|
|
@@ -58,7 +56,6 @@
|
|
|
58
56
|
"@types/node": "^20.12.10",
|
|
59
57
|
"@types/react": "^18.3.5",
|
|
60
58
|
"@types/react-dom": "^18.3.0",
|
|
61
|
-
"@types/react-syntax-highlighter": "^15.5.13",
|
|
62
59
|
"@vitejs/plugin-react": "^4.3.1",
|
|
63
60
|
"eslint": "^9.37.0",
|
|
64
61
|
"eslint-config-prettier": "^9.1.0",
|
|
@@ -106,5 +103,5 @@
|
|
|
106
103
|
"url": "https://github.com/oslokommune/punkt/issues"
|
|
107
104
|
},
|
|
108
105
|
"license": "MIT",
|
|
109
|
-
"gitHead": "
|
|
106
|
+
"gitHead": "4d233101e6a9c3512c9d2b11279f0dabc813e44f"
|
|
110
107
|
}
|
|
@@ -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 {
|
|
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<
|
|
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
|
-
|
|
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
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
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?: (
|
|
25
|
+
onClose?: () => void
|
|
26
|
+
children?: ReactNode
|
|
17
27
|
}
|
|
18
28
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
|