search_bar.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import React, {Component} from 'react';
  2. import {StyleSheet, Text, View, TextInput, PixelRatio} from 'react-native';
  3. //输入框组件
  4. export default class search_bar extends Component {
  5. //构造函数
  6. constructor(props) {
  7. super(props);
  8. this.state = {text: '', show: false};
  9. }
  10. //组件渲染
  11. render() {
  12. return (
  13. <View style={styles.flex}>
  14. <View style={[styles.flexDirection, styles.inputHeight]}>
  15. <View style={styles.flex}>
  16. <TextInput
  17. style={styles.input}
  18. returnKeyType="search"
  19. placeholder="请输入关键字"
  20. value={this.state.text}
  21. onChangeText={this.textChange.bind(this)}
  22. />
  23. </View>
  24. <View style={styles.btn}>
  25. <Text
  26. style={styles.search}
  27. onPress={() => this.props._searchData(this.state.text)}>
  28. 搜索
  29. </Text>
  30. </View>
  31. </View>
  32. {/*{this.state.show?*/}
  33. {/* <View style={styles.list}>*/}
  34. {/* <Text onPress={this.hideList.bind(this, this.state.text + '网站')}*/}
  35. {/* style={styles.item} numberOfLines={1}>{this.state.text}网站</Text>*/}
  36. {/* <Text onPress={this.hideList.bind(this, this.state.text + '文章')}*/}
  37. {/* style={styles.item} numberOfLines={1}>{this.state.text}文章</Text>*/}
  38. {/* <Text onPress={this.hideList.bind(this, this.state.text + '最新消息')}*/}
  39. {/* style={styles.item} numberOfLines={1}>{this.state.text}最新消息</Text>*/}
  40. {/* </View>*/}
  41. {/* : null*/}
  42. {/*}*/}
  43. </View>
  44. );
  45. }
  46. //输入框文字改变
  47. textChange(text) {
  48. this.setState({
  49. show: text != '' ? true : false,
  50. text: text,
  51. });
  52. }
  53. //隐藏自动提示列表
  54. hideList(text) {
  55. this.setState({
  56. show: false,
  57. text: text,
  58. });
  59. }
  60. //搜索按钮点击
  61. search() {
  62. alert('您输入的内容为:' + this.state.text);
  63. }
  64. }
  65. //样式定义
  66. const styles = StyleSheet.create({
  67. flex: {
  68. height: 50,
  69. flexGrow: 1,
  70. },
  71. flexDirection: {
  72. flexDirection: 'row',
  73. },
  74. topStatus: {
  75. marginTop: 15,
  76. },
  77. inputHeight: {
  78. height: 45,
  79. },
  80. input: {
  81. height: 45,
  82. borderWidth: 1,
  83. marginLeft: 5,
  84. paddingLeft: 5,
  85. borderColor: '#ccc',
  86. borderRadius: 4,
  87. },
  88. btn: {
  89. width: 55,
  90. marginLeft: -5,
  91. marginRight: 5,
  92. backgroundColor: '#23BEFF',
  93. height: 45,
  94. justifyContent: 'center',
  95. alignItems: 'center',
  96. },
  97. search: {
  98. color: '#fff',
  99. fontSize: 15,
  100. fontWeight: 'bold',
  101. },
  102. // list:{
  103. // marginTop: 1/PixelRatio.get(),
  104. // marginLeft:5,
  105. // marginRight:5,
  106. // height:200,
  107. // borderColor:'#ccc',
  108. // borderTopWidth: 1/PixelRatio.get(),
  109. // },
  110. // item:{
  111. // fontSize:16,
  112. // padding:5,
  113. // paddingTop:10,
  114. // paddingBottom:10,
  115. // borderWidth: 1/PixelRatio.get(),
  116. // borderColor:'#ddd',
  117. // borderTopWidth:0,
  118. // }
  119. });