@registrucentras/rc-ses-react-components 2.0.0-rc.0 → 2.0.0-rc.2
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/components/form/inputs/Select/index.d.ts +1 -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 +4137 -3249
- package/dist/stories/components/actions/Button.stories.d.ts +4 -3
- package/dist/stories/components/display/AdvancedList.stories.d.ts +33 -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 +228 -0
- package/src/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemTrailing.tsx +270 -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 +340 -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/Card/index.tsx +2 -2
- 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/FileDropzone.tsx +1 -0
- package/src/components/form/inputs/FileUpload.tsx +1 -0
- package/src/components/form/inputs/NumberStepper.tsx +1 -0
- package/src/components/form/inputs/PhoneInputFormControl/index.tsx +1 -1
- package/src/components/form/inputs/RadioButtonGroup.test.tsx +71 -0
- package/src/components/form/inputs/RadioButtonGroup.tsx +1 -0
- package/src/components/form/inputs/Select/index.test.tsx +159 -0
- package/src/components/form/inputs/Select/index.tsx +107 -52
- package/src/components/layout/Footer/index.test.tsx +13 -0
- package/src/components/layout/Footer/index.tsx +5 -2
- 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 +660 -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
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Box, Radio, Skeleton, Stack, Typography } from '@mui/material'
|
|
2
2
|
|
|
3
|
+
import RcSesButton from '@/components/common/Button'
|
|
3
4
|
import RcSesCard from '@/components/common/Card'
|
|
4
5
|
import ListWithIcons from '@/components/common/ListWithIcons'
|
|
5
6
|
import palette from '@/theme/palette'
|
|
@@ -13,6 +14,9 @@ type Props = {
|
|
|
13
14
|
selected: boolean
|
|
14
15
|
onSelect: () => void
|
|
15
16
|
isLoading?: boolean
|
|
17
|
+
hasActionButton?: boolean
|
|
18
|
+
actionButtonLabel?: string
|
|
19
|
+
onActionButtonClick?: () => void
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
const SelectableCardListItem = ({
|
|
@@ -22,6 +26,9 @@ const SelectableCardListItem = ({
|
|
|
22
26
|
selected,
|
|
23
27
|
onSelect,
|
|
24
28
|
isLoading = false,
|
|
29
|
+
hasActionButton = false,
|
|
30
|
+
actionButtonLabel,
|
|
31
|
+
onActionButtonClick,
|
|
25
32
|
}: Props) => {
|
|
26
33
|
const titleTemplate = isLoading ? (
|
|
27
34
|
<Stack
|
|
@@ -109,6 +116,33 @@ const SelectableCardListItem = ({
|
|
|
109
116
|
},
|
|
110
117
|
}}
|
|
111
118
|
title={titleTemplate}
|
|
119
|
+
headerAction={
|
|
120
|
+
isLoading ? (
|
|
121
|
+
<Skeleton variant='rounded' width={100} height={38} />
|
|
122
|
+
) : (
|
|
123
|
+
hasActionButton && (
|
|
124
|
+
<RcSesButton
|
|
125
|
+
color='grey'
|
|
126
|
+
sx={{
|
|
127
|
+
height: 'auto',
|
|
128
|
+
padding: '0.5rem 1rem',
|
|
129
|
+
fontSize: '0.875rem',
|
|
130
|
+
lineHeight: '1.25rem',
|
|
131
|
+
'&.MuiButton-colorGrey': {
|
|
132
|
+
color: palette.grey[800],
|
|
133
|
+
},
|
|
134
|
+
}}
|
|
135
|
+
onClick={(e) => {
|
|
136
|
+
e.stopPropagation()
|
|
137
|
+
onActionButtonClick?.()
|
|
138
|
+
}}
|
|
139
|
+
variant='outlined'
|
|
140
|
+
>
|
|
141
|
+
{actionButtonLabel}
|
|
142
|
+
</RcSesButton>
|
|
143
|
+
)
|
|
144
|
+
)
|
|
145
|
+
}
|
|
112
146
|
slotProps={{
|
|
113
147
|
title: {
|
|
114
148
|
sx: {
|
|
@@ -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
|
|
@@ -179,7 +179,7 @@ function RcSesPhoneInputFormControl(props: Props) {
|
|
|
179
179
|
</InputAdornment>
|
|
180
180
|
|
|
181
181
|
<InputAdornment position='start'>
|
|
182
|
-
<Typography
|
|
182
|
+
<Typography sx={{ color: palette.grey[900] }} component='span'>
|
|
183
183
|
{country.code}
|
|
184
184
|
</Typography>
|
|
185
185
|
</InputAdornment>
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ThemeProvider } from '@mui/material/styles'
|
|
2
|
+
import { fireEvent, render, screen } from '@testing-library/react'
|
|
3
|
+
import { useState } from 'react'
|
|
4
|
+
import { useForm } from 'react-hook-form'
|
|
5
|
+
|
|
6
|
+
import theme from '@/theme/light'
|
|
7
|
+
|
|
8
|
+
import RcSesRadioButtonGroup from './RadioButtonGroup'
|
|
9
|
+
|
|
10
|
+
const options = [
|
|
11
|
+
{ label: 'Fizinis asmuo', value: 'natural' },
|
|
12
|
+
{ label: 'Juridinis asmuo', value: 'legal' },
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
type TestWrapperProps = {
|
|
16
|
+
shouldUnregister?: boolean
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const TestWrapper = ({ shouldUnregister }: TestWrapperProps) => {
|
|
20
|
+
const { control, getValues } = useForm({
|
|
21
|
+
defaultValues: { ownerType: 'natural' },
|
|
22
|
+
})
|
|
23
|
+
const [mounted, setMounted] = useState(true)
|
|
24
|
+
const [formValues, setFormValues] = useState('')
|
|
25
|
+
|
|
26
|
+
const slotProps =
|
|
27
|
+
shouldUnregister === undefined ? undefined : { controller: { shouldUnregister } }
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<ThemeProvider theme={theme}>
|
|
31
|
+
{mounted && (
|
|
32
|
+
<RcSesRadioButtonGroup
|
|
33
|
+
id='ownerType'
|
|
34
|
+
name='ownerType'
|
|
35
|
+
control={control}
|
|
36
|
+
label='Savininko tipas'
|
|
37
|
+
options={options}
|
|
38
|
+
slotProps={slotProps}
|
|
39
|
+
/>
|
|
40
|
+
)}
|
|
41
|
+
|
|
42
|
+
<button type='button' onClick={() => setMounted(false)}>
|
|
43
|
+
Hide
|
|
44
|
+
</button>
|
|
45
|
+
<button type='button' onClick={() => setFormValues(JSON.stringify(getValues()))}>
|
|
46
|
+
Read
|
|
47
|
+
</button>
|
|
48
|
+
<div data-testid='form-values'>{formValues}</div>
|
|
49
|
+
</ThemeProvider>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
describe('RcSesRadioButtonGroup', () => {
|
|
54
|
+
test('keeps the value in the form by default when the field unmounts', () => {
|
|
55
|
+
render(<TestWrapper />)
|
|
56
|
+
|
|
57
|
+
fireEvent.click(screen.getByRole('button', { name: 'Hide' }))
|
|
58
|
+
fireEvent.click(screen.getByRole('button', { name: 'Read' }))
|
|
59
|
+
|
|
60
|
+
expect(screen.getByTestId('form-values')).toHaveTextContent('natural')
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test('drops the value when slotProps.controller turns shouldUnregister on', () => {
|
|
64
|
+
render(<TestWrapper shouldUnregister />)
|
|
65
|
+
|
|
66
|
+
fireEvent.click(screen.getByRole('button', { name: 'Hide' }))
|
|
67
|
+
fireEvent.click(screen.getByRole('button', { name: 'Read' }))
|
|
68
|
+
|
|
69
|
+
expect(screen.getByTestId('form-values')).not.toHaveTextContent('natural')
|
|
70
|
+
})
|
|
71
|
+
})
|