-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchBar.js
63 lines (56 loc) · 1.98 KB
/
SearchBar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import React, { useState } from 'react';
import { View, TextInput, TouchableOpacity } from 'react-native';
import { FontAwesome } from '@expo/vector-icons';
import { tw } from 'react-native-tailwindcss';
const SearchBar = ({ onSearch }) => {
const [searchText, setSearchText] = useState('');
const [isFocused, setIsFocused] = useState(false);
// Função pesquisa quando o usuário clica no ícone de pesquisa ou pressiona "OK" no teclado.
const handleSearch = () => {
if (searchText.trim() !== '') {
onSearch(searchText);
} else {
setIsFocused(true);
}
};
//Verificando se o usuario digitou
const handleInputChange = (text) => {
setSearchText(text);
setIsFocused(false);
};
//limpando o campo de buscar para retornar a pagina inicial
const handleClear = () => {
setSearchText('');
setIsFocused(true);
onSearch('');
};
//Função de disparada quando usuario aperta ok no teclado
const handleSearchOnSubmit = () => {
handleSearch();
};
return (
<View style={tw.mB4}>
<View style={tw.flexRow}>
<TextInput
style={[ tw.flex1, tw.pX4, tw.pY2, tw.textWhite, tw.bgGray800, tw.roundedLg, tw.mR2,
isFocused && tw.borderPrimary,
]}
placeholder="Qual livro você procura?"
placeholderTextColor="gray"
value={searchText}
onChangeText={handleInputChange}
onSubmitEditing={handleSearchOnSubmit}
/>
{searchText !== '' && (
<TouchableOpacity style={[tw.itemsCenter, tw.justifyCenter, tw.bgPrimary, tw.rounded, tw.p2, tw.mR2]} onPress={handleClear}>
<FontAwesome name="times" size={18} color="white" />
</TouchableOpacity>
)}
<TouchableOpacity style={[tw.itemsCenter, tw.justifyCenter, tw.bgPrimary, tw.rounded, tw.p2]} onPress={handleSearch}>
<FontAwesome name="search" size={24} color="white" />
</TouchableOpacity>
</View>
</View>
);
};
export default SearchBar;