Editable
Edit text inline by switching a preview to an input
Source
Package
Usage
Editable displays a value as a preview and switches it to an input for inline editing.
The edit is committed on Enter or blur and cancelled on Escape:
Activation mode
activationMode prop controls how the preview enters edit mode:
focus(default) – when the preview receives focus (Tabor click)click– on a single clickdblclick– on a double clicknone– only with the controller or withEditable.EditTrigger
Submit mode
submitMode prop controls what commits the edit. Escape cancels the edit in all modes:
both(default) –Enterand blurenter– onlyEnterblur– only blur, including when the browser window loses focusnone– only the controller orEditable.SubmitTrigger
submitMode="both" (default)
submitMode="enter"
submitMode="blur"
Select on focus
By default, the input contents are selected when the field enters edit mode.
Set selectOnFocus={false} to place the caret at the end of the value instead:
Placeholder
Set placeholder to display text when the value is empty. It can be a string or an object with
preview and edit keys to use different text in each mode:
Controlled value
Use value and onChange props to control the value. onChange is called on every keystroke,
onValueCommit when the edit is confirmed and onValueRevert when it is cancelled:
Controlled editing state
Use editing and onEditingChange props to control the editing state. Set defaultEditing to
start in edit mode in uncontrolled mode:
Multiline
Set multiline prop to use a textarea. Enter then inserts a new line and the edit is committed
with Cmd/Ctrl + Enter or on blur. Set autosize to grow the field with its content:
useEditable hook
Editable supports the controller pattern with the useEditable hook:
useEditable options
useEditable accepts an options object typed as EditableControllerOptions:
Controlling with useEditable
Pass the controller to the controller prop. The options accepted by useEditable are then
ignored on Editable, pass them to the hook instead.
Buttons outside of Editable that call submit or cancel must call event.preventDefault()
in onMouseDown, otherwise blur commits the edit before the click is handled:
Value: Controlled by the hook — editing: no
Custom layout
Pass compound components as children to change the layout. Triggers are displayed only in
the relevant mode: the edit trigger in preview mode, the submit and cancel triggers in edit mode.
Use Editable.Textarea instead of Editable.Input for multiline values:
Editable with uncontrolled forms
Set name to include the value in native form submission.
In preview mode the value is submitted with a hidden input, so required is enforced only
in edit mode. Validate the value yourself if it must not be empty.
Focus management
When the edit is committed or cancelled with the keyboard or triggers, focus returns to the preview.
Set finalFocusRef to move focus to a different element instead. Moving focus to a trigger inside
Editable does not commit the edit:
Accessibility
The preview is a focusable button named by its text. If the value can be empty and
placeholder is not set, set aria-label on Editable:
When a trigger renders only an icon, set an aria-label: