Skip to content

Commit 84e656c

Browse files
authored
feat(add sorting by search string and jsdom-16 to use waitFor in testing-library): (#15)
Add sorting by search string globally and jsdom-16
1 parent 21e1eba commit 84e656c

7 files changed

Lines changed: 543 additions & 39 deletions

File tree

README.md

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ type ColumnType<T> = {
7777
hidden?: boolean;
7878
sort?: ((a: RowType<T>, b: RowType<T>) => number) | undefined;
7979
render?: ({ value, row }: { value: any; row: T }) => React.ReactNode;
80-
headerRender?: HeaderRenderType;
80+
headerRender?: ({ label }: { label: string }) => React.ReactNode;
8181
};
8282
```
8383

@@ -141,41 +141,56 @@ const MyTable = () => {
141141
};
142142
```
143143

144-
### Advanced Example
144+
### Filtering
145145

146146
```jsx
147-
import React, { useMemo } from 'react';
148-
import { useTable } from 'react-final-table';
147+
const TableWithFilter: FC = () => {
148+
const { headers, rows, setSearchString } = useTable(
149+
columns,
150+
data,
151+
);
152+
153+
return (
154+
<>
155+
<input
156+
type="text"
157+
onChange={e => {
158+
setSearchString(e.target.value);
159+
}}
160+
></input>
161+
<table>
162+
<thead>
163+
<tr>
164+
{headers.map((header, idx) => (
165+
<th key={idx}>
166+
{header.render()}
167+
</th>
168+
))}
169+
</tr>
170+
</thead>
171+
<tbody>
172+
{rows.map((row, idx) => (
173+
<tr key={idx}>
174+
{row.cells.map((cell, idx) => (
175+
<td key={idx}>{cell.render()}</td>
176+
))}
177+
</tr>
178+
))}
179+
</tbody>
180+
</table>
181+
</>
182+
);
183+
```
149184
150-
const columns = [
151-
{ name: 'id', hidden: true },
152-
{
153-
name: 'first_name',
154-
label: 'First Name',
155-
render: ({ value }: { value: string }) => <span>Sir {value}</span>,
156-
},
157-
{
158-
name: 'last_name',
159-
label: 'Last Name',
160-
},
161-
];
185+
### Row Selection
162186
163-
const data = [
164-
{
165-
id: 1,
166-
first_name: 'Frodo',
167-
last_name: 'Baggins',
168-
},
169-
{
170-
id: 2,
171-
first_name: 'Samwise',
172-
last_name: 'Gamgee',
173-
},
174-
];
187+
```jsx
188+
import React, { useMemo } from 'react';
189+
import { useTable } from 'react-final-table';
190+
import makeData from 'makeData'; // replace this with your own data
175191

176192
function App() {
177-
const memoColumns = useMemo(() => columns, []);
178-
const memoData = useMemo(() => data, []);
193+
const { columns, rows } = makeData();
179194

180195
const { headers, rows, selectRow, selectedRows } = useTable(
181196
memoColumns,
@@ -214,21 +229,18 @@ function App() {
214229
))}
215230
</tbody>
216231
</table>
217-
<pre>
218-
<code>{JSON.stringify(selectedRows, null, 2)}</code>
219-
</pre>
220232
</>
221233
);
222234
}
223235

224236
export default App;
225237
```
226238
227-
## Test
239+
## Performance
228240
229-
```bash
230-
npm run test
231-
```
241+
It's recommended that you memoize your columns and data using `useMemo`. This is
242+
to prevent the table from rerendering everytime your component rerenders, which
243+
can have negative consequences on performance.
232244
233245
## Contributing
234246

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
testEnvironment: 'jest-environment-jsdom-sixteen',
3+
};

0 commit comments

Comments
 (0)