Skip to content

Commit

Permalink
new useMantineReactTableHook beta 12
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Jun 20, 2023
1 parent ccec503 commit 0c5d162
Show file tree
Hide file tree
Showing 35 changed files with 1,318 additions and 1,197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect, useMemo, useState } from 'react';
import Link from 'next/link';
import {
MantineReactTable,
type MantineReactTableProps,
type MRT_TableOptions,
type MRT_ColumnDef,
} from 'mantine-react-table';
import { Anchor, Text } from '@mantine/core';
Expand All @@ -12,7 +12,7 @@ import { type PropRow, rootProps } from './rootProps';
import { getPrimaryColor } from 'mantine-react-table/src/column.utils';

interface Props {
onlyProps?: Set<keyof MantineReactTableProps>;
onlyProps?: Set<keyof MRT_TableOptions>;
}

const RootPropTable = ({ onlyProps }: Props) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { type MantineReactTableProps } from 'mantine-react-table';
import { type MRT_TableOptions } from 'mantine-react-table';

export type PropRow = {
defaultValue?: string;
description?: string;
link?: string;
linkText?: string;
propName: keyof MantineReactTableProps;
propName: keyof MRT_TableOptions;
required?: boolean;
source?: 'MRT' | 'TanStack Table' | 'TanStack Virtual' | 'Mantine' | '';
type?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useCallback, useMemo, useState } from 'react';
import {
MantineReactTable,
MantineReactTableProps,
MRT_TableOptions,
MRT_Cell,
MRT_ColumnDef,
MRT_Row,
Expand Down Expand Up @@ -42,7 +42,7 @@ const Example = () => {
setTableData([...tableData]);
};

const handleSaveRowEdits: MantineReactTableProps<Person>['onEditingRowSave'] =
const handleSaveRowEdits: MRT_TableOptions<Person>['onEditingRowSave'] =
async ({ exitEditingMode, row, values }) => {
if (!Object.keys(validationErrors).length) {
tableData[row.index] = values;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useMemo, useState } from 'react';
import {
MantineReactTable,
MantineReactTableProps,
MRT_TableOptions,
MRT_ColumnDef,
} from 'mantine-react-table';
import { data, Person } from './makeData';
Expand Down Expand Up @@ -38,14 +38,17 @@ const Example = () => {

const [tableData, setTableData] = useState<Person[]>(() => data);

const handleSaveRow: MantineReactTableProps<Person>['onEditingRowSave'] =
async ({ exitEditingMode, row, values }) => {
//if using flat data and simple accessorKeys/ids, you can just do a simple assignment here.
tableData[row.index] = values;
//send/receive api updates here
setTableData([...tableData]);
exitEditingMode(); //required to exit editing mode
};
const handleSaveRow: MRT_TableOptions<Person>['onEditingRowSave'] = async ({
exitEditingMode,
row,
values,
}) => {
//if using flat data and simple accessorKeys/ids, you can just do a simple assignment here.
tableData[row.index] = values;
//send/receive api updates here
setTableData([...tableData]);
exitEditingMode(); //required to exit editing mode
};

return (
<MantineReactTable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useMemo, useState } from 'react';
import {
MantineReactTable,
MantineReactTableProps,
MRT_TableOptions,
MRT_ColumnDef,
} from 'mantine-react-table';
import { data, Person } from './makeData';
Expand Down Expand Up @@ -38,14 +38,17 @@ const Example = () => {

const [tableData, setTableData] = useState<Person[]>(() => data);

const handleSaveRow: MantineReactTableProps<Person>['onEditingRowSave'] =
async ({ exitEditingMode, row, values }) => {
//if using flat data and simple accessorKeys/ids, you can just do a simple assignment here.
tableData[row.index] = values;
//send/receive api updates here
setTableData([...tableData]);
exitEditingMode(); //required to exit editing mode
};
const handleSaveRow: MRT_TableOptions<Person>['onEditingRowSave'] = async ({
exitEditingMode,
row,
values,
}) => {
//if using flat data and simple accessorKeys/ids, you can just do a simple assignment here.
tableData[row.index] = values;
//send/receive api updates here
setTableData([...tableData]);
exitEditingMode(); //required to exit editing mode
};

return (
<MantineReactTable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useMemo, useState } from 'react';
import {
MantineReactTable,
MantineReactTableProps,
MRT_TableOptions,
MRT_ColumnDef,
MRT_Row,
} from 'mantine-react-table';
Expand Down Expand Up @@ -35,7 +35,7 @@ const Example = () => {
const [draggingRow, setDraggingRow] = useState<MRT_Row<Person> | null>(null);
const [hoveredTable, setHoveredTable] = useState<string | null>(null);

const commonTableProps: Partial<MantineReactTableProps<Person>> & {
const commonTableProps: Partial<MRT_TableOptions<Person>> & {
columns: MRT_ColumnDef<Person>[];
} = {
columns,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "mantine-react-table-example-localization-i18n-sv",
"name": "mantine-react-table-example-localization-i18n-no",
"version": "0.0.0",
"private": true,
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions apps/mantine-react-table-docs/pages/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import Head from 'next/head';

### V1

#### V1.0.0-beta.12

- New `useMantineReactTable` hook to replace `tableInstanceRef` prop (more to come on this)
- **ALL** internal MRT components are now exported from `mantine-react-table`!
- Renamed type `MantineReactTableProps` to `MRT_TableOptions`

#### V1.0.0-beta.10

- Implemented Edit Select variant
Expand Down
2 changes: 1 addition & 1 deletion packages/mantine-react-table/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.0-beta.11",
"version": "1.0.0-beta.12",
"license": "MIT",
"name": "mantine-react-table",
"description": "A fully featured Mantine implementation of TanStack React Table V8, written from the ground up in TypeScript.",
Expand Down
195 changes: 40 additions & 155 deletions packages/mantine-react-table/src/MantineReactTable.tsx
Original file line number Diff line number Diff line change
@@ -1,164 +1,49 @@
import { useMemo } from 'react';
import { MRT_AggregationFns } from './aggregationFns';
import { MRT_DefaultColumn, MRT_DefaultDisplayColumn } from './column.utils';
import { MRT_FilterFns } from './filterFns';
import { MRT_Default_Icons } from './icons';
import { MRT_SortingFns } from './sortingFns';
import { MRT_TableRoot } from './table/MRT_TableRoot';
import { MRT_Localization_EN } from './_locales/en';
import { type MantineReactTableProps } from './types';
import { MRT_TablePaper } from './table/MRT_TablePaper';
import { useMantineReactTable } from './useMantineReactTable';
import { type MRT_TableOptions, type MRT_TableInstance } from './types';
import { useEffect, useRef } from 'react';

export { MRT_AggregationFns, MRT_FilterFns, MRT_SortingFns };
type TableInstanceProp<TData extends Record<string, any>> = {
table: MRT_TableInstance<TData>;
};

export const MantineReactTable = <TData extends Record<string, any> = {}>({
aggregationFns,
autoResetExpanded = false,
columnResizeMode = 'onChange',
defaultColumn,
defaultDisplayColumn,
editingMode = 'modal',
enableBottomToolbar = true,
enableColumnActions = true,
enableColumnFilters = true,
enableColumnOrdering = false,
enableColumnResizing = false,
enableDensityToggle = true,
enableExpandAll = true,
enableFilterMatchHighlighting = true,
enableFilters = true,
enableFullScreenToggle = true,
enableGlobalFilter = true,
enableGlobalFilterRankedResults = true,
enableGrouping = false,
enableHiding = true,
enableMultiRowSelection = true,
enableMultiSort = true,
enablePagination = true,
enablePinning = false,
enableRowSelection = false,
enableSelectAll = true,
enableSorting = true,
enableStickyHeader = false,
enableTableFooter = true,
enableTableHead = true,
enableToolbarInternalActions = true,
enableTopToolbar = true,
filterFns,
icons,
layoutMode = 'semantic',
localization,
manualFiltering,
manualGrouping,
manualPagination,
manualSorting,
positionActionsColumn = 'first',
positionExpandColumn = 'first',
positionGlobalFilter = 'right',
positionPagination = 'bottom',
positionToolbarAlertBanner = 'top',
positionToolbarDropZone = 'top',
rowNumberMode = 'original',
selectAllMode = 'page',
sortingFns,
...rest
}: MantineReactTableProps<TData>): JSX.Element => {
const _icons = useMemo(() => ({ ...MRT_Default_Icons, ...icons }), [icons]);
const _localization = useMemo(
() => ({
...MRT_Localization_EN,
...localization,
}),
[localization],
);
const _aggregationFns = useMemo(
() => ({ ...MRT_AggregationFns, ...aggregationFns }),
[],
);
const _filterFns = useMemo(() => ({ ...MRT_FilterFns, ...filterFns }), []);
const _sortingFns = useMemo(() => ({ ...MRT_SortingFns, ...sortingFns }), []);
const _defaultColumn = useMemo(
() => ({ ...MRT_DefaultColumn, ...defaultColumn }),
[defaultColumn],
);
const _defaultDisplayColumn = useMemo(
() => ({
...MRT_DefaultDisplayColumn,
...defaultDisplayColumn,
}),
[defaultDisplayColumn],
);
type Props<TData extends Record<string, any>> =
| TableInstanceProp<TData>
| MRT_TableOptions<TData>;

if (rest.enableRowVirtualization || rest.enableColumnVirtualization) {
layoutMode = 'grid';
}
const isTableInstanceProp = <TData extends Record<string, any>>(
props: Props<TData>,
): props is TableInstanceProp<TData> =>
(props as TableInstanceProp<TData>).table !== undefined;

if (rest.enableRowVirtualization) {
enableStickyHeader = true;
}
export const MantineReactTable = <TData extends Record<string, any>>(
props: Props<TData>,
) => {
let table: MRT_TableInstance<TData>;

if (enablePagination === false && manualPagination === undefined) {
manualPagination = true;
if (isTableInstanceProp(props)) {
table = props.table;
} else {
table = useMantineReactTable(props);
}

if (!rest.data?.length) {
manualFiltering = true;
manualGrouping = true;
manualPagination = true;
manualSorting = true;
}
const initialBodyHeight = useRef<string>();

useEffect(() => {
if (typeof window !== 'undefined') {
initialBodyHeight.current = document.body.style.height;
}
}, []);

useEffect(() => {
if (typeof window !== 'undefined') {
if (table.getState().isFullScreen) {
document.body.style.height = '100vh';
} else {
document.body.style.height = initialBodyHeight.current as string;
}
}
}, [table.getState().isFullScreen]);

return (
<MRT_TableRoot
aggregationFns={_aggregationFns}
autoResetExpanded={autoResetExpanded}
columnResizeMode={columnResizeMode}
defaultColumn={_defaultColumn}
defaultDisplayColumn={_defaultDisplayColumn}
editingMode={editingMode}
enableBottomToolbar={enableBottomToolbar}
enableColumnActions={enableColumnActions}
enableColumnFilters={enableColumnFilters}
enableColumnOrdering={enableColumnOrdering}
enableColumnResizing={enableColumnResizing}
enableDensityToggle={enableDensityToggle}
enableExpandAll={enableExpandAll}
enableFilterMatchHighlighting={enableFilterMatchHighlighting}
enableFilters={enableFilters}
enableFullScreenToggle={enableFullScreenToggle}
enableGlobalFilter={enableGlobalFilter}
enableGlobalFilterRankedResults={enableGlobalFilterRankedResults}
enableGrouping={enableGrouping}
enableHiding={enableHiding}
enableMultiRowSelection={enableMultiRowSelection}
enableMultiSort={enableMultiSort}
enablePagination={enablePagination}
enablePinning={enablePinning}
enableRowSelection={enableRowSelection}
enableSelectAll={enableSelectAll}
enableSorting={enableSorting}
enableStickyHeader={enableStickyHeader}
enableTableFooter={enableTableFooter}
enableTableHead={enableTableHead}
enableToolbarInternalActions={enableToolbarInternalActions}
enableTopToolbar={enableTopToolbar}
filterFns={_filterFns}
icons={_icons}
layoutMode={layoutMode}
localization={_localization}
manualFiltering={manualFiltering}
manualGrouping={manualGrouping}
manualPagination={manualPagination}
manualSorting={manualSorting}
positionActionsColumn={positionActionsColumn}
positionExpandColumn={positionExpandColumn}
positionGlobalFilter={positionGlobalFilter}
positionPagination={positionPagination}
positionToolbarAlertBanner={positionToolbarAlertBanner}
positionToolbarDropZone={positionToolbarDropZone}
rowNumberMode={rowNumberMode}
selectAllMode={selectAllMode}
sortingFns={_sortingFns}
{...rest}
/>
);
return <MRT_TablePaper table={table as any} />;
};
6 changes: 6 additions & 0 deletions packages/mantine-react-table/src/body/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './MRT_TableBody';
export * from './MRT_TableBodyCell';
export * from './MRT_TableBodyCellValue';
export * from './MRT_TableBodyRow';
export * from './MRT_TableBodyRowGrabHandle';
export * from './MRT_TableDetailPanel';
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ export const MRT_ExpandAllButton = ({ table }: Props) => {
sx={(theme) => ({
marginLeft:
density === 'xl' ? '-6px' : density === 'md' ? '0' : '6px',
opacity: 0.8,
'&:disabled': {
backgroundColor: 'transparent',
border: 'none',
},
'&:hover': {
opacity: 1,
},
...(actionIconProps?.sx instanceof Function
? actionIconProps?.sx(theme)
: (actionIconProps?.sx as any)),
Expand Down
Loading

2 comments on commit 0c5d162

@vercel
Copy link

@vercel vercel bot commented on 0c5d162 Jun 20, 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 0c5d162 Jun 20, 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.