source: node_modules/react-debounce-input/README.md@ 65b6638

main
Last change on this file since 65b6638 was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 5.6 KB
Line 
1# react-debounce-input [![npm](https://img.shields.io/npm/v/react-debounce-input.svg?style=flat-square)](https://www.npmjs.com/package/react-debounce-input)
2
3[![CircleCI](https://img.shields.io/circleci/project/nkbt/react-debounce-input.svg?style=flat-square&label=build)](https://circleci.com/gh/nkbt/react-debounce-input)
4[![Dependencies](https://img.shields.io/david/nkbt/react-debounce-input.svg?style=flat-square)](https://david-dm.org/nkbt/react-debounce-input)
5[![Dev Dependencies](https://img.shields.io/david/dev/nkbt/react-debounce-input.svg?style=flat-square)](https://david-dm.org/nkbt/react-debounce-input#info=devDependencies)
6
7React component that renders an Input, Textarea or other element with debounced onChange. Can be used as drop-in replacement for `<input type="text" />` or `<textarea />`
8
9![React Debounce Input](./example/react-debounce-input.gif)
10
11## Live design system demo
12
13[https://www.jinno.io/app/19](https://www.jinno.io/app/19/?source=react-debounce-input)
14
15## Simple web demo
16
17[http://nkbt.github.io/react-debounce-input](http://nkbt.github.io/react-debounce-input)
18
19## Codepen demo
20
21[http://codepen.io/nkbt/pen/VvmzLQ](http://codepen.io/nkbt/pen/VvmzLQ?editors=0010)
22
23## Installation
24
25### NPM
26
27```sh
28npm install --save react-debounce-input
29```
30
31### yarn
32
33```sh
34yarn add react-debounce-input
35```
36
37### 1998 Script Tag:
38```html
39<script src="https://unpkg.com/react@16.0.0/umd/react.production.min.js"></script>
40<script src="https://unpkg.com/react-debounce-input/build/react-debounce-input.js"></script>
41(Module exposed as `DebounceInput`)
42```
43
44## Usage
45```js
46import React from 'react';
47import ReactDOM from 'react-dom';
48import {DebounceInput} from 'react-debounce-input';
49
50class App extends React.Component {
51 state = {
52 value: ''
53 };
54
55 render() {
56 return (
57 <div>
58 <DebounceInput
59 minLength={2}
60 debounceTimeout={300}
61 onChange={event => this.setState({value: event.target.value})} />
62
63 <p>Value: {this.state.value}</p>
64 </div>
65 );
66 }
67}
68
69const appRoot = document.createElement('div');
70document.body.appendChild(appRoot);
71ReactDOM.render(<App />, appRoot);
72```
73
74## Options
75
76### `element` : PropTypes.string or React.PropTypes.func (default: "input")
77
78You can specify element="textarea". For Example:
79
80```js
81<DebounceInput element="textarea" />
82```
83
84Will result in
85
86```js
87<textarea />
88```
89
90Note: when rendering a `<textarea />` you may wish to set `forceNotifyByEnter = {false}` so the user can make new lines without forcing notification of the current value.
91
92This package has only been tested with `<input />` and `<textarea />` but should work with any element which has `value` and `onChange` props.
93
94You can also use a custom react component as the element. For Example:
95
96```js
97<DebounceInput element={CustomReactComponent} />
98```
99
100Will result in
101
102```js
103<CustomReactComponent />
104```
105
106### `onChange`: PropTypes.func.isRequired
107
108Function called when value is changed (debounced) with original event passed through
109
110
111### `value`: PropTypes.string
112
113Value of the Input box. Can be omitted, so component works as usual non-controlled input.
114
115
116### `minLength`: PropTypes.number (default: 0)
117
118Minimal length of text to start notify, if value becomes shorter then `minLength` (after removing some characters), there will be a notification with empty value `''`.
119
120
121### `debounceTimeout`: PropTypes.number (default: 100)
122
123Notification debounce timeout in ms. If set to `-1`, disables automatic notification completely. Notification will only happen by pressing `Enter` then.
124
125
126### `forceNotifyByEnter`: PropTypes.bool (default: true)
127
128Notification of current value will be sent immediately by hitting `Enter` key. Enabled by-default. Notification value follows the same rule as with debounced notification, so if Length is less, then `minLength` - empty value `''` will be sent back.
129
130*NOTE* if `onKeyDown` callback prop was present, it will be still invoked transparently.
131
132### `forceNotifyOnBlur`: PropTypes.bool (default: true)
133
134Same as `forceNotifyByEnter`, but notification will be sent when focus leaves the input field.
135
136### `inputRef`: PropTypes.func (default: undefined)
137
138Will pass `ref={inputRef}` to generated input element. We needed to rename `ref` to `inputRef` since `ref` is a special prop in React and cannot be passed to children.
139
140See [./example/Ref.js](./example/Ref.js) for usage example.
141
142### Arbitrary props will be transferred to rendered `<input>`
143
144```js
145<DebounceInput
146 type="number"
147 onChange={event => this.setState({value: event.target.value})}
148 placeholder="Name"
149 className="user-name" />
150```
151
152Will result in
153
154```js
155<input
156 type="number"
157 placeholder="Name"
158 className="user-name" />
159```
160
161## Typescript
162
163This library has typescript typings, import them the same way as in javascript:
164
165```typescript
166import {DebounceInput} from 'react-debounce-input';
167```
168
169Also there are helper types `DebounceTextArea` and `Debounced` to provide strict interfaces for wrapping components different from standard `<input />`. Check usage examples in `example/typescript-example.tsx`.
170
171
172*NOTE* library author is not using Typescript, so if you are using typings and found an issue, please submit a PR with fix. Thanks @iyegoroff for the initial TS support!
173
174
175## Development and testing
176
177Currently is being developed and tested with the latest stable `Node` on `OSX`.
178
179To run example covering all `DebounceInput` features, use `yarn start`, which will compile `example/Example.js`
180
181```bash
182git clone git@github.com:nkbt/react-debounce-input.git
183cd react-debounce-input
184yarn install
185yarn start
186
187# then
188open http://localhost:8080
189```
190
191## Tests
192
193```bash
194# to run ESLint check
195yarn lint
196
197# to run tests
198yarn test
199```
200
201## License
202
203MIT
Note: See TracBrowser for help on using the repository browser.