index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import React, { Component } from 'react';
  2. import { Modal, Row, Col, Checkbox, Alert } from 'antd';
  3. import './index.less';
  4. class SimilarQuestionNo extends Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = { show: !!props.modal, loading: false };
  8. this.questionNos = [];
  9. }
  10. onConfirm() {
  11. const { questionNo } = this.state;
  12. if (!questionNo) {
  13. this.setState({ err: '请选择需要加载的题目' });
  14. return;
  15. }
  16. if (this.props.onConfirm && this.props.modal) {
  17. this.setState({ loading: true });
  18. this.props
  19. .onConfirm(questionNo)
  20. .then(() => {
  21. this.setState({ loading: false });
  22. this.onCancel();
  23. })
  24. .catch(e => {
  25. this.setState({ loading: false, err: e.message });
  26. });
  27. } else {
  28. this.onCancel();
  29. }
  30. }
  31. onCancel() {
  32. if (this.props.modal) this.setState({ show: false });
  33. if (this.props.onCancel) this.props.onCancel();
  34. }
  35. renderForm() {
  36. const { questionNos = [] } = this.props;
  37. const { questionNo } = this.state;
  38. return <div>
  39. {questionNos.map(row => {
  40. return <Row style={{ width: '100%' }}>
  41. <Col span={1}><Checkbox checked={questionNo && row.id === questionNo.id} onChange={(value) => {
  42. this.setState({ questionNo: value ? row : null });
  43. }} /></Col>
  44. <Col span={4} offset={1}>
  45. {row.title}
  46. </Col>
  47. <Col span={16} offset={1}>
  48. {row.question.description}
  49. </Col>
  50. </Row>;
  51. })}
  52. </div>;
  53. }
  54. render() {
  55. const { modal, title, confirmText = '确定', cancelText = '取消' } = this.props;
  56. const { show, loading, err } = this.state;
  57. return modal ? (
  58. <Modal
  59. title={title}
  60. visible={show}
  61. okText={confirmText}
  62. cancelText={cancelText}
  63. confirmLoading={loading}
  64. onOk={() => this.onConfirm()}
  65. onCancel={() => this.onCancel()}
  66. >
  67. {err && <Alert type="error" showIcon message={err} closable onClose={() => this.setState({ err: '' })} />}
  68. {this.renderForm()}
  69. </Modal>
  70. ) : (<div>{this.renderForm()}</div>);
  71. }
  72. }
  73. export default SimilarQuestionNo;