@integrigo/integrigo-ui 1.6.18-a → 1.6.18-c

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.
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
- import { render } from '@testing-library/react';
2
+ import { fireEvent, render } from '@testing-library/react';
3
+ import { Column, createColumnHelper, Row } from '@tanstack/react-table';
3
4
  import { Table } from './Table';
4
- import { Column, createColumnHelper } from '@tanstack/react-table';
5
5
 
6
6
  type Person = {
7
7
  firstName: string
@@ -43,6 +43,7 @@ const columnHelper = createColumnHelper<Person>();
43
43
 
44
44
  const columns = [
45
45
  columnHelper.accessor('firstName', {
46
+ header: 'First Name',
46
47
  cell: info => info.getValue(),
47
48
  footer: info => info.column.id,
48
49
  }),
@@ -73,10 +74,77 @@ const columns = [
73
74
 
74
75
  describe('<Table />', () => {
75
76
  it('should render', () => {
76
- render(<Table columns={columns} data={data}/>);
77
+ const { container, getByText } = render(<Table columns={columns} data={data}/>);
78
+ expect(container.getElementsByTagName('table').length).toEqual(1);
79
+ expect(container.getElementsByTagName('th').length).toEqual(6);
80
+ expect(getByText(/First Name/)).toBeInTheDocument();
81
+ expect(getByText(/Last Name/)).toBeInTheDocument();
82
+ expect(getByText(/Age/)).toBeInTheDocument();
83
+ expect(getByText(/Visits/)).toBeInTheDocument();
84
+ expect(getByText(/Profile Progress/)).toBeInTheDocument();
77
85
  });
78
86
  it('enabled - should match snapshot', () => {
79
87
  const { container } = render(<Table columns={columns} data={data}/>);
80
88
  expect(container).toMatchSnapshot();
81
89
  });
90
+ it('expandable rows - should match snapshot',() => {
91
+ const { container } = render(
92
+ <Table
93
+ columns={columns}
94
+ data={data}
95
+ renderExpandCollapseComponent={(row: Row<Person>) => (
96
+ <div
97
+ style={{ cursor: 'pointer' }}
98
+ onClick={() => {
99
+ console.log('click');
100
+ row.toggleExpanded();
101
+ }}
102
+ data-testid={`expand-button-${row.index}`}
103
+ >
104
+ Edit
105
+ </div>
106
+ )}
107
+ renderExpandableRowComponent={(row: Row<Person>) => (
108
+ <div data-testid={`expand-content-${row.index}`}>Hello {row.original.firstName} {row.original.lastName}</div>
109
+ )}
110
+ />,
111
+ );
112
+ expect(container).toMatchSnapshot();
113
+ });
114
+ it('should render expandable rows', () => {
115
+ const { container, getByTestId, queryByTestId } = render(
116
+ <Table
117
+ columns={columns}
118
+ data={data}
119
+ renderExpandCollapseComponent={(row: Row<Person>) => (
120
+ <div
121
+ style={{ cursor: 'pointer' }}
122
+ onClick={() => row.toggleExpanded()}
123
+ data-testid={`expand-button-${row.index}`}
124
+ >
125
+ Edit
126
+ </div>
127
+ )}
128
+ renderExpandableRowComponent={(row: Row<Person>) => (
129
+ <div data-testid={`expand-content-${row.index}`}>Hello {row.original.firstName} {row.original.lastName}</div>
130
+ )}
131
+ />,
132
+ );
133
+ expect(container.getElementsByTagName('table').length).toEqual(1);
134
+ expect(container.getElementsByTagName('th').length).toEqual(7);
135
+ expect(getByTestId(/expand-button-1/)).toBeInTheDocument();
136
+ expect(queryByTestId(/expand-content-1/)).not.toBeInTheDocument();
137
+
138
+ const expandCollapseButton = getByTestId(/expand-button-1/);
139
+
140
+ fireEvent.click(expandCollapseButton);
141
+
142
+ expect(getByTestId(/expand-content-1/)).toBeInTheDocument();
143
+
144
+ // I don't know why, but it does not work when used with the expandCollapseButton above
145
+ const sameExpandCollapseButton = getByTestId(/expand-button-1/);
146
+ fireEvent.click(sameExpandCollapseButton);
147
+
148
+ expect(queryByTestId(/expand-content-1/)).not.toBeInTheDocument();
149
+ });
82
150
  });
@@ -1,9 +1,10 @@
1
- import React from 'react';
1
+ import React, { ReactNode } from 'react';
2
2
  import {
3
3
  Column,
4
4
  flexRender,
5
5
  getCoreRowModel,
6
6
  useReactTable,
7
+ Row,
7
8
  } from '@tanstack/react-table';
8
9
  import styled from 'styled-components';
9
10
 
@@ -25,7 +26,9 @@ export interface Props <T extends Record<string, unknown>> extends React.TableHT
25
26
  data: T[];
26
27
  columns: Column<T>[];
27
28
  variant?: Variant;
28
- textAlign?: Alignment
29
+ textAlign?: Alignment;
30
+ renderExpandableRowComponent?: (row: Row<T>) => ReactNode;
31
+ renderExpandCollapseComponent?: (row: Row<T>) => ReactNode;
29
32
  }
30
33
 
31
34
  export const Table = <T extends Record<string, unknown>>({
@@ -33,12 +36,34 @@ export const Table = <T extends Record<string, unknown>>({
33
36
  columns,
34
37
  variant = 'primary',
35
38
  textAlign = 'center',
39
+ renderExpandableRowComponent,
40
+ renderExpandCollapseComponent,
36
41
  ...props
37
42
  }: Props<T>) => {
38
43
 
44
+ const tableColumns = [
45
+ ...columns,
46
+ ...(typeof renderExpandableRowComponent !== 'undefined' && typeof renderExpandCollapseComponent !== 'undefined'
47
+ ? [
48
+ {
49
+ accessorKey: 'expandable-column',
50
+ header: '',
51
+ cell: ({ row }: { row: Row<T> }) => {
52
+ return (
53
+ <>
54
+ {renderExpandCollapseComponent(row)}
55
+ </>
56
+ );
57
+ },
58
+ enableSorting: false,
59
+ },
60
+ ]
61
+ : []),
62
+ ];
63
+
39
64
  const table = useReactTable({
40
65
  data,
41
- columns,
66
+ columns: tableColumns,
42
67
  getCoreRowModel: getCoreRowModel(),
43
68
  });
44
69
 
@@ -63,13 +88,22 @@ export const Table = <T extends Record<string, unknown>>({
63
88
  </StyledHead>
64
89
  <StyledBody textAlign={textAlign}>
65
90
  {table.getRowModel().rows.map(row => (
66
- <tr key={row.id}>
91
+ <React.Fragment key={row.id}>
92
+ <tr>
67
93
  {row.getVisibleCells().map(cell => (
68
94
  <td key={cell.id}>
69
95
  {flexRender(cell.column.columnDef.cell, cell.getContext())}
70
96
  </td>
71
97
  ))}
72
98
  </tr>
99
+ {row.getIsExpanded() && typeof renderExpandableRowComponent !== 'undefined' && (
100
+ <tr>
101
+ <td colSpan={tableColumns.length + 1}>
102
+ {renderExpandableRowComponent(row)}
103
+ </td>
104
+ </tr>
105
+ )}
106
+ </React.Fragment>
73
107
  ))}
74
108
  </StyledBody>
75
109
  </StyledTable>
@@ -77,9 +111,7 @@ export const Table = <T extends Record<string, unknown>>({
77
111
  );
78
112
  };
79
113
 
80
- const StyledTable = styled.table`
81
-
82
- `;
114
+ const StyledTable = styled.table``;
83
115
 
84
116
  StyledTable.displayName = 'StyledTable';
85
117
 
@@ -5,14 +5,14 @@ exports[`<Table /> enabled - should match snapshot 1`] = `
5
5
  <div>
6
6
  <table
7
7
  cellspacing="0"
8
- class="sc-bcXHqe eCGCkv"
8
+ class="sc-bdvvtL gGYjnV"
9
9
  >
10
10
  <thead
11
- class="sc-gswNZR qcRyf"
11
+ class="sc-gsDKAQ goIZuE"
12
12
  >
13
13
  <tr>
14
14
  <th>
15
- firstName
15
+ First Name
16
16
  </th>
17
17
  <th>
18
18
  Last Name
@@ -32,7 +32,7 @@ exports[`<Table /> enabled - should match snapshot 1`] = `
32
32
  </tr>
33
33
  </thead>
34
34
  <tbody
35
- class="sc-dkrFOg jkyDVI"
35
+ class="sc-dkPtRN eONmCs"
36
36
  >
37
37
  <tr>
38
38
  <td>
@@ -99,3 +99,128 @@ exports[`<Table /> enabled - should match snapshot 1`] = `
99
99
  </div>
100
100
  </div>
101
101
  `;
102
+
103
+ exports[`<Table /> expandable rows - should match snapshot 1`] = `
104
+ <div>
105
+ <div>
106
+ <table
107
+ cellspacing="0"
108
+ class="sc-bdvvtL gGYjnV"
109
+ >
110
+ <thead
111
+ class="sc-gsDKAQ goIZuE"
112
+ >
113
+ <tr>
114
+ <th>
115
+ First Name
116
+ </th>
117
+ <th>
118
+ Last Name
119
+ </th>
120
+ <th>
121
+ Age
122
+ </th>
123
+ <th>
124
+ Visits
125
+ </th>
126
+ <th>
127
+ Status
128
+ </th>
129
+ <th>
130
+ Profile Progress
131
+ </th>
132
+ <th />
133
+ </tr>
134
+ </thead>
135
+ <tbody
136
+ class="sc-dkPtRN eONmCs"
137
+ >
138
+ <tr>
139
+ <td>
140
+ tanner
141
+ </td>
142
+ <td>
143
+ linsley
144
+ </td>
145
+ <td>
146
+ 24
147
+ </td>
148
+ <td>
149
+ 100
150
+ </td>
151
+ <td>
152
+ In Relationship
153
+ </td>
154
+ <td>
155
+ 50
156
+ </td>
157
+ <td>
158
+ <div
159
+ data-testid="expand-button-0"
160
+ style="cursor: pointer;"
161
+ >
162
+ Edit
163
+ </div>
164
+ </td>
165
+ </tr>
166
+ <tr>
167
+ <td>
168
+ tandy
169
+ </td>
170
+ <td>
171
+ miller
172
+ </td>
173
+ <td>
174
+ 40
175
+ </td>
176
+ <td>
177
+ 40
178
+ </td>
179
+ <td>
180
+ Single
181
+ </td>
182
+ <td>
183
+ 80
184
+ </td>
185
+ <td>
186
+ <div
187
+ data-testid="expand-button-1"
188
+ style="cursor: pointer;"
189
+ >
190
+ Edit
191
+ </div>
192
+ </td>
193
+ </tr>
194
+ <tr>
195
+ <td>
196
+ joe
197
+ </td>
198
+ <td>
199
+ dirte
200
+ </td>
201
+ <td>
202
+ 45
203
+ </td>
204
+ <td>
205
+ 20
206
+ </td>
207
+ <td>
208
+ Complicated
209
+ </td>
210
+ <td>
211
+ 10
212
+ </td>
213
+ <td>
214
+ <div
215
+ data-testid="expand-button-2"
216
+ style="cursor: pointer;"
217
+ >
218
+ Edit
219
+ </div>
220
+ </td>
221
+ </tr>
222
+ </tbody>
223
+ </table>
224
+ </div>
225
+ </div>
226
+ `;