Skip to content

Instantly share code, notes, and snippets.

@gnowland
Last active June 28, 2022 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnowland/c44af42b91da2bf31527797c1eeea79b to your computer and use it in GitHub Desktop.
Save gnowland/c44af42b91da2bf31527797c1eeea79b to your computer and use it in GitHub Desktop.
Override TypeScript type declarations from imported modules
// Credit:
// https://stackoverflow.com/questions/43952198/how-to-override-or-extend-a-libary-type-definition-in-typescript
// https://stackoverflow.com/questions/40322788/how-to-overwrite-incorrect-typescript-type-definition-installed-via-types-packa
import {
MUIDataTable,
TableToolbarSelect,
} from 'mui-datatables';
// ⭐️ This is where the magic happens:
declare module 'mui-datatables' { // re-declare import
export interface MUIDataTableToolbarSelect { // re-declare interface
selectedRows: MUIDataTableSelectedRows; // add/override missing prop
}
}
interface ToolbarBodyFooterOptions {
columns: any[];
count: number;
data: any[];
selectableRows: SelectableRows;
selectedRows: MUIDataTableSelectedRows;
}
export const DataTable: React.FC<DataTableProps> = ({ options, data, columns, ...props}) => {
const toolbarBodyFooter = (
toolbarOptions: ToolbarBodyFooterOptions
) => (
<TableToolbarSelect
options={{
customToolbarSelect: () => <Button>Whatever</Button>,
textLabels: {
selectedRows: {
text: 'row(s) selected',
delete: 'Delete',
deleteAria: 'Delete Selected Rows',
}
}
}}
selectedRows={toolbarOptions.selectedRows}
// onRowsDelete={this.selectRowDelete}
// displayData={displayData}
// selectRowUpdate={this.selectRowUpdate}
// components={this.props.components}
/>
);
const tableOptions = {
customTableBodyFooterRender: (
toolbarOptions: NestedMUIDataTableToolbarBodyFooterOptions
) => toolbarBodyFooter(toolbarOptions),
...options
}
return (
<ThemeProvider theme={(baseTheme: Theme) => themeOverrides(baseTheme, options)}>
<MUIDataTable
data={data}
columns={columns}
options={tableOptions}
components={{ Checkbox: CustomCheckbox }}
/>
</ThemeProvider>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment