react-input-debounce 1.0.6 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +103 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1 +1,103 @@
|
|
|
1
|
-
#
|
|
1
|
+
# react input debounce [](https://www.npmjs.com/package/react-input-debounce) [](LICENSE) [](https://github.com/Bexet/react-input-debounce)
|
|
2
|
+
|
|
3
|
+
`react input debounce` provides a simple and efficient React component to debounce the `onChange` event of a standard HTML input element. This prevents excessive re-renders or API calls while a user is actively typing, improving performance and user experience.
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
import { DebounceInput } from 'react-input-debounce';
|
|
7
|
+
...
|
|
8
|
+
<DebounceInput debounceTimeout={500} onChange={...} />
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## ✨ Features
|
|
12
|
+
|
|
13
|
+
* **Debounced `onChange` Event**: Automatically debounces the `onChange` event of an input field, triggering your handler only after a specified delay.
|
|
14
|
+
* **Configurable Debounce Timeout**: Easily customize the debounce delay using the `debounceTimeout` prop.
|
|
15
|
+
* **Standard Input Compatibility**: Supports all standard HTML `<input>` attributes and props.
|
|
16
|
+
* **TypeScript Support**: Built with TypeScript for a robust and type-safe development experience.
|
|
17
|
+
|
|
18
|
+
## 📦 Installation
|
|
19
|
+
|
|
20
|
+
Install the package using npm or yarn:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install react-input-debounce
|
|
24
|
+
|
|
25
|
+
# or
|
|
26
|
+
yarn add react-input-debounce
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 🚀 Usage
|
|
30
|
+
|
|
31
|
+
Integrate `DebounceInput` into your React components just like a regular `<input>` element. Provide an `onChange` handler and an optional `debounceTimeout` (defaults to `100ms`).
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import React, { ChangeEvent, useState } from 'react';
|
|
35
|
+
import { DebounceInput, DebounceInputProps } from 'react-input-debounce';
|
|
36
|
+
|
|
37
|
+
function MyDebouncedForm() {
|
|
38
|
+
const [searchValue, setSearchValue] = useState('');
|
|
39
|
+
const [debouncedSearchValue, setDebouncedSearchValue] = useState('');
|
|
40
|
+
const [timeout, setTimeout] = useState(500);
|
|
41
|
+
|
|
42
|
+
// This handler receives the *debounced* change event
|
|
43
|
+
const handleDebouncedChange = (event: ChangeEvent<HTMLInputElement>) => {
|
|
44
|
+
console.log('Debounced value:', event.target.value);
|
|
45
|
+
setDebouncedSearchValue(event.target.value);
|
|
46
|
+
// You would typically perform an API call or expensive operation here
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// This handler receives the *immediate* change event for display purposes
|
|
50
|
+
const handleImmediateChange = (event: ChangeEvent<HTMLInputElement>) => {
|
|
51
|
+
setSearchValue(event.target.value);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<div>
|
|
56
|
+
<h1>Search with Debounce</h1>
|
|
57
|
+
<div>
|
|
58
|
+
<label htmlFor="debounce-timeout">Debounce Timeout (ms): </label>
|
|
59
|
+
<input
|
|
60
|
+
id="debounce-timeout"
|
|
61
|
+
type="number"
|
|
62
|
+
value={timeout}
|
|
63
|
+
onChange={(e) => setTimeout(Number(e.target.value))}
|
|
64
|
+
style={{ marginBottom: '15px' }}
|
|
65
|
+
/>
|
|
66
|
+
</div>
|
|
67
|
+
|
|
68
|
+
<label htmlFor="search-input">Search Term:</label>
|
|
69
|
+
<DebounceInput
|
|
70
|
+
id="search-input"
|
|
71
|
+
type="text"
|
|
72
|
+
placeholder="Type to search..."
|
|
73
|
+
debounceTimeout={timeout} // Dynamic timeout example
|
|
74
|
+
onChange={handleDebouncedChange}
|
|
75
|
+
// All other standard input props are passed through
|
|
76
|
+
className="my-custom-input"
|
|
77
|
+
style={{ width: '300px', padding: '8px' }}
|
|
78
|
+
/>
|
|
79
|
+
|
|
80
|
+
<p>Immediate input value: **{searchValue}**</p>
|
|
81
|
+
<p>Debounced search value: **{debouncedSearchValue}**</p>
|
|
82
|
+
</div>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export default MyDebouncedForm;
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Props
|
|
90
|
+
|
|
91
|
+
The `DebounceInput` component accepts all standard `InputHTMLAttributes<HTMLInputElement>` in addition to the following specific prop:
|
|
92
|
+
|
|
93
|
+
| Prop | Type | Default | Description |
|
|
94
|
+
| :---------------- | :------- | :------ | :------------------------------------------------------ |
|
|
95
|
+
| `debounceTimeout` | `number` | `100` | The delay in milliseconds before the `onChange` event is fired. |
|
|
96
|
+
|
|
97
|
+
## 🤝 Contributing
|
|
98
|
+
|
|
99
|
+
Contributions are welcome! Feel free to open issues or pull requests on the [GitHub repository](https://github.com/Bexet/react-input-debounce).
|
|
100
|
+
|
|
101
|
+
## 📄 License
|
|
102
|
+
|
|
103
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|