MRT logoMantine React Table

On This Page

    Editing Feature Guide

    If your tables need full CRUD functionality, you can enable editing features in Mantine React Table.

    There are 4 visually distinct editing modes to choose from, whether you want to let users edit data in a modal, inline 1 row at a time, 1 cell at a time, or just always have editing enabled for every cell.

    Relevant Table Options

    #
    Prop Name
    Type
    Default Value
    More Info Links
    1'modal' | 'row' | 'custom''modal'MRT Editing Docs
    2'modal' | 'row' | 'cell' | 'table' | 'custom''modal'MRT Editing Docs
    3boolean | (row: MRT_Row<TData>) => booleanMRT Editing Docs
    4ModalProps | ({ row, table }) => ModalPropsMantine Modal Docs
    5ModalProps | ({ row, table }) => ModalPropsMantine Modal Docs
    6SelectProps | ({ cell, column, row, table }) => SelectPropsMantine Select Docs
    7TextInputProps | ({ cell, column, row, table }) => TextInputPropsMantine TextInput Docs
    8({ row, table }) => voidMRT Editing Docs
    9OnChangeFn<MRT_Row<TData> | null>
    10({ exitEditingMode, row, table, values}) => Promise<void> | voidMRT Editing Docs
    11OnChangeFn<MRT_Cell<TData> | null>
    12({ row, table }) => voidMRT Editing Docs
    13OnChangeFn<MRT_Row<TData> | null>
    14({ exitEditingMode, row, table, values}) => Promise<void> | voidMRT Editing Docs
    15({ row, table, internalEditComponents }) => ReactNode
    16({ row, table, internalEditComponents }) => ReactNode

    Relevant Column Options

    #
    Column Option
    Type
    Default Value
    More Info Links
    1'select' | 'text''text'MRT Editing Docs
    2boolean | (row: MRT_Row<TData>) => boolean
    3SelectProps | ({ cell, column, row, table }) => SelectPropsMantine Select Docs
    4TextInputProps | ({ cell, column, row, table }) => TextInputPropsMantine TextInput API

    Relevant State Options

    #
    State Option
    Type
    Default Value
    More Info Links
    1MRT_Row
    2MRT_Cell
    3MRT_Row
    4booleanfalse

    Enable Editing

    To enable editing, first you just need to set the enableEditing table option to true.

    const table = useMantineReactTable({
      columns,
      data,
      enableEditing: true,
    });

    However, this is just the start. You will need to hook up logic and event listeners, but it depends on which editing mode you want to use.

    Editing Modes

    Mantine React Table has 4 supported editing modes: "modal" (default), "row", "cell" and "table". You can specify which editing mode you want to use by passing the editDisplayMode prop.

    The "modal" editing mode opens up a dialog where the user can edit data for 1 row at a time. No data is saved to the table until the user clicks the save button. Clicking the cancel button clears out any changes that were made on that row.

    An onEditingRowSave callback function prop must be provided where you will get access to the updated row data so that changes can be processed and saved. It is up to you how you handle the data. This function has a exitEditingMode parameter that must be called in order to exit editing mode upon save. The reason for this is so that you can perform validation checks before letting the modal close.

    The onEditingRowSave callback function prop includes an exitEditingMode parameter that must be called in order to exit editing mode upon save. The reason for this is so that you can perform validation checks before letting the modal close.

    Actions
    First Name
    Last Name
    Address
    City
    State
    DylanMurray261 Erdman FordEast DaphneKentucky
    RaquelKohler769 Dominic GroveColumbusOhio
    ErvinReinger566 Brakus InletSouth LindaWest Virginia
    BrittanyMcCullough722 Emie StreamLincolnNebraska
    BransonFrami32188 Larkin TurnpikeCharlestonSouth Carolina

    Rows per page

    1-5 of 5

    import '@mantine/core/styles.css';
    import '@mantine/dates/styles.css'; //if using mantine date picker features
    import 'mantine-react-table/styles.css'; //make sure MRT styles were imported in your app root (once)
    import { useMemo, useState } from 'react';
    import {
      MantineReactTable,
      MRT_TableOptions,
      MRT_ColumnDef,
    } from 'mantine-react-table';
    import { data, type Person } from './makeData';
    
    const Example = () => {
      const columns = useMemo<MRT_ColumnDef<Person>[]>(
        () => [
          {
            accessorKey: 'firstName',
            header: 'First Name',
          },
          {
            accessorKey: 'lastName',
            header: 'Last Name',
          },
    
          {
            accessorKey: 'address',
            header: 'Address',
          },
          {
            accessorKey: 'city',
            header: 'City',
          },
    
          {
            accessorKey: 'state',
            header: 'State',
          },
        ],
        [],
      );
    
      const [tableData, setTableData] = useState<Person[]>(() => data);
    
      const handleSaveRow: MRT_TableOptions<Person>['onEditingRowSave'] = async ({
        table,
        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]);
        table.setEditingRow(null); //exit editing mode
      };
    
      return (
        <MantineReactTable
          columns={columns}
          data={tableData}
          editDisplayMode="modal" //default
          enableEditing
          onEditingRowSave={handleSaveRow}
        />
      );
    };
    
    export default Example;

    Row Editing Mode

    The row editing mode is an inline row editing mode. When edit mode is activated, the row shows the edit components in the data cells. No data is saved to the table until the user clicks the save button. Clicking the cancel button clears out any changes that were made on that row.

    You must provide an onEditingRowSave callback function prop where you will get access to the updated row data so that changes can be processed and saved. It is up to you how you handle the data. This function has a exitEditingMode parameter that must be called in order to exit editing mode upon save. The reason for this is so that you can perform validation checks before letting the modal close.

    The onEditingRowSave callback function prop includes an exitEditingMode parameter that must be called in order to exit editing mode upon save. The reason for this is so that you can perform validation checks before letting the modal close.

    Actions
    First Name
    Last Name
    Address
    City
    State
    DylanMurray261 Erdman FordEast DaphneKentucky
    RaquelKohler769 Dominic GroveColumbusOhio
    ErvinReinger566 Brakus InletSouth LindaWest Virginia
    BrittanyMcCullough722 Emie StreamLincolnNebraska
    BransonFrami32188 Larkin TurnpikeCharlestonSouth Carolina

    Rows per page

    1-5 of 5

    import '@mantine/core/styles.css';
    import '@mantine/dates/styles.css'; //if using mantine date picker features
    import 'mantine-react-table/styles.css'; //make sure MRT styles were imported in your app root (once)
    import { useMemo, useState } from 'react';
    import {
      MantineReactTable,
      MRT_TableOptions,
      MRT_ColumnDef,
    } from 'mantine-react-table';
    import { data, type Person } from './makeData';
    
    const Example = () => {
      const columns = useMemo<MRT_ColumnDef<Person>[]>(
        () => [
          {
            accessorKey: 'firstName',
            header: 'First Name',
          },
          {
            accessorKey: 'lastName',
            header: 'Last Name',
          },
    
          {
            accessorKey: 'address',
            header: 'Address',
          },
          {
            accessorKey: 'city',
            header: 'City',
          },
    
          {
            accessorKey: 'state',
            header: 'State',
          },
        ],
        [],
      );
    
      const [tableData, setTableData] = useState<Person[]>(() => data);
    
      const handleSaveRow: MRT_TableOptions<Person>['onEditingRowSave'] = async ({
        table,
        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]);
        table.setEditingRow(null); //exit editing mode
      };
    
      return (
        <MantineReactTable
          columns={columns}
          data={tableData}
          editDisplayMode="row"
          enableEditing
          onEditingRowSave={handleSaveRow}
        />
      );
    };
    
    export default Example;

    Cell Editing Mode

    The cell editing mode is a bit simpler visually. Uses double-click cells to activate editing mode, but only for that cell.

    Then there is a bit of work for you to do to wire up either the onBlur, onChange, etc. events yourself in order to save the table data. This can be done in the mantineEditTextInputProps table or column option.

    First Name
    Last Name
    Address
    City
    State
    DylanMurray261 Erdman FordEast DaphneKentucky
    RaquelKohler769 Dominic GroveColumbusOhio
    ErvinReinger566 Brakus InletSouth LindaWest Virginia
    BrittanyMcCullough722 Emie StreamLincolnNebraska
    BransonFrami32188 Larkin TurnpikeCharlestonSouth Carolina

    Double-Click a Cell to Edit

    Rows per page

    1-5 of 5

    import '@mantine/core/styles.css';
    import '@mantine/dates/styles.css'; //if using mantine date picker features
    import 'mantine-react-table/styles.css'; //make sure MRT styles were imported in your app root (once)
    import { useMemo, useState } from 'react';
    import {
      MantineReactTable,
      MRT_Cell,
      MRT_ColumnDef,
    } from 'mantine-react-table';
    import { Text } from '@mantine/core';
    import { data, type Person } from './makeData';
    
    const Example = () => {
      const columns = useMemo<MRT_ColumnDef<Person>[]>(
        () => [
          {
            accessorKey: 'firstName',
            header: 'First Name',
          },
          {
            accessorKey: 'lastName',
            header: 'Last Name',
          },
    
          {
            accessorKey: 'address',
            header: 'Address',
          },
          {
            accessorKey: 'city',
            header: 'City',
          },
          {
            accessorKey: 'state',
            header: 'State',
          },
        ],
        [],
      );
    
      const [tableData, setTableData] = useState<Person[]>(() => data);
    
      const handleSaveCell = (cell: MRT_Cell<Person>, value: any) => {
        //if using flat data and simple accessorKeys/ids, you can just do a simple assignment here
        tableData[cell.row.index][cell.column.id as keyof Person] = value;
        //send/receive api updates here
        setTableData([...tableData]); //re-render with new data
      };
    
      return (
        <MantineReactTable
          columns={columns}
          data={tableData}
          editDisplayMode="cell"
          enableEditing
          mantineEditTextInputProps={({ cell }) => ({
            //onBlur is more efficient, but could use onChange instead
            onBlur: (event) => {
              handleSaveCell(cell, event.target.value);
            },
          })}
          renderBottomToolbarCustomActions={() => (
            <Text style={{ fontStyle: 'italic', padding: '0 16px' }}>
              Double-Click a Cell to Edit
            </Text>
          )}
        />
      );
    };
    
    export default Example;

    Table Editing Mode

    The table editing mode is similar to the cell editing mode, but it simply has all of the data cells in the table become editable all at once.

    To save data, you must hook up either the onBlur, onChange, etc. events yourself. This can be done in the mantineEditTextInputProps table or column option.

    First Name
    Last Name
    Address
    City
    State

    Rows per page

    1-5 of 5

    import '@mantine/core/styles.css';
    import '@mantine/dates/styles.css'; //if using mantine date picker features
    import 'mantine-react-table/styles.css'; //make sure MRT styles were imported in your app root (once)
    import { useMemo, useState } from 'react';
    import {
      MantineReactTable,
      MRT_Cell,
      MRT_ColumnDef,
    } from 'mantine-react-table';
    import { data, type Person } from './makeData';
    
    const Example = () => {
      const columns = useMemo<MRT_ColumnDef<Person>[]>(
        () => [
          {
            accessorKey: 'firstName',
            header: 'First Name',
          },
          {
            accessorKey: 'lastName',
            header: 'Last Name',
          },
    
          {
            accessorKey: 'address',
            header: 'Address',
          },
          {
            accessorKey: 'city',
            header: 'City',
          },
          {
            accessorKey: 'state',
            header: 'State',
          },
        ],
        [],
      );
    
      const [tableData, setTableData] = useState<Person[]>(() => data);
    
      const handleSaveCell = (cell: MRT_Cell<Person>, value: any) => {
        //if using flat data and simple accessorKeys/ids, you can just do a simple assignment here
        tableData[cell.row.index][cell.column.id as keyof Person] = value;
        //send/receive api updates here
        setTableData([...tableData]); //re-render with new data
      };
    
      return (
        <MantineReactTable
          columns={columns}
          data={tableData}
          editDisplayMode="table"
          enableEditing
          mantineEditTextInputProps={({ cell }) => ({
            //onBlur is more efficient, but could use onChange instead
            onBlur: (event) => {
              handleSaveCell(cell, event.target.value);
            },
            variant: 'unstyled', //default for editDisplayMode="table"
          })}
        />
      );
    };
    
    export default Example;

    Customizing Editing Components

    You can customize both the Editing Modal and the Editing Inputs.

    Customizing the Editing Modal

    You can pass any Mantine Modal Props with the mantineEditRowModalProps prop.

    const table = useMantineReactTable({
      columns,
      data,
      enableEditing: true,
      mantineEditRowModalProps: {
        closeOnClickOutside: true,
        withCloseButton: true,
      },
    });

    You can also override all the content of the editing modal by passing a renderEditRowModalContent prop.

    const table = useMantineReactTable({
      columns,
      data,
      enableEditing: true,
      renderEditRowModalContent: ({ internalEditComponents, row, table }) => (
        <Stack>
          <Title order={5}>My Custom Edit Modal</Title>
          {internalEditComponents} {/*or map over row.getAllCells() and render your own components */}
          <Flex justify="flex-end">
            <MRT_EditActionButtons row={row} table={table} variant="text" />{' '}
            {/*or render your own buttons */}
          </Flex>
        </Stack>
      ),
    });

    Customizing the Editing Inputs

    You can pass any Mantine TextInput Props with the mantineEditTextInputProps prop.

    const columns = [
      {
        accessor: 'age',
        header: 'Age',
        mantineEditTextInputProps: {
          required: true,
          variant: 'filled',
        },
      },
    ];

    Add Validation to Editing Components

    You can add validation to the editing components by using the mantineEditTextInputProps events. You can write your validation logic and hook it up to either the onBlur, onChange, etc. events, then set the error and helperText props accordingly.

    If you are implementing validation, you may also need to use the onEditingRowCancel prop to clear the validation error state.

    const [validationErrors, setValidationErrors] = useState({});
    
    const columns = [
      {
        accessor: 'age',
        header: 'Age',
        mantineEditTextInputProps: {
          error: validationErrors.age,
          required: true,
          type: 'number',
          onChange: (event) => {
            const value = event.target.value;
            //validation logic
            if (!value) {
              setValidationErrors((prev) => ({ ...prev, age: 'Age is required' }));
            } else if (value < 18) {
              setValidationErrors({
                ...validationErrors,
                age: 'Age must be 18 or older',
              });
            } else {
              delete validationErrors.age;
              setValidationErrors({ ...validationErrors });
            }
          },
        },
      },
    ];

    Use Custom Editing Components

    If you need to use a much more complicated Editing component than the built-in textfield, you can specify a custom editing component with the Edit column definition option.

    const columns = [
      {
        accessorKey: 'email',
        header: 'Email',
        Edit: ({ cell, column, table }) => <Autocomplete />, //see MRT_EditCellTextInput source code for more info
      },
    ];

    Customize Actions/Edit Column

    You can customize the actions column in a few different ways in the displayColumnDefOptions prop's 'mrt-row-actions' section.

    const table = useMantineReactTable({
      data,
      columns,
      displayColumnDefOptions: {
        'mrt-row-actions': {
          header: 'Edit', //change "Actions" to "Edit"
          //use a text button instead of a icon button
          Cell: ({ row, table }) => (
            <Button onClick={() => table.setEditingRow(row)}>Edit Customer</Button>
          ),
        },
      },
    });