|
@@ -1,32 +1,63 @@
|
1
|
|
-import React, { useState } from 'react';
|
2
|
|
-import { Button, Row, Col, Checkbox, Radio, Input, Space } from 'antd';
|
3
|
|
-import WangEditor from '@/components/WangEditor';
|
|
1
|
+import React, { useEffect, useState } from 'react';
|
|
2
|
+import { Button, Row, Col, Checkbox, Radio, Input, Space, notification } from 'antd';
|
|
3
|
+// import WangEditor from '@/components/WangEditor';
|
|
4
|
+
|
|
5
|
+const RedRequire = () => <i style={{ color: 'red' }}>*</i>
|
|
6
|
+
|
|
7
|
+const defaultData = {
|
|
8
|
+ title: undefined,
|
|
9
|
+ question: undefined,
|
|
10
|
+ answerType: 'radio',
|
|
11
|
+ correctAnswers: undefined,
|
|
12
|
+ optionA: undefined,
|
|
13
|
+ optionB: undefined,
|
|
14
|
+ optionC: undefined,
|
|
15
|
+ optionD: undefined,
|
|
16
|
+}
|
4
|
17
|
|
5
|
18
|
export default (props) => {
|
6
|
|
- const [formData, setFormData] = useState({
|
7
|
|
- question: undefined,
|
8
|
|
- answerType: 'radio',
|
9
|
|
- correctAnswers: undefined,
|
10
|
|
- optionA: undefined,
|
11
|
|
- optionB: undefined,
|
12
|
|
- optionC: undefined,
|
13
|
|
- optionD: undefined,
|
14
|
|
- });
|
15
|
|
-
|
16
|
|
- // const [answerTypeValue, setAnswerTypeValue] = useState([])
|
|
19
|
+ const [formData, setFormData] = useState(defaultData);
|
|
20
|
+
|
|
21
|
+ const [correctAnswers, setCorrectAnswers] = useState([])
|
17
|
22
|
|
18
|
23
|
const handleFormChange = (field) => (e) => {
|
19
|
24
|
const value = e && e.target ? e.target.value : e;
|
20
|
|
- console.log('------111--->', field, e, value);
|
21
|
25
|
setFormData({
|
22
|
26
|
...formData,
|
23
|
27
|
[field]: value,
|
24
|
28
|
});
|
25
|
29
|
};
|
26
|
30
|
|
27
|
|
- const handleCorrectAnswers = () => {};
|
|
31
|
+ const handleCorrectAnswers = (e) => {
|
|
32
|
+ if (formData.answerType === 'radio' || formData.answerType === 'switch') {
|
|
33
|
+ setCorrectAnswers([e.target.value])
|
|
34
|
+ } else {
|
|
35
|
+ setCorrectAnswers(e && e.length > 0 ? e : [])
|
|
36
|
+ }
|
|
37
|
+ };
|
28
|
38
|
|
29
|
|
- const handleSubmit = () => {};
|
|
39
|
+ const handleSubmit = () => {
|
|
40
|
+ if (!formData.title) {
|
|
41
|
+ notification.warning({ message: '请填写考题' })
|
|
42
|
+ return
|
|
43
|
+ }
|
|
44
|
+ if (!formData.optionA && !formData.optionB && !formData.optionC && !formData.optionD) {
|
|
45
|
+ notification.warning({ message: '请填写选项' })
|
|
46
|
+ return
|
|
47
|
+ }
|
|
48
|
+ if (!formData.answerType) {
|
|
49
|
+ notification.warning({ message: '请设置题型' })
|
|
50
|
+ return
|
|
51
|
+ }
|
|
52
|
+ if (!formData.correctAnswers) {
|
|
53
|
+ notification.warning({ message: '请设置正确答案' })
|
|
54
|
+ return
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ if (props.onSubmit) {
|
|
58
|
+ props.onSubmit(formData)
|
|
59
|
+ }
|
|
60
|
+ };
|
30
|
61
|
|
31
|
62
|
const handleCancel = () => {
|
32
|
63
|
if (props.onCancel) {
|
|
@@ -34,104 +65,121 @@ export default (props) => {
|
34
|
65
|
}
|
35
|
66
|
};
|
36
|
67
|
|
37
|
|
- const answerTypeDict = [
|
38
|
|
- { value: 'switch', label: '判断' },
|
39
|
|
- { value: 'radio', label: '单选' },
|
40
|
|
- { value: 'checkbox', label: '多选' },
|
41
|
|
- ];
|
|
68
|
+ const answerTypeDict = Object.keys(props.answerTypeDict).map((key) => ({ value: key, label: props.answerTypeDict[key] }));
|
|
69
|
+
|
|
70
|
+ useEffect(() => {
|
|
71
|
+ formData.correctAnswers = (correctAnswers || []).join(',')
|
|
72
|
+ }, [correctAnswers, formData])
|
|
73
|
+
|
|
74
|
+ useEffect(() => {
|
|
75
|
+ setFormData(props.value || defaultData)
|
|
76
|
+ if (props.value?.correctAnswers) {
|
|
77
|
+ setCorrectAnswers(props.value.correctAnswers.split(','))
|
|
78
|
+ }
|
|
79
|
+ }, [props.value])
|
42
|
80
|
|
43
|
81
|
return (
|
44
|
82
|
<div>
|
45
|
83
|
<Space size="large" direction="vertical" style={{ width: '100%' }}>
|
46
|
84
|
<section>
|
47
|
|
- <h3>试题:</h3>
|
48
|
|
- <WangEditor value={formData.question} onChange={handleFormChange('question')} />
|
|
85
|
+ <h3><RedRequire />题型:</h3>
|
|
86
|
+ <Radio.Group
|
|
87
|
+ options={answerTypeDict}
|
|
88
|
+ value={formData.answerType}
|
|
89
|
+ onChange={handleFormChange('answerType')}
|
|
90
|
+ />
|
49
|
91
|
</section>
|
50
|
92
|
|
51
|
93
|
<section>
|
52
|
|
- <h3>选项:</h3>
|
|
94
|
+ <h3><RedRequire />考题:</h3>
|
|
95
|
+ <Input placeholder="不能超过 300 字" value={formData.title} onChange={handleFormChange('title')} />
|
|
96
|
+ </section>
|
|
97
|
+
|
|
98
|
+ {/* <section>
|
|
99
|
+ <h3>详细内容:</h3>
|
|
100
|
+ <WangEditor
|
|
101
|
+ height={120}
|
|
102
|
+ value={formData.question}
|
|
103
|
+ onChange={handleFormChange('question')}
|
|
104
|
+ />
|
|
105
|
+ </section> */}
|
|
106
|
+
|
|
107
|
+ <section>
|
|
108
|
+ <h3><RedRequire />选项:</h3>
|
53
|
109
|
<Space size="large" direction="vertical" style={{ width: '100%' }}>
|
54
|
110
|
<Row gutter="24">
|
55
|
111
|
<Col span={12}>
|
56
|
|
- <Input addonBefore="A" onChange={handleFormChange('optionA')} />
|
57
|
|
- </Col>
|
58
|
|
- <Col span={12}>
|
59
|
|
- <Input addonBefore="B" onChange={handleFormChange('optionB')} />
|
60
|
|
- </Col>
|
61
|
|
- </Row>
|
62
|
|
- <Row gutter="24">
|
63
|
|
- <Col span={12}>
|
64
|
|
- <Input addonBefore="C" onChange={handleFormChange('optionC')} />
|
|
112
|
+ <Input addonBefore="A" value={formData.optionA} onChange={handleFormChange('optionA')} />
|
65
|
113
|
</Col>
|
66
|
114
|
<Col span={12}>
|
67
|
|
- <Input addonBefore="D" onChange={handleFormChange('optionD')} />
|
|
115
|
+ <Input addonBefore="B" value={formData.optionB} onChange={handleFormChange('optionB')} />
|
68
|
116
|
</Col>
|
69
|
117
|
</Row>
|
|
118
|
+ {
|
|
119
|
+ formData.answerType !== 'switch' && (
|
|
120
|
+ <Row gutter="24">
|
|
121
|
+ <Col span={12}>
|
|
122
|
+ <Input addonBefore="C" value={formData.optionC} onChange={handleFormChange('optionC')} />
|
|
123
|
+ </Col>
|
|
124
|
+ <Col span={12}>
|
|
125
|
+ <Input addonBefore="D" value={formData.optionD} onChange={handleFormChange('optionD')} />
|
|
126
|
+ </Col>
|
|
127
|
+ </Row>
|
|
128
|
+ )
|
|
129
|
+ }
|
70
|
130
|
</Space>
|
71
|
131
|
</section>
|
72
|
132
|
|
73
|
133
|
<section>
|
74
|
|
- <Row gutter="24">
|
75
|
|
- <Col span={12}>
|
76
|
|
- <h3>题型:</h3>
|
77
|
|
- <Radio.Group
|
78
|
|
- options={answerTypeDict}
|
79
|
|
- value={formData.answerType}
|
80
|
|
- onChange={handleFormChange('answerType')}
|
81
|
|
- />
|
82
|
|
- </Col>
|
83
|
|
- <Col span={12}>
|
84
|
|
- <h3>正确答案:</h3>
|
85
|
|
- {formData.answerType === 'switch' && (
|
86
|
|
- <Radio.Group onChange={handleCorrectAnswers} style={{ width: '100%' }}>
|
87
|
|
- <Row gutter="12">
|
88
|
|
- <Col span={6}>
|
89
|
|
- <Radio value="A">是</Radio>
|
90
|
|
- </Col>
|
91
|
|
- <Col span={6}>
|
92
|
|
- <Radio value="B">否</Radio>
|
93
|
|
- </Col>
|
94
|
|
- </Row>
|
95
|
|
- </Radio.Group>
|
96
|
|
- )}
|
97
|
|
- {formData.answerType === 'radio' && (
|
98
|
|
- <Radio.Group onChange={handleCorrectAnswers} style={{ width: '100%' }}>
|
99
|
|
- <Row gutter="12">
|
100
|
|
- <Col span={6}>
|
101
|
|
- <Radio value="A">A</Radio>
|
102
|
|
- </Col>
|
103
|
|
- <Col span={6}>
|
104
|
|
- <Radio value="B">B</Radio>
|
105
|
|
- </Col>
|
106
|
|
- <Col span={6}>
|
107
|
|
- <Radio value="C">C</Radio>
|
108
|
|
- </Col>
|
109
|
|
- <Col span={6}>
|
110
|
|
- <Radio value="D">D</Radio>
|
111
|
|
- </Col>
|
112
|
|
- </Row>
|
113
|
|
- </Radio.Group>
|
114
|
|
- )}
|
115
|
|
- {formData.answerType === 'checkbox' && (
|
116
|
|
- <Checkbox.Group onChange={handleCorrectAnswers} style={{ width: '100%' }}>
|
117
|
|
- <Row gutter="12">
|
118
|
|
- <Col span={6}>
|
119
|
|
- <Checkbox value="A">A</Checkbox>
|
120
|
|
- </Col>
|
121
|
|
- <Col span={6}>
|
122
|
|
- <Checkbox value="B">B</Checkbox>
|
123
|
|
- </Col>
|
124
|
|
- <Col span={6}>
|
125
|
|
- <Checkbox value="C">C</Checkbox>
|
126
|
|
- </Col>
|
127
|
|
- <Col span={6}>
|
128
|
|
- <Checkbox value="D">D</Checkbox>
|
129
|
|
- </Col>
|
130
|
|
- </Row>
|
131
|
|
- </Checkbox.Group>
|
132
|
|
- )}
|
133
|
|
- </Col>
|
134
|
|
- </Row>
|
|
134
|
+ <h3><RedRequire />正确答案:</h3>
|
|
135
|
+ {formData.answerType === 'switch' && (
|
|
136
|
+ <Radio.Group value={correctAnswers[0]} onChange={handleCorrectAnswers} style={{ width: '100%' }}>
|
|
137
|
+ <Row gutter="12">
|
|
138
|
+ <Col span={6}>
|
|
139
|
+ <Radio value="A">是</Radio>
|
|
140
|
+ </Col>
|
|
141
|
+ <Col span={6}>
|
|
142
|
+ <Radio value="B">否</Radio>
|
|
143
|
+ </Col>
|
|
144
|
+ </Row>
|
|
145
|
+ </Radio.Group>
|
|
146
|
+ )}
|
|
147
|
+ {formData.answerType === 'radio' && (
|
|
148
|
+ <Radio.Group value={correctAnswers[0]} onChange={handleCorrectAnswers} style={{ width: '100%' }}>
|
|
149
|
+ <Row gutter="12">
|
|
150
|
+ <Col span={6}>
|
|
151
|
+ <Radio value="A">A</Radio>
|
|
152
|
+ </Col>
|
|
153
|
+ <Col span={6}>
|
|
154
|
+ <Radio value="B">B</Radio>
|
|
155
|
+ </Col>
|
|
156
|
+ <Col span={6}>
|
|
157
|
+ <Radio value="C">C</Radio>
|
|
158
|
+ </Col>
|
|
159
|
+ <Col span={6}>
|
|
160
|
+ <Radio value="D">D</Radio>
|
|
161
|
+ </Col>
|
|
162
|
+ </Row>
|
|
163
|
+ </Radio.Group>
|
|
164
|
+ )}
|
|
165
|
+ {formData.answerType === 'checkbox' && (
|
|
166
|
+ <Checkbox.Group value={correctAnswers} onChange={handleCorrectAnswers} style={{ width: '100%' }}>
|
|
167
|
+ <Row gutter="12">
|
|
168
|
+ <Col span={6}>
|
|
169
|
+ <Checkbox value="A">A</Checkbox>
|
|
170
|
+ </Col>
|
|
171
|
+ <Col span={6}>
|
|
172
|
+ <Checkbox value="B">B</Checkbox>
|
|
173
|
+ </Col>
|
|
174
|
+ <Col span={6}>
|
|
175
|
+ <Checkbox value="C">C</Checkbox>
|
|
176
|
+ </Col>
|
|
177
|
+ <Col span={6}>
|
|
178
|
+ <Checkbox value="D">D</Checkbox>
|
|
179
|
+ </Col>
|
|
180
|
+ </Row>
|
|
181
|
+ </Checkbox.Group>
|
|
182
|
+ )}
|
135
|
183
|
</section>
|
136
|
184
|
|
137
|
185
|
<section>
|