@loadsmart/loadsmart-ui 5.3.1 → 5.5.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/components/Table/Table.d.ts +9 -1
- package/dist/components/Table/Table.stories.d.ts +6 -0
- package/dist/components/Table/Table.types.d.ts +8 -0
- package/dist/components/TablePagination/TablePagination.types.d.ts +3 -0
- package/dist/index.js +81 -70
- package/dist/index.js.map +1 -1
- package/package.json +1 -4
- package/src/components/Breadcrumbs/Breadbrumbs.test.tsx +10 -4
- package/src/components/Breadcrumbs/Breadcrumb.tsx +12 -4
- package/src/components/Breadcrumbs/Breadcrumbs.tsx +13 -8
- package/src/components/Table/Table.stories.tsx +61 -0
- package/src/components/Table/Table.test.tsx +128 -0
- package/src/components/Table/Table.tsx +89 -12
- package/src/components/Table/Table.types.ts +9 -0
- package/src/components/TablePagination/TablePagination.stories.tsx +6 -1
- package/src/components/TablePagination/TablePagination.test.tsx +0 -1
- package/src/components/TablePagination/TablePagination.tsx +1 -6
- package/src/components/TablePagination/TablePagination.types.ts +3 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loadsmart/loadsmart-ui",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.5.0",
|
|
4
4
|
"description": "Miranda UI, a React UI library",
|
|
5
5
|
"main": "dist",
|
|
6
6
|
"files": [
|
|
@@ -103,7 +103,6 @@
|
|
|
103
103
|
"@testing-library/react-hooks": "5.1.2",
|
|
104
104
|
"@testing-library/user-event": "13.1.9",
|
|
105
105
|
"@types/chance": "1.1.1",
|
|
106
|
-
"@types/enzyme": "3.10.8",
|
|
107
106
|
"@types/jest": "26.0.23",
|
|
108
107
|
"@types/lodash.debounce": "4.0.6",
|
|
109
108
|
"@types/lodash.flatten": "4.4.6",
|
|
@@ -117,7 +116,6 @@
|
|
|
117
116
|
"@types/styled-components": "5.1.9",
|
|
118
117
|
"@typescript-eslint/eslint-plugin": "4.22.1",
|
|
119
118
|
"@typescript-eslint/parser": "4.22.1",
|
|
120
|
-
"@wojtekmaj/enzyme-adapter-react-17": "0.6.1",
|
|
121
119
|
"@zerollup/ts-transform-paths": "1.7.18",
|
|
122
120
|
"autoprefixer": "9",
|
|
123
121
|
"babel-jest": "26.6.3",
|
|
@@ -130,7 +128,6 @@
|
|
|
130
128
|
"commitizen": "4.2.4",
|
|
131
129
|
"cross-env": "7.0.3",
|
|
132
130
|
"cz-conventional-changelog": "3.3.0",
|
|
133
|
-
"enzyme": "3.11.0",
|
|
134
131
|
"eslint": "7.26.0",
|
|
135
132
|
"eslint-config-prettier": "8.3.0",
|
|
136
133
|
"eslint-plugin-jest-dom": "3.8.1",
|
|
@@ -14,7 +14,7 @@ describe('<Breadcrumbs />', () => {
|
|
|
14
14
|
|
|
15
15
|
setup({ entries })
|
|
16
16
|
|
|
17
|
-
expect(screen.
|
|
17
|
+
expect(screen.getByRole('list')).not.toBeEmptyDOMElement()
|
|
18
18
|
|
|
19
19
|
for (let i = 0; i < entries.length; i++) {
|
|
20
20
|
screen.getByText(entries[i].label)
|
|
@@ -26,7 +26,7 @@ describe('<Breadcrumbs />', () => {
|
|
|
26
26
|
|
|
27
27
|
setup({ entries })
|
|
28
28
|
|
|
29
|
-
expect(screen.
|
|
29
|
+
expect(screen.getByRole('list')).toBeEmptyDOMElement()
|
|
30
30
|
})
|
|
31
31
|
|
|
32
32
|
it('renders active breadcrumb correctly', () => {
|
|
@@ -36,13 +36,15 @@ describe('<Breadcrumbs />', () => {
|
|
|
36
36
|
({ index }) => ({
|
|
37
37
|
label: generator.word(),
|
|
38
38
|
active: index === activeIndex,
|
|
39
|
+
url: '/',
|
|
39
40
|
}),
|
|
40
41
|
9
|
|
41
42
|
)
|
|
42
43
|
|
|
43
44
|
setup({ entries })
|
|
44
45
|
|
|
45
|
-
expect(screen.
|
|
46
|
+
expect(screen.getAllByRole('listitem')[activeIndex]).toHaveClass('is-active')
|
|
47
|
+
expect(screen.getAllByRole('link')[activeIndex]).toHaveAttribute('aria-current', 'page')
|
|
46
48
|
})
|
|
47
49
|
|
|
48
50
|
it('render non-url and non-onClick breadcrumb correctly', () => {
|
|
@@ -114,6 +116,10 @@ describe('<Breadcrumbs />', () => {
|
|
|
114
116
|
|
|
115
117
|
setup({ entries, onBack: jest.fn() })
|
|
116
118
|
|
|
117
|
-
expect(
|
|
119
|
+
expect(
|
|
120
|
+
screen.getByRole('button', {
|
|
121
|
+
name: 'Back to previous page',
|
|
122
|
+
})
|
|
123
|
+
).toBeInTheDocument()
|
|
118
124
|
})
|
|
119
125
|
})
|
|
@@ -47,22 +47,30 @@ const StyledLi = styled.li<BreadcrumbProps>`
|
|
|
47
47
|
position: absolute;
|
|
48
48
|
left: calc(100% + 14px);
|
|
49
49
|
|
|
50
|
-
font-weight:
|
|
50
|
+
font-weight: ${token('font-weight-bold')};
|
|
51
51
|
font-size: ${token('breadcrumbs-font-size')};
|
|
52
52
|
|
|
53
|
+
line-height: ${token('breadcrumbs-font-height')};
|
|
54
|
+
|
|
53
55
|
content: '/';
|
|
54
56
|
}
|
|
55
57
|
`
|
|
56
58
|
|
|
57
59
|
function getWrappedLabel(props: Partial<BreadcrumbProps>) {
|
|
58
|
-
const { url, label, onClick, ...rest } = props
|
|
60
|
+
const { url, label, onClick, active, ...rest } = props
|
|
59
61
|
|
|
60
62
|
if (isNil(url) && !onClick) {
|
|
61
63
|
return label
|
|
62
64
|
}
|
|
63
65
|
|
|
64
66
|
return (
|
|
65
|
-
<StyledLink
|
|
67
|
+
<StyledLink
|
|
68
|
+
href={url}
|
|
69
|
+
target="_self"
|
|
70
|
+
onClick={onClick}
|
|
71
|
+
{...(active && {'aria-current': 'page'})}
|
|
72
|
+
{...rest}
|
|
73
|
+
>
|
|
66
74
|
{label}
|
|
67
75
|
</StyledLink>
|
|
68
76
|
)
|
|
@@ -76,7 +84,7 @@ function Breadcrumb({ active, label, url, ...rest }: BreadcrumbProps): JSX.Eleme
|
|
|
76
84
|
data-testid="breadcrumb"
|
|
77
85
|
active={active}
|
|
78
86
|
>
|
|
79
|
-
{getWrappedLabel({ label, url, ...rest })}
|
|
87
|
+
{getWrappedLabel({ label, url, active, ...rest })}
|
|
80
88
|
</StyledLi>
|
|
81
89
|
)
|
|
82
90
|
}
|
|
@@ -12,12 +12,12 @@ export interface BreadcrumbsProps {
|
|
|
12
12
|
onBack?: (e: MouseEvent<HTMLButtonElement>) => void
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
const StyledWrapper = styled.
|
|
15
|
+
const StyledWrapper = styled.nav`
|
|
16
16
|
display: flex;
|
|
17
17
|
flex-direction: row;
|
|
18
18
|
`
|
|
19
19
|
|
|
20
|
-
const
|
|
20
|
+
const StyledOl = styled.ol`
|
|
21
21
|
display: flex;
|
|
22
22
|
flex-direction: row;
|
|
23
23
|
margin: 0;
|
|
@@ -31,18 +31,23 @@ const StyledBackButton = styled(BackButton)`
|
|
|
31
31
|
margin: 0 ${token('space-m')} 0 0;
|
|
32
32
|
`
|
|
33
33
|
|
|
34
|
-
function Breadcrumbs({ entries, onBack, disabled, ...others }: BreadcrumbsProps): JSX.Element {
|
|
34
|
+
function Breadcrumbs({ entries = [], onBack, disabled, ...others }: BreadcrumbsProps): JSX.Element {
|
|
35
35
|
return (
|
|
36
|
-
<StyledWrapper {...others}>
|
|
36
|
+
<StyledWrapper aria-label="Breadcrumb" {...others}>
|
|
37
37
|
{onBack && (
|
|
38
|
-
<StyledBackButton
|
|
38
|
+
<StyledBackButton
|
|
39
|
+
data-testid="breadcrumbs-back"
|
|
40
|
+
onClick={onBack}
|
|
41
|
+
disabled={disabled}
|
|
42
|
+
aria-label="Back to previous page"
|
|
43
|
+
/>
|
|
39
44
|
)}
|
|
40
45
|
|
|
41
|
-
<
|
|
42
|
-
{
|
|
46
|
+
<StyledOl data-testid="breadcrumbs">
|
|
47
|
+
{entries.map((entry: BreadcrumbProps) => {
|
|
43
48
|
return <Breadcrumb key={entry.label} {...entry} />
|
|
44
49
|
})}
|
|
45
|
-
</
|
|
50
|
+
</StyledOl>
|
|
46
51
|
</StyledWrapper>
|
|
47
52
|
)
|
|
48
53
|
}
|
|
@@ -508,3 +508,64 @@ This function is passed a boolean \`selected\` and a function \`toggle\`.
|
|
|
508
508
|
},
|
|
509
509
|
},
|
|
510
510
|
}
|
|
511
|
+
|
|
512
|
+
export function WithExpandableRow(args: TableProps): JSX.Element {
|
|
513
|
+
const leading = (expanded: boolean) => {
|
|
514
|
+
if (expanded) {
|
|
515
|
+
return <div style={{ fontWeight: 'bold' }}>Open</div>
|
|
516
|
+
}
|
|
517
|
+
return <div style={{ fontWeight: 'bold' }}>Closed</div>
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
return (
|
|
521
|
+
<div className="p-4 bg-neutral-white">
|
|
522
|
+
<Table {...args}>
|
|
523
|
+
<Table.Head>
|
|
524
|
+
<Table.Row>
|
|
525
|
+
<Table.Expandable.HeadCell />
|
|
526
|
+
<Table.Expandable.HeadCell>Reference</Table.Expandable.HeadCell>
|
|
527
|
+
<Table.Expandable.HeadCell>Product</Table.Expandable.HeadCell>
|
|
528
|
+
<Table.Expandable.HeadCell>Price</Table.Expandable.HeadCell>
|
|
529
|
+
</Table.Row>
|
|
530
|
+
</Table.Head>
|
|
531
|
+
|
|
532
|
+
<Table.Body>
|
|
533
|
+
<Table.Expandable.Row
|
|
534
|
+
index={0}
|
|
535
|
+
leading={leading}
|
|
536
|
+
expandableContent={<div>Forced expanded with custom leading</div>}
|
|
537
|
+
expanded
|
|
538
|
+
>
|
|
539
|
+
<Table.Expandable.Cell>123456</Table.Expandable.Cell>
|
|
540
|
+
<Table.Expandable.Cell>Body 1</Table.Expandable.Cell>
|
|
541
|
+
<Table.Expandable.Cell>12345</Table.Expandable.Cell>
|
|
542
|
+
</Table.Expandable.Row>
|
|
543
|
+
<Table.Expandable.Row
|
|
544
|
+
index={1}
|
|
545
|
+
leading={leading}
|
|
546
|
+
expandableContent={<div>Initial expanded row with custom leading</div>}
|
|
547
|
+
initialExpanded
|
|
548
|
+
>
|
|
549
|
+
<Table.Expandable.Cell>123456</Table.Expandable.Cell>
|
|
550
|
+
<Table.Expandable.Cell>Body 2</Table.Expandable.Cell>
|
|
551
|
+
<Table.Expandable.Cell>12345</Table.Expandable.Cell>
|
|
552
|
+
</Table.Expandable.Row>
|
|
553
|
+
<Table.Expandable.Row index={2} expandableContent={<div>Expandable row</div>}>
|
|
554
|
+
<Table.Expandable.Cell>123456</Table.Expandable.Cell>
|
|
555
|
+
<Table.Expandable.Cell>Body 3</Table.Expandable.Cell>
|
|
556
|
+
<Table.Expandable.Cell>12345</Table.Expandable.Cell>
|
|
557
|
+
</Table.Expandable.Row>
|
|
558
|
+
<Table.Expandable.Row index={3} expanded>
|
|
559
|
+
<Table.Expandable.Cell>123456</Table.Expandable.Cell>
|
|
560
|
+
<Table.Expandable.Cell>Body 5</Table.Expandable.Cell>
|
|
561
|
+
<Table.Expandable.Cell>12345</Table.Expandable.Cell>
|
|
562
|
+
</Table.Expandable.Row>
|
|
563
|
+
</Table.Body>
|
|
564
|
+
</Table>
|
|
565
|
+
</div>
|
|
566
|
+
)
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
WithExpandableRow.args = {
|
|
570
|
+
scale: 'default',
|
|
571
|
+
}
|
|
@@ -182,4 +182,132 @@ describe('<Table />', () => {
|
|
|
182
182
|
expect(checkboxes[0]).toBeChecked()
|
|
183
183
|
expect(checkboxes[1]).toBeChecked()
|
|
184
184
|
})
|
|
185
|
+
|
|
186
|
+
it('Table.Expandable renders correclty', () => {
|
|
187
|
+
const onExpandedChange = jest.fn()
|
|
188
|
+
|
|
189
|
+
const expandableTable = ({ controlledExpanded4 }: { controlledExpanded4: boolean }) => (
|
|
190
|
+
<React.Fragment>
|
|
191
|
+
<Table.Head>
|
|
192
|
+
<Table.Row>
|
|
193
|
+
<Table.Expandable.HeadCell />
|
|
194
|
+
<Table.HeadCell># Number</Table.HeadCell>
|
|
195
|
+
<Table.HeadCell>Company</Table.HeadCell>
|
|
196
|
+
<Table.HeadCell alignment="right">Price $</Table.HeadCell>
|
|
197
|
+
</Table.Row>
|
|
198
|
+
</Table.Head>
|
|
199
|
+
<Table.Body>
|
|
200
|
+
<Table.Expandable.Row
|
|
201
|
+
index={0}
|
|
202
|
+
expandableContent={<div>This is an expandable content on index 0</div>}
|
|
203
|
+
expanded
|
|
204
|
+
>
|
|
205
|
+
<Table.Cell format="number">#000000-1</Table.Cell>
|
|
206
|
+
<Table.Cell>Modal X 21</Table.Cell>
|
|
207
|
+
<Table.Cell format="currency" alignment="right">
|
|
208
|
+
$9876,50
|
|
209
|
+
</Table.Cell>
|
|
210
|
+
</Table.Expandable.Row>
|
|
211
|
+
<Table.Expandable.Row index={1}>
|
|
212
|
+
<Table.Cell format="number">#000000-2</Table.Cell>
|
|
213
|
+
<Table.Cell>Modal X 21</Table.Cell>
|
|
214
|
+
<Table.Cell format="currency" alignment="right">
|
|
215
|
+
$9876,50
|
|
216
|
+
</Table.Cell>
|
|
217
|
+
</Table.Expandable.Row>
|
|
218
|
+
<Table.Expandable.Row
|
|
219
|
+
index={2}
|
|
220
|
+
expandableContent={<div>This is an expandable content on index 2</div>}
|
|
221
|
+
leading={(expanded) => (expanded ? 'open' : 'closed')}
|
|
222
|
+
onExpandedChange={onExpandedChange}
|
|
223
|
+
>
|
|
224
|
+
<Table.Cell format="number">#000000-3</Table.Cell>
|
|
225
|
+
<Table.Cell>Modal X 21</Table.Cell>
|
|
226
|
+
<Table.Cell format="currency" alignment="right">
|
|
227
|
+
$9876,50
|
|
228
|
+
</Table.Cell>
|
|
229
|
+
</Table.Expandable.Row>
|
|
230
|
+
<Table.Expandable.Row
|
|
231
|
+
index={3}
|
|
232
|
+
expandableContent={<div>This is an expandable content on index 3</div>}
|
|
233
|
+
expanded={controlledExpanded4}
|
|
234
|
+
>
|
|
235
|
+
<Table.Cell format="number">#000000-4</Table.Cell>
|
|
236
|
+
<Table.Cell>Modal X 21</Table.Cell>
|
|
237
|
+
<Table.Cell format="currency" alignment="right">
|
|
238
|
+
$9876,50
|
|
239
|
+
</Table.Cell>
|
|
240
|
+
</Table.Expandable.Row>
|
|
241
|
+
</Table.Body>
|
|
242
|
+
</React.Fragment>
|
|
243
|
+
)
|
|
244
|
+
const props = {
|
|
245
|
+
children: expandableTable({ controlledExpanded4: true }),
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const { rerender } = setup(props)
|
|
249
|
+
|
|
250
|
+
// Check if table is displaying 3 rows
|
|
251
|
+
const row1 = screen.getByText(/000000-1/i).closest('tr')
|
|
252
|
+
expect(row1).toBeInTheDocument()
|
|
253
|
+
|
|
254
|
+
const row2 = screen.getByText(/000000-2/i).closest('tr')
|
|
255
|
+
expect(row2).toBeInTheDocument()
|
|
256
|
+
|
|
257
|
+
const row3 = screen.getByText(/000000-3/i).closest('tr')
|
|
258
|
+
expect(row3).toBeInTheDocument()
|
|
259
|
+
|
|
260
|
+
const row4 = screen.getByText(/000000-4/i).closest('tr')
|
|
261
|
+
expect(row4).toBeInTheDocument()
|
|
262
|
+
|
|
263
|
+
// Row 1 should be forced to be expanded
|
|
264
|
+
expect(screen.getByText(/This is an expandable content on index 0/i)).toBeInTheDocument()
|
|
265
|
+
|
|
266
|
+
// Clicking row1
|
|
267
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
268
|
+
user.click(row1!)
|
|
269
|
+
|
|
270
|
+
// Row 1 can't be collapsed
|
|
271
|
+
expect(screen.getByText(/This is an expandable content on index 0/i)).toBeInTheDocument()
|
|
272
|
+
|
|
273
|
+
// Checking if Row 3 has custom trailing (row is collapsed)
|
|
274
|
+
expect(screen.getByText(/close/i)).toBeInTheDocument()
|
|
275
|
+
|
|
276
|
+
// Expanding row3
|
|
277
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
278
|
+
user.click(row3!)
|
|
279
|
+
expect(screen.getByText(/This is an expandable content on index 2/i)).toBeInTheDocument()
|
|
280
|
+
|
|
281
|
+
// Check if onExpandedChange is triggered
|
|
282
|
+
expect(onExpandedChange).toHaveBeenCalledTimes(1)
|
|
283
|
+
expect(onExpandedChange).toHaveBeenCalledWith(true)
|
|
284
|
+
|
|
285
|
+
// Checking if Row 3 has custom trailing (row is expanded)
|
|
286
|
+
expect(screen.getByText(/open/i)).toBeInTheDocument()
|
|
287
|
+
|
|
288
|
+
// Collapsing row3
|
|
289
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
290
|
+
user.click(row3!)
|
|
291
|
+
expect(screen.queryByText(/This is an expandable content on index 2/i)).not.toBeInTheDocument()
|
|
292
|
+
|
|
293
|
+
// Check if onExpandedChange is triggered
|
|
294
|
+
expect(onExpandedChange).toHaveBeenCalledTimes(2)
|
|
295
|
+
expect(onExpandedChange).toHaveBeenCalledWith(false)
|
|
296
|
+
|
|
297
|
+
// Checking controlled expanded (row4), it's opened by default
|
|
298
|
+
expect(screen.queryByText(/This is an expandable content on index 4/i)).not.toBeInTheDocument()
|
|
299
|
+
|
|
300
|
+
// Clicking on row4 shouldn't change the open state because it's controlled externally
|
|
301
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
302
|
+
user.click(row4!)
|
|
303
|
+
|
|
304
|
+
// row4 should be still opened
|
|
305
|
+
expect(screen.queryByText(/This is an expandable content on index 4/i)).not.toBeInTheDocument()
|
|
306
|
+
|
|
307
|
+
// Re-render to change controlled state
|
|
308
|
+
rerender(expandableTable({ controlledExpanded4: false }))
|
|
309
|
+
|
|
310
|
+
// row4 should be collapsed because externally controlled state is changed
|
|
311
|
+
expect(screen.queryByText(/This is an expandable content on index 4/i)).not.toBeInTheDocument()
|
|
312
|
+
})
|
|
185
313
|
})
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { Children, Fragment, isValidElement, useEffect } from 'react'
|
|
1
|
+
import React, { Children, Fragment, isValidElement, useCallback, useEffect, useState } from 'react'
|
|
2
2
|
import styled, { css } from 'styled-components'
|
|
3
3
|
import type { ReactNode } from 'react'
|
|
4
4
|
import { isFunction } from '@loadsmart/utils-function'
|
|
@@ -35,6 +35,7 @@ import type {
|
|
|
35
35
|
SelectionCellProps,
|
|
36
36
|
TablePickerItemProps,
|
|
37
37
|
TablePickerProps,
|
|
38
|
+
ExpandableTableRowProps,
|
|
38
39
|
} from './Table.types'
|
|
39
40
|
|
|
40
41
|
const StyledTableBody = styled.tbody`
|
|
@@ -104,27 +105,31 @@ const StyledTableHead = styled.thead`
|
|
|
104
105
|
}
|
|
105
106
|
`
|
|
106
107
|
|
|
107
|
-
const
|
|
108
|
+
const BaseStyledTableRow = styled.tr`
|
|
108
109
|
${StyledTableHead} > & {
|
|
109
110
|
height: 36px;
|
|
110
111
|
|
|
111
112
|
background-color: ${token('color-neutral-white')};
|
|
112
113
|
}
|
|
113
114
|
|
|
115
|
+
${StyledTableBody} > & {
|
|
116
|
+
${hoverable`
|
|
117
|
+
outline: 1px solid ${token('color-accent')};
|
|
118
|
+
`}
|
|
119
|
+
|
|
120
|
+
${focusable`
|
|
121
|
+
box-shadow: inset ${token('shadow-glow-primary')};
|
|
122
|
+
`}
|
|
123
|
+
}
|
|
124
|
+
`
|
|
125
|
+
|
|
126
|
+
const StyledTableRow = styled(BaseStyledTableRow)<{ selected?: boolean }>`
|
|
114
127
|
background-color: ${conditional({
|
|
115
128
|
'table-row-selected-color': whenProps({ selected: true }),
|
|
116
129
|
'color-transparent': whenProps({ selected: false }),
|
|
117
130
|
})};
|
|
118
131
|
|
|
119
132
|
${StyledTableBody} > & {
|
|
120
|
-
${hoverable`
|
|
121
|
-
outline: 1px solid ${token('color-accent')};
|
|
122
|
-
`}
|
|
123
|
-
|
|
124
|
-
${focusable`
|
|
125
|
-
box-shadow: inset ${token('shadow-glow-primary')};
|
|
126
|
-
`}
|
|
127
|
-
|
|
128
133
|
&:nth-child(odd) ${StyledTableCell} {
|
|
129
134
|
background: ${token('table-row-variant-color')};
|
|
130
135
|
}
|
|
@@ -135,6 +140,13 @@ const StyledTableRow = styled.tr<{ selected?: boolean }>`
|
|
|
135
140
|
}
|
|
136
141
|
`
|
|
137
142
|
|
|
143
|
+
const StyledExpandableTableRow = styled(BaseStyledTableRow)<{ $even: boolean }>`
|
|
144
|
+
background-color: ${conditional({
|
|
145
|
+
'table-row-variant-color': whenProps({ $even: true }),
|
|
146
|
+
'color-transparent': whenProps({ $even: false }),
|
|
147
|
+
})};
|
|
148
|
+
`
|
|
149
|
+
|
|
138
150
|
const StyledTable = styled.table<{ scale?: string }>`
|
|
139
151
|
width: 100%;
|
|
140
152
|
|
|
@@ -144,7 +156,8 @@ const StyledTable = styled.table<{ scale?: string }>`
|
|
|
144
156
|
|
|
145
157
|
border-collapse: collapse;
|
|
146
158
|
|
|
147
|
-
${StyledTableBody} ${StyledTableRow}
|
|
159
|
+
${StyledTableBody} ${StyledTableRow},
|
|
160
|
+
${StyledTableBody} ${StyledExpandableTableRow} {
|
|
148
161
|
height: ${conditional({
|
|
149
162
|
'24px': whenProps({ scale: 'small' }),
|
|
150
163
|
'48px': whenProps({ scale: 'default' }),
|
|
@@ -153,7 +166,9 @@ const StyledTable = styled.table<{ scale?: string }>`
|
|
|
153
166
|
}
|
|
154
167
|
|
|
155
168
|
${StyledTableBody} ${StyledTableRow} ${StyledTableCell},
|
|
156
|
-
${StyledTableHead} ${StyledTableRow} ${StyledTableHeadCell}
|
|
169
|
+
${StyledTableHead} ${StyledTableRow} ${StyledTableHeadCell},
|
|
170
|
+
${StyledTableBody} ${StyledExpandableTableRow} ${StyledTableCell},
|
|
171
|
+
${StyledTableHead} ${StyledExpandableTableRow} ${StyledTableHeadCell} {
|
|
157
172
|
padding: ${conditional({
|
|
158
173
|
'space-xs': whenProps({ scale: 'small' }),
|
|
159
174
|
'space-s': whenProps({ scale: ['default', 'large'] }),
|
|
@@ -166,6 +181,12 @@ const StyledTable = styled.table<{ scale?: string }>`
|
|
|
166
181
|
}
|
|
167
182
|
`
|
|
168
183
|
|
|
184
|
+
const RotatableIcon = styled(Icon)<{ $rotate: boolean }>`
|
|
185
|
+
${conditional({
|
|
186
|
+
'transform: rotate(90deg);': whenProps({ $rotate: true }),
|
|
187
|
+
})}
|
|
188
|
+
`
|
|
189
|
+
|
|
169
190
|
function Table<T>({
|
|
170
191
|
children,
|
|
171
192
|
selection,
|
|
@@ -269,6 +290,14 @@ function SelectionHeadCell<T>(props: SelectionCellProps<T>): JSX.Element {
|
|
|
269
290
|
)
|
|
270
291
|
}
|
|
271
292
|
|
|
293
|
+
function ExpandableHeadCell(props: TableCellProps): JSX.Element {
|
|
294
|
+
return <TableHeadCell {...props} />
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function ExpandableCell(props: TableCellProps): JSX.Element {
|
|
298
|
+
return <TableCell {...props} />
|
|
299
|
+
}
|
|
300
|
+
|
|
272
301
|
function TableRow({ children, ...others }: TableRowProps): JSX.Element {
|
|
273
302
|
const selected = useIsRowSelected(children)
|
|
274
303
|
|
|
@@ -279,6 +308,49 @@ function TableRow({ children, ...others }: TableRowProps): JSX.Element {
|
|
|
279
308
|
)
|
|
280
309
|
}
|
|
281
310
|
|
|
311
|
+
function ExpandableTableRow({
|
|
312
|
+
index,
|
|
313
|
+
expandableContent,
|
|
314
|
+
expanded,
|
|
315
|
+
leading: propsLeading,
|
|
316
|
+
children,
|
|
317
|
+
onExpandedChange,
|
|
318
|
+
initialExpanded = false,
|
|
319
|
+
...others
|
|
320
|
+
}: ExpandableTableRowProps): JSX.Element {
|
|
321
|
+
const [openState, setOpenState] = useState(initialExpanded)
|
|
322
|
+
|
|
323
|
+
const open = expanded ?? openState
|
|
324
|
+
const colSpan = Array.isArray(children) ? children.length + 1 : 1
|
|
325
|
+
const isEven = index % 2 === 0
|
|
326
|
+
|
|
327
|
+
let leading: ReactNode = <RotatableIcon name="caret-right" $rotate={open} />
|
|
328
|
+
if (propsLeading) {
|
|
329
|
+
leading = isFunction(propsLeading) ? propsLeading(open) : propsLeading
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function toggle() {
|
|
333
|
+
if (!expandableContent) return
|
|
334
|
+
|
|
335
|
+
onExpandedChange?.(!open)
|
|
336
|
+
setOpenState(!open)
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return (
|
|
340
|
+
<>
|
|
341
|
+
<StyledExpandableTableRow {...others} onClick={toggle} $even={isEven}>
|
|
342
|
+
<ExpandableCell>{expandableContent && leading}</ExpandableCell>
|
|
343
|
+
{children}
|
|
344
|
+
</StyledExpandableTableRow>
|
|
345
|
+
{open && expandableContent && (
|
|
346
|
+
<StyledExpandableTableRow $even={isEven}>
|
|
347
|
+
<StyledTableCell colSpan={colSpan}>{expandableContent}</StyledTableCell>
|
|
348
|
+
</StyledExpandableTableRow>
|
|
349
|
+
)}
|
|
350
|
+
</>
|
|
351
|
+
)
|
|
352
|
+
}
|
|
353
|
+
|
|
282
354
|
function TableHeadCell({
|
|
283
355
|
alignment = 'left',
|
|
284
356
|
children,
|
|
@@ -436,6 +508,11 @@ Table.Selection = {
|
|
|
436
508
|
Cell: SelectionCell,
|
|
437
509
|
HeadCell: SelectionHeadCell,
|
|
438
510
|
}
|
|
511
|
+
Table.Expandable = {
|
|
512
|
+
HeadCell: ExpandableHeadCell,
|
|
513
|
+
Cell: ExpandableCell,
|
|
514
|
+
Row: ExpandableTableRow,
|
|
515
|
+
}
|
|
439
516
|
Table.SortHandle = TableSortHandle
|
|
440
517
|
TablePicker.Item = TablePickerItem
|
|
441
518
|
Table.Picker = TablePicker
|
|
@@ -35,6 +35,15 @@ export interface TableSectionProps extends HTMLAttributes<HTMLTableSectionElemen
|
|
|
35
35
|
|
|
36
36
|
export type TableRowProps = HTMLAttributes<HTMLTableRowElement>
|
|
37
37
|
|
|
38
|
+
export type ExpandableTableRowProps = TableRowProps & {
|
|
39
|
+
index: number
|
|
40
|
+
expandableContent?: ReactNode
|
|
41
|
+
leading?: ReactNode | ((expanded: boolean) => ReactNode)
|
|
42
|
+
initialExpanded?: boolean
|
|
43
|
+
expanded?: boolean
|
|
44
|
+
onExpandedChange?: (expanded: boolean) => void
|
|
45
|
+
}
|
|
46
|
+
|
|
38
47
|
export interface TableCellProps extends HTMLAttributes<HTMLTableCellElement> {
|
|
39
48
|
alignment?: 'left' | 'right'
|
|
40
49
|
format?: 'default' | 'number' | 'currency'
|
|
@@ -15,6 +15,11 @@ export const Playground: Story<TablePaginationProps> = (args: TablePaginationPro
|
|
|
15
15
|
const [page, setPage] = useState(args.page)
|
|
16
16
|
const [rowsPerPage, setRowsPerPage] = useState(args.rowsPerPage)
|
|
17
17
|
|
|
18
|
+
const handleRowsPerPageChange = (rowsPerPage: number) => {
|
|
19
|
+
setRowsPerPage(rowsPerPage)
|
|
20
|
+
setPage(0)
|
|
21
|
+
}
|
|
22
|
+
|
|
18
23
|
return (
|
|
19
24
|
<Layout.Group>
|
|
20
25
|
<TablePagination
|
|
@@ -22,7 +27,7 @@ export const Playground: Story<TablePaginationProps> = (args: TablePaginationPro
|
|
|
22
27
|
onPageChange={(page) => setPage(page)}
|
|
23
28
|
page={page}
|
|
24
29
|
rowsPerPage={rowsPerPage}
|
|
25
|
-
onRowsPerPageChange={
|
|
30
|
+
onRowsPerPageChange={handleRowsPerPageChange}
|
|
26
31
|
/>
|
|
27
32
|
</Layout.Group>
|
|
28
33
|
)
|
|
@@ -70,7 +70,6 @@ describe('TablePagination', () => {
|
|
|
70
70
|
userEvent.click(screen.getByText(/50 per page/i))
|
|
71
71
|
|
|
72
72
|
expect(onRowsPerPageChange).toHaveBeenLastCalledWith(50)
|
|
73
|
-
expect(onPageChange).toHaveBeenLastCalledWith(0)
|
|
74
73
|
})
|
|
75
74
|
|
|
76
75
|
it('hides some actions for the compact variant', () => {
|
|
@@ -20,17 +20,12 @@ function TablePagination(props: TablePaginationProps): JSX.Element {
|
|
|
20
20
|
...rest
|
|
21
21
|
} = props
|
|
22
22
|
|
|
23
|
-
const handleRowsPerPageChange = (selectedOption: number) => {
|
|
24
|
-
onRowsPerPageChange(selectedOption)
|
|
25
|
-
onPageChange(0)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
23
|
return (
|
|
29
24
|
<Layout.Group space="xl" align="center" justify="space-between" {...rest}>
|
|
30
25
|
<RowsPerPage
|
|
31
26
|
page={page}
|
|
32
27
|
count={count}
|
|
33
|
-
onRowsPerPageChange={
|
|
28
|
+
onRowsPerPageChange={onRowsPerPageChange}
|
|
34
29
|
rowsPerPage={rowsPerPage}
|
|
35
30
|
rowsPerPageOptions={rowsPerPageOptions}
|
|
36
31
|
labelRowsPerPage={labelRowsPerPage}
|
|
@@ -21,6 +21,9 @@ export interface TablePaginationProps extends GroupProps {
|
|
|
21
21
|
onPageChange: (page: number) => void
|
|
22
22
|
/**
|
|
23
23
|
* Callback fired when the number of rows per page is changed.
|
|
24
|
+
*
|
|
25
|
+
* Note: Resetting the page to zero after the number of rows per page is changed isn't part of
|
|
26
|
+
* the component and has to be implemented where it's necessary
|
|
24
27
|
*/
|
|
25
28
|
onRowsPerPageChange: (rowsPerPage: number) => void
|
|
26
29
|
/**
|