• Stars
    star
    447
  • Rank 94,326 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React component that renders Input with debounced onChange

react-debounce-input npm

CircleCI Dependencies Dev Dependencies

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 />

React Debounce Input

Live design system demo

https://www.jinno.io/app/19

Simple web demo

http://nkbt.github.io/react-debounce-input

Codepen demo

http://codepen.io/nkbt/pen/VvmzLQ

Installation

NPM

npm install --save react-debounce-input

yarn

yarn add react-debounce-input 

1998 Script Tag:

<script src="https://unpkg.com/[email protected]/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-debounce-input/build/react-debounce-input.js"></script>
(Module exposed as `DebounceInput`)

Usage

import React from 'react';
import ReactDOM from 'react-dom';
import {DebounceInput} from 'react-debounce-input';

class App extends React.Component {
  state = {
    value: ''
  };

  render() {
    return (
      <div>
        <DebounceInput
          minLength={2}
          debounceTimeout={300}
          onChange={event => this.setState({value: event.target.value})} />

        <p>Value: {this.state.value}</p>
      </div>
    );
  }
}

const appRoot = document.createElement('div');
document.body.appendChild(appRoot);
ReactDOM.render(<App />, appRoot);

Options

element : PropTypes.string or React.PropTypes.func (default: "input")

You can specify element="textarea". For Example:

<DebounceInput element="textarea" />

Will result in

<textarea />

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.

This package has only been tested with <input /> and <textarea /> but should work with any element which has value and onChange props.

You can also use a custom react component as the element. For Example:

<DebounceInput element={CustomReactComponent} />

Will result in

<CustomReactComponent />

onChange: PropTypes.func.isRequired

Function called when value is changed (debounced) with original event passed through

value: PropTypes.string

Value of the Input box. Can be omitted, so component works as usual non-controlled input.

minLength: PropTypes.number (default: 0)

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 ''.

debounceTimeout: PropTypes.number (default: 100)

Notification debounce timeout in ms. If set to -1, disables automatic notification completely. Notification will only happen by pressing Enter then.

forceNotifyByEnter: PropTypes.bool (default: true)

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.

NOTE if onKeyDown callback prop was present, it will be still invoked transparently.

forceNotifyOnBlur: PropTypes.bool (default: true)

Same as forceNotifyByEnter, but notification will be sent when focus leaves the input field.

inputRef: PropTypes.func (default: undefined)

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.

See ./example/Ref.js for usage example.

Arbitrary props will be transferred to rendered <input>

<DebounceInput
  type="number"
  onChange={event => this.setState({value: event.target.value})}
  placeholder="Name"
  className="user-name" />

Will result in

<input
  type="number"
  placeholder="Name"
  className="user-name" />

Typescript

This library has typescript typings, import them the same way as in javascript:

import {DebounceInput} from 'react-debounce-input';

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.

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!

Development and testing

Currently is being developed and tested with the latest stable Node on OSX.

To run example covering all DebounceInput features, use yarn start, which will compile example/Example.js

git clone [email protected]:nkbt/react-debounce-input.git
cd react-debounce-input
yarn install
yarn start

# then
open http://localhost:8080

Tests

# to run ESLint check
yarn lint

# to run tests
yarn test

License

MIT

More Repositories

1

react-copy-to-clipboard

Copy-to-clipboard React component
JavaScript
2,290
star
2

react-collapse

Component-wrapper for collapse animation with react-motion for elements with variable (and dynamic) height
JavaScript
1,116
star
3

react-height

Component-wrapper to determine and report children elements height
JavaScript
181
star
4

component-router

Redux-based component routing solution
JavaScript
93
star
5

react-component-template

Base for React Components
JavaScript
56
star
6

esnext-quickstart

Bring compilation and code validation to your front-end development process
JavaScript
55
star
7

react-works

πŸ‡ Monorepo for React components
JavaScript
46
star
8

