SearchProject.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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, Toast, Provider, 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. disabled_ids: this.props.navigation.state.params.disabled_ids ?? [], // 加载已添加的项目
  22. };
  23. }
  24. componentDidMount() {
  25. this._fetchData();
  26. }
  27. _fetchData = () => {
  28. this.props.dispatch({
  29. type: 'customer/fetchReportDict',
  30. });
  31. };
  32. onChange = (checked, item) => {
  33. let {ids, names} = this.state;
  34. if (checked) {
  35. ids.push(item.id);
  36. names.push(item.name);
  37. } else {
  38. ids.splice(ids.indexOf(item.id), 1);
  39. names.splice(names.indexOf(item.name), 1);
  40. }
  41. this.setState({names, ids});
  42. };
  43. onSave = () => {
  44. if (!this.state.ids.length) {
  45. Toast.info('请选择项目', 1);
  46. return false;
  47. }
  48. const name = this.state.names.join(',');
  49. this.state.callback(this.state.ids, name ? name : '请选择');
  50. this.props.navigation.goBack();
  51. };
  52. render() {
  53. const {projectDict} = this.props.customer;
  54. return (
  55. <View style={{flex: 1, backgroundColor: '#fff'}}>
  56. <Provider>
  57. <ScrollView>
  58. <List style={{flex: 1}}>
  59. {projectDict.map((item, index) => {
  60. // 判断是否选择过了
  61. const checked = this.state.ids.indexOf(item.id) > -1 ? true : false;
  62. const disabled = this.state.disabled_ids.indexOf(item.id) > -1 ? true : false;
  63. return (<CheckboxItem
  64. key={index}
  65. checked={checked}
  66. disabled={disabled}
  67. onChange={event => {
  68. this.onChange(event.target.checked, item);
  69. }}
  70. >
  71. {item.name}
  72. </CheckboxItem>
  73. );
  74. })}
  75. </List>
  76. </ScrollView>
  77. <Button
  78. type="primary"
  79. onPress={() => this.onSave()}
  80. style={ComponentsStyles.button}><Text
  81. style={{color: '#fff'}}>确定</Text>
  82. </Button>
  83. </Provider>
  84. </View>
  85. );
  86. }
  87. }
  88. export default SearchProject;