@loadsmart/loadsmart-ui 5.4.0 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loadsmart/loadsmart-ui",
3
- "version": "5.4.0",
3
+ "version": "5.5.0",
4
4
  "description": "Miranda UI, a React UI library",
5
5
  "main": "dist",
6
6
  "files": [
@@ -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 StyledTableRow = styled.tr<{ selected?: boolean }>`
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'