index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import React, { Component } from 'react';
  2. import { Form, Modal, Alert } from 'antd';
  3. import './index.less';
  4. import Select from '@src/components/Select';
  5. import QuestionNoList from '../QuestionNoList';
  6. class Association extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = { ids: this.props.ids, nos: this.props.nos, show: !!props.modal, loading: false, err: '' };
  10. this.questionNos = [];
  11. }
  12. onConfirm() {
  13. this.props.form.validateFields((err, fieldsValue) => {
  14. if (err) {
  15. return;
  16. }
  17. if (this.props.onConfirm && this.props.modal) {
  18. this.setState({ loading: true });
  19. this.props
  20. .onConfirm(fieldsValue)
  21. .then(() => {
  22. this.setState({ loading: false });
  23. this.onCancel();
  24. })
  25. .catch(e => {
  26. this.setState({ loading: false, err: e.message });
  27. });
  28. } else {
  29. this.onCancel();
  30. }
  31. });
  32. }
  33. onCancel() {
  34. if (this.props.modal) this.setState({ show: false });
  35. if (this.props.onCancel) this.props.onCancel();
  36. }
  37. renderForm() {
  38. const { getFieldDecorator, setFieldsValue } = this.props.form;
  39. const { field = 'questionNoIds' } = this.props;
  40. return <Form>
  41. <Form.Item>
  42. {getFieldDecorator(field)(
  43. <Select mode='tags' maxTagCount={200} notFoundContent={null} placeholder='输入题目id, 逗号分隔' tokenSeparators={[',', ',']} onChange={(values) => {
  44. this.setState({ nos: values });
  45. }} />,
  46. )}
  47. </Form.Item>
  48. <QuestionNoList module={this.props.module} loading={false} ids={this.state.ids} nos={this.state.nos} onChange={(questionNos) => {
  49. this.questionNos = questionNos;
  50. getFieldDecorator(field);
  51. const nos = questionNos.map(row => row.no);
  52. setFieldsValue({ [field]: nos });
  53. }} />
  54. </Form>;
  55. }
  56. render() {
  57. const { modal, title, confirmText = '确定', cancelText = '取消' } = this.props;
  58. const { show, loading, err } = this.state;
  59. return modal ? (
  60. <Modal
  61. title={title}
  62. visible={show}
  63. okText={confirmText}
  64. cancelText={cancelText}
  65. confirmLoading={loading}
  66. onOk={() => this.onConfirm()}
  67. onCancel={() => this.onCancel()}
  68. >
  69. {err && <Alert type="error" showIcon message={err} closable onClose={() => this.setState({ err: '' })} />}
  70. {this.renderForm()}
  71. </Modal>
  72. ) : (<div>{this.renderForm()}</div>);
  73. }
  74. }
  75. export default Form.create()(Association);