UNPKG

6.14 kBMarkdownView Raw
1[![Test](https://img.shields.io/npm/v/react-easy-edit.svg?style=flat)](https://www.npmjs.com/package/react-easy-edit)
2[![NPM](https://img.shields.io/npm/dm/react-easy-edit.svg)](https://www.npmjs.com/package/react-easy-edit)
3[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4[![CircleCI](https://circleci.com/gh/giorgosart/react-easy-edit.svg?style=shield)](https://circleci.com/gh/giorgosart/react-easy-edit)
5[![Known Vulnerabilities](https://snyk.io/test/github/giorgosart/react-easy-edit/badge.svg?targetFile=package.json)](https://snyk.io/test/github/giorgosart/react-easy-edit?targetFile=package.json)
6[![install size](https://packagephobia.now.sh/badge?p=react-easy-edit@latest)](https://packagephobia.now.sh/result?p=react-easy-edit@latest)
7[![DeepScan grade](https://deepscan.io/api/teams/6030/projects/7886/branches/87202/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=6030&pid=7886&bid=87202)
8
9![](https://i.imgur.com/vwqcqeD.gif)
10
11# react-easy-edit
12A React library that allows inline editing on HTML5 input components, try the sandbox **[here](https://codesandbox.io/s/react-easy-edit-sandbox-2y97j)**!
13
14For full library documentation, **[visit this site](https://giorgosart.gitbook.io/react-easy-edit/)**!
15
16### :pencil: Features
17- Supports `input` (most types, even inputs with `datalist`), `textarea`,`radio`, `checkbox` and `select` HTML types
18- Validates user input
19- Allows customisation on all elements including the save and cancel buttons
20- Supports custom editComponent and custom displayComponent for each type
21
22## :rocket: Installation
23```npm i react-easy-edit``` or ```yarn add react-easy-edit```
24
25## :pray: Show your support
26Give a :star: if this project helped you in any way!
27
28[![paypal](https://www.paypalobjects.com/en_GB/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=TUJKB5DPLHA3N&currency_code=GBP&source=url)
29[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/Y8Y611NE2)
30
31## :cool: Props
32For the full list of props, please visit **[our documentation](https://giorgosart.gitbook.io/react-easy-edit/props)**.
33
34## :page_facing_up: Examples
35More examples can be found **[here](https://giorgosart.gitbook.io/react-easy-edit/examples)**
36#### A simple example - Textbox
37```jsx
38import React, { Component } from 'react';
39import EasyEdit from 'react-easy-edit';
40
41function App() {
42
43 const save = (value) => {alert(value)}
44 const cancel = () => {alert("Cancelled")}
45
46 return (
47 <EasyEdit
48 type="text"
49 onSave={save}
50 onCancel={cancel}
51 saveButtonLabel="Save Me"
52 cancelButtonLabel="Cancel Me"
53 attributes={{ name: "awesome-input", id: 1}}
54 instructions="Star this repo!"
55 />
56 );
57}
58```
59
60#### Radio buttons
61```jsx
62<EasyEdit
63 type="radio"
64 value="one"
65 onSave={save}
66 options={[
67 {label: 'First option', value: 'one'},
68 {label: 'Second option', value: 'two'}]}
69 instructions="Custom instructions"
70/>
71```
72
73#### Date
74```jsx
75<EasyEdit
76 type="date"
77 onSave={save}
78 instructions="Select your date of birth"
79/>
80```
81
82#### Dropdown
83```jsx
84<EasyEdit
85 type="select"
86 options={[
87 {label: 'First option', value: 'one'},
88 {label: 'Second option', value: 'two'}]}
89 onSave={save}
90 placeholder="My Placeholder"
91 instructions="Custom instructions"
92/>
93```
94
95#### Datalist
96```jsx
97<EasyEdit
98 type="datalist"
99 options={[
100 {label: 'First option', value: 'one'},
101 {label: 'Second option', value: 'two'}]}
102 onSave={save}
103 instructions="Custom instructions"
104/>
105```
106
107#### Checkboxes
108```jsx
109<EasyEdit
110 type="checkbox"
111 options={[
112 {label: 'First option', value: 'one'},
113 {label: 'Second option', value: 'two'}]}
114 onSave={save}
115 value={['one', 'two']} // this will preselect both options
116/>
117```
118
119#### Custom components
120
121When using custom input components, they must be passed in as props, like so:
122```jsx
123<EasyEdit
124 type="text"
125 onSave={() => {}}
126 editComponent={<CustomInput />}
127 displayComponent={<CustomDisplay />}
128/>
129```
130
131When 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.
132
133For 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.
134
135e.g.
136```jsx
137// Inside your custom input
138
139onChange(searchTerm) {
140 getUserIdByUsername(searchTerm).then((userId) => {
141 this.props.setParentValue(userId);
142 })
143}
144```
145
146You can use a custom EditComponent and still use the saveOnBlur behavior. An ```onBlur``` function will be passed to your custom component, for you to trigger the behavior.
147
148```jsx
149class CustomComponent extends React.Component{
150 constructor(props){
151 super(props);
152 }
153
154 render(){
155 return <div>
156 <p>Custom editor</p>
157 <input onBlur={this.props.onBlur} />
158 </diV>;
159 }
160}
161```
162
163## :handshake: Contributing
164Contributions, issues and feature requests are welcome.
165
166Feel free to check [issues page](https://github.com/giorgosart/react-easy-edit/issues) if you want to contribute.
167
168## :1234: Versioning
169For transparency and insight into our release cycle, releases will be numbered with the following format:
170
171```<major>.<minor>.<patch>```
172
173And constructed with the following guidelines:
174
175- Breaking backwards compatibility bumps the major
176- New additions without breaking backwards compatibility bumps the minor
177- Bug fixes and misc changes bump the patch
178- For more information on semantic versioning, please visit http://semver.org/.
179
180## :scroll: Licence
181Copyright © 2020 [George Artemiou](https://github.com/giorgosart).
182
183This project is [MIT licensed](https://github.com/giorgosart/react-easy-edit/blob/master/LICENSE).
184
\No newline at end of file