A React library that allows inline editing on HTML5 input components
react-easy-edit
A React library that allows inline editing on HTML5 input components.
Inline editing library for React.
Features
- Supports
input
(most types, even inputs withdatalist
),textarea
,radio
,checkbox
andselect
HTML types - Validates user input
- Allows customisation on all elements including the save and cancel buttons
- Supports custom editComponent and custom displayComponent for each type
Installation
npm i react-easy-edit
or yarn add react-easy-edit
Examples
More examples can be found here
A simple example - Textbox
import React, { Component } from 'react';
import EasyEdit from 'react-easy-edit';
function App() {
const save = (value) => {alert(value)}
const cancel = () => {alert("Cancelled")}
return (
<EasyEdit
type="text"
onSave={save}
onCancel={cancel}
saveButtonLabel="Save Me"
cancelButtonLabel="Cancel Me"
attributes={{ name: "awesome-input", id: 1}}
instructions="Star this repo!"
/>
);
}
Radio buttons
<EasyEdit
type="radio"
value="one"
onSave={save}
options={[
{label: 'First option', value: 'one'},
{label: 'Second option', value: 'two'}]}
instructions="Custom instructions"
/>
Date
<EasyEdit
type="date"
onSave={save}
instructions="Select your date of birth"
/>
Dropdown
<EasyEdit
type="select"
options={[
{label: 'First option', value: 'one'},
{label: 'Second option', value: 'two'}]}
onSave={save}
placeholder="My Placeholder"
instructions="Custom instructions"
/>
Datalist
<EasyEdit
type="datalist"
options={[
{label: 'First option', value: 'one'},
{label: 'Second option', value: 'two'}]}
onSave={save}
instructions="Custom instructions"
/>
Checkboxes
<EasyEdit
type="checkbox"
options={[
{label: 'First option', value: 'one'},
{label: 'Second option', value: 'two'}]}
onSave={save}
value={['one', 'two']} // this will preselect both options
/>
Custom components
When using custom input components, they must be passed in as props, like so:
<EasyEdit
type="text"
onSave={() => {}}
editComponent={<CustomInput />}
displayComponent={<CustomDisplay />}
/>
When defining a custom input component, the function setParentValue
is injected into your custom component, which must be called in order to pass the desired value up to the parent EasyEdit
component.
For example, if your component was a text field that needed to set the EasyEdit
value as a user id based on a username entered, you would need to pass the id to this.props.setParentValue
in order to get that value to the EasyEdit
component.
e.g.
// Inside your custom input
onChange(searchTerm) {
getUserIdByUsername(searchTerm).then((userId) => {
this.props.setParentValue(userId);
})
}