Skip to content

Instantly share code, notes, and snippets.

@lrohde
Created September 13, 2020 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lrohde/bf6ff1c4af9cd2f86044769cc26ed5e7 to your computer and use it in GitHub Desktop.
Save lrohde/bf6ff1c4af9cd2f86044769cc26ed5e7 to your computer and use it in GitHub Desktop.
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