Advertisement
mak1986

Untitled

Jan 3rd, 2020
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import {
  3.   NavigationScreenProps,
  4.   NavigationScreenProp,
  5.   SectionList
  6. } from 'react-navigation';
  7. import { View } from 'react-native-animatable';
  8. import { SearchBar, ListItem, Button, Text } from 'react-native-elements';
  9. import _ from 'lodash';
  10. import { Option } from '@shared/interfaces';
  11.  
  12. interface Props {
  13.   navigation: NavigationScreenProp<any, any>;
  14. }
  15.  
  16. interface State {
  17.   query: string;
  18.   selectedOptions: Array<Option>;
  19.   filteredOptions: Array<Option>;
  20.   options: Array<Option>;
  21. }
  22.  
  23. class SelectScreen extends React.Component<Props, State> {
  24.   static navigationOptions = ({ navigation }: NavigationScreenProps) => {
  25.     const { params } = navigation.state;
  26.     return params;
  27.   };
  28.   selectedOptionsTitle = 'Selected Options';
  29.   selectedOptionsSubtitle = '';
  30.   availableOptionsTitle = 'Available Options';
  31.   availableOptionsSubtitle = '';
  32.  
  33.   constructor(props: Props) {
  34.     super(props);
  35.  
  36.     const { navigation } = this.props;
  37.  
  38.     navigation.setParams({
  39.       headerTitle: navigation.getParam('title')
  40.     });
  41.  
  42.     this.state = {
  43.       query: '',
  44.       selectedOptions: [],
  45.       filteredOptions: [],
  46.       options: []
  47.     };
  48.   }
  49.  
  50.   componentDidMount() {
  51.     this.initializeOptions();
  52.     this.initializeSelectedOptions();
  53.     this.initializeFilteredOptions();
  54.     this.count++;
  55.   }
  56.  
  57.   initializeOptions = () => {
  58.     const { navigation } = this.props;
  59.     const options = navigation.getParam('options');
  60.  
  61.     console.log('initializeOptions');
  62.     console.log('options', options);
  63.     this.setState({
  64.       options
  65.     });
  66.   };
  67.  
  68.   initializeSelectedOptions = () => {
  69.     const { navigation } = this.props;
  70.     const options = navigation.getParam('options');
  71.     const selected = navigation.getParam('selectedOptions');
  72.     console.log('initializeSelectedOptions');
  73.     console.log('options', options, 'selected', selected);
  74.     this.setState({
  75.       selectedOptions: options.filter((option: Option) =>
  76.         selected.includes(option.value)
  77.       )
  78.     });
  79.   };
  80.  
  81.   initializeFilteredOptions = () => {
  82.     const { navigation } = this.props;
  83.     const options = navigation.getParam('options');
  84.  
  85.     console.log('initializeFilteredOptions');
  86.     this.setState({
  87.       filteredOptions: options
  88.     });
  89.   };
  90.  
  91.   filter = (query: string) => {
  92.     const { options } = this.state;
  93.     let filteredOptions = options;
  94.  
  95.     if (query != '') {
  96.       filteredOptions = options.filter(
  97.         option => option.display.toLowerCase().indexOf(query.toLowerCase()) > -1
  98.       );
  99.     }
  100.  
  101.     this.setState({ query, filteredOptions });
  102.   };
  103.  
  104.   deselect = (option: Option) => {
  105.     const { selectedOptions } = this.state;
  106.  
  107.     this.setState({
  108.       selectedOptions: _.reject(
  109.         selectedOptions,
  110.         selectedOption => selectedOption.value === option.value
  111.       )
  112.     });
  113.   };
  114.  
  115.   select = (option: Option) => {
  116.     const { selectedOptions } = this.state;
  117.  
  118.     const existingSelectedOption = _.find(selectedOptions, {
  119.       value: option.value
  120.     });
  121.  
  122.     if (!existingSelectedOption) {
  123.       this.setState({
  124.         selectedOptions: [option].concat(selectedOptions)
  125.       });
  126.     }
  127.   };
  128.  
  129.   renderSearch = () => {
  130.     const { query } = this.state;
  131.     return (
  132.       <SearchBar
  133.         placeholder="Search Here..."
  134.         onChangeText={this.filter}
  135.         value={query}
  136.       />
  137.     );
  138.   };
  139.  
  140.   renderSelectedOptions = ({ item }) => {
  141.     return (
  142.       <ListItem
  143.        title={item.display}
  144.        rightIcon={{ name: 'close', onPress: () => this.deselect(item) }}
  145.       />
  146.     );
  147.   };
  148.   renderOptions = ({ item }) => {
  149.     const { selectedOptions } = this.state;
  150.     const isSelected = _.find(selectedOptions, { value: item.value });
  151.     return (
  152.       <ListItem
  153.        key={item.value}
  154.        title={item.display}
  155.        titleStyle={{ color: isSelected ? '#ddd' : '#000' }}
  156.        bottomDivider
  157.        onPress={() => this.select(item)}
  158.       />
  159.     );
  160.   };
  161.  
  162.   renderSectionHeader = ({ section }) => {
  163.     return (
  164.       <ListItem
  165.         containerStyle={{ backgroundColor: '#eee' }}
  166.         title={section.title}
  167.         subtitle={section.subtitle === '' ? null : section.subtitle}
  168.       />
  169.     );
  170.   };
  171.   renderSectionFooter = ({ section }) => {
  172.     if (section.data.length == 0) {
  173.       if (section.title === this.selectedOptionsTitle) {
  174.         return <ListItem title={'No selected options.'} />;
  175.       } else if (section.title === this.availableOptionsTitle) {
  176.         return <ListItem title={'No available options.'} />;
  177.       }
  178.     }
  179.     return null;
  180.   };
  181.  
  182.   renderSubmitButton = () => {
  183.     const { navigation } = this.props;
  184.     const { selectedOptions } = this.state;
  185.     const onSelect = navigation.getParam('onSelect');
  186.  
  187.     return (
  188.       <Button
  189.        title="Select"
  190.        onPress={() => {
  191.           navigation.goBack();
  192.           onSelect(selectedOptions);
  193.         }}
  194.       />
  195.     );
  196.   };
  197.  
  198.   render() {
  199.     const { filteredOptions, selectedOptions } = this.state;
  200.  
  201.     return (
  202.       <View style={{ flex: 1 }}>
  203.         {this.renderSearch()}
  204.  
  205.         <SectionList
  206.          sections={[
  207.            {
  208.              title: this.selectedOptionsTitle,
  209.              subtitle: this.selectedOptionsSubtitle,
  210.              data: selectedOptions,
  211.              renderItem: this.renderSelectedOptions,
  212.              keyExtractor: item => item.value
  213.             },
  214.             {
  215.               title: this.availableOptionsTitle,
  216.               subtitle: this.availableOptionsSubtitle,
  217.               data: filteredOptions,
  218.               renderItem: this.renderOptions,
  219.               keyExtractor: item => item.value
  220.             }
  221.           ]}
  222.           renderSectionHeader={this.renderSectionHeader}
  223.           renderSectionFooter={this.renderSectionFooter}
  224.         />
  225.  
  226.         {this.renderSubmitButton()}
  227.       </View>
  228.     );
  229.   }
  230. }
  231.  
  232. export default SelectScreen;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement