css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              .app {
  padding: 2em;
  max-width: 800px;
}

.section {
  padding-bottom: 40px;
}

.config {
  padding-bottom: 20px;
}

.label {
  padding-right: 20px;
}

.input {
  margin-right: 10px;
  margin-left: 10px;
  vertical-align: middle;
}

.item {
  margin-right: 10px;
}

            
          
!
            
              class Customizable extends React.Component {
  state = {
    value: '',
    minLength: 0,
    debounceTimeout: 500,
    infinite: false,
    forceNotifyByEnter: true,
    forceNotifyOnBlur: true
  };

  render() {
    const {
      minLength, infinite, debounceTimeout,
      forceNotifyByEnter, forceNotifyOnBlur,
      value, key
    } = this.state;

    return (
      <div>
        <div className="config">
          <label className="label">
            Min length:
            <input
              className="input"
              type="range"
              value={minLength}
              step={1}
              min={0}
              max={10}
              onChange={e => this.setState({minLength: parseInt(e.target.value, 10)})} />
            {minLength}
          </label>

          <label className="label">
            Debounce timeout:
            <input
              className="input"
              type="range"
              disabled={infinite}
              value={debounceTimeout}
              step={100}
              min={0}
              max={2000}
              onChange={e => this.setState({debounceTimeout: parseInt(e.target.value, 10)})} />
            {debounceTimeout}
          </label>

          <label className="label">
            Infinite timeout:
            <input
              className="input"
              type="checkbox"
              checked={infinite}
              onChange={e => this.setState({infinite: e.target.checked})} />
          </label>
        </div>

        <div className="config">
          <label className="label">
            Notify by:
          </label>

          <label className="label">
            Enter keypress:
            <input
              className="input"
              type="checkbox"
              checked={forceNotifyByEnter}
              onChange={e => this.setState({forceNotifyByEnter: e.target.checked})} />
          </label>

          <label className="label">
            Blur:
            <input
              className="input"
              type="checkbox"
              checked={forceNotifyOnBlur}
              onChange={e => this.setState({forceNotifyOnBlur: e.target.checked})} />
          </label>
        </div>

        <DebounceInput
          forceNotifyByEnter={forceNotifyByEnter}
          forceNotifyOnBlur={forceNotifyOnBlur}
          minLength={minLength}
          debounceTimeout={infinite ? -1 : debounceTimeout}
          onChange={e => this.setState({value: e.target.value})}
          onKeyDown={e => this.setState({key: e.key})} />
        <p>Value: {value}</p>
        <p>Key pressed: {key}</p>
      </div>

    );
  }
}


class Controllable extends React.Component {
  state = {
    value: '',
    debouncedValue: ''
  };

  render() {
    const {value, debouncedValue} = this.state;

    return (
      <div>
        <div className="config">
          <label className="label">
            Contollable input:
            <input
              className="input"
              type="text"
              value={value}
              onChange={e => this.setState({value: e.target.value})} />
            {value}
          </label>
        </div>

        <div className="config">
          <label className="label">
            Debounced input:
            <DebounceInput
              className="input"
              value={value}
              minLength={2}
              debounceTimeout={500}
              onChange={e =>
                this.setState({value: e.target.value, debouncedValue: e.target.value})} />
            {debouncedValue}
          </label>
        </div>
      </div>
    );
  }
}


class Textarea extends React.Component {
  state = {
    value: '',
    minLength: 0,
    debounceTimeout: 500,
    infinite: false,
    forceNotifyByEnter: false,
    forceNotifyOnBlur: true
  };

