Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow extending core table features #399

Merged
merged 1 commit into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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