Skip to content

Commit

Permalink
Allow extending core table features (#399)
Browse files Browse the repository at this point in the history
Co-authored-by: Hitendra Malviya <hitendra.malviya@dynacommerce.com>
  • Loading branch information
hitendramalviya and Hitendra Malviya committed Feb 26, 2023
1 parent 03fc49c commit b3cf4bd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions apps/material-react-table-docs/components/prop-tables/rootProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1895,4 +1895,14 @@ export const rootProps: PropRow[] = [
source: 'MRT',
type: 'MutableRefObject<Virtualizer | null>',
},
{
propName: 'tableFeatures',
defaultValue: '',
description: '',
link: '',
linkText: '',
required: false,
source: '',
type: 'Array<(table: MRT_TableInstance<any>) => any>',
},
];
9 changes: 9 additions & 0 deletions packages/material-react-table/src/MaterialReactTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,11 @@ export type MRT_DisplayColumnIds =
| 'mrt-row-numbers'
| 'mrt-row-select';

export type MRT_CreateTableFeature<
TData extends Record<string, any> = {},
TFeature = any,
> = (table: MRT_TableInstance<TData>) => TFeature;

/**
* `columns` and `data` props are the only required props, but there are over 150 other optional props.
*
Expand Down Expand Up @@ -949,6 +954,10 @@ export type MaterialReactTableProps<TData extends Record<string, any> = {}> =
* @deprecated Use `rowVirtualizerProps` instead
*/
virtualizerProps?: any;
/**
* Sequence of features important any dependent feature must be defined first
*/
tableFeatures?: Array<MRT_CreateTableFeature<TData>>;
};

const MaterialReactTable = <TData extends Record<string, any> = {}>({
Expand Down
6 changes: 6 additions & 0 deletions packages/material-react-table/src/table/MRT_TableRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ export const MRT_TableRoot: any = <TData extends Record<string, any> = {}>(
props.onShowToolbarDropZoneChange ?? setShowToolbarDropZone,
} as MRT_TableInstance<TData>;

if (props.tableFeatures) {
props.tableFeatures.forEach(feature => {
Object.assign(table, feature(table));
});
}

if (props.tableInstanceRef) {
props.tableInstanceRef.current = table;
}
Expand Down

2 comments on commit b3cf4bd

@vercel
Copy link

@vercel vercel bot commented on b3cf4bd Feb 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on b3cf4bd Feb 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.