  render() {
    const {
      minLength, infinite, debounceTimeout,
      forceNotifyByEnter, forceNotifyOnBlur,
      value, key
    } = this.state;

    return (
      <div>
        <div className="config">
          <label className="label">
            Min length:
            <input
              className="input"
              type="range"
              value={minLength}
              step={1}
              min={0}
              max={10}
              onChange={e => this.setState({minLength: parseInt(e.target.value, 10)})} />
            {minLength}
          </label>

          <label className="label">
            Debounce timeout:
            <input
              className="input"
              type="range"
              disabled={infinite}
              value={debounceTimeout}
              step={100}
              min={0}
              max={2000}
              onChange={e => this.setState({debounceTimeout: parseInt(e.target.value, 10)})} />
            {debounceTimeout}
          </label>

          <label className="label">
            Infinite timeout:
            <input
              className="input"
              type="checkbox"
              checked={infinite}
              onChange={e => this.setState({infinite: e.target.checked})} />
          </label>
        </div>

        <div className="config">
          <label className="label">
            Notify by:
          </label>

          <label className="label">
            Enter keypress:
            <input
              className="input"
              type="checkbox"
              checked={forceNotifyByEnter}
              onChange={e => this.setState({forceNotifyByEnter: e.target.checked})} />
          </label>

          <label className="label">
            Blur:
            <input
              className="input"
              type="checkbox"
              checked={forceNotifyOnBlur}
              onChange={e => this.setState({forceNotifyOnBlur: e.target.checked})} />
          </label>
        </div>

        <DebounceInput
          cols="60"
          rows="7"
          element="textarea"
          forceNotifyByEnter={forceNotifyByEnter}
          forceNotifyOnBlur={forceNotifyOnBlur}
          minLength={minLength}
          debounceTimeout={infinite ? -1 : debounceTimeout}
          onChange={e => this.setState({value: e.target.value})}
          onKeyDown={e => this.setState({key: e.key})} />
        <p>Value: {value}</p>
        <p>Key pressed: {key}</p>
      </div>
    );
  }
}



class UndoRedo extends React.Component {
  state = {
    value: '',
    history: [''],
    historyIndex: 0
  };

  onChange = ({target: {value}}) => {
    const {historyIndex, history} = this.state;

    this.setState({
      value,
      historyIndex: historyIndex + 1,
      history: [...history.slice(0, historyIndex + 1), value]
    });
  };

  onRedo = () => {
    this.setValueFromHistory(this.state.historyIndex + 1);
  };

  onUndo = () => {
    this.setValueFromHistory(this.state.historyIndex - 1);
  };

  setValueFromHistory = index => {
    const {history} = this.state;
    const historyIndex = Math.min(Math.max(index, 0), history.length - 1);

    this.setState({
      value: history[historyIndex],
      historyIndex
    });
  };

  render() {
    const history = this.state.history.map((value, key) =>
      (<span className="item" key={key}>
        {key === this.state.historyIndex ? <b>{`"${value}"`}</b> : <span>{`"${value}"`}</span>}
      </span>));


    return (
      <div>
        <div className="config">
          <label className="label">
            Debounced Input:
            <DebounceInput
              className="input"
              value={this.state.value}
              minLength={2}
              debounceTimeout={500}
              forceNotifyOnBlur={false}
              onChange={this.onChange} />
          </label>

          <label className="label">
            <button className="input" onClick={this.onUndo}>Undo</button>
          </label>

          <label className="label">
            <button className="input" onClick={this.onRedo}>Redo</button>
          </label>
        </div>

        <p>Current Value: {this.state.value}</p>
        <p>History: {history}</p>
        <p>History Index: {this.state.historyIndex}</p>
      </div>
    );
  }
}



class Ref extends React.Component {
  state = {
    value: '',
    key: undefined
  };


  render() {
    const {
      value,
      key
    } = this.state;

    return (
      <div>
        <div className="config">
          <label className="label">
            <button onClick={() => this.ref.focus()}>Focus, please</button>
          </label>

          <label className="label">
            <button onClick={() => this.ref.blur()}>Blur, please</button>
          </label>
        </div>

        <DebounceInput
          inputRef={ref => {
            this.ref = ref;
          }}
          onChange={e => this.setState({value: e.target.value})}
          onKeyDown={e => this.setState({key: e.key})} />
        <p>Value: {value}</p>
        <p>Key pressed: {key}</p>
      </div>

    );
  }
}



const App = () => (
  <div className="app">
    <h1>react-debounce-input</h1>
    <section className="section">
      <h2>Example 1. Customizable</h2>
      <Customizable />
    </section>

    <section className="section">
      <h2>Example 2. Controllable</h2>
      <Controllable />
    </section>

    <section className="section">
      <h2>Example 3. Undo-Redo</h2>
      <UndoRedo />
    </section>

    <section className="section">
      <h2>Example 4. Debounced Textarea</h2>
      <Textarea />
    </section>

    <section className="section">
      <h2>Example 5. Custom ref</h2>
      <Ref />
    </section>
  </div>
);



ReactDOM.render(<App />, document.querySelector('#app'));
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console