search

Home  >  Q&A  >  body text

Close input text box: ReactJs operation when value changes

I have the following problem: I have an input textbox and I am scanning a barcode. But the textbox doesn't close automatically. If I enter numbers using the keyboard it automatically closes. Did I do something wrong? Is the scanner too fast? Any ideas?

const [open, setOpen] = useState(false);
    
const onValueChange = (event) => {
        if(event.target.value.length===16){
          setOpen(false);
        }
    };

<TextField autoFocus onChange={onValueChange} margin="dense" id="number"  type="text" inputProps={{minlength: 16,maxlength:16}} fullWidth variant="outlined" />
P粉386318086P粉386318086432 days ago595

reply all(1)I'll reply

  • P粉486138196

    P粉4861381962023-09-09 16:29:06

    Do this:

    <TextField autoFocus 
    onChange={onValueChange} 
    margin="dense" 
    id="number"  
    type="text" 
    inputProps={{minlength: 16,maxlength:16}} 
    fullWidth 
    variant="outlined"
    onBlur={() => setOpen(false)} /* NEW */
     />

    reply
    0
  • Cancelreply