react-page-click

πŸ„πŸ» **MOVED** React component-wrapper to detect page clicks (outside of wrapped element).
JavaScript
45
star
9

react-interval

πŸ„πŸ» **MOVED** Safe React wrapper for setInterval
37
star
10

ng-modular

Truly modular Angular app, built with Webpack. Slides
JavaScript
36
star
11

react-motion-loop

Looping animation for react-motion Spring
JavaScript
28
star
12

react-text-filter

πŸ„πŸ» **MOVED** React component that renders filtering Input
18
star
13

react-swap

πŸ„πŸ» **MOVED** React component-wrapper to swap one element with another and back
18
star
14

nightwatch-autorun

Zero-configuration runner to automatically install and run End-to-End tests with Nightwatch and Selenium
JavaScript
14
star
15

react-normalized-select

πŸ„πŸ» **MOVED** Normalized Select wrapper, returns array of values in multiple mode
14
star
16

react-element-resize

πŸ„πŸ» **MOVED** Element resize sensor with optional debounce
10
star
17

vagrant-node

Bootstrap for Node development.
Scheme
8
star
18

css-modules-component-template

Styled UI component template with CSS-Modules
JavaScript
5
star
19

minicat

Windows compatible `cat` shell command polyfill
JavaScript
5
star
20

build

Automatic building with Grunt
JavaScript
3
star
21

geovis

GeoVis
JavaScript
3
star
22

ui

Monorepo for UI components, React hooks, elements, no build, only sources
JavaScript
2
star
23

node-girls

🎀 Slides for NodeGirls Sydney @ 1.0.0
JavaScript
2
star
24

un-responsive-demo

Demo app for Un.Responsive talk
JavaScript
2
star
25

wtf-tf

Terraform for beginners, demystifying CI/CD for developers
HCL
2
star
26

itunes-scrobbler

Scrobble retrospectively plays and loved tracks to Last.FM from iTunes database.
JavaScript
2
star
27

cf-react-component-template

🚫 **DEPRECATED** React component template scaffold
JavaScript
2
star
28

gleam

Reusable models for the Node server and browsers
JavaScript
2
star
29

flux-common-store

Common Flux store
JavaScript
2
star
30

react-component-router

Official ComponentRouter bindings for React
JavaScript
2
star
31

gleam-demo

Simple chat application to demo Gleam library.
CSS
2
star
32

react-bulkhead

πŸ„πŸ» **MOVED** React component to allow 3rd party components to operate over DOM-tree (d3, three.js)
2
star
33

sydjs2

SydJS talk: Modular, please
1
star
34

oss

OpenSource Talk
JavaScript
1
star
35

scrobbler

Manual bulk scrobbler from text or json file to LastFM
JavaScript
1
star
36

opera-github-sidebar

Opera sidebar plugin for Github
JavaScript
1
star
37

haskell-book

HaskellBook exercises
Haskell
1
star
38

v7tabs

Restyled V7 Tabs Opera extension
JavaScript
1
star
39

sublime2-todo

TODO language and syntax highlight for Sublime Text 2
1
star
40

react-jasmine-matchers

🚫 **DEPRECATED** Custom matchers for ReactTestUtils
JavaScript
1
star
41

puma

Fast and simple framework for modular application
JavaScript
1
star
42

nnj

Node Ninjas talk
JavaScript
1
star
43

un-responsive

Un.Responsive talk for ReactSydney
JavaScript
1
star
44

cli-tester

Lean promisified wrapper to test NodeJS CLI scripts
JavaScript
1
star
45

geovis-standalone

GeoVis Standalone
JavaScript
1
star
46

event-done

Extension of EventEmitter which allows to pass callback that will be invoked when all listeners completed
JavaScript
1
star
47

wscli

WebSocket CLI util
JavaScript
1
star
48

css-modules-component-template-test

Example consumer app for css-modules-component-template
1
star