react-easy-edit
A React library that allows inline editing on HTML5 input components, try the sandbox here!
📝 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
🙏 Show your support
Give a
🆒 Props
Prop | Type | Required | Default | Description |
---|---|---|---|---|
type | string | Yes | The type of the input element to display. Supported types are text , number , color , textarea , date , datetime-local ,time , month , week , radio , email , checkbox , select , range and datalist
|
|
value | string or number or array | No | null | The value of the input element depended on its type |
options | array | No | A key value pair object that is used as available options for select, radio and checkbox.options = [{label:'Test One', value: '1'},{label:'Test Two', value: '2'}];
|
|
saveButtonLabel | string or element | No | Save | The label to be used for the "Save" button |
saveButtonStyle | string | No | easy-edit-button | One or more CSS classes to be used to style the "Save" button |
cancelButtonLabel | string or element | No | Cancel | The label to be used for the "Cancel" button |
cancelButtonStyle | string | No | easy-edit-button | One or more CSS classes to be used to style the "Cancel" button |
placeholder | string | No | Click to edit | The text to be shown as a hint that describes the expected value of the input element |
onCancel | function | No | () => {} | A function that will be called when editing is cancelled. Also called when the Esc button is pressed |
onSave | function | Yes | A function that will be called when editing is saved. Also called when the Enter button is pressed (Textarea component is excluded) | |
onValidate | function | No | () => {} | A function that will be called before the onSave() event. It must return true or false and has one parameter which is the value of the component being edited |
validationMessage | string | No | The text to be displayed if validation fails | |
allowEdit | boolean | No | true | Determines whether the component itself should be editable or not |
attributes | object | No | {} | A key value pair of HTML attributes to be applied on the element |
instructions | string | No | Instructions to be shown below the component | |
editComponent | element | No | null | The custom component to be displayed when editing the value. This will override the standard input shown for the type provided |
displayComponent | element | No | null | The custom component to be displayed the value when not editing |
Usage
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);
})
}
🤝 Contributing
Contributions, issues and feature requests are welcome.
Feel free to check issues page if you want to contribute.
Contributors
@giorgosart | @mnnalxndr | @liamwithers |
|
|
🔢 Versioning
For transparency and insight into our release cycle, releases will be numbered with the following format:
<major>.<minor>.<patch>
And constructed with the following guidelines:
- Breaking backwards compatibility bumps the major
- New additions without breaking backwards compatibility bumps the minor
- Bug fixes and misc changes bump the patch
- For more information on semantic versioning, please visit http://semver.org/.
📜 Licence
Copyright © 2019 George Artemiou.
This project is MIT licensed.