PickerSearch.js 4.55 KB
Newer Older
Giang Tran committed
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
// @flow
import React, {Component} from 'react';
import {StyleSheet, Text, TouchableOpacity, View} from 'react-native';
import AntDesign from 'react-native-vector-icons/AntDesign';
import R from '../../assets/R';
import {HEIGHTXD, WIDTHXD, getFontXD} from '../../Config/Functions';
import ModalSearch from './ModalSearch';

/**
 * This Function to show piker search with list date (for example [{name:'Picker1'},{name:'Picker2}])
 * This show view with input when touch it show a modal search
 * @callback onValueChange return value of item you choice
 * @param title title of popup search
 * @param containerStyle custom containerStyle of view
 * @param data style value of date
 * @param width width of picker
 * @param date value of date you choice
 * @param value value of date you choice
 * @param searchPickerStyle custom searchPickerStyle of TouchableOpacity
 * @param placeholder value of placeholder
 * @param textStyle custom text in Text
 * other you can make minDate,maxDate... with props of libary react-native-datepicker
 */
class PickerSearch extends Component {
  constructor(props) {
    super(props);
    this.state = {
      value: '',
      item: null,
      reRender: false,
    };
  }

  componentDidMount = async () => {
    const {value} = this.props;
    if (value) {
      this.setState({value});
    }
  };

  componentWillReceiveProps(nextProps) {
    if (
      nextProps.data !== this.props.data &&
      (nextProps.data.length > 0 || nextProps.isNeedClearData)
    ) {
      this.setState({reRender: this.state.reRender});
    }
  }

  onChangeText = (text) => {
    this.ModalSearch && this.ModalSearch.changeName(text);
  };

  render() {
    const {value, item} = this.state;
    const {
      textStyle,
      containerStyle,
      placeholder,
      width,
      data,
      title,
      onValueChange,
      findData,
      disabled,
      height,
      tempDisabled,
      onShowPorm,
      style,
    } = this.props;
    const choosed =
      this.props.value && this.props.value != '' && this.props.value !== null
        ? true
        : false;

    return (
      <View style={[styles.container, style ? style : {}]}>
        <TouchableOpacity
          disabled={disabled}
          onPress={() => {
            tempDisabled
              ? onShowPorm && onShowPorm()
              : this.ModalSearch.setModalVisible(true);
          }}
          style={[
            styles.searchPicker,
            containerStyle !== null && containerStyle,
            width && {width},
            height && {height},
          ]}>
          {value === '' && this.props.value && this.props.value === '' ? (
            <Text>{placeholder !== null && placeholder}</Text>
          ) : (
            <Text
              numberOfLines={1}
              style={[
                styles.textStyle,
                textStyle !== null && textStyle,
                width && {width: width - WIDTHXD(150)},
              ]}>
              {this.props.value !== null && this.props.value}
            </Text>
          )}

          <TouchableOpacity
            onPress={() => {
              this.setState({value: '', item: null});
              onValueChange && onValueChange('', null);
            }}
            hitSlop={{left: 20, top: 20, right: 20, bottom: 20}}
            disabled={!choosed || disabled}>
            <AntDesign
              name={!choosed || disabled ? 'search1' : 'close'}
              size={WIDTHXD(43)}
              color={R.colors.iconGray}
            />
          </TouchableOpacity>
        </TouchableOpacity>

        <ModalSearch
          ref={(ref) => {
            this.ModalSearch = ref;
          }}
          data={data}
          isNeedClearData={this.props.isNeedClearData}
          findData={findData !== null && findData}
          onValueChange={(Value, item) => {
            this.setState({value: Value, item: item});
            onValueChange && onValueChange(item.value, item);
          }}
          title={title}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    borderRadius: HEIGHTXD(8),
    flexDirection: 'row',
    alignItems: 'center',
    width: WIDTHXD(960),
  },
  searchPicker: {
    width: WIDTHXD(960),
    borderRadius: HEIGHTXD(20),
    paddingHorizontal: WIDTHXD(36),
    justifyContent: 'space-between',
    flexDirection: 'row',
    alignItems: 'center',
    height: HEIGHTXD(99),
    borderWidth: 0.3,
    borderColor: R.colors.iconGray,
    color: R.colors.black0,
  },
  textStyle: {
    fontFamily: R.fonts.RobotoRegular,
    width: WIDTHXD(800),
    fontSize: getFontXD(42),
  },
});

export default PickerSearch;