@oslokommune/punkt-react 16.19.0 → 16.20.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/CHANGELOG.md +18 -0
- package/dist/index.d.ts +4 -5
- package/dist/punkt-react.es.js +2866 -2854
- package/dist/punkt-react.umd.js +146 -146
- package/package.json +2 -2
- package/src/components/helptext/Helptext.test.tsx +98 -0
- package/src/components/helptext/Helptext.tsx +84 -26
- package/src/components/inputwrapper/InputWrapper.tsx +10 -41
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oslokommune/punkt-react",
|
|
3
|
-
"version": "16.
|
|
3
|
+
"version": "16.20.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",
|
|
@@ -109,5 +109,5 @@
|
|
|
109
109
|
"url": "https://github.com/oslokommune/punkt/issues"
|
|
110
110
|
},
|
|
111
111
|
"license": "MIT",
|
|
112
|
-
"gitHead": "
|
|
112
|
+
"gitHead": "dee5033c347f7c10530f57d5dc05c5411b75033f"
|
|
113
113
|
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import '@testing-library/jest-dom'
|
|
2
|
+
|
|
3
|
+
import { fireEvent, render, screen } from '@testing-library/react'
|
|
4
|
+
import { vi } from 'vitest'
|
|
5
|
+
|
|
6
|
+
import { PktHelptext } from './Helptext'
|
|
7
|
+
|
|
8
|
+
describe('PktHelptext', () => {
|
|
9
|
+
test('renders the container without the has-helptext modifier when empty', () => {
|
|
10
|
+
const { container } = render(<PktHelptext forId="field" />)
|
|
11
|
+
|
|
12
|
+
const root = container.querySelector('.pkt-inputwrapper__helptext-container')
|
|
13
|
+
expect(root).toBeInTheDocument()
|
|
14
|
+
expect(root).not.toHaveClass('pkt-inputwrapper__has-helptext')
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
test('renders helptext string as HTML inside the helptext div', () => {
|
|
18
|
+
const { container } = render(<PktHelptext forId="field" helptext="Hjelp <strong>her</strong>" />)
|
|
19
|
+
|
|
20
|
+
const helptext = container.querySelector(`#field-helptext`)
|
|
21
|
+
expect(helptext).toBeInTheDocument()
|
|
22
|
+
expect(helptext?.querySelector('strong')).toHaveTextContent('her')
|
|
23
|
+
expect(container.querySelector('.pkt-inputwrapper__helptext-container')).toHaveClass(
|
|
24
|
+
'pkt-inputwrapper__has-helptext',
|
|
25
|
+
)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('renders helptext ReactNode directly', () => {
|
|
29
|
+
render(
|
|
30
|
+
<PktHelptext forId="field" helptext={<em data-testid="rn">Rich</em>} />,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
expect(screen.getByTestId('rn')).toHaveTextContent('Rich')
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test('renders children inside pkt-contents', () => {
|
|
37
|
+
const { container } = render(
|
|
38
|
+
<PktHelptext forId="field">
|
|
39
|
+
<span data-testid="slot">Slot content</span>
|
|
40
|
+
</PktHelptext>,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
const contents = container.querySelector('.pkt-contents')
|
|
44
|
+
expect(contents).toBeInTheDocument()
|
|
45
|
+
expect(contents?.querySelector('[data-testid="slot"]')).toHaveTextContent('Slot content')
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('renders the dropdown toggle button with the default label when no button text is provided', () => {
|
|
49
|
+
render(<PktHelptext forId="field" helptextDropdown="Mer info" />)
|
|
50
|
+
|
|
51
|
+
const button = screen.getByRole('button')
|
|
52
|
+
expect(button.querySelector('.pkt-btn__text')).toHaveTextContent('Les mer')
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('renders a custom helptextDropdownButton as HTML', () => {
|
|
56
|
+
render(
|
|
57
|
+
<PktHelptext
|
|
58
|
+
forId="field"
|
|
59
|
+
helptextDropdown="Mer info"
|
|
60
|
+
helptextDropdownButton="Vis <em>detaljer</em>"
|
|
61
|
+
/>,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
const button = screen.getByRole('button')
|
|
65
|
+
expect(button.querySelector('em')).toHaveTextContent('detaljer')
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('toggles the dropdown open class and calls onToggleHelpText', () => {
|
|
69
|
+
const onToggle = vi.fn()
|
|
70
|
+
const { container } = render(
|
|
71
|
+
<PktHelptext
|
|
72
|
+
forId="field"
|
|
73
|
+
helptextDropdown="Mer info"
|
|
74
|
+
onToggleHelpText={onToggle}
|
|
75
|
+
/>,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
const expandable = container.querySelector('.pkt-inputwrapper__helptext-expandable')
|
|
79
|
+
const innerHelptext = expandable?.querySelector('.pkt-inputwrapper__helptext')
|
|
80
|
+
expect(innerHelptext).toHaveClass('pkt-inputwrapper__helptext-expandable-closed')
|
|
81
|
+
|
|
82
|
+
fireEvent.click(screen.getByRole('button'))
|
|
83
|
+
expect(innerHelptext).toHaveClass('pkt-inputwrapper__helptext-expandable-open')
|
|
84
|
+
expect(onToggle).toHaveBeenLastCalledWith(true)
|
|
85
|
+
|
|
86
|
+
fireEvent.click(screen.getByRole('button'))
|
|
87
|
+
expect(innerHelptext).toHaveClass('pkt-inputwrapper__helptext-expandable-closed')
|
|
88
|
+
expect(onToggle).toHaveBeenLastCalledWith(false)
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('does not render an id on the helptext div when forId is missing', () => {
|
|
92
|
+
const { container } = render(<PktHelptext helptext="No id here" />)
|
|
93
|
+
|
|
94
|
+
const helptext = container.querySelector('.pkt-inputwrapper__helptext')
|
|
95
|
+
expect(helptext).toBeInTheDocument()
|
|
96
|
+
expect(helptext).not.toHaveAttribute('id')
|
|
97
|
+
})
|
|
98
|
+
})
|
|
@@ -1,37 +1,95 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
import
|
|
3
|
+
import classNames from 'classnames'
|
|
4
|
+
import { forwardRef, HTMLAttributes, ReactNode, useState } from 'react'
|
|
5
|
+
|
|
6
|
+
import { PktIcon } from '../icon/Icon'
|
|
7
|
+
|
|
8
|
+
const defaultDropdownButton = "Les mer <span class='pkt-sr-only'>om feltet</span>"
|
|
7
9
|
|
|
8
10
|
export interface IPktHelptextProps extends HTMLAttributes<HTMLDivElement> {
|
|
9
|
-
helptext?: string | ReactNode
|
|
10
|
-
helptextDropdown?: string | ReactNode
|
|
11
|
+
helptext?: string | ReactNode
|
|
12
|
+
helptextDropdown?: string | ReactNode
|
|
11
13
|
helptextDropdownButton?: string
|
|
12
14
|
forId?: string
|
|
13
|
-
|
|
14
|
-
onToggleHelpText?: (e: CustomEvent) => void
|
|
15
|
+
onToggleHelpText?: (isOpen: boolean) => void
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
18
|
+
const renderContent = (content: string | ReactNode | undefined): ReactNode => {
|
|
19
|
+
if (content === undefined || content === null || content === '') return null
|
|
20
|
+
if (typeof content === 'string') {
|
|
21
|
+
return <span dangerouslySetInnerHTML={{ __html: content }} />
|
|
22
|
+
}
|
|
23
|
+
return content
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const PktHelptext = forwardRef<HTMLDivElement, IPktHelptextProps>(function PktHelptext(
|
|
27
|
+
{
|
|
28
|
+
helptext,
|
|
29
|
+
helptextDropdown,
|
|
30
|
+
helptextDropdownButton,
|
|
31
|
+
forId,
|
|
32
|
+
onToggleHelpText,
|
|
33
|
+
className,
|
|
34
|
+
children,
|
|
35
|
+
...rest
|
|
34
36
|
},
|
|
35
|
-
|
|
37
|
+
ref,
|
|
38
|
+
) {
|
|
39
|
+
const [isOpen, setIsOpen] = useState(false)
|
|
40
|
+
|
|
41
|
+
const toggle = () => {
|
|
42
|
+
const next = !isOpen
|
|
43
|
+
setIsOpen(next)
|
|
44
|
+
onToggleHelpText?.(next)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const hasContent = Boolean(helptext || helptextDropdown || children)
|
|
48
|
+
|
|
49
|
+
const containerClasses = classNames(
|
|
50
|
+
'pkt-inputwrapper__helptext-container',
|
|
51
|
+
hasContent && 'pkt-inputwrapper__has-helptext',
|
|
52
|
+
className,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<div {...rest} ref={ref} className={containerClasses}>
|
|
57
|
+
<div className="pkt-inputwrapper__helptext" id={forId ? `${forId}-helptext` : undefined}>
|
|
58
|
+
{children && <div className="pkt-contents">{children}</div>}
|
|
59
|
+
{renderContent(helptext)}
|
|
60
|
+
</div>
|
|
61
|
+
{helptextDropdown && (
|
|
62
|
+
<div className="pkt-inputwrapper__helptext-expandable">
|
|
63
|
+
<button
|
|
64
|
+
type="button"
|
|
65
|
+
className="pkt-link pkt-link--icon-right pkt-btn pkt-btn--small pkt-btn--tertiary pkt-btn--icon-right"
|
|
66
|
+
onClick={toggle}
|
|
67
|
+
>
|
|
68
|
+
<PktIcon
|
|
69
|
+
className="pkt-btn__icon"
|
|
70
|
+
name={isOpen ? 'chevron-thin-up' : 'chevron-thin-down'}
|
|
71
|
+
/>
|
|
72
|
+
<span
|
|
73
|
+
className="pkt-btn__text"
|
|
74
|
+
dangerouslySetInnerHTML={{
|
|
75
|
+
__html: helptextDropdownButton ?? defaultDropdownButton,
|
|
76
|
+
}}
|
|
77
|
+
/>
|
|
78
|
+
</button>
|
|
79
|
+
<div
|
|
80
|
+
className={classNames(
|
|
81
|
+
'pkt-inputwrapper__helptext',
|
|
82
|
+
isOpen
|
|
83
|
+
? 'pkt-inputwrapper__helptext-expandable-open'
|
|
84
|
+
: 'pkt-inputwrapper__helptext-expandable-closed',
|
|
85
|
+
)}
|
|
86
|
+
>
|
|
87
|
+
{renderContent(helptextDropdown)}
|
|
88
|
+
</div>
|
|
89
|
+
</div>
|
|
90
|
+
)}
|
|
91
|
+
</div>
|
|
92
|
+
)
|
|
93
|
+
})
|
|
36
94
|
|
|
37
95
|
PktHelptext.displayName = 'PktHelptext'
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
|
|
3
|
-
import { ForwardedRef, forwardRef, type ReactNode, RefAttributes
|
|
3
|
+
import { ForwardedRef, forwardRef, type ReactNode, RefAttributes } from 'react'
|
|
4
4
|
|
|
5
5
|
import { useFocusModality } from '../../hooks/useFocusModality'
|
|
6
6
|
import { PktAlert } from '../alert/Alert'
|
|
7
|
-
import {
|
|
7
|
+
import { PktHelptext } from '../helptext/Helptext'
|
|
8
8
|
|
|
9
9
|
export interface IPktInputWrapper extends RefAttributes<HTMLElement> {
|
|
10
10
|
forId: string
|
|
@@ -63,8 +63,6 @@ export const PktInputWrapper = forwardRef(
|
|
|
63
63
|
}: IPktInputWrapper,
|
|
64
64
|
ref: ForwardedRef<HTMLDivElement>,
|
|
65
65
|
) => {
|
|
66
|
-
const [isHelpTextOpen, setIsHelpTextOpen] = useState(false)
|
|
67
|
-
|
|
68
66
|
const focusModality = useFocusModality<HTMLDivElement>(ref)
|
|
69
67
|
|
|
70
68
|
const describedBy = ariaDescribedby || (helptext ? `${forId}-helptext` : undefined)
|
|
@@ -72,8 +70,6 @@ export const PktInputWrapper = forwardRef(
|
|
|
72
70
|
const counterTop = showCounter && counterPosition === 'top'
|
|
73
71
|
const counterBottom = showCounter && counterPosition === 'bottom'
|
|
74
72
|
|
|
75
|
-
const toggleHelpText = () => setIsHelpTextOpen((prev) => !prev)
|
|
76
|
-
|
|
77
73
|
const wrapperClasses = [
|
|
78
74
|
'pkt-inputwrapper',
|
|
79
75
|
className,
|
|
@@ -134,43 +130,16 @@ export const PktInputWrapper = forwardRef(
|
|
|
134
130
|
}
|
|
135
131
|
|
|
136
132
|
const renderHelptext = () => {
|
|
137
|
-
|
|
133
|
+
const visibleHelptext = useWrapper ? helptext : undefined
|
|
134
|
+
if (!visibleHelptext && !helptextDropdown) return null
|
|
138
135
|
|
|
139
136
|
return (
|
|
140
|
-
|
|
141
|
-
{
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
{helptextDropdown && (
|
|
147
|
-
<div className="pkt-inputwrapper__helptext-expandable">
|
|
148
|
-
<PktButton
|
|
149
|
-
skin="tertiary"
|
|
150
|
-
size="small"
|
|
151
|
-
variant="icon-right"
|
|
152
|
-
iconName={isHelpTextOpen ? 'chevron-thin-up' : 'chevron-thin-down'}
|
|
153
|
-
className="pkt-link pkt-link--icon-right"
|
|
154
|
-
onClick={toggleHelpText}
|
|
155
|
-
>
|
|
156
|
-
<span
|
|
157
|
-
dangerouslySetInnerHTML={{
|
|
158
|
-
__html: helptextDropdownButton ?? 'Les mer <span class="pkt-sr-only">om inputfeltet</span>',
|
|
159
|
-
}}
|
|
160
|
-
/>
|
|
161
|
-
</PktButton>
|
|
162
|
-
<div
|
|
163
|
-
className={`pkt-inputwrapper__helptext ${
|
|
164
|
-
isHelpTextOpen
|
|
165
|
-
? 'pkt-inputwrapper__helptext-expandable-open'
|
|
166
|
-
: 'pkt-inputwrapper__helptext-expandable-closed'
|
|
167
|
-
}`}
|
|
168
|
-
>
|
|
169
|
-
{helptextDropdown}
|
|
170
|
-
</div>
|
|
171
|
-
</div>
|
|
172
|
-
)}
|
|
173
|
-
</>
|
|
137
|
+
<PktHelptext
|
|
138
|
+
forId={forId}
|
|
139
|
+
helptext={visibleHelptext}
|
|
140
|
+
helptextDropdown={helptextDropdown}
|
|
141
|
+
helptextDropdownButton={helptextDropdownButton}
|
|
142
|
+
/>
|
|
174
143
|
)
|
|
175
144
|
}
|
|
176
145
|
|