Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

675
Views
Unfocus/blur a Material UI input

I have an input field that can be reset/cleared by a button click:

https://codesandbox.io/s/basictextfields-material-demo-forked-m1z5l?file=/demo.js:127-482

  const searchInput = useRef(null);

  const clearInput = () => {
    searchInput.current.value = '';
    searchInput.current.blur();
  }

  return (
    <div>
      <Input
        inputRef={searchInput} 
        id="outlined-basic" 
        label="Outlined" 
        variant="outlined" />
      <button onClick={clearInput}>Clear</button>
    </div>
  );

The issue is that the label does not reset to it's original position after clearing the input. I'm guessing because the input field still thinks it has the focus. I tried adding a blur() but that doesn't seem to do anything.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This is in no way an elegant solution. But it's what I got to make it work as you wanted

import React, {useRef, useState, } from 'react';
import Input from '@mui/material/TextField';

export default function BasicTextFields() {
  const searchInput = useRef(null);
  const [isFocused, setFocus] = useState(false);

  const clearInput = () => {
    searchInput.current.value = '';
    searchInput.current.blur();
    setInputFocus(false)
  }

  const setInputFocus = (state)=>{
    const notEmpty = !!searchInput.current?.value;

    if(notEmpty) return setFocus(true)

    setFocus(state)
  }

  return (
    <div>
      <Input
        inputRef={searchInput} 
        id="outlined-basic" 
        label="Outlined" 
        InputLabelProps={{shrink: isFocused}}
        onFocus={()=>setInputFocus(true)}
        onBlur={()=>setInputFocus(false)}
        variant="outlined" />
      <button onClick={clearInput}>Clear</button>
    </div>
  );
}

Edit BasicTextFields Material Demo (forked)

about 4 years ago · Juan Pablo Isaza Report

0

If what you really want is clearing the TextField programmatically, then just use controlled mode and reset the value state when needed. See uncontrolled-vs-controlled:

export default function BasicTextFields() {
  const [value, setValue] = useState("");
  const clearInput = () => setValue("");

  return (
    <>
      <Input
        value={value}
        onChange={(e) => setValue(e.target.value)}
        label="Outlined"
        variant="outlined"
      />
      <button onClick={clearInput}>Clear</button>
    </>
  );
}

Live Demo

Codesandbox Demo

about 4 years ago · Juan Pablo Isaza Report

0

You can use inputProps for catch onBlur event.

    <Input
        id="outlined-basic" 
        label="Outlined" 
        variant="outlined"
        inputProps={{
            onBlur: () => {
                console.log('foo bar')
            }
        }}/>
      <button onClick={clearInput}>Clear</button>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!