@oslokommune/punkt-react 13.11.0 → 13.12.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.11.0",
3
+ "version": "13.12.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": "f25b08c9f4382250d0097d22bc8541cfd459fe73"
109
+ "gitHead": "d3cc0fca4886e3b69ab5cbe305765cef483eab32"
110
110
  }
@@ -0,0 +1,63 @@
1
+ import { fireEvent, render as originalRender } from '@testing-library/react'
2
+ import { ReactNode } from 'react'
3
+
4
+ import { PktMessagebox } from './Messagebox'
5
+
6
+ describe('Messagebox', () => {
7
+ const render = (jsx: ReactNode) => {
8
+ const renderResult = originalRender(jsx)
9
+ const messageBox = renderResult.container.children[0]
10
+ return { ...renderResult, messageBox }
11
+ }
12
+
13
+ it('render with defaults', async () => {
14
+ const { messageBox } = render(<PktMessagebox />)
15
+
16
+ expect(messageBox.className).toEqual('pkt-messagebox')
17
+ })
18
+
19
+ it('render with skin', async () => {
20
+ const { messageBox } = render(<PktMessagebox skin={'green'} />)
21
+
22
+ expect(messageBox.classList).toContain('pkt-messagebox--green')
23
+ })
24
+
25
+ it('render compact', async () => {
26
+ const { messageBox } = render(<PktMessagebox compact />)
27
+
28
+ expect(messageBox.classList).toContain('pkt-messagebox--compact')
29
+ })
30
+
31
+ it('render simple title', async () => {
32
+ const { getByText } = render(<PktMessagebox title={'Messagebox title'} />)
33
+ const titleElement = getByText('Messagebox title')
34
+ expect(titleElement.classList).toContain('pkt-messagebox__title')
35
+ })
36
+ it('render complex title', async () => {
37
+ const { messageBox } = render(
38
+ <PktMessagebox
39
+ title={
40
+ <>
41
+ Message<strong>box</strong> title
42
+ </>
43
+ }
44
+ />,
45
+ )
46
+ const titleElement = messageBox.querySelector('.pkt-messagebox__title')!
47
+ expect(titleElement.innerHTML).toEqual('Message<strong>box</strong> title')
48
+ })
49
+
50
+ it('should render empty title element when no title specified', async () => {
51
+ const { messageBox } = render(<PktMessagebox title={''} />)
52
+ expect(messageBox.querySelector('.pkt-messagebox__title')).toBeInTheDocument()
53
+ })
54
+
55
+ it('closable', async () => {
56
+ const { getByRole, messageBox } = render(<PktMessagebox closable />)
57
+ const closeButton = getByRole('button', { name: 'Lukk' })
58
+
59
+ fireEvent.click(closeButton)
60
+
61
+ expect(messageBox.classList).toContain('pkt-hide')
62
+ })
63
+ })
@@ -1,36 +1,66 @@
1
1
  'use client'
2
2
 
3
- import React, { FC, ForwardedRef, forwardRef, ReactElement } from 'react'
4
- import { createComponent, EventName } from '@lit/react'
5
- import { PktMessagebox as PktElMessagebox } from '@oslokommune/punkt-elements'
6
- import type { PktElType, PktElConstructor } from '@/interfaces/IPktElements'
7
-
8
- export interface IPktMessagebox extends PktElType {
9
- skin?: 'beige' | 'red' | 'green' | 'blue'
10
- title?: string
3
+ import classNames from 'classnames'
4
+ import { createRef, FC, HTMLAttributes, ReactNode, useCallback, useState } from 'react'
5
+
6
+ import { PktIcon } from '..'
7
+
8
+ export type TMessageboxSkin = 'beige' | 'blue' | 'red' | 'green'
9
+
10
+ export interface IPktMessagebox extends Omit<HTMLAttributes<HTMLDivElement>, 'title'> {
11
+ skin?: TMessageboxSkin
12
+ title?: ReactNode
11
13
  compact?: boolean
12
14
  closable?: boolean
13
- onClose?: (e: CustomEvent) => void
15
+ onClose?: () => void
14
16
  }
15
17
 
16
- const LitComponent: FC<IPktMessagebox> = createComponent({
17
- tagName: 'pkt-messagebox',
18
- elementClass: PktElMessagebox as PktElConstructor<HTMLElement>,
19
- react: React,
20
- displayName: 'PktMessagebox',
21
- events: {
22
- onClose: 'close' as EventName<CustomEvent>,
23
- },
24
- })
25
-
26
- export const PktMessagebox: FC<IPktMessagebox> = forwardRef(
27
- ({ children, ...props }: IPktMessagebox, ref: ForwardedRef<HTMLElement>): ReactElement => {
28
- return (
29
- <LitComponent {...props} ref={ref}>
30
- <div className="pkt-contents">{children}</div>
31
- </LitComponent>
32
- )
33
- },
34
- )
18
+ export const PktMessagebox: FC<IPktMessagebox> = ({
19
+ children,
20
+ className,
21
+ skin,
22
+ title,
23
+ compact,
24
+ closable,
25
+ onClose,
26
+ ...props
27
+ }: IPktMessagebox) => {
28
+ const [closed, setClosed] = useState(false)
29
+
30
+ const classes = {
31
+ 'pkt-messagebox': true,
32
+ 'pkt-messagebox--compact': compact,
33
+ [`pkt-messagebox--${skin}`]: skin,
34
+ 'pkt-messagebox--closable': closable,
35
+ 'pkt-hide': closed,
36
+ }
37
+
38
+ const componentRootRef = createRef<HTMLDivElement>()
39
+
40
+ const close = useCallback(() => {
41
+ setClosed(true)
42
+ if (onClose) {
43
+ onClose()
44
+ }
45
+ }, [setClosed, onClose])
46
+
47
+ return (
48
+ <div {...props} className={classNames(classes, className)} ref={componentRootRef}>
49
+ {closable && (
50
+ <div className={'"pkt-messagebox__close"'}>
51
+ <button
52
+ onClick={close}
53
+ className={'pkt-btn pkt-btn--tertiary pkt-btn--small pkt-btn--icon-only'}
54
+ aria-label={'Lukk'}
55
+ >
56
+ <PktIcon name="close" className="pkt-link__icon" />
57
+ </button>
58
+ </div>
59
+ )}
60
+ <div className="pkt-messagebox__title">{title}</div>
61
+ <div className={'pkt-messagebox__text'}>{children}</div>
62
+ </div>
63
+ )
64
+ }
35
65
 
36
66
  PktMessagebox.displayName = 'PktMessagebox'