@registrucentras/rc-ses-react-components 2.0.0-rc.0 → 2.0.0-rc.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/README.md +5 -1
- package/dist/assets/icons/CaretUpBoldIcon.d.ts +3 -0
- package/dist/assets/icons/CaretUpIcon.d.ts +3 -0
- package/dist/assets/icons/ListIcon.d.ts +3 -0
- package/dist/assets/icons/MinusIcon.d.ts +3 -0
- package/dist/assets/icons/NotePencilIcon.d.ts +3 -0
- package/dist/assets/icons/PlusIcon.d.ts +3 -0
- package/dist/assets/icons/TrashIcon.d.ts +3 -0
- package/dist/assets/icons/UserIcon.d.ts +3 -0
- package/dist/components/common/AdvancedList/components/AdvancedListItem/AdvancedListItem.types.d.ts +106 -0
- package/dist/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemLeading.d.ts +12 -0
- package/dist/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemTrailing.d.ts +7 -0
- package/dist/components/common/AdvancedList/components/AdvancedListItem/hooks/usePrefersReducedMotion.d.ts +2 -0
- package/dist/components/common/AdvancedList/components/AdvancedListItem/index.d.ts +4 -0
- package/dist/components/common/AdvancedList/index.d.ts +11 -0
- package/dist/components/common/Button/index.d.ts +13 -3
- package/dist/components/common/SelectableCardList/components/SelectableCardListItem.d.ts +4 -1
- package/dist/components/common/SelectableCardList/index.d.ts +4 -1
- package/dist/components/common/Tooltip/index.d.ts +20 -0
- package/dist/library/icons.d.ts +9 -1
- package/dist/library/index.d.ts +5 -0
- package/dist/library/types.d.ts +2 -1
- package/dist/registrucentras-rc-ses-react-components.cjs +1 -1
- package/dist/registrucentras-rc-ses-react-components.js +4107 -3246
- package/dist/stories/components/actions/Button.stories.d.ts +4 -3
- package/dist/stories/components/display/AdvancedList.stories.d.ts +32 -0
- package/dist/stories/components/display/AdvancedListItem.stories.d.ts +33 -0
- package/dist/stories/components/display/SelectableCardList.stories.d.ts +8 -1
- package/dist/stories/components/display/Tooltip.stories.d.ts +48 -0
- package/dist/stories/components/inputs/DatePicker.stories.d.ts +23 -0
- package/dist/stories/components/loaders/LoadingSpinner.stories.d.ts +3 -3
- package/dist/stories/components/overlays/Dialog.stories.d.ts +6 -0
- package/dist/stories/components/overlays/Popover.stories.d.ts +12 -0
- package/dist/stories/theme/ThemedMuiComponents.stories.d.ts +25 -0
- package/dist/theme/light/MuiTooltip.d.ts +3 -0
- package/dist/theme/motion.d.ts +16 -0
- package/package.json +2 -1
- package/src/assets/icons/CaretUpBoldIcon.tsx +17 -0
- package/src/assets/icons/CaretUpIcon.tsx +9 -0
- package/src/assets/icons/ListIcon.tsx +17 -0
- package/src/assets/icons/MinusIcon.tsx +9 -0
- package/src/assets/icons/NotePencilIcon.tsx +9 -0
- package/src/assets/icons/PlusIcon.tsx +9 -0
- package/src/assets/icons/TrashIcon.tsx +23 -0
- package/src/assets/icons/UserIcon.tsx +17 -0
- package/src/components/common/AdvancedList/components/AdvancedListItem/AdvancedListItem.types.ts +100 -0
- package/src/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemLeading.tsx +224 -0
- package/src/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemTrailing.tsx +263 -0
- package/src/components/common/AdvancedList/components/AdvancedListItem/hooks/usePrefersReducedMotion.test.ts +80 -0
- package/src/components/common/AdvancedList/components/AdvancedListItem/hooks/usePrefersReducedMotion.ts +28 -0
- package/src/components/common/AdvancedList/components/AdvancedListItem/index.test.tsx +631 -0
- package/src/components/common/AdvancedList/components/AdvancedListItem/index.tsx +332 -0
- package/src/components/common/AdvancedList/index.test.tsx +97 -0
- package/src/components/common/AdvancedList/index.tsx +52 -0
- package/src/components/common/Button/index.tsx +15 -1
- package/src/components/common/ListWithIcons/components/ListWithIconsItem.tsx +7 -5
- package/src/components/common/SelectableCardList/components/SelectableCardListItem.test.tsx +50 -0
- package/src/components/common/SelectableCardList/components/SelectableCardListItem.tsx +34 -0
- package/src/components/common/SelectableCardList/index.test.tsx +56 -2
- package/src/components/common/SelectableCardList/index.tsx +29 -10
- package/src/components/common/Tooltip/index.test.tsx +85 -0
- package/src/components/common/Tooltip/index.tsx +168 -0
- package/src/components/form/inputs/Select/index.test.tsx +102 -0
- package/src/components/form/inputs/Select/index.tsx +105 -52
- package/src/i18n/namespaces/common/en.json +14 -0
- package/src/i18n/namespaces/common/lt.json +14 -0
- package/src/library/icons.ts +16 -0
- package/src/library/index.ts +5 -0
- package/src/library/types.ts +2 -0
- package/src/setupTests.ts +7 -1
- package/src/stories/components/display/AdvancedList.stories.tsx +529 -0
- package/src/stories/components/display/AdvancedListItem.stories.tsx +653 -0
- package/src/stories/components/display/SelectableCardList.stories.tsx +19 -0
- package/src/stories/components/display/Tooltip.stories.tsx +67 -0
- package/src/stories/components/inputs/DatePicker.stories.tsx +36 -1
- package/src/stories/components/inputs/Select.stories.tsx +38 -2
- package/src/stories/components/overlays/Dialog.stories.tsx +34 -0
- package/src/stories/components/overlays/Popover.stories.tsx +65 -0
- package/src/stories/theme/ThemedMuiComponents.stories.tsx +177 -0
- package/src/theme/light/MuiAutocomplete.ts +3 -1
- package/src/theme/light/MuiInputBase.ts +4 -0
- package/src/theme/light/MuiTooltip.ts +40 -0
- package/src/theme/light/index.tsx +2 -0
- package/src/theme/motion.tsx +10 -0
|
@@ -41,7 +41,7 @@ describe('SelectableCardList', () => {
|
|
|
41
41
|
expect(screen.queryByRole('radio')).not.toBeInTheDocument()
|
|
42
42
|
})
|
|
43
43
|
|
|
44
|
-
test('renders all items on the first page when count <=
|
|
44
|
+
test('renders all items on the first page when count <= desktop page size', () => {
|
|
45
45
|
renderList(<SelectableCardList items={makeItems(3)} onSelect={vi.fn()} />)
|
|
46
46
|
|
|
47
47
|
expect(screen.getByText('Item 1')).toBeInTheDocument()
|
|
@@ -49,7 +49,7 @@ describe('SelectableCardList', () => {
|
|
|
49
49
|
expect(screen.getByText('Item 3')).toBeInTheDocument()
|
|
50
50
|
})
|
|
51
51
|
|
|
52
|
-
test('shows only 5 items per page', () => {
|
|
52
|
+
test('shows only 5 items per page on desktop', () => {
|
|
53
53
|
renderList(<SelectableCardList items={makeItems(8)} onSelect={vi.fn()} />)
|
|
54
54
|
|
|
55
55
|
expect(screen.getByText('Item 1')).toBeInTheDocument()
|
|
@@ -68,6 +68,60 @@ describe('SelectableCardList', () => {
|
|
|
68
68
|
expect(screen.queryByText('Item 1')).not.toBeInTheDocument()
|
|
69
69
|
})
|
|
70
70
|
|
|
71
|
+
test('shows only 3 items per page on mobile, split across 3 pages', () => {
|
|
72
|
+
mockedUseMediaQuery.mockReturnValue(true)
|
|
73
|
+
|
|
74
|
+
renderList(<SelectableCardList items={makeItems(8)} onSelect={vi.fn()} />)
|
|
75
|
+
|
|
76
|
+
expect(screen.getByText('Item 1')).toBeInTheDocument()
|
|
77
|
+
expect(screen.getByText('Item 3')).toBeInTheDocument()
|
|
78
|
+
expect(screen.queryByText('Item 4')).not.toBeInTheDocument()
|
|
79
|
+
expect(screen.getByRole('button', { name: 'page 1' })).toBeInTheDocument()
|
|
80
|
+
expect(screen.getByRole('button', { name: 'Go to page 3' })).toBeInTheDocument()
|
|
81
|
+
expect(screen.queryByRole('button', { name: 'Go to page 4' })).not.toBeInTheDocument()
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('resets to page 1 when switching from desktop to mobile page size', () => {
|
|
85
|
+
const { rerender } = renderList(
|
|
86
|
+
<SelectableCardList items={makeItems(8)} onSelect={vi.fn()} />,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
fireEvent.click(screen.getByRole('button', { name: 'Kitas' }))
|
|
90
|
+
expect(screen.getByText('Item 6')).toBeInTheDocument()
|
|
91
|
+
|
|
92
|
+
mockedUseMediaQuery.mockReturnValue(true)
|
|
93
|
+
rerender(
|
|
94
|
+
<ThemeProvider theme={theme}>
|
|
95
|
+
<SelectableCardList items={makeItems(8)} onSelect={vi.fn()} />
|
|
96
|
+
</ThemeProvider>,
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
expect(screen.getByText('Item 1')).toBeInTheDocument()
|
|
100
|
+
expect(screen.getByText('Item 3')).toBeInTheDocument()
|
|
101
|
+
expect(screen.queryByText('Item 4')).not.toBeInTheDocument()
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('resets to page 1 when switching from mobile to desktop page size', () => {
|
|
105
|
+
mockedUseMediaQuery.mockReturnValue(true)
|
|
106
|
+
const { rerender } = renderList(
|
|
107
|
+
<SelectableCardList items={makeItems(8)} onSelect={vi.fn()} />,
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
fireEvent.click(screen.getByRole('button', { name: 'Go to page 3' }))
|
|
111
|
+
expect(screen.getByText('Item 7')).toBeInTheDocument()
|
|
112
|
+
|
|
113
|
+
mockedUseMediaQuery.mockReturnValue(false)
|
|
114
|
+
rerender(
|
|
115
|
+
<ThemeProvider theme={theme}>
|
|
116
|
+
<SelectableCardList items={makeItems(8)} onSelect={vi.fn()} />
|
|
117
|
+
</ThemeProvider>,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
expect(screen.getByText('Item 1')).toBeInTheDocument()
|
|
121
|
+
expect(screen.getByText('Item 5')).toBeInTheDocument()
|
|
122
|
+
expect(screen.queryByText('Item 6')).not.toBeInTheDocument()
|
|
123
|
+
})
|
|
124
|
+
|
|
71
125
|
test('calls onSelect with the correct item id when a card is clicked', async () => {
|
|
72
126
|
const onSelect = vi.fn()
|
|
73
127
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Box, Container } from '@mui/material'
|
|
1
|
+
import { Box, Container, useMediaQuery, useTheme } from '@mui/material'
|
|
2
2
|
import { ReactNode, useMemo, useState } from 'react'
|
|
3
3
|
|
|
4
4
|
import DataPagination from '../DataPagination'
|
|
@@ -10,6 +10,9 @@ export type SelectableCardListItemData = {
|
|
|
10
10
|
title: string
|
|
11
11
|
subtitle?: string
|
|
12
12
|
listItems?: ListWithIconsItemData[]
|
|
13
|
+
hasActionButton?: boolean
|
|
14
|
+
actionButtonLabel?: string
|
|
15
|
+
onActionButtonClick?: () => void
|
|
13
16
|
}
|
|
14
17
|
export interface SelectableCardListProps {
|
|
15
18
|
items?: SelectableCardListItemData[]
|
|
@@ -18,29 +21,34 @@ export interface SelectableCardListProps {
|
|
|
18
21
|
error?: ReactNode
|
|
19
22
|
}
|
|
20
23
|
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
+
const MOBILE_PAGE_SIZE = 3
|
|
25
|
+
const DESKTOP_PAGE_SIZE = 5
|
|
26
|
+
type SelectableCardListPagedProps = SelectableCardListProps & {
|
|
27
|
+
pageSize: number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const SelectableCardListPaged = ({
|
|
24
31
|
items,
|
|
25
32
|
selectedId,
|
|
26
33
|
onSelect,
|
|
27
34
|
error,
|
|
28
|
-
|
|
35
|
+
pageSize,
|
|
36
|
+
}: SelectableCardListPagedProps) => {
|
|
29
37
|
const [page, setPage] = useState(1)
|
|
30
38
|
|
|
31
|
-
const totalPages = items ? Math.ceil(items.length /
|
|
39
|
+
const totalPages = items ? Math.ceil(items.length / pageSize) : 0
|
|
32
40
|
const currentPage = totalPages > 0 ? Math.min(page, totalPages) : 1
|
|
33
41
|
const pagedItems = useMemo(() => {
|
|
34
42
|
if (!items) {
|
|
35
43
|
return []
|
|
36
44
|
}
|
|
37
45
|
|
|
38
|
-
const startIndex = (currentPage - 1) *
|
|
39
|
-
return items.slice(startIndex, startIndex +
|
|
40
|
-
}, [currentPage, items])
|
|
46
|
+
const startIndex = (currentPage - 1) * pageSize
|
|
47
|
+
return items.slice(startIndex, startIndex + pageSize)
|
|
48
|
+
}, [currentPage, items, pageSize])
|
|
41
49
|
|
|
42
50
|
const renderSkeletons = () =>
|
|
43
|
-
Array.from({ length:
|
|
51
|
+
Array.from({ length: pageSize }).map((_, i) => (
|
|
44
52
|
<SelectableCardListItem
|
|
45
53
|
// eslint-disable-next-line react/no-array-index-key
|
|
46
54
|
key={i}
|
|
@@ -81,6 +89,9 @@ const SelectableCardList = ({
|
|
|
81
89
|
listItems={item.listItems || []}
|
|
82
90
|
selected={selectedId === item.id}
|
|
83
91
|
onSelect={() => onSelect(item.id)}
|
|
92
|
+
hasActionButton={item.hasActionButton}
|
|
93
|
+
actionButtonLabel={item.actionButtonLabel}
|
|
94
|
+
onActionButtonClick={item.onActionButtonClick}
|
|
84
95
|
/>
|
|
85
96
|
))}
|
|
86
97
|
</Box>
|
|
@@ -96,4 +107,12 @@ const SelectableCardList = ({
|
|
|
96
107
|
)
|
|
97
108
|
}
|
|
98
109
|
|
|
110
|
+
const SelectableCardList = (props: SelectableCardListProps) => {
|
|
111
|
+
const theme = useTheme()
|
|
112
|
+
const isMobile = useMediaQuery(theme.breakpoints.down('md'))
|
|
113
|
+
const pageSize = isMobile ? MOBILE_PAGE_SIZE : DESKTOP_PAGE_SIZE
|
|
114
|
+
|
|
115
|
+
return <SelectableCardListPaged key={pageSize} {...props} pageSize={pageSize} />
|
|
116
|
+
}
|
|
117
|
+
|
|
99
118
|
export default SelectableCardList
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
2
|
+
import { createRef } from 'react'
|
|
3
|
+
import { describe, expect, test, vi } from 'vitest'
|
|
4
|
+
|
|
5
|
+
import RcSesTooltip from '.'
|
|
6
|
+
|
|
7
|
+
describe('RcSesTooltip', () => {
|
|
8
|
+
test('renders default trigger button', () => {
|
|
9
|
+
render(<RcSesTooltip title='Help text' />)
|
|
10
|
+
|
|
11
|
+
expect(screen.getByRole('button', { name: 'Paaiškinimas' })).toBeInTheDocument()
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
test('opens tooltip on trigger click', async () => {
|
|
15
|
+
render(<RcSesTooltip title='Text' />)
|
|
16
|
+
|
|
17
|
+
fireEvent.click(screen.getByRole('button'))
|
|
18
|
+
|
|
19
|
+
expect(await screen.findByRole('tooltip')).toHaveTextContent('Text')
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test('closes on pointerdown outside', async () => {
|
|
23
|
+
render(<RcSesTooltip title='Text' />)
|
|
24
|
+
|
|
25
|
+
fireEvent.click(screen.getByRole('button'))
|
|
26
|
+
await screen.findByRole('tooltip')
|
|
27
|
+
|
|
28
|
+
fireEvent.pointerDown(document.body)
|
|
29
|
+
|
|
30
|
+
await waitFor(() => {
|
|
31
|
+
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test('keeps only one tooltip open at a time', async () => {
|
|
36
|
+
render(
|
|
37
|
+
<>
|
|
38
|
+
<RcSesTooltip title='A' />
|
|
39
|
+
<RcSesTooltip title='B' />
|
|
40
|
+
</>,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
const [first, second] = screen.getAllByRole('button')
|
|
44
|
+
|
|
45
|
+
fireEvent.click(first)
|
|
46
|
+
await screen.findByRole('tooltip')
|
|
47
|
+
|
|
48
|
+
fireEvent.click(second)
|
|
49
|
+
|
|
50
|
+
await waitFor(() => {
|
|
51
|
+
const tooltips = screen.getAllByRole('tooltip')
|
|
52
|
+
expect(tooltips).toHaveLength(1)
|
|
53
|
+
expect(tooltips[0]).toHaveTextContent('B')
|
|
54
|
+
})
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('passes trigger slotProps to the default button', () => {
|
|
58
|
+
render(
|
|
59
|
+
<RcSesTooltip title='Text' slotProps={{ trigger: { 'aria-label': 'Custom' } }} />,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
expect(screen.getByRole('button', { name: 'Custom' })).toBeInTheDocument()
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('custom children trigger opens tooltip, chains onClick and forwards ref', async () => {
|
|
66
|
+
const onClick = vi.fn()
|
|
67
|
+
const ref = createRef<HTMLButtonElement>()
|
|
68
|
+
|
|
69
|
+
render(
|
|
70
|
+
<RcSesTooltip title='Text'>
|
|
71
|
+
<button type='button' ref={ref} onClick={onClick}>
|
|
72
|
+
Custom trigger
|
|
73
|
+
</button>
|
|
74
|
+
</RcSesTooltip>,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
const trigger = screen.getByRole('button', { name: 'Custom trigger' })
|
|
78
|
+
expect(ref.current).toBe(trigger)
|
|
79
|
+
|
|
80
|
+
fireEvent.click(trigger)
|
|
81
|
+
|
|
82
|
+
expect(onClick).toHaveBeenCalledTimes(1)
|
|
83
|
+
expect(await screen.findByRole('tooltip')).toHaveTextContent('Text')
|
|
84
|
+
})
|
|
85
|
+
})
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import type { IconButtonProps, PopperProps, TooltipProps } from '@mui/material'
|
|
2
|
+
import { Fade, IconButton, Tooltip } from '@mui/material'
|
|
3
|
+
import { useForkRef } from '@mui/material/utils'
|
|
4
|
+
import React from 'react'
|
|
5
|
+
import { useTranslation } from 'react-i18next'
|
|
6
|
+
|
|
7
|
+
import QuestionIcon from '@/assets/icons/QuestionIcon'
|
|
8
|
+
|
|
9
|
+
export type RcSesTooltipProps = {
|
|
10
|
+
title: string
|
|
11
|
+
placement?: TooltipProps['placement']
|
|
12
|
+
} & (
|
|
13
|
+
| {
|
|
14
|
+
/* overrides for default trigger button */
|
|
15
|
+
slotProps?: { trigger?: Partial<IconButtonProps> }
|
|
16
|
+
children?: never
|
|
17
|
+
}
|
|
18
|
+
| {
|
|
19
|
+
slotProps?: never
|
|
20
|
+
/* custom trigger replaces default button, but should forward ref and define a11y name */
|
|
21
|
+
children: React.ReactElement
|
|
22
|
+
}
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
// only one tooltip should be visible, no need for context for that
|
|
26
|
+
let closeCurrent: (() => void) | null = null
|
|
27
|
+
|
|
28
|
+
// MUI 9 removed Tooltip's PopperProps in favour of the `popper` slot.
|
|
29
|
+
const popperProps: Partial<PopperProps> = {
|
|
30
|
+
popperOptions: {
|
|
31
|
+
modifiers: [
|
|
32
|
+
{ name: 'arrow', options: { padding: 14 } },
|
|
33
|
+
{
|
|
34
|
+
name: 'offset',
|
|
35
|
+
options: {
|
|
36
|
+
offset: ({ placement }: { placement: string }) => {
|
|
37
|
+
if (placement.endsWith('-end')) {
|
|
38
|
+
return [11, -4]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (placement.endsWith('-start')) {
|
|
42
|
+
return [-11, -4]
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return [0, -4]
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Controlled MUI Tooltip, ensures only one tooltip is visible and adds tap-to-dismiss to mobile
|
|
55
|
+
* (ClickAwayListener doesn't work with bubble itself)
|
|
56
|
+
*/
|
|
57
|
+
function RcSesTooltip({
|
|
58
|
+
title,
|
|
59
|
+
placement = 'top-start',
|
|
60
|
+
slotProps,
|
|
61
|
+
children,
|
|
62
|
+
}: RcSesTooltipProps) {
|
|
63
|
+
const { t } = useTranslation('common')
|
|
64
|
+
|
|
65
|
+
const [open, setOpen] = React.useState(false)
|
|
66
|
+
|
|
67
|
+
const triggerRef = React.useRef<HTMLElement>(null)
|
|
68
|
+
|
|
69
|
+
// if children is used, child ref merge is needed
|
|
70
|
+
const childRef = (
|
|
71
|
+
children as (React.ReactElement & { ref?: React.Ref<HTMLElement> }) | undefined
|
|
72
|
+
)?.ref
|
|
73
|
+
|
|
74
|
+
const handleTriggerRef = useForkRef(triggerRef, childRef ?? null)
|
|
75
|
+
|
|
76
|
+
const hide = React.useCallback(() => setOpen(false), [])
|
|
77
|
+
|
|
78
|
+
const show = React.useCallback(() => {
|
|
79
|
+
if (closeCurrent !== hide) {
|
|
80
|
+
closeCurrent?.()
|
|
81
|
+
closeCurrent = hide
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
setOpen(true)
|
|
85
|
+
}, [hide])
|
|
86
|
+
|
|
87
|
+
// on mobile: tap anywhere to dismiss
|
|
88
|
+
React.useEffect(() => {
|
|
89
|
+
if (!open) {
|
|
90
|
+
return undefined
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const onPointerDown = ({ target }: PointerEvent) => {
|
|
94
|
+
const element = target instanceof Element ? target : null
|
|
95
|
+
|
|
96
|
+
if (
|
|
97
|
+
triggerRef.current?.contains(element) ||
|
|
98
|
+
element?.closest('.MuiTooltip-popper')
|
|
99
|
+
) {
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
hide()
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
document.addEventListener('pointerdown', onPointerDown)
|
|
107
|
+
|
|
108
|
+
return () => document.removeEventListener('pointerdown', onPointerDown)
|
|
109
|
+
}, [open, hide])
|
|
110
|
+
|
|
111
|
+
React.useEffect(
|
|
112
|
+
() => () => {
|
|
113
|
+
if (closeCurrent === hide) {
|
|
114
|
+
closeCurrent = null
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
[hide],
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
const { sx: triggerSx, ...triggerProps } = slotProps?.trigger ?? {}
|
|
121
|
+
|
|
122
|
+
const trigger = children ?? (
|
|
123
|
+
<IconButton
|
|
124
|
+
size='small'
|
|
125
|
+
aria-label={t('components.Tooltip.trigger')}
|
|
126
|
+
{...triggerProps}
|
|
127
|
+
sx={[
|
|
128
|
+
{
|
|
129
|
+
p: 0.25,
|
|
130
|
+
ml: 0.5,
|
|
131
|
+
verticalAlign: 'text-bottom',
|
|
132
|
+
color: 'grey.600',
|
|
133
|
+
'&:hover, &.Mui-focusVisible': { color: 'primary.main' },
|
|
134
|
+
},
|
|
135
|
+
...(Array.isArray(triggerSx) ? triggerSx : [triggerSx]),
|
|
136
|
+
]}
|
|
137
|
+
>
|
|
138
|
+
<QuestionIcon size={16} aria-hidden />
|
|
139
|
+
</IconButton>
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
return (
|
|
143
|
+
<Tooltip
|
|
144
|
+
title={title}
|
|
145
|
+
open={open}
|
|
146
|
+
describeChild
|
|
147
|
+
arrow
|
|
148
|
+
placement={placement}
|
|
149
|
+
leaveDelay={100}
|
|
150
|
+
disableTouchListener
|
|
151
|
+
slots={{ transition: Fade }}
|
|
152
|
+
slotProps={{ popper: popperProps }}
|
|
153
|
+
onOpen={show}
|
|
154
|
+
onClose={hide}
|
|
155
|
+
>
|
|
156
|
+
{/* same principle as in MUI Tooltip */}
|
|
157
|
+
{React.cloneElement(trigger, {
|
|
158
|
+
ref: handleTriggerRef,
|
|
159
|
+
onClick: (event: React.MouseEvent) => {
|
|
160
|
+
trigger.props.onClick?.(event)
|
|
161
|
+
show()
|
|
162
|
+
},
|
|
163
|
+
})}
|
|
164
|
+
</Tooltip>
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export default RcSesTooltip
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { ThemeProvider } from '@mui/material/styles'
|
|
2
|
+
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
3
|
+
import { useForm } from 'react-hook-form'
|
|
4
|
+
|
|
5
|
+
import theme from '@/theme/light'
|
|
6
|
+
|
|
7
|
+
import RcSesSelect from '.'
|
|
8
|
+
|
|
9
|
+
type TestWrapperProps = {
|
|
10
|
+
defaultValue?: string | null
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const options = [
|
|
14
|
+
{
|
|
15
|
+
value: 'long-option',
|
|
16
|
+
label: 'Juridiniu asmenu registro isplestinis pavadinimas su ilgu tekstu',
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
value: 'short-option',
|
|
20
|
+
label: 'Trumpas pasirinkimas',
|
|
21
|
+
},
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
const TestWrapper = ({ defaultValue = null }: TestWrapperProps) => {
|
|
25
|
+
const { control } = useForm({
|
|
26
|
+
defaultValues: {
|
|
27
|
+
selection: defaultValue,
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<ThemeProvider theme={theme}>
|
|
33
|
+
<RcSesSelect
|
|
34
|
+
id='selection'
|
|
35
|
+
name='selection'
|
|
36
|
+
control={control}
|
|
37
|
+
label='Pasirinkti'
|
|
38
|
+
options={options}
|
|
39
|
+
/>
|
|
40
|
+
</ThemeProvider>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
describe('RcSesSelect', () => {
|
|
45
|
+
test('single select rodo pilna pasirinktos reiksmes label', () => {
|
|
46
|
+
render(<TestWrapper defaultValue='long-option' />)
|
|
47
|
+
|
|
48
|
+
expect(
|
|
49
|
+
screen.getByText(
|
|
50
|
+
'Juridiniu asmenu registro isplestinis pavadinimas su ilgu tekstu',
|
|
51
|
+
),
|
|
52
|
+
).toBeInTheDocument()
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('single select lieka pasirenkamas klaviatura kai niekas nepasirinkta', async () => {
|
|
56
|
+
render(<TestWrapper />)
|
|
57
|
+
|
|
58
|
+
const input = screen.getByRole('combobox')
|
|
59
|
+
expect(input).not.toHaveAttribute('readonly')
|
|
60
|
+
|
|
61
|
+
fireEvent.mouseDown(input)
|
|
62
|
+
fireEvent.keyDown(input, { key: 'ArrowDown' })
|
|
63
|
+
fireEvent.keyDown(input, { key: 'Enter' })
|
|
64
|
+
|
|
65
|
+
await waitFor(() => {
|
|
66
|
+
expect(
|
|
67
|
+
screen.getByText(
|
|
68
|
+
'Juridiniu asmenu registro isplestinis pavadinimas su ilgu tekstu',
|
|
69
|
+
),
|
|
70
|
+
).toBeInTheDocument()
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('single select siulo visas parinktis kai reiksme jau pasirinkta', async () => {
|
|
75
|
+
render(<TestWrapper defaultValue='long-option' />)
|
|
76
|
+
|
|
77
|
+
fireEvent.mouseDown(screen.getByRole('combobox'))
|
|
78
|
+
|
|
79
|
+
await waitFor(() => {
|
|
80
|
+
expect(screen.getAllByRole('option')).toHaveLength(options.length)
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('single select leidzia pakeisti jau pasirinkta reiksme', async () => {
|
|
85
|
+
render(<TestWrapper defaultValue='long-option' />)
|
|
86
|
+
|
|
87
|
+
fireEvent.mouseDown(screen.getByRole('combobox'))
|
|
88
|
+
fireEvent.click(await screen.findByRole('option', { name: 'Trumpas pasirinkimas' }))
|
|
89
|
+
|
|
90
|
+
await waitFor(() => {
|
|
91
|
+
expect(
|
|
92
|
+
screen.getByText('Trumpas pasirinkimas', { selector: 'div' }),
|
|
93
|
+
).toBeInTheDocument()
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test('single select neleidzia rasti kai reiksme jau pasirinkta', () => {
|
|
98
|
+
render(<TestWrapper defaultValue='long-option' />)
|
|
99
|
+
|
|
100
|
+
expect(screen.getByRole('combobox')).toHaveAttribute('readonly')
|
|
101
|
+
})
|
|
102
|
+
})
|