Skip to content

Commit 0da7cc6

Browse files
committed
Tables can now have a context pass down to column renderers.
1 parent 813ed74 commit 0da7cc6

File tree

10 files changed

+1312
-759
lines changed

10 files changed

+1312
-759
lines changed

components/createOfflineTableComponent/index.tsx

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,20 @@ const normalize = (cell: KeyableTableCell | NonKeyableTableCell): string => {
3131
* table.
3232
* @template TRow The type of a row of data provided to the
3333
* table.
34+
* @template TContext The type of the context in which the table is
35+
* being rendered.
3436
* @param style The style to apply to the table.
3537
* @param schema The schema of the table to show.
3638
* @returns The created React component.
3739
*/
3840
export const createOfflineTableComponent = <
3941
TKeyableColumnKey extends string,
4042
TNonKeyableColumnKey extends string,
41-
TRow extends TableRow<TKeyableColumnKey, TNonKeyableColumnKey>
43+
TRow extends TableRow<TKeyableColumnKey, TNonKeyableColumnKey>,
44+
TContext
4245
>(
4346
style: TableStyle,
44-
schema: TableSchema<TKeyableColumnKey, TNonKeyableColumnKey, TRow>
47+
schema: TableSchema<TKeyableColumnKey, TNonKeyableColumnKey, TRow, TContext>
4548
): React.FunctionComponent<{
4649
/**
4750
* The data to show in the table.
@@ -81,6 +84,11 @@ export const createOfflineTableComponent = <
8184
* Shown when there are no rows to display.
8285
*/
8386
readonly whenEmpty: string;
87+
88+
/**
89+
* The context in which the table is being rendered.
90+
*/
91+
readonly context: TContext;
8492
}> => {
8593
const rowViewBase: ViewStyle = {
8694
width: `100%`,
@@ -302,7 +310,15 @@ export const createOfflineTableComponent = <
302310
const oddRowCellStyles = StyleSheet.create(oddRowCellInput);
303311
const evenRowCellStyles = StyleSheet.create(evenRowCellInput);
304312

305-
return ({ data, whenEmpty, filter, sortBy, sortDirection, onSortChange }) => {
313+
return ({
314+
data,
315+
whenEmpty,
316+
filter,
317+
sortBy,
318+
sortDirection,
319+
onSortChange,
320+
context,
321+
}) => {
306322
let rows = [...data.rows];
307323

308324
filter = normalize(filter);
@@ -323,7 +339,7 @@ export const createOfflineTableComponent = <
323339

324340
case `customText`:
325341
{
326-
const value = column.render(row[column.key] as never);
342+
const value = column.render(row[column.key] as never, context);
327343

328344
if (normalize(value).includes(filter)) {
329345
return true;
@@ -332,7 +348,7 @@ export const createOfflineTableComponent = <
332348
break;
333349

334350
case `customElement`:
335-
if (column.containsSearchTerm(row, filter)) {
351+
if (column.containsSearchTerm(row, filter, context)) {
336352
return true;
337353
}
338354

@@ -484,7 +500,10 @@ export const createOfflineTableComponent = <
484500
}
485501

486502
case `customText`: {
487-
const value = column.render(row[column.key] as never);
503+
const value = column.render(
504+
row[column.key] as never,
505+
context
506+
);
488507

489508
// TODO: why does TypeScript think this cannot be null, false or true?
490509
switch (value as unknown) {
@@ -540,7 +559,7 @@ export const createOfflineTableComponent = <
540559
key={String(columnIndex)}
541560
style={customCellStyles[columnIndex]}
542561
>
543-
{column.render(row)}
562+
{column.render(row, context)}
544563
</View>
545564
);
546565
}

0 commit comments

Comments
 (0)