Created
September 13, 2020 18:30
-
-
Save lrohde/bf6ff1c4af9cd2f86044769cc26ed5e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useRef } from 'react'; | |
import debounce from 'lodash/debounce'; | |
import { Container, TInput, Icon, IconContainer, SearchIcon } from './styles'; | |
export default function SearchInput({ onChangeText }) { | |
const delayedQuery = useRef(debounce(value => onChangeText(value), 500)) | |
.current; | |
const [searchText, setSearchText] = useState(''); | |
function handleReset() { | |
delayedQuery(''); | |
setSearchText(''); | |
} | |
function handleChangeSearchInput(text) { | |
setSearchText(text); | |
delayedQuery(text); | |
} | |
return ( | |
<Container> | |
<IconContainer> | |
<SearchIcon /> | |
</IconContainer> | |
<TInput | |
value={searchText} | |
placeholder="Busca" | |
onChangeText={onChangeText && handleChangeSearchInput} | |
/> | |
{searchText !== '' && ( | |
<Icon name="ios-close-circle" onPress={() => handleReset()} /> | |
)} | |
</Container> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment