Table
'use client';
import React from 'react';
import { Plate } from '@udecode/plate/react';
import { editorPlugins } from '@/components/editor/plugins/editor-plugins';
import { useCreateEditor } from '@/components/editor/use-create-editor';
import { Editor, EditorContainer } from '@/components/plate-ui/editor';
import { DEMO_VALUES } from './values/demo-values';
export default function Demo({ id }: { id: string }) {
const editor = useCreateEditor({
plugins: [...editorPlugins],
value: DEMO_VALUES[id],
});
return (
<Plate editor={editor}>
<EditorContainer variant="demo">
<Editor />
</EditorContainer>
</Plate>
);
}
Installation
npm install @udecode/plate-table
Usage
import { TablePlugin } from '@udecode/plate-table/react';
const plugins = [
// ...otherPlugins,
TablePlugin(),
];
Plugins
TablePlugin
Options
- Default:
48
Disables the merging behavior of cells.
Disables the expansion of the table when inserting cells.
Disables the left resizer of the first column in the table.
Disables unsetting the width of the first column when the table has only one column. Set this to true
if you want to resize the table width when there's only one column. Leave it false
if you have a full-width table.
If defined, a normalizer will set each undefined table colSizes
to this value divided by the number of columns. Note that merged cells are not yet supported.
The minimum width of a column in the table.
TableRowPlugin
Plugin for table rows.
TableCellPlugin
Plugin for table cells.
TableCellHeaderPlugin
Plugin for table header cells.
API
editor.api.create.table
editor.api.create.tableCell
Creates an empty cell node for a table.
editor.api.create.tableRow
Creates an empty row node with the specified number of columns.
editor.api.table.getCellBorders
Gets the border styles for a table cell, handling special cases for first row and first column cells.
editor.api.table.getCellChildren
Gets the children of a table cell.
editor.api.table.getCellSize
Gets the width and minimum height of a table cell, taking into account column spans and column sizes.
editor.api.table.getColSpan
Gets the column span of a table cell.
editor.api.table.getRowSpan
Gets the row span of a table cell.
getCellType
Get the plugin cell types.
getNextTableCell
Gets the next cell in the table.
getPreviousTableCell
Gets the previous cell in the table.
getTableColumnCount
Gets the number of columns in a table.
getTableColumnIndex
Gets the column index of a cell node within a table.
getTableEntries
Gets the table, row, and cell node entries based on the current selection or a specified location.
getTableGridAbove
Gets the sub table above the anchor and focus positions based on the specified format.
getTableGridByRange
Gets the sub table between two cell paths within a given range.
getTableRowIndex
Gets the row index of a cell node within a table.
getTopTableCell
isTableBorderHidden
Gets the cell above the current cell in the table.
moveSelectionFromCell
Moves the selection by cell unit within a table.
onKeyDownTable
Handles keyboard events for table cells.
setBorderSize
Sets the border size of a table cell.
setTableColSize
setTableMarginLeft
Sets the margin left of a table.
setTableRowSize
Sets the size of a table row.
TablePlugin
Plugin for table functionality.
- Default:
48
Disables the merging behavior of cells.
Disables the expansion of the table when inserting cells.
Disables the left resizer of the first column in the table.
Disables unsetting the width of the first column when the table has only one column. Set this to true
if you want to resize the table width when there's only one column. Leave it false
if you have a full-width table.
If defined, a normalizer will set each undefined table colSizes
to this value divided by the number of columns. Note that merged cells are not yet supported.
The minimum width of a column in the table.
useIsCellSelected
Checks if a table cell is currently selected.
useSelectedCells
Gets the currently selected table cells.
useTableBordersDropdownMenuContentState
Gets the state of the table borders dropdown menu.
Indicates whether the selected table cells have a bottom border.
Indicates whether the selected table cells have a top border.
Indicates whether the selected table cells have a left border.
Indicates whether the selected table cells have a right border.
Indicates whether the selected table cells have no borders.
Indicates whether the selected table cells have outer borders (i.e., borders on all sides).
A factory function that returns the onSelectTableBorder
handler for a specific border type.
useTableCellElement
Gets the state of a table cell element.
The border styles of the table cell.
The ending column index (considering colSpan).
The number of columns this cell spans.
Whether cells are currently being selected.
The minimum height of the cell.
The ending row index (considering rowSpan).
Whether this cell is currently selected.
The width of the cell.
useTableCellElementResizable
Gets the resizable properties of a table cell element.
Props for the bottom resize handle, including resize direction and handler.
Whether the left resize handle should be hidden. True if not the first column or margin left is disabled.
Props for the left resize handle, including resize direction and handler.
Props for the right resize handle, including resize direction, initial size, and handler.
useTableColSizes
Gets the column sizes of a table.
useTableElement
Gets the state of a table element.
useTableStore
Gets the table store.
withDeleteTable
Overrides the editor's delete behavior to handle table-specific deletion logic.
withGetFragmentTable
Overrides the editor's getFragment
behavior to handle table-specific fragment logic. When getting a fragment that includes table cells:
withInsertFragmentTable
withInsertTextTable
Handles inserting text within a table.
withNormalizeTable
Normalizes the table structure and content.
withSelectionTable
Handles adjusting the selection within a table.
withSetFragmentDataTable
Handles setting the fragment data when copying or cutting table data.
withTable
Enhance the editor instance with table-related functionality.
withNormalizeTable
: Normalize table structure and contentwithDeleteTable
: Prevent cell deletion within a tablewithGetFragmentTable
: Handle getting the fragment data when copying or cutting table cellswithInsertFragmentTable
: Handle inserting table fragmentswithInsertTextTable
: Handle inserting text within a tablewithSelectionTable
: Handle adjusting the selection within a tablewithSetFragmentDataTable
: Handle setting the fragment data when copying or cutting table data
The enhanced editor instance with table functionality: