@registrucentras/rc-ses-react-components 1.12.0 → 1.13.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/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/SelectableCardList/components/SelectableCardListItem.d.ts +4 -1
- package/dist/components/common/SelectableCardList/index.d.ts +3 -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 +3 -0
- package/dist/registrucentras-rc-ses-react-components.cjs +10 -10
- package/dist/registrucentras-rc-ses-react-components.js +3807 -3102
- 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/loaders/LoadingSpinner.stories.d.ts +3 -3
- package/dist/theme/motion.d.ts +16 -0
- package/package.json +1 -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 +214 -0
- package/src/components/common/AdvancedList/components/AdvancedListItem/components/AdvancedListItemTrailing.tsx +241 -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 +329 -0
- package/src/components/common/AdvancedList/index.test.tsx +97 -0
- package/src/components/common/AdvancedList/index.tsx +52 -0
- 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.tsx +6 -0
- package/src/components/common/Tooltip/index.test.tsx +1 -3
- 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/RadioButtonGroup.test.tsx +71 -0
- package/src/components/form/inputs/RadioButtonGroup.tsx +1 -0
- package/src/components/form/inputs/Select/index.test.tsx +57 -0
- package/src/components/form/inputs/Select/index.tsx +2 -0
- package/src/i18n/namespaces/common/en.json +11 -0
- package/src/i18n/namespaces/common/lt.json +11 -0
- package/src/library/icons.ts +16 -0
- package/src/library/index.ts +3 -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 +593 -0
- package/src/stories/components/display/SelectableCardList.stories.tsx +3 -0
- package/src/theme/motion.tsx +10 -0
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { Box, Collapse, Stack, Typography } from '@mui/material'
|
|
2
|
+
import { useCallback, useEffect, useId, useMemo, useRef } from 'react'
|
|
3
|
+
|
|
4
|
+
import RcSesBadge from '@/components/common/Badge'
|
|
5
|
+
import ListWithIcons from '@/components/common/ListWithIcons'
|
|
6
|
+
import motion from '@/theme/motion'
|
|
7
|
+
import palette from '@/theme/palette'
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
AdvancedListItemLeading as AdvancedListItemLeadingData,
|
|
11
|
+
AdvancedListItemProps,
|
|
12
|
+
AdvancedListItemState,
|
|
13
|
+
} from './AdvancedListItem.types'
|
|
14
|
+
import AdvancedListItemLeading from './components/AdvancedListItemLeading'
|
|
15
|
+
import AdvancedListItemTrailing from './components/AdvancedListItemTrailing'
|
|
16
|
+
import usePrefersReducedMotion from './hooks/usePrefersReducedMotion'
|
|
17
|
+
|
|
18
|
+
const BORDER_RADIUS = '0.5rem'
|
|
19
|
+
const BORDER_WIDTH = '0.0625rem'
|
|
20
|
+
const FOCUS_RING_WIDTH = '0.0625rem'
|
|
21
|
+
|
|
22
|
+
const STATE_BORDER_COLOR: Record<AdvancedListItemState, string> = {
|
|
23
|
+
rest: palette.grey[300],
|
|
24
|
+
selected: palette.primary.main,
|
|
25
|
+
disabled: palette.grey[200],
|
|
26
|
+
error: palette.error.main,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const LEADING_MEDIA_SX = {
|
|
30
|
+
width: '2rem',
|
|
31
|
+
height: '2rem',
|
|
32
|
+
flexShrink: 0,
|
|
33
|
+
display: 'flex',
|
|
34
|
+
alignItems: 'center',
|
|
35
|
+
justifyContent: 'center',
|
|
36
|
+
} as const
|
|
37
|
+
|
|
38
|
+
const INTERACTIVE_LEADING_TYPES = new Set(['reorder', 'checkbox', 'radio'])
|
|
39
|
+
const INTERACTIVE_TRAILING_TYPES = new Set([
|
|
40
|
+
'actions',
|
|
41
|
+
'toggle',
|
|
42
|
+
'checkbox',
|
|
43
|
+
'radio',
|
|
44
|
+
'stepper',
|
|
45
|
+
])
|
|
46
|
+
|
|
47
|
+
const AdvancedListItem = ({
|
|
48
|
+
title,
|
|
49
|
+
label,
|
|
50
|
+
subtitle,
|
|
51
|
+
metaItems,
|
|
52
|
+
leadingMedia,
|
|
53
|
+
expandedContent,
|
|
54
|
+
expanded = false,
|
|
55
|
+
onExpandedChange,
|
|
56
|
+
leading = { type: 'none' },
|
|
57
|
+
trailing = { type: 'none' },
|
|
58
|
+
state = 'rest',
|
|
59
|
+
onClick,
|
|
60
|
+
}: AdvancedListItemProps) => {
|
|
61
|
+
const isDisabled = state === 'disabled'
|
|
62
|
+
const isSelectable = leading.type === 'checkbox' || leading.type === 'radio'
|
|
63
|
+
const isClickable = (!!onClick || isSelectable) && !isDisabled
|
|
64
|
+
|
|
65
|
+
const hasInteractiveChild =
|
|
66
|
+
INTERACTIVE_LEADING_TYPES.has(leading.type) ||
|
|
67
|
+
INTERACTIVE_TRAILING_TYPES.has(trailing.type) ||
|
|
68
|
+
(trailing.type === 'price' && !!trailing.onDelete)
|
|
69
|
+
|
|
70
|
+
const isRootFocusable = isClickable && !hasInteractiveChild
|
|
71
|
+
const isExpandable = !!expandedContent
|
|
72
|
+
const canAttachExpandToLeading =
|
|
73
|
+
isExpandable && (leading.type === 'radio' || leading.type === 'checkbox')
|
|
74
|
+
const needsRootFocusFallback =
|
|
75
|
+
isExpandable && !canAttachExpandToLeading && !isRootFocusable
|
|
76
|
+
|
|
77
|
+
let rootTabIndex: number | undefined
|
|
78
|
+
if (isRootFocusable) {
|
|
79
|
+
rootTabIndex = 0
|
|
80
|
+
} else if (needsRootFocusFallback) {
|
|
81
|
+
rootTabIndex = -1
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const contentId = useId()
|
|
85
|
+
const titleId = useId()
|
|
86
|
+
const selectorRef = useRef<HTMLInputElement>(null)
|
|
87
|
+
const rootRef = useRef<HTMLDivElement>(null)
|
|
88
|
+
const contentRef = useRef<HTMLDivElement>(null)
|
|
89
|
+
const prefersReducedMotion = usePrefersReducedMotion()
|
|
90
|
+
|
|
91
|
+
const borderColor = STATE_BORDER_COLOR[state]
|
|
92
|
+
const backgroundColor = state === 'selected' ? palette.primary[50] : undefined
|
|
93
|
+
const focusRingShadow = `0 0 0 ${FOCUS_RING_WIDTH} ${palette.primary.main}`
|
|
94
|
+
const transitionTiming = `${motion.duration.standard}ms ${motion.easing.standard}`
|
|
95
|
+
|
|
96
|
+
const leadingWithExpandNotify: AdvancedListItemLeadingData = useMemo(() => {
|
|
97
|
+
if (
|
|
98
|
+
!canAttachExpandToLeading ||
|
|
99
|
+
(leading.type !== 'radio' && leading.type !== 'checkbox')
|
|
100
|
+
) {
|
|
101
|
+
return leading
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
...leading,
|
|
105
|
+
onChange: (checked: boolean) => {
|
|
106
|
+
leading.onChange(checked)
|
|
107
|
+
onExpandedChange?.(checked)
|
|
108
|
+
},
|
|
109
|
+
}
|
|
110
|
+
}, [canAttachExpandToLeading, leading, onExpandedChange])
|
|
111
|
+
|
|
112
|
+
// Runs for every collapse, not just ones driven by the leading control's own onChange —
|
|
113
|
+
// an external trigger (e.g. a parent-level "collapse" action) sets `expanded` directly,
|
|
114
|
+
// bypassing that onChange entirely. Without this, focus is left stranded inside content
|
|
115
|
+
// that's about to become inert.
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
if (!expanded && contentRef.current?.contains(document.activeElement)) {
|
|
118
|
+
if (selectorRef.current) {
|
|
119
|
+
selectorRef.current.focus()
|
|
120
|
+
} else {
|
|
121
|
+
rootRef.current?.focus()
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}, [expanded])
|
|
125
|
+
|
|
126
|
+
const isInsideExpandedContent = useCallback(
|
|
127
|
+
(target: EventTarget | null) => !!contentRef.current?.contains(target as Node),
|
|
128
|
+
[],
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
const activate = useCallback(
|
|
132
|
+
(event: React.SyntheticEvent) => {
|
|
133
|
+
if (isInsideExpandedContent(event.target)) {
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const clickedInputDirectly =
|
|
138
|
+
isSelectable && selectorRef.current?.contains(event.target as Node)
|
|
139
|
+
|
|
140
|
+
if (isSelectable && !clickedInputDirectly) {
|
|
141
|
+
if (
|
|
142
|
+
leadingWithExpandNotify.type === 'checkbox' ||
|
|
143
|
+
leadingWithExpandNotify.type === 'radio'
|
|
144
|
+
) {
|
|
145
|
+
const nextChecked =
|
|
146
|
+
leadingWithExpandNotify.type === 'radio'
|
|
147
|
+
? true
|
|
148
|
+
: !leadingWithExpandNotify.checked
|
|
149
|
+
leadingWithExpandNotify.onChange(nextChecked)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
onClick?.()
|
|
153
|
+
},
|
|
154
|
+
[isInsideExpandedContent, isSelectable, leadingWithExpandNotify, onClick],
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
const handleRootClick = useCallback(
|
|
158
|
+
(event: React.MouseEvent) => activate(event),
|
|
159
|
+
[activate],
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
const handleRootKeyDown = useCallback(
|
|
163
|
+
(event: React.KeyboardEvent) => {
|
|
164
|
+
if (isInsideExpandedContent(event.target)) {
|
|
165
|
+
return
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
169
|
+
event.preventDefault()
|
|
170
|
+
activate(event)
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
[activate, isInsideExpandedContent],
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
return (
|
|
177
|
+
<Box
|
|
178
|
+
ref={rootRef}
|
|
179
|
+
role={isRootFocusable ? 'button' : undefined}
|
|
180
|
+
tabIndex={rootTabIndex}
|
|
181
|
+
onClick={isClickable ? handleRootClick : undefined}
|
|
182
|
+
onKeyDown={isRootFocusable ? handleRootKeyDown : undefined}
|
|
183
|
+
aria-disabled={isDisabled || undefined}
|
|
184
|
+
sx={{
|
|
185
|
+
display: 'flex',
|
|
186
|
+
flexDirection: 'column',
|
|
187
|
+
gap: '0.75rem',
|
|
188
|
+
width: '100%',
|
|
189
|
+
boxSizing: 'border-box',
|
|
190
|
+
border: `${BORDER_WIDTH} solid`,
|
|
191
|
+
borderColor,
|
|
192
|
+
backgroundColor,
|
|
193
|
+
borderRadius: BORDER_RADIUS,
|
|
194
|
+
padding: '0.5rem 0.75rem',
|
|
195
|
+
opacity: isDisabled ? 0.6 : 1,
|
|
196
|
+
pointerEvents: isDisabled ? 'none' : 'auto',
|
|
197
|
+
cursor: isClickable ? 'pointer' : 'default',
|
|
198
|
+
boxShadow: state === 'selected' ? focusRingShadow : 'none',
|
|
199
|
+
transition: prefersReducedMotion
|
|
200
|
+
? 'none'
|
|
201
|
+
: `border-color ${transitionTiming}, box-shadow ${transitionTiming}`,
|
|
202
|
+
'&:hover': isClickable
|
|
203
|
+
? { borderColor: state === 'rest' ? palette.grey[400] : borderColor }
|
|
204
|
+
: undefined,
|
|
205
|
+
'&:focus-visible, &:focus-within': !isDisabled
|
|
206
|
+
? { borderColor: palette.primary.main, boxShadow: focusRingShadow }
|
|
207
|
+
: undefined,
|
|
208
|
+
}}
|
|
209
|
+
>
|
|
210
|
+
<Stack
|
|
211
|
+
direction='row'
|
|
212
|
+
alignItems='flex-start'
|
|
213
|
+
flexWrap={{ xs: 'wrap', md: 'nowrap' }}
|
|
214
|
+
gap='0.75rem'
|
|
215
|
+
>
|
|
216
|
+
{leading.type !== 'none' && (
|
|
217
|
+
<Box sx={{ alignSelf: 'center', order: { xs: -1, md: 0 } }}>
|
|
218
|
+
<AdvancedListItemLeading
|
|
219
|
+
leading={leadingWithExpandNotify}
|
|
220
|
+
disabled={isDisabled}
|
|
221
|
+
inputRef={canAttachExpandToLeading ? selectorRef : undefined}
|
|
222
|
+
expandableId={canAttachExpandToLeading ? contentId : undefined}
|
|
223
|
+
expanded={canAttachExpandToLeading ? expanded : undefined}
|
|
224
|
+
titleId={titleId}
|
|
225
|
+
/>
|
|
226
|
+
</Box>
|
|
227
|
+
)}
|
|
228
|
+
|
|
229
|
+
<Box
|
|
230
|
+
sx={{
|
|
231
|
+
display: 'flex',
|
|
232
|
+
flexDirection: 'row',
|
|
233
|
+
alignItems: 'flex-start',
|
|
234
|
+
gap: '0.75rem',
|
|
235
|
+
minWidth: 0,
|
|
236
|
+
flex: {
|
|
237
|
+
xs: trailing.type !== 'none' ? '1 1 100%' : '1 1 0%',
|
|
238
|
+
md: '1 1 0%',
|
|
239
|
+
},
|
|
240
|
+
alignSelf: subtitle ? undefined : 'center',
|
|
241
|
+
}}
|
|
242
|
+
>
|
|
243
|
+
{!!leadingMedia && <Box sx={LEADING_MEDIA_SX}>{leadingMedia}</Box>}
|
|
244
|
+
|
|
245
|
+
<Box
|
|
246
|
+
sx={{
|
|
247
|
+
flex: 1,
|
|
248
|
+
minWidth: 0,
|
|
249
|
+
display: 'flex',
|
|
250
|
+
flexDirection: 'column',
|
|
251
|
+
gap: '0.25rem',
|
|
252
|
+
}}
|
|
253
|
+
>
|
|
254
|
+
<Stack
|
|
255
|
+
direction='row'
|
|
256
|
+
alignItems='center'
|
|
257
|
+
flexWrap='wrap'
|
|
258
|
+
gap='0.5rem'
|
|
259
|
+
sx={{ alignSelf: 'stretch' }}
|
|
260
|
+
>
|
|
261
|
+
<Typography
|
|
262
|
+
id={titleId}
|
|
263
|
+
variant='h3'
|
|
264
|
+
sx={{ fontSize: '1rem', fontWeight: 600, lineHeight: '1.375rem' }}
|
|
265
|
+
color={palette.grey[900]}
|
|
266
|
+
>
|
|
267
|
+
{title}
|
|
268
|
+
</Typography>
|
|
269
|
+
{!!label && <RcSesBadge label={label} variant='neutral' size='small' />}
|
|
270
|
+
</Stack>
|
|
271
|
+
|
|
272
|
+
{!!subtitle && (
|
|
273
|
+
<Typography
|
|
274
|
+
variant='body2'
|
|
275
|
+
sx={{ fontSize: '0.875rem', fontWeight: 400, lineHeight: '1.25rem' }}
|
|
276
|
+
color={palette.grey[600]}
|
|
277
|
+
>
|
|
278
|
+
{subtitle}
|
|
279
|
+
</Typography>
|
|
280
|
+
)}
|
|
281
|
+
|
|
282
|
+
{!!metaItems?.length && (
|
|
283
|
+
<ListWithIcons items={metaItems} layout='horizontal' />
|
|
284
|
+
)}
|
|
285
|
+
</Box>
|
|
286
|
+
</Box>
|
|
287
|
+
|
|
288
|
+
{trailing.type !== 'none' && (
|
|
289
|
+
<Box
|
|
290
|
+
sx={{
|
|
291
|
+
alignSelf: 'center',
|
|
292
|
+
order: leading.type !== 'none' ? { xs: -1, md: 0 } : 0,
|
|
293
|
+
marginLeft: leading.type !== 'none' ? { xs: 'auto', md: 0 } : 0,
|
|
294
|
+
}}
|
|
295
|
+
>
|
|
296
|
+
<AdvancedListItemTrailing trailing={trailing} disabled={isDisabled} />
|
|
297
|
+
</Box>
|
|
298
|
+
)}
|
|
299
|
+
</Stack>
|
|
300
|
+
|
|
301
|
+
{isExpandable && (
|
|
302
|
+
<Collapse
|
|
303
|
+
in={expanded}
|
|
304
|
+
timeout={prefersReducedMotion ? 0 : motion.duration.standard}
|
|
305
|
+
easing={motion.easing.standard}
|
|
306
|
+
>
|
|
307
|
+
<Box
|
|
308
|
+
ref={contentRef}
|
|
309
|
+
id={contentId}
|
|
310
|
+
sx={{
|
|
311
|
+
marginLeft: leading.type !== 'none' ? { xs: 0, md: '2rem' } : 0,
|
|
312
|
+
backgroundColor: palette.grey[50],
|
|
313
|
+
borderRadius: BORDER_RADIUS,
|
|
314
|
+
padding: '0.75rem 1rem',
|
|
315
|
+
}}
|
|
316
|
+
{...({
|
|
317
|
+
inert: expanded ? undefined : '',
|
|
318
|
+
} as React.HTMLAttributes<HTMLDivElement>)}
|
|
319
|
+
>
|
|
320
|
+
{expandedContent}
|
|
321
|
+
</Box>
|
|
322
|
+
</Collapse>
|
|
323
|
+
)}
|
|
324
|
+
</Box>
|
|
325
|
+
)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export default AdvancedListItem
|
|
329
|
+
export type { AdvancedListItemProps }
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { useMediaQuery } from '@mui/material'
|
|
2
|
+
import { ThemeProvider } from '@mui/material/styles'
|
|
3
|
+
import { fireEvent, render, screen } from '@testing-library/react'
|
|
4
|
+
import { type ReactElement } from 'react'
|
|
5
|
+
import { beforeEach, describe, expect, test, vi } from 'vitest'
|
|
6
|
+
|
|
7
|
+
import theme from '@/theme/light'
|
|
8
|
+
|
|
9
|
+
import AdvancedList, { type AdvancedListItemData } from './index'
|
|
10
|
+
|
|
11
|
+
vi.mock('@mui/material', async () => {
|
|
12
|
+
const actual = await vi.importActual<typeof import('@mui/material')>('@mui/material')
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
...actual,
|
|
16
|
+
useMediaQuery: vi.fn(() => false),
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const mockedUseMediaQuery = vi.mocked(useMediaQuery)
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
mockedUseMediaQuery.mockReturnValue(false)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
const renderList = (ui: ReactElement) =>
|
|
27
|
+
render(<ThemeProvider theme={theme}>{ui}</ThemeProvider>)
|
|
28
|
+
|
|
29
|
+
const makeItems = (count: number): AdvancedListItemData[] =>
|
|
30
|
+
Array.from({ length: count }, (_, i) => ({
|
|
31
|
+
id: `item-${i + 1}`,
|
|
32
|
+
title: `Item ${i + 1}`,
|
|
33
|
+
subtitle: `Subtitle ${i + 1}`,
|
|
34
|
+
}))
|
|
35
|
+
|
|
36
|
+
describe('AdvancedList', () => {
|
|
37
|
+
test('renders all items on the first page when count <= page size', () => {
|
|
38
|
+
renderList(<AdvancedList items={makeItems(3)} />)
|
|
39
|
+
|
|
40
|
+
expect(screen.getByText('Item 1')).toBeInTheDocument()
|
|
41
|
+
expect(screen.getByText('Item 2')).toBeInTheDocument()
|
|
42
|
+
expect(screen.getByText('Item 3')).toBeInTheDocument()
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
test('shows only 5 items per page', () => {
|
|
46
|
+
renderList(<AdvancedList items={makeItems(8)} />)
|
|
47
|
+
|
|
48
|
+
expect(screen.getByText('Item 1')).toBeInTheDocument()
|
|
49
|
+
expect(screen.getByText('Item 5')).toBeInTheDocument()
|
|
50
|
+
expect(screen.queryByText('Item 6')).not.toBeInTheDocument()
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
test('navigating to next page shows the next set of items', () => {
|
|
54
|
+
renderList(<AdvancedList items={makeItems(8)} />)
|
|
55
|
+
|
|
56
|
+
fireEvent.click(screen.getByRole('button', { name: 'Kitas' }))
|
|
57
|
+
|
|
58
|
+
expect(screen.getByText('Item 6')).toBeInTheDocument()
|
|
59
|
+
expect(screen.getByText('Item 7')).toBeInTheDocument()
|
|
60
|
+
expect(screen.getByText('Item 8')).toBeInTheDocument()
|
|
61
|
+
expect(screen.queryByText('Item 1')).not.toBeInTheDocument()
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test("passes each item's own leading/trailing/expanded config through to AdvancedListItem", () => {
|
|
65
|
+
const onChange = vi.fn()
|
|
66
|
+
|
|
67
|
+
renderList(
|
|
68
|
+
<AdvancedList
|
|
69
|
+
items={[
|
|
70
|
+
{
|
|
71
|
+
id: 'radio-item',
|
|
72
|
+
title: 'Savininkas juridinis asmuo',
|
|
73
|
+
leading: { type: 'radio', checked: true, onChange },
|
|
74
|
+
expanded: true,
|
|
75
|
+
expandedContent: <input aria-label='Įmonės kodas' />,
|
|
76
|
+
},
|
|
77
|
+
]}
|
|
78
|
+
/>,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
expect(screen.getByRole('radio')).toBeChecked()
|
|
82
|
+
expect(screen.getByLabelText('Įmonės kodas')).toBeInTheDocument()
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('renders error node and hides the list when error is provided', () => {
|
|
86
|
+
renderList(<AdvancedList items={makeItems(3)} error={<p>Something went wrong</p>} />)
|
|
87
|
+
|
|
88
|
+
expect(screen.getByText('Something went wrong')).toBeInTheDocument()
|
|
89
|
+
expect(screen.queryByText('Item 1')).not.toBeInTheDocument()
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
test('does not render pagination controls when everything fits on one page', () => {
|
|
93
|
+
renderList(<AdvancedList items={makeItems(2)} />)
|
|
94
|
+
|
|
95
|
+
expect(screen.queryByRole('button', { name: 'Kitas' })).not.toBeInTheDocument()
|
|
96
|
+
})
|
|
97
|
+
})
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Box, Container } from '@mui/material'
|
|
2
|
+
import { ReactNode, useState } from 'react'
|
|
3
|
+
|
|
4
|
+
import DataPagination from '@/components/common/DataPagination'
|
|
5
|
+
|
|
6
|
+
import AdvancedListItem, {
|
|
7
|
+
type AdvancedListItemProps,
|
|
8
|
+
} from './components/AdvancedListItem'
|
|
9
|
+
|
|
10
|
+
export type AdvancedListItemData = AdvancedListItemProps & { id: string }
|
|
11
|
+
|
|
12
|
+
export interface AdvancedListProps {
|
|
13
|
+
items: AdvancedListItemData[]
|
|
14
|
+
error?: ReactNode
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const PAGE_SIZE = 5
|
|
18
|
+
|
|
19
|
+
const AdvancedList = ({ items, error }: AdvancedListProps) => {
|
|
20
|
+
const [page, setPage] = useState(1)
|
|
21
|
+
|
|
22
|
+
if (error != null) {
|
|
23
|
+
return (
|
|
24
|
+
<Container disableGutters sx={{ display: 'flex', justifyContent: 'center' }}>
|
|
25
|
+
{error}
|
|
26
|
+
</Container>
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const totalPages = Math.ceil(items.length / PAGE_SIZE)
|
|
31
|
+
const currentPage = totalPages > 0 ? Math.min(page, totalPages) : 1
|
|
32
|
+
const startIndex = (currentPage - 1) * PAGE_SIZE
|
|
33
|
+
const pagedItems = items.slice(startIndex, startIndex + PAGE_SIZE)
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<Container
|
|
37
|
+
disableGutters
|
|
38
|
+
sx={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '1rem' }}
|
|
39
|
+
>
|
|
40
|
+
<Box
|
|
41
|
+
sx={{ display: 'flex', flexDirection: 'column', gap: '0.625rem', width: '100%' }}
|
|
42
|
+
>
|
|
43
|
+
{pagedItems.map(({ id, ...itemProps }) => (
|
|
44
|
+
<AdvancedListItem key={id} {...itemProps} />
|
|
45
|
+
))}
|
|
46
|
+
</Box>
|
|
47
|
+
<DataPagination count={totalPages} page={currentPage} onChange={setPage} />
|
|
48
|
+
</Container>
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default AdvancedList
|
|
@@ -148,6 +148,56 @@ describe('SelectableCardListItem', () => {
|
|
|
148
148
|
expect(onSelect).toHaveBeenCalledTimes(1)
|
|
149
149
|
})
|
|
150
150
|
|
|
151
|
+
test('renders action button with label when hasActionButton is true', () => {
|
|
152
|
+
renderItem(
|
|
153
|
+
<SelectableCardListItem
|
|
154
|
+
title='With action'
|
|
155
|
+
selected={false}
|
|
156
|
+
onSelect={vi.fn()}
|
|
157
|
+
listItems={[]}
|
|
158
|
+
hasActionButton
|
|
159
|
+
actionButtonLabel='Veiksmas'
|
|
160
|
+
/>,
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
expect(screen.getByRole('button', { name: 'Veiksmas' })).toBeInTheDocument()
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
test('omits action button when hasActionButton is false', () => {
|
|
167
|
+
renderItem(
|
|
168
|
+
<SelectableCardListItem
|
|
169
|
+
title='No action'
|
|
170
|
+
selected={false}
|
|
171
|
+
onSelect={vi.fn()}
|
|
172
|
+
listItems={[]}
|
|
173
|
+
actionButtonLabel='Veiksmas'
|
|
174
|
+
/>,
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
expect(screen.queryByRole('button', { name: 'Veiksmas' })).not.toBeInTheDocument()
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
test('clicking action button calls onActionButtonClick and not onSelect', () => {
|
|
181
|
+
const onSelect = vi.fn()
|
|
182
|
+
const onActionButtonClick = vi.fn()
|
|
183
|
+
renderItem(
|
|
184
|
+
<SelectableCardListItem
|
|
185
|
+
title='With action'
|
|
186
|
+
selected={false}
|
|
187
|
+
onSelect={onSelect}
|
|
188
|
+
listItems={[]}
|
|
189
|
+
hasActionButton
|
|
190
|
+
actionButtonLabel='Veiksmas'
|
|
191
|
+
onActionButtonClick={onActionButtonClick}
|
|
192
|
+
/>,
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
fireEvent.click(screen.getByRole('button', { name: 'Veiksmas' }))
|
|
196
|
+
|
|
197
|
+
expect(onActionButtonClick).toHaveBeenCalledTimes(1)
|
|
198
|
+
expect(onSelect).not.toHaveBeenCalled()
|
|
199
|
+
})
|
|
200
|
+
|
|
151
201
|
test('loading card is not selectable', () => {
|
|
152
202
|
const onSelect = vi.fn()
|
|
153
203
|
const { container } = renderItem(
|
|
@@ -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 direction='row' alignItems='center' gap='0.75rem'>
|
|
@@ -97,6 +104,33 @@ const SelectableCardListItem = ({
|
|
|
97
104
|
},
|
|
98
105
|
}}
|
|
99
106
|
title={titleTemplate}
|
|
107
|
+
headerAction={
|
|
108
|
+
isLoading ? (
|
|
109
|
+
<Skeleton variant='rounded' width={100} height={38} />
|
|
110
|
+
) : (
|
|
111
|
+
hasActionButton && (
|
|
112
|
+
<RcSesButton
|
|
113
|
+
color='grey'
|
|
114
|
+
sx={{
|
|
115
|
+
height: 'auto',
|
|
116
|
+
padding: '0.5rem 1rem',
|
|
117
|
+
fontSize: '0.875rem',
|
|
118
|
+
lineHeight: '1.25rem',
|
|
119
|
+
'&.MuiButton-colorGrey': {
|
|
120
|
+
color: palette.grey[800],
|
|
121
|
+
},
|
|
122
|
+
}}
|
|
123
|
+
onClick={(e) => {
|
|
124
|
+
e.stopPropagation()
|
|
125
|
+
onActionButtonClick?.()
|
|
126
|
+
}}
|
|
127
|
+
variant='outlined'
|
|
128
|
+
>
|
|
129
|
+
{actionButtonLabel}
|
|
130
|
+
</RcSesButton>
|
|
131
|
+
)
|
|
132
|
+
)
|
|
133
|
+
}
|
|
100
134
|
slotProps={{
|
|
101
135
|
title: {
|
|
102
136
|
sx: {
|
|
@@ -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[]
|
|
@@ -86,6 +89,9 @@ const SelectableCardListPaged = ({
|
|
|
86
89
|
listItems={item.listItems || []}
|
|
87
90
|
selected={selectedId === item.id}
|
|
88
91
|
onSelect={() => onSelect(item.id)}
|
|
92
|
+
hasActionButton={item.hasActionButton}
|
|
93
|
+
actionButtonLabel={item.actionButtonLabel}
|
|
94
|
+
onActionButtonClick={item.onActionButtonClick}
|
|
89
95
|
/>
|
|
90
96
|
))}
|
|
91
97
|
</Box>
|
|
@@ -8,9 +8,7 @@ describe('RcSesTooltip', () => {
|
|
|
8
8
|
test('renders default trigger button', () => {
|
|
9
9
|
render(<RcSesTooltip title='Help text' />)
|
|
10
10
|
|
|
11
|
-
expect(
|
|
12
|
-
screen.getByRole('button', { name: 'components.Tooltip.trigger' }),
|
|
13
|
-
).toBeInTheDocument()
|
|
11
|
+
expect(screen.getByRole('button', { name: 'Paaiškinimas' })).toBeInTheDocument()
|
|
14
12
|
})
|
|
15
13
|
|
|
16
14
|
test('opens tooltip on trigger click', async () => {
|