๐The search bar is an input box that allows you to search within the app.
1. react-native-freelifemakers-ui NPM Module Install
npm install react-native-freelifemakers-ui
2.PeerDependinceis Module Install
โ๏ธThe package.json file within the module contains the following peerDependencies:
โ๏ธYou can install it using the npm command.
โ๏ธFlmSearchBar requires the react-native-vector-icons module to be installed.
โ๏ธPlease install the latest version of react-native-vector-icons.
"peerDependencies": {
"react": "*",
"react-native": "*",
"react-native-gesture-handler": ">=2.12.0",
"react-native-reanimated": ">=3.17.0",
"react-native-vector-icons": ">=10.2.0"
},
2.Usage
โ๏ธThe part highlighted in bold and italics is FlmSearchBar.
import React, { useState } from "react";
import {
View,
Text,
StyleSheet,
SafeAreaView,
ScrollView,
Alert,
} from "react-native";
import {
***FlmSearchBar,***
FlmButton,
FlmText,
} from "react-native-freelifemakers-ui";
export default function FlmSearchBarApp() {
const [searchText, setSearchText] = useState("");
//
// Search submission handler
const handleSearchSubmit = () => {
console.log("๊ฒ์ ์ ์ถ/Submit Search:", searchText);
// ์ค์ ๊ฒ์ ๋ก์ง์ ์ฌ๊ธฐ์ ๊ตฌํํฉ๋๋ค.
// Implement the actual search logic here.
Alert.alert(
`๊ฒ์ ์ ์ถ/Submit Search`,
`'${searchText}'(์ผ)๋ก ๊ฒ์ํฉ๋๋ค./Search for '${searchText}'.`,
);
};
// ๊ฒ์์ด ์ง์ฐ๊ธฐ ํธ๋ค๋ฌ
// Clear search term handler
const handleClearSearch = () => {
console.log("๊ฒ์์ด ์ง์ฐ๊ธฐ/Clear search term");
// ๊ฒ์์ด ์ง์ฐ๊ธฐ ์ ์ถ๊ฐ์ ์ธ ๋ก์ง์ด ํ์ํ๋ฉด ์ฌ๊ธฐ์ ๊ตฌํํฉ๋๋ค.
// If additional logic is needed when clearing search terms, implement it here.
Alert.alert(
`๊ฒ์์ด ์ง์ฐ๊ธฐ/Clear search term`,
`๊ฒ์์ด๊ฐ ์ง์์ก์ต๋๋ค./Your search term has been cleared.`,
);
};
return (
<SafeAreaView>
<ScrollView>
***<FlmSearchBar
value={searchText}
onChangeText={setSearchText}
placeholder="์ฌ๊ธฐ์ ๊ฒ์์ด๋ฅผ ์
๋ ฅํ์ธ์/Enter your search term here"
onSubmitEditing={handleSearchSubmit}
onClear={handleClearSearch}
style={styles.searchBarMargin}
/>***
</ScrollView>
</SafeAreaView>
);
}
}
3.Full Code
โ๏ธIn the code below, import Ionicons from โreact-native-vector-icons/Ioniconsโ; This part is only for ReactNative CLI. โ๏ธExpo users, please comment out the code above.
// flmSearchBar.js
import React, {useState} from 'react';
import {
View,
Text,
StyleSheet,
SafeAreaView,
ScrollView,
Alert,
} from 'react-native';
// ReactNative CLI์ธ๊ฒฝ์ฐ
// npm install react-native-vector-icons
import Ionicons from 'react-native-vector-icons/Ionicons';
// Expo๋ react-native-vector-icons๋ฅผ ๋ด์ฅํ๊ณ ์์
// Expo includes react-native-vector-icons by default.
// import {Ionicons} from '@expo/vector-icons';
// react-native-freelifemakers-ui ํจํค์ง์์ FlmSearchBar ์ํฌํธ
import {FlmSearchBar, FlmButton, FlmText} from 'react-native-freelifemakers-ui';
export default function FlmSearchBarApp() {
const [searchText, setSearchText] = useState('');
// ๊ฒ์ ์ ์ถ ํธ๋ค๋ฌ
// Search submission handler
const handleSearchSubmit = () => {
console.log('๊ฒ์ ์ ์ถ/Submit Search:', searchText);
// ์ค์ ๊ฒ์ ๋ก์ง์ ์ฌ๊ธฐ์ ๊ตฌํํฉ๋๋ค.
// Implement the actual search logic here.
Alert.alert(
`๊ฒ์ ์ ์ถ/Submit Search`,
`'${searchText}'(์ผ)๋ก ๊ฒ์ํฉ๋๋ค./Search for '${searchText}'.`,
);
};
// ๊ฒ์์ด ์ง์ฐ๊ธฐ ํธ๋ค๋ฌ
// Clear search term handler
const handleClearSearch = () => {
console.log('๊ฒ์์ด ์ง์ฐ๊ธฐ/Clear search term');
// ๊ฒ์์ด ์ง์ฐ๊ธฐ ์ ์ถ๊ฐ์ ์ธ ๋ก์ง์ด ํ์ํ๋ฉด ์ฌ๊ธฐ์ ๊ตฌํํฉ๋๋ค.
// If additional logic is needed when clearing search terms, implement it here.
Alert.alert(
`๊ฒ์์ด ์ง์ฐ๊ธฐ/Clear search term`,
`๊ฒ์์ด๊ฐ ์ง์์ก์ต๋๋ค./Your search term has been cleared.`,
);
};
return (
<SafeAreaView style={styles.safeArea}>
<ScrollView contentContainerStyle={styles.scrollViewContent}>
<Text style={styles.header}>FlmSearchBar</Text>
{/* ๊ธฐ๋ณธ ๊ฒ์ ๋ฐ / Default Search Bar */}
<FlmText style={styles.subHeader}>
๊ธฐ๋ณธ ๊ฒ์ ๋ฐ / Default Search Bar
</FlmText>
<FlmSearchBar
value={searchText}
onChangeText={setSearchText}
placeholder="์ฌ๊ธฐ์ ๊ฒ์์ด๋ฅผ ์
๋ ฅํ์ธ์/Enter your search term here"
onSubmitEditing={handleSearchSubmit}
onClear={handleClearSearch}
style={styles.searchBarMargin}
/>
<FlmText style={styles.currentSearchText}>
ํ์ฌ ๊ฒ์์ด/ Current search term: {searchText || '์์/none'}
</FlmText>
{/* ์ปค์คํฐ๋ง์ด์ง๋ ๊ฒ์ ๋ฐ ์์ / Example of a customized search bar */}
<FlmText style={styles.subHeader}>
์ปค์คํ
์คํ์ผ ๊ฒ์ ๋ฐ / Custom style search bar
</FlmText>
<FlmSearchBar
value={searchText}
onChangeText={setSearchText}
placeholder="๋ค๋ฅธ ์คํ์ผ์ ๊ฒ์ ๋ฐ/Search bar in different styles"
placeholderTextColor="#aaa"
style={styles.customSearchBar}
inputStyle={styles.customInput}
showClearButton={false} // ์ง์ฐ๊ธฐ ๋ฒํผ ์จ๊ธฐ๊ธฐ / Hide the erase button
/>
{/* ๋ค๋ฅธ ์ปดํฌ๋ํธ์์ ์กฐํฉ ์์ / Example of combination with other components */}
<FlmText style={styles.subHeader}>
๋ฒํผ๊ณผ ํจ๊ป ๊ฒ์ / Search with button
</FlmText>
<FlmSearchBar
value={searchText}
onChangeText={setSearchText}
placeholder="๋ฒํผ๊ณผ ํจ๊ป ๊ฒ์/Search with button"
onSubmitEditing={handleSearchSubmit}
keyboardType="web-search"
/>
<FlmButton
title="๊ฒ์ ์คํ/Run search"
onPress={handleSearchSubmit}
style={styles.searchButton}
/>
{/* ํ๋จ ์ฌ๋ฐฑ / bottom margin */}
<View style={{height: 50}} />
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
safeArea: {
flex: 1,
backgroundColor: '#f8f8f8',
},
scrollViewContent: {
flexGrow: 1,
paddingVertical: 20,
alignItems: 'center',
},
header: {
fontSize: 28,
fontWeight: 'bold',
marginBottom: 30,
color: '#333',
},
subHeader: {
fontSize: 20,
fontWeight: '600',
marginTop: 30,
marginBottom: 15,
color: '#555',
},
searchBarMargin: {
marginBottom: 20,
},
currentSearchText: {
fontSize: 16,
color: '#666',
marginBottom: 20,
},
customSearchBar: {
backgroundColor: '#e0f7fa', // ๋ฐ์ ํ๋์ ๋ฐฐ๊ฒฝ / light blue background
borderRadius: 10,
borderWidth: 1,
borderColor: '#b2ebf2',
height: 45,
marginVertical: 10,
},
customInput: {
color: '#00796b', // ์งํ ์ฒญ๋ก์ ํ
์คํธ / Dark turquoise text
fontWeight: 'bold',
},
searchButton: {
marginTop: 10,
backgroundColor: '#007bff',
},
});