/*
* @Author: Rogan
* @format
*
* @Description: 搜索页面
* @Date: 2019-05-09 19:24:16
* @LastEditTime: 2019-07-28 22:04:25
*/
import * as React from 'react';
import {
Text,
View,
FlatList,
TouchableOpacity,
StyleSheet
} from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import FCButton from '../Button';
import FCSearchBar from './searchBar';
import baseI18n from '../../utils/i18n'
// interface Props {
// navigation: any
// }
// interface State {
// data: string[],
// keyword: string
// }
const saveKey = 'Search_Contact';
export default class FCSearchPage extends React.PureComponent {
static navigationOptions = { header: null };
state = {
data: [],
keyword: this.props.navigation.getParam('value'),
placeholder: ''
};
constructor(props) {
super(props);
const { placeholder } = this.props.navigation.state.params;
if (placeholder !== null && placeholder !== undefined) {
this.setState({
placeholder
});
}
}
componentDidMount() {
this.get();
}
componentWillUnmount() {
// const { data } = this.state
// this.save(data)
}
onClearAll = () => {
this.setState({ data: [] });
this.save([]);
};
onItem = item => {
const callBack = this.props.navigation.getParam('callBack', () => { });
callBack && callBack(item);
this.props.navigation.pop();
};
onDeleteItem = (item, index) => {
const { data } = this.state;
data.splice(index, 1);
this.setState({ data: [...data] });
this.save(data);
};
onChangeText = keyword => {
this.setState({ keyword });
};
onSubmitEditing = () => {
const { data, keyword } = this.state;
const callBack = this.props.navigation.getParam('callBack', () => { });
const result = [keyword, ...data];
const s1 = new Set(result);
if (keyword.length === 0) {}
this.setState({ data: Array.from(s1) }, () => {
if(keyword.length > 0){
this.save(Array.from(s1));
}
callBack && callBack(keyword);
this.props.navigation.pop();
});
};
render() {
const { data, keyword, placeholder } = this.state;
return (
{baseI18n.t('历史记录')}
{baseI18n.t('清空历史')}
);
}
renderItem = ({ item, index }) => (
{item}
);
keyExtractor = (item, index) => item + index;
save = value => {
AsyncStorage.setItem(saveKey, JSON.stringify(value));
};
get = () => {
AsyncStorage.getItem(saveKey).then(value => {
this.setState({ data: value ? JSON.parse(value) : [] });
})
.catch(() => {
});
};
onBack = () => {
const callBack = this.props.navigation.getParam('callBack', () => { });
callBack && callBack('');
this.props.navigation.pop();
};
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 25,
backgroundColor: '#f9f9f9'
},
header: {
height: 40,
flexDirection: 'row',
alignItems: 'center',
marginTop: 20,
marginHorizontal: 15,
justifyContent: 'space-between'
},
headerTitle: { fontSize: 16 },
headerButton: {
backgroundColor: 'transparent',
paddingHorizontal: 0
},
headerButtonText: {
fontSize: 14,
color: '#919199'
},
searchBar: {
backgroundColor: '#f9f9f9',
padding: 0
},
searchInput: {
backgroundColor: '#fff',
borderRadius: 10
},
item: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingVertical: 10,
marginTop: 10
},
itemText: { color: '#3A3A3D' },
itemDeleteButton: {
height: 20,
width: 20,
backgroundColor: 'transparent',
borderRadius: 10
}
});