1 | # react-debounce-input [](https://www.npmjs.com/package/react-debounce-input)
|
---|
2 |
|
---|
3 | [](https://circleci.com/gh/nkbt/react-debounce-input)
|
---|
4 | [](https://david-dm.org/nkbt/react-debounce-input)
|
---|
5 | [](https://david-dm.org/nkbt/react-debounce-input#info=devDependencies)
|
---|
6 |
|
---|
7 | React 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 | 
|
---|
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
|
---|
28 | npm install --save react-debounce-input
|
---|
29 | ```
|
---|
30 |
|
---|
31 | ### yarn
|
---|
32 |
|
---|
33 | ```sh
|
---|
34 | yarn 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
|
---|
46 | import React from 'react';
|
---|
47 | import ReactDOM from 'react-dom';
|
---|
48 | import {DebounceInput} from 'react-debounce-input';
|
---|
49 |
|
---|
50 | class 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 |
|
---|
69 | const appRoot = document.createElement('div');
|
---|
70 | document.body.appendChild(appRoot);
|
---|
71 | ReactDOM.render(<App />, appRoot);
|
---|
72 | ```
|
---|
73 |
|
---|
74 | ## Options
|
---|
75 |
|
---|
76 | ### `element` : PropTypes.string or React.PropTypes.func (default: "input")
|
---|
77 |
|
---|
78 | You can specify element="textarea". For Example:
|
---|
79 |
|
---|
80 | ```js
|
---|
81 | <DebounceInput element="textarea" />
|
---|
82 | ```
|
---|
83 |
|
---|
84 | Will result in
|
---|
85 |
|
---|
86 | ```js
|
---|
87 | <textarea />
|
---|
88 | ```
|
---|
89 |
|
---|
90 | Note: 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 |
|
---|
92 | This package has only been tested with `<input />` and `<textarea />` but should work with any element which has `value` and `onChange` props.
|
---|
93 |
|
---|
94 | You can also use a custom react component as the element. For Example:
|
---|
95 |
|
---|
96 | ```js
|
---|
97 | <DebounceInput element={CustomReactComponent} />
|
---|
98 | ```
|
---|
99 |
|
---|
100 | Will result in
|
---|
101 |
|
---|
102 | ```js
|
---|
103 | <CustomReactComponent />
|
---|
104 | ```
|
---|
105 |
|
---|
106 | ### `onChange`: PropTypes.func.isRequired
|
---|
107 |
|
---|
108 | Function called when value is changed (debounced) with original event passed through
|
---|
109 |
|
---|
110 |
|
---|
111 | ### `value`: PropTypes.string
|
---|
112 |
|
---|
113 | Value of the Input box. Can be omitted, so component works as usual non-controlled input.
|
---|
114 |
|
---|
115 |
|
---|
116 | ### `minLength`: PropTypes.number (default: 0)
|
---|
117 |
|
---|
118 | Minimal 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 |
|
---|
123 | Notification 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 |
|
---|
128 | Notification 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 |
|
---|
134 | Same as `forceNotifyByEnter`, but notification will be sent when focus leaves the input field.
|
---|
135 |
|
---|
136 | ### `inputRef`: PropTypes.func (default: undefined)
|
---|
137 |
|
---|
138 | Will 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 |
|
---|
140 | See [./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 |
|
---|
152 | Will result in
|
---|
153 |
|
---|
154 | ```js
|
---|
155 | <input
|
---|
156 | type="number"
|
---|
157 | placeholder="Name"
|
---|
158 | className="user-name" />
|
---|
159 | ```
|
---|
160 |
|
---|
161 | ## Typescript
|
---|
162 |
|
---|
163 | This library has typescript typings, import them the same way as in javascript:
|
---|
164 |
|
---|
165 | ```typescript
|
---|
166 | import {DebounceInput} from 'react-debounce-input';
|
---|
167 | ```
|
---|
168 |
|
---|
169 | Also 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 |
|
---|
177 | Currently is being developed and tested with the latest stable `Node` on `OSX`.
|
---|
178 |
|
---|
179 | To run example covering all `DebounceInput` features, use `yarn start`, which will compile `example/Example.js`
|
---|
180 |
|
---|
181 | ```bash
|
---|
182 | git clone git@github.com:nkbt/react-debounce-input.git
|
---|
183 | cd react-debounce-input
|
---|
184 | yarn install
|
---|
185 | yarn start
|
---|
186 |
|
---|
187 | # then
|
---|
188 | open http://localhost:8080
|
---|
189 | ```
|
---|
190 |
|
---|
191 | ## Tests
|
---|
192 |
|
---|
193 | ```bash
|
---|
194 | # to run ESLint check
|
---|
195 | yarn lint
|
---|
196 |
|
---|
197 | # to run tests
|
---|
198 | yarn test
|
---|
199 | ```
|
---|
200 |
|
---|
201 | ## License
|
---|
202 |
|
---|
203 | MIT
|
---|