editable-cell.tsx
Overview
editable-cell.tsx is a React component module designed to provide editable table rows and cells within an Ant Design (antd) Table. It leverages Ant Design's Form component and React Context to enable inline editing of table data with validation and seamless user experience.
The file exports two main React functional components:
EditableRow: Wraps a table row (
<tr>) with an Ant Design Form context to manage form state for editable cells within that row.EditableCell: A table cell (
<td>) that can toggle between display and edit modes, allowing inline editing of data with validation and save functionality.
These components are typically used together to create tables where the user can click on a cell, edit its content, and have the changes saved back to the data model.
Components
EditableRow
interface EditableRowProps {
index: number;
}
export const EditableRow: React.FC<EditableRowProps>;
Description
EditableRow renders a table row (<tr>) wrapped inside an Ant Design Form component. It provides a React Context (EditableContext) with the form instance, enabling editable cells within the row to share the same form state.
Props
Prop | Type | Description |
|---|---|---|
index | number | The index of the row (used internally, not rendered) |
Usage Example
<EditableRow index={0}>
{/* Table cells go here */}
</EditableRow>
Implementation Details
Uses
Form.useForm()hook to create a form instance.Uses
EditableContext.Providerto pass the form instance down to editable cells.Uses
lodash.omitto remove theindexprop from the<tr>element (since<tr>does not acceptindexattribute).
EditableCell
interface EditableCellProps {
title: React.ReactNode;
editable: boolean;
children: React.ReactNode;
dataIndex: keyof Item;
record: Item;
handleSave: (record: Item) => void;
}
export const EditableCell: React.FC<EditableCellProps>;
Description
EditableCell renders a table cell (<td>) that can switch between a read-only display mode and an inline editing mode. When editable, clicking the cell toggles to an input field with validation. On blur or pressing Enter, the value is saved using a callback.
Props
Prop | Type | Description |
|---|---|---|
title |
| The title or label of the cell; used in validation messages. |
editable |
| Determines if the cell is editable or not. |
children |
| The cell's displayed content when not editing. |
dataIndex |
| The key of the data item that this cell represents (e.g., |
record |
| The full data record object for the row. |
handleSave |
| Callback function invoked with the updated record when the cell's value is saved. |
Usage Example
<EditableCell
title="Name"
editable={true}
dataIndex="name"
record={record}
handleSave={(updatedRecord) => console.log(updatedRecord)}
>
{record.name}
</EditableCell>
Internal State and Methods
editing(boolean): Tracks whether the cell is currently in editing mode.inputRef(React ref): References the input field to focus it when editing starts.form: Accesses the form instance from context to manipulate form state and validation.
Methods
toggleEdit(): Toggles theeditingstate and initializes the form field with the current cell value.save(): Validates the form field; if valid, toggles editing off and callshandleSave()with the updated record.
Behavior
When not editing, the cell renders its children inside a div with a click handler to enter edit mode.
When editing, the cell renders an Ant Design
Form.Itemwith anInputfield:Validation rules require input to be non-empty.
On pressing Enter or losing focus (
onBlur), thesave()method is triggered.
If validation fails, errors are logged but the cell remains in editing mode.
Data Types
interface Item {
key: string;
name: string;
age: string;
address: string;
}
Item defines the shape of the data records displayed and edited in the table rows/cells.
Implementation Details and Algorithms
Form Context Sharing:
Instead of creating a form per cell, a single form per row is created (EditableRow), and the form instance is shared with all editable cells viaEditableContext. This improves performance and ensures consistent form state management.Editing State in Cells:
EachEditableCellmanages its owneditingstate locally to toggle between display and input modes.Validation and Save:
Validation is performed on the input using Ant Design's form validation. On successful validation, the updated data is merged with the existing record and passed up throughhandleSave.Focus Management:
When editing is enabled, the input automatically receives focus using auseEffecthook and a ref to improve UX.Lodash
omit:
Removes theindexprop from the row props to prevent React warnings about unknown DOM attributes.
Interaction with Other Parts of the System
Parent Table Component:
Typically, these components are used inside a parent Table component (usually Ant Design'sTable) where each row is rendered usingEditableRowand each cell usingEditableCell.Data Management:
The parent component manages the table data state and passeshandleSavecallbacks to update the data source with edited values.Form Validation:
Relies on Ant Design's Form system for validation and error handling.
Visual Diagram
classDiagram
class EditableRow {
-form: FormInstance
+props: EditableRowProps
+render()
}
class EditableCell {
-editing: boolean
-inputRef: InputRef
-form: FormInstance
+props: EditableCellProps
+toggleEdit()
+save()
+render()
}
EditableCell ..> EditableContext : uses
EditableRow o-- EditableContext : provides
Summary
editable-cell.tsx provides reusable React components to build inline editable tables using Ant Design's form and input components. It uses React Context to share form state across cells in a row, enabling efficient editing with validation and save functionality. It is designed to be integrated into larger table components managing data state and rendering multiple editable rows and cells.