react-code-input icon indicating copy to clipboard operation
react-code-input copied to clipboard

Support clear all input?

Open xilanhthanki opened this issue 7 years ago • 14 comments

Do we have the ability to clear all inputs? I want to clear all inputs when user enter wrong OTP.

Thanks

xilanhthanki avatar Nov 12 '18 12:11 xilanhthanki

I am still facing the same issue. Please let me know how to clear all inputs at once.

Thanks

atifaziz2 avatar Jan 18 '19 11:01 atifaziz2

Hi, I also need to clear all inputs at once, Please add this feature. Thanks!

rubysim avatar Apr 30 '19 13:04 rubysim

Same

bretep avatar Jun 27 '19 05:06 bretep

As for now, i use this method, which works fine for me 😊.

const codeRef = useRef(undefined)

...

function clear() {
    if(codeRef.current.textInput[0]) codeRef.current.textInput[0].focus()
    codeRef.current.state.input[0] = ""
    codeRef.current.state.input[1] = ""
    codeRef.current.state.input[2] = ""
    codeRef.current.state.input[3] = ""
    codeRef.current.state.input[4] = ""
    codeRef.current.state.input[5] = ""
}

...

<ReactCodeInput ref={codeRef} ... />

caritasverein avatar Jun 28 '19 08:06 caritasverein

@caritasverein

CreateUser.js:76 Uncaught ReferenceError: useRef is not defined.

How did you do that ?

tarun-nagpal-github avatar Jul 15 '19 13:07 tarun-nagpal-github

@tarun-nagpal-github import React, {useRef} from 'react'

caritasverein avatar Jul 15 '19 13:07 caritasverein

I've tried @caritasverein 's method with the useRef hook but adapting to simple react refs, and it didn't help me. I had to change the value of the value field of each element from textInput array to "" too. And, here's the result :

setRef = ref => this.pin = ref;

clear = () =>
{
    for (let i = 0; i < this[key].textInput.length; i++)
    {
        this.pin.state.input[i] = '';
        this.pin.textInput[i].value = '';
    }
}

...

<ReactCodeInput ref={this.setRef} ... />

20SangHun00 avatar Nov 28 '19 18:11 20SangHun00

@20SangHun00 and @caritasverein solution works for now, but this is really dirty.

Should be implemented in the lib itself.

pedro-lb avatar Apr 09 '20 00:04 pedro-lb

is there other options how to clear all value ?

arjayGarcia15 avatar Aug 14 '20 06:08 arjayGarcia15

You can use key to re-render the component.. Something like:

const  [pin, setPin] = useState('');
const [key, setKey] = useState(1);
  useEffect(() => {
      // if value was cleared, set key to re-render the element
      if (pin.length === 0) {
        setKey(key + 1);
        return;
      }

      // do something with the pin
  }, [pin]);
        <ReactCodeInput
          onChange={(value) => setPin(value)}
          isValid={isValid}
          value={pin}
          key={key}
          // more props if you need'em
        ></ReactCodeInput>

dadvir avatar Nov 17 '20 16:11 dadvir

codeRef.current.clearvalues();

slinker-hiwa avatar Dec 29 '20 10:12 slinker-hiwa

@slinker-hiwa not too sure if that works – when console logging codeRef.current, the function appears to be: __clearvalues__();

I think this will do the trick: codeRef.current.__clearvalues__();

Sorry for the edits – forgot this was markdown.

sidd-mittal avatar Apr 14 '21 16:04 sidd-mittal

i made some updates to support clearing all input by passing in this.props.value as an empty string. here’s a very simple example where the reset is triggered by a “Clear” button:

function Clearable() {
    const [value, setValue] = React.useState(null);

    return (
        <React.Fragment>
          <ReactCodeInput onChange={() => setValue(null)} value={value} />
            <button onClick={() => setValue('')}>Clear</button>
        </React.Fragment>
    );
}

you can try it out via npm i @acusti/react-code-input or yarn add @acusti/react-code-input

acusti avatar Oct 15 '21 01:10 acusti

As a workaround in case you're rendering multiple inputs in a map(), you can clean up the inputs by doing this:

import {useRef, useState} from 'react'
const itemsRef = useRef([]);
const [inputsValueState, setInputsValueState] = useState([])
function resetInputs(){
    setInputsValueState([])
    
    itemsRef.current.forEach(function(item) {
      for (let i = 0; i < item.state.input.length; i++) {
        item.state.input[i] = ''
      }
    })

    itemsRef.current[0].textInput[0].focus()
  }
<ReactCodeInput
    onChange={(e) => handleChange(e, index)}
    value={inputsValueState[index]}
    ref={el => itemsRef.current[index] = el}
    {...props}
/>

padovanitr avatar May 21 '22 18:05 padovanitr