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(),
];
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
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
editor.api.table.getRowSpan
getCellType
getNextTableCell
getPreviousTableCell
getTableColumnCount
getTableColumnIndex
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
getTopTableCell
isTableBorderHidden
moveSelectionFromCell
Moves the selection by cell unit within a table.
onKeyDownTable
setBorderSize
setTableColSize
setTableMarginLeft
setTableRowSize
TablePlugin
- 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
useSelectedCells
useTableBordersDropdownMenuContentState
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
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
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
useTableElement
useTableStore
withDeleteTable
withGetFragmentTable
withInsertFragmentTable
withInsertTextTable
withNormalizeTable
withSelectionTable
withSetFragmentDataTable
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: