All files / searchbar SearchBar-android.js

63.64% Statements 21/33
100% Branches 6/6
40% Functions 6/15
63.64% Lines 21/33
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192                                2x 2x 2x                                                       2x 2x       1x 1x       1x 1x       15x 15x                                     19x 19x 19x   19x             19x             19x                                                                               2x                                   2x           1x       2x                                                
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import {
  Dimensions,
  StyleSheet,
  View,
  ActivityIndicator,
  Text,
} from 'react-native';
 
import ViewPropTypes from '../config/ViewPropTypes';
import nodeType from '../helpers/nodeType';
import Input from '../input/Input';
import Icon from '../icons/Icon';
import renderNode from '../helpers/renderNode';
 
const SCREEN_WIDTH = Dimensions.get('window').width;
const ANDROID_GRAY = 'rgba(0, 0, 0, 0.54)';
const defaultSearchIcon = {
  type: 'material-community',
  size: 25,
  color: ANDROID_GRAY,
  name: 'magnify',
};
 
class SearchBar extends Component {
  focus = () => {
    this.input.focus();
  };
 
  blur = () => {
    this.input.blur();
  };
 
  clear = () => {
    this.input.clear();
    this.onChangeText('');
    this.props.onClear();
  };
 
  cancel = () => {
    this.blur();
    this.props.onCancel();
  };
 
  onFocus = () => {
    this.props.onFocus();
    this.setState({ hasFocus: true });
  };
 
  onBlur = () => {
    this.props.onBlur();
    this.setState({ hasFocus: false });
  };
 
  onChangeText = text => {
    this.props.onChangeText(text);
    this.setState({ isEmpty: text === '' });
  };
 
  constructor(props) {
    super(props);
    this.state = {
      hasFocus: false,
      isEmpty: true,
    };
  }
 
  render() {
    const {
      clearIcon,
      containerStyle,
      leftIconContainerStyle,
      rightIconContainerStyle,
      inputContainerStyle,
      inputStyle,
      searchIcon,
      cancelIcon,
      showLoading,
      loadingProps,
      ...attributes
    } = this.props;
    const { hasFocus, isEmpty } = this.state;
    const { style: loadingStyle, ...otherLoadingProps } = loadingProps;
 
    const defaultCancelIcon = {
      type: 'material-community',
      size: 25,
      color: ANDROID_GRAY,
      name: 'arrow-left',
      onPress: this.cancel,
    };
    const defaultClearIcon = {
      type: 'material-community',
      name: 'close',
      size: 25,
      color: ANDROID_GRAY,
      onPress: this.clear,
    };
    return (
      <View style={[styles.container, containerStyle]}>
        <Input
          {...attributes}
          onFocus={this.onFocus}
          onBlur={this.onBlur}
          onChangeText={this.onChangeText}
          ref={input => (this.input = input)}
          inputStyle={[styles.input, inputStyle]}
          inputContainerStyle={[styles.inputContainer, inputContainerStyle]}
          leftIcon={
            hasFocus
              ? renderNode(Icon, cancelIcon, defaultCancelIcon)
              : renderNode(Icon, searchIcon, defaultSearchIcon)
          }
          leftIconContainerStyle={[
            styles.leftIconContainerStyle,
            leftIconContainerStyle,
          ]}
          rightIcon={
            <View style={{ flexDirection: 'row' }}>
              {showLoading && (
                <ActivityIndicator
                  style={[{ marginRight: 5 }, loadingStyle]}
                  {...otherLoadingProps}
                />
              )}
              {!isEmpty && renderNode(Icon, clearIcon, defaultClearIcon)}
            </View>
          }
          rightIconContainerStyle={[
            styles.rightIconContainerStyle,
            rightIconContainerStyle,
          ]}
        />
      </View>
    );
  }
}
 
SearchBar.propTypes = {
  clearIcon: nodeType,
  searchIcon: nodeType,
  cancelIcon: nodeType,
  loadingProps: PropTypes.object,
  showLoading: PropTypes.bool,
  containerStyle: ViewPropTypes.style,
  leftIconContainerStyle: ViewPropTypes.style,
  rightIconContainerStyle: ViewPropTypes.style,
  inputContainerStyle: ViewPropTypes.style,
  inputStyle: Text.propTypes.style,
  onClear: PropTypes.func,
  onCancel: PropTypes.func,
  onFocus: PropTypes.func,
  onBlur: PropTypes.func,
  onChangeText: PropTypes.func,
};
 
SearchBar.defaultProps = {
  loadingProps: {},
  showLoading: false,
  onClear: () => null,
  onCancel: () => null,
  onFocus: () => null,
  onBlur: () => null,
  onChangeText: () => null,
};
 
const styles = StyleSheet.create({
  container: {
    backgroundColor: 'white',
    width: SCREEN_WIDTH,
    paddingTop: 8,
    paddingBottom: 8,
  },
  input: {
    marginLeft: 24,
    marginRight: 8,
  },
  inputContainer: {
    borderBottomWidth: 0,
    width: SCREEN_WIDTH,
  },
  rightIconContainerStyle: {
    marginRight: 8,
  },
  leftIconContainerStyle: {
    marginLeft: 8,
  },
});
 
export default SearchBar;