Guest User

Untitled

a guest
Aug 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import React from 'react';
  2. import { StyleSheet, TextInput, View } from 'react-native';
  3. import PropTypes from 'prop-types';
  4.  
  5. export default class SearchInput extends React.Component {
  6.  
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. text: '',
  11. };
  12. }
  13.  
  14. handleChangeText = text => {
  15. this.setState({ text });
  16. };
  17.  
  18. handleSubmitEditing = () => {
  19. const { onSubmit } = this.props;
  20. const { text } = this.state;
  21.  
  22. if (!text) return;
  23.  
  24. onSubmit (text);
  25. this.setState({ text: '' });
  26. };
  27.  
  28. render() {
  29. const { placeholder } = this.props;
  30. const { text } = this.state;
  31.  
  32. return (
  33. <View style={styles.container}>
  34. <TextInput
  35. autoCorrect={false}
  36. value={text}
  37. placeholder={placeholder}
  38. placeholderTextColor="gray"
  39. underlineColorAndroid="transparent"
  40. textAlign="center"
  41. style={styles.textInput}
  42. clearButtonMode="always"
  43. onChangeText={this.handleChangeText}
  44. onSubmitEditing={this.handleSubmitEditing}
  45. />
  46. </View>
  47. );
  48. }
  49. }
  50.  
  51. SearchInput.propTypes = {
  52. onSubmit: PropTypes.func.isRequired,
  53. placeholder: PropTypes.string,
  54. };
  55.  
  56. SearchInput.defaultProps = {
  57. placeholder: '',
  58. };
  59.  
  60. const styles = StyleSheet.create({
  61. container: {
  62. height: 60,
  63. marginTop: 20,
  64. backgroundColor: 'rgba(255,255,255,0.5)',
  65. marginHorizontal: 20,
  66. paddingHorizontal: 10,
  67. borderRadius: 5,
  68. },
  69. textInput: {
  70. flex: 1,
  71. fontSize: 20,
  72. color: '#222',
  73. },
  74. });
Add Comment
Please, Sign In to add comment