SearchProject.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import React, {Component} from 'react';
  2. import {
  3. View,
  4. Text, StyleSheet, ScrollView,
  5. } from 'react-native';
  6. import {connect} from 'react-redux';
  7. import {Button, Checkbox, List,} from '@ant-design/react-native';
  8. import ComponentsStyles from '../../components/ComponentsStyles';
  9. const CheckboxItem = Checkbox.CheckboxItem;
  10. @connect(customer => ({...customer}))
  11. class SearchProject extends Component {
  12. // 多选项目
  13. constructor(props) {
  14. super(props);
  15. const checked_names = this.props.navigation.state.params.checked_names
  16. this.state = {
  17. callback: this.props.navigation.state.params.callback,
  18. ids: this.props.navigation.state.params.checked_ids ?? [], // 加载已选择的项目
  19. names: checked_names && checked_names !== '请选择' ? checked_names.split(',') : [],
  20. };
  21. }
  22. onChange = (checked, item) => {
  23. let {ids, names} = this.state;
  24. if (checked) {
  25. ids.push(item.id);
  26. names.push(item.name);
  27. } else {
  28. ids.splice(ids.indexOf(item.id), 1);
  29. names.splice(names.indexOf(item.name), 1);
  30. }
  31. this.setState({names, ids});
  32. };
  33. onSave = () => {
  34. const name = this.state.names.join(',')
  35. this.state.callback(this.state.ids, name ? name : '请选择')
  36. this.props.navigation.goBack();
  37. };
  38. render() {
  39. const {projectDict} = this.props.customer;
  40. return (
  41. <View style={{flex: 1, backgroundColor: '#fff'}}>
  42. <ScrollView>
  43. <List style={{flex: 1}}>
  44. {projectDict.map((item, index) => {
  45. // 判断是否选择过了
  46. const checked = this.state.ids.indexOf(item.id) > -1 ? true : false
  47. return (<CheckboxItem
  48. key={index}
  49. checked={checked}
  50. onChange={event => {
  51. this.onChange(event.target.checked, item);
  52. }}
  53. >
  54. {item.name}
  55. </CheckboxItem>
  56. );
  57. })}
  58. </List>
  59. </ScrollView>
  60. <Button
  61. type="primary"
  62. onPress={() => this.onSave()}
  63. style={ComponentsStyles.button}><Text
  64. style={{color: '#fff'}}>确定</Text>
  65. </Button>
  66. </View>
  67. );
  68. }
  69. }
  70. export default SearchProject;