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

69
Views
React TextField with collapse

How to hide the lists of cities when the textfield is empty and when the user start typing the lists will show.

https://codesandbox.io/s/loving-platform-t2cyb?file=/src/LocationWidget.js

  const openSearch = () => {
    setViewLocationList(true);
    startSearch();
  };
  const stopSearch = () => {
    setSearchParameter('')
    setViewLocationList(false);
  };


  <TextField
    variant="outlined"
      placeholder="Search Locations"
      onFocus={openSearch}
      onChange={filterResults}
      value={searchParameter}
      classes={{notchedOutline:classes.input}}
      InputProps={{
        endAdornment: (
          <IconButton onClick={stopSearch} edge="end">
            <ClearIcon />
          </IconButton>
        ),
        classes:{notchedOutline:classes.noBorder},
        startAdornment: (
          <InputAdornment position="start">
            <SearchIcon />
          </InputAdornment>
        ),
      }}
    />
  </Box>
  <Collapse in={viewLocationList} sx={{ my: '2px' }}>
    <Box className="rounded-scrollbar widget-result-container">
    {filteredLocations.map((location, index) => (
         <LocationWidgetItem
         key={index}
         location={location}
         onClickLocation={setActiveLocation}
       />
      ))}
    </Box>
  </Collapse>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You have to take state variable in which you have to store whatever user is typing:

const [text,setText] = useState("");

and in your filterLocations you have to update its value

const filterLocations = (txt) => {
    setText(txt.target.value);  
    let filteredLocations = locations.filter((e) =>
    e.name.toLowerCase().includes(txt.target.value.toLowerCase())
    );
    setSearchParameter(filteredLocations);
  };

And Finally in your render, render ul conditioanlly

{ !!text && <ul>
        {searchParameter.map((location) => (
          <li key={location.name}>{location.name}</li>
        ))}
      </ul>}

https://codesandbox.io/s/dreamy-roentgen-0jnoi?file=/src/LocationWidget.js

about 4 years ago · Juan Pablo Isaza Report

0

please find below code.

export default function LocationWidget({ locations }) {
const [searchParameter, setSearchParameter] = useState(locations);
const [inputText, setInputText] = useState(null);
const filterLocations = (txt) => {
setInputText(txt.target.value);
let filteredLocations = locations.filter((e) =>
  e.name.toLowerCase().includes(txt.target.value.toLowerCase())
);
setSearchParameter(filteredLocations);
};
return (
<>
  <input type="text" onChange={filterLocations} />
  {inputText && <ul>
    {searchParameter.map((location) => (
      <li key={location.name}>{location.name}</li>
    ))}
  </ul>}
</>
);
}
about 4 years ago · Juan Pablo Isaza Report

0

You'd like to set an 'input' eventListener on the input field. And check if the inputField length > 0, then set the setViewLocationList(true); and if the inputField length === 0, then set the setViewLocationList(false).

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!