Skip to content

Commit

Permalink
fix column order and create dynamic editing example
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Dec 5, 2023
1 parent 2e5a286 commit 5a29bb0
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ export const useMRT_DisplayColumns = <TData extends Record<string, any> = {}>(
const { creatingRow, columnOrder, grouping, tableOptions } = params;
return useMemo(() => {
const order = tableOptions.state?.columnOrder ?? columnOrder;
const ret = [
const displayColumns = [
makePinColumn(params, order),
makeDragColumn(params, order),
makeRowActionsColumn(params, order),
makeExpandColumn(params, order),
makeRowSelectColumn(params, order),
makeRowNumbersColumn(params, order),
].filter((x) => x) as MRT_ColumnDef<TData>[];
console.log('displayColumns', ret);
return ret;
return displayColumns;
}, [
creatingRow,
columnOrder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export const useMRT_TableInstance: <TData extends Record<string, any> = {}>(

const initialState: Partial<MRT_TableState<TData>> = useMemo(() => {
const initState = tableOptions.initialState ?? {};
// initState.columnOrder =
// initState.columnOrder ?? getDefaultColumnOrderIds(tableOptions);
initState.columnOrder =
initState.columnOrder ?? getDefaultColumnOrderIds(tableOptions);
initState.globalFilterFn = tableOptions.globalFilterFn ?? 'fuzzy';
return initState;
}, []);
Expand Down
188 changes: 145 additions & 43 deletions packages/mantine-react-table/stories/features/Editing.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
MRT_EditActionButtons,
type MRT_Cell,
type MRT_TableOptions,
type MRT_ColumnOrderState,
} from '../../src';
import { faker } from '@faker-js/faker';
import { Flex, Stack, Switch, Title } from '@mantine/core';
Expand Down Expand Up @@ -90,8 +91,6 @@ const data: Person[] = [...Array(100)].map(() => ({

export const EditingEnabledEditModeModalDefault = () => {
const [tableData, setTableData] = useState(data);
const [enableRowNumbers, setEnableRowNumbers] = useState(false);
const [enableEditing, setEnableEditing] = useState(false);
const handleSaveRow: MRT_TableOptions<Person>['onEditingRowSave'] = ({
exitEditingMode,
row,
Expand All @@ -102,48 +101,38 @@ export const EditingEnabledEditModeModalDefault = () => {
exitEditingMode();
};

const columns = [
{
header: 'First Name',
accessorKey: 'firstName',
},
{
header: 'Last Name',
accessorKey: 'lastName',
},
{
header: 'Address',
accessorKey: 'address',
},
{
header: 'State',
accessorKey: 'state',
},
{
header: 'Phone Number',
accessorKey: 'phoneNumber',
enableEditing: false,
},
];

return (
<Stack>
<Switch
checked={enableEditing}
onChange={(e) => setEnableEditing(e.currentTarget.checked)}
label="Enable Editing"
/>{' '}
<Switch
checked={enableRowNumbers}
onChange={(e) => setEnableRowNumbers(e.currentTarget.checked)}
label="Enable Row Numbers"
/>
<MantineReactTable
columns={[
{
header: 'First Name',
accessorKey: 'firstName',
},
{
header: 'Last Name',
accessorKey: 'lastName',
},
{
header: 'Address',
accessorKey: 'address',
},
{
header: 'State',
accessorKey: 'state',
},
{
header: 'Phone Number',
accessorKey: 'phoneNumber',
enableEditing: false,
},
]}
data={tableData}
enableEditing={enableEditing}
enableRowNumbers={enableRowNumbers}
onEditingRowSave={handleSaveRow}
/>
</Stack>
<MantineReactTable
columns={columns}
data={tableData}
enableEditing
enableRowNumbers
onEditingRowSave={handleSaveRow}
/>
);
};

Expand Down Expand Up @@ -1056,3 +1045,116 @@ export const EnableEditingConditionallyTable = () => {
/>
);
};

export const EditingTurnedOnDynamically = () => {
const [tableData, setTableData] = useState(data);
const handleSaveRow: MRT_TableOptions<Person>['onEditingRowSave'] = ({
exitEditingMode,
row,
values,
}) => {
tableData[row.index] = values;
setTableData([...tableData]);
exitEditingMode();
};

const columns = [
{
header: 'First Name',
accessorKey: 'firstName',
},
{
header: 'Last Name',
accessorKey: 'lastName',
},
{
header: 'Address',
accessorKey: 'address',
},
{
header: 'State',
accessorKey: 'state',
},
{
header: 'Phone Number',
accessorKey: 'phoneNumber',
enableEditing: false,
},
];

const [enableRowNumbers, _setEnableRowNumbers] = useState(false);
const [enableEditing, _setEnableEditing] = useState(false);

const [columnOrder, setColumnOrder] = useState<MRT_ColumnOrderState>(() => {
return [
enableEditing && 'mrt-row-actions',
enableRowNumbers && 'mrt-row-numbers',
...columns.map((c) => c.accessorKey),
].filter(Boolean) as MRT_ColumnOrderState;
});

const updateColumnOrder = ({
enableEditing,
enableRowNumbers,
}: {
enableEditing: boolean;
enableRowNumbers: boolean;
}) => {
let newColumnOrder = columnOrder;
if (enableRowNumbers) {
newColumnOrder = ['mrt-row-numbers', ...newColumnOrder];
} else {
newColumnOrder = newColumnOrder.filter(
(col) => col !== 'mrt-row-numbers',
);
}
if (enableEditing) {
newColumnOrder = ['mrt-row-actions', ...newColumnOrder];
} else {
newColumnOrder = newColumnOrder.filter(
(col) => col !== 'mrt-row-actions',
);
}
setColumnOrder(newColumnOrder);
};

const setEnableEditing = (value: boolean) => {
_setEnableEditing(value);
updateColumnOrder({
enableEditing: value,
enableRowNumbers,
});
};

const setEnableRowNumbers = (value: boolean) => {
_setEnableRowNumbers(value);
updateColumnOrder({
enableEditing,
enableRowNumbers: value,
});
};

return (
<Stack>
<Switch
checked={enableEditing}
onChange={(e) => setEnableEditing(e.currentTarget.checked)}
label="Enable Editing"
/>{' '}
<Switch
checked={enableRowNumbers}
onChange={(e) => setEnableRowNumbers(e.currentTarget.checked)}
label="Enable Row Numbers"
/>
<MantineReactTable
columns={columns}
data={tableData}
enableEditing={enableEditing}
enableRowNumbers={enableRowNumbers}
onEditingRowSave={handleSaveRow}
state={{ columnOrder }}
onColumnOrderChange={setColumnOrder}
/>
</Stack>
);
};

0 comments on commit 5a29bb0

Please sign in to comment.