javascript - Highlight the Search Text in FlatList - React Native
问题描述
I have a React Native App and i have a Search Bar and a FlatList. My search logic is as below
handleSearch(text) {
const newData = _.filter(this.state.temp, (item) => {
const itemData = item.Desc ? item.Desc.toUpperCase() : ''.toUpperCase();
const textParts = text.toUpperCase().split(' ');
let idx = itemData.indexOf(textParts);
let shouldInclude = true;
for (let i = 0; i < textParts.length; i++) {
const part = textParts[i];
shouldInclude = shouldInclude && (itemData.indexOf(part) > -1);
if (!shouldInclude) {
return false;
}
}
return true;
});
this.setState({
notifications: newData,
value: text
});
}
SearchBar:
<SearchBar
lightTheme
value= {value}
inputStyle={{margin: 0}}
placeholder="Search here...."
onChangeText={(text)=>{this.handleSearch(text,false)}}
onTouchStart={()=>{this.renderDropdown(true)}}
/>
FlatList:
render() {
return
<FlatList
keyExtractor={(item, id) => item.id}
data={notifications}
renderItem={this.renderItem.bind(this)}
/>
}
render item:
renderItem({item}) {
return <Item item={item}
onPress = {()=> {this.goToDetails()}
/>
}
Item Component:
export default class Item extends Component {
handlePress() {
return this.props.onPress();
}
render() {
const {
row,
textContainer,
id,
ordername,
desc,
seperator
} = styles;
const {
item
} = this.props;
const {
Id,
OrderName,
Desc
} = item;
return (
<View>
<TouchableOpacity onPress = {
() => {this.handlePress()>
<View style = {seperator}>
</View>
<View style = {row}>
<View style = {textContainer}>
<Text style = {id}>{Id}</Text>
<Text style = {ordername}>{OrderName}</Text>
<Text style = {desc}>{Desc}</Text>
</View>
<View style = {seperator}>
</View>
</View>
</TouchableOpacity>
</View>
)
}
}
const styles = StyleSheet.create({
row: {
flexDirection: 'row',
alignItems: 'flex-start',
marginLeft: 15,
marginRight: 15,
backgroundColor: 'white',
paddingTop: 5,
paddingBottom: 10
},
textContainer: {
flex: 3,
paddingLeft: 5,
fontWeight: 'bold',
fontSize: 8
},
id: {
flex: 1,
fontSize: 14,
color: 'black',
fontSize: 15
},
ordername: {
flex: 1,
fontSize: 14,
color: 'blue',
fontSize: 15
},
desc: {
flex: 1,
fontSize: 14,
color: 'blue',
fontSize: 15
},
seperator: {
padding: 20
}
})
I want a Search Filter Highlight like below where as soon as i start typing in the searchBar the text in the FlatList has to be highlighted.
How can i implement this?? React native highlight word? Any function? Please help
解决方案
它看起来像SearcBar
存储为的值this.state.value
,将其传递给Item
并用于regex
拆分您的文本(用作Desc
参考,使用它适合您...)
renderItem({item}) {
return <Item
item={item}
onPress = {()=> {this.goToDetails()}
value={this.state.value}
/>
}
//Item.js
getHighlightedText = text => {
const { value } = this.props
const parts = text.split(new RegExp(`(${value})`, 'gi'));
return <Text style={desc}>
{parts.map(part => part.toLowerCase() === value.toLowerCase()
? <View style={{backgroundColor: 'red'}}>{part}</View>
: part)}
</Text>;
}
<View> {this.getHighlightedText(Desc)} </View>
推荐阅读
- database - 我无法更新数据库室中的项目列表并将该项目添加到收藏夹列表
- javascript - 窗口加载仅显示 1 件事
- bash - awk:第 1 行:在或附近出现语法错误,需要一些 bash 帮助
- c# - C# Automapper 如何使用嵌套映射创建列表?
- spring - 钥匙披风错误!登录用户没有电子邮件
- javascript - 如何为 ngrx 商店应用程序设置初始状态
- firebase - Flutter:获取当前登录 Firebase 用户以外的人的显示名称
- java - 在二叉搜索树中增加高度变量
- python - pytorch.load 并保存 - 尝试继续训练时加载 state_dict 时出错
- r - R:如果两行中的字符相等,则在另一列中赋值