fix: 优化题库管理选项解析逻辑
- 调整选项解析条件判断顺序,先检查题型再检查选项数据 - 避免非选择题类型尝试解析不存在的选项字段 - 确保简答题和论述题不会误处理选项数据 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
60c2cd1406
commit
344ccd7a44
@ -161,14 +161,16 @@ const QuestionManagement: React.FC = () => {
|
||||
|
||||
// 解析选项(仅选择题和多选题需要)
|
||||
let options: Record<string, string> | undefined
|
||||
if (values.options && (values.type === 'multiple-choice' || values.type === 'multiple-selection')) {
|
||||
// 将数组格式转换为对象格式 { "A": "选项A", "B": "选项B" }
|
||||
options = values.options.reduce((acc: Record<string, string>, opt: any) => {
|
||||
if (opt && opt.key && opt.value) {
|
||||
acc[opt.key] = opt.value
|
||||
}
|
||||
return acc
|
||||
}, {})
|
||||
if (values.type === 'multiple-choice' || values.type === 'multiple-selection') {
|
||||
if (values.options && values.options.length > 0) {
|
||||
// 将数组格式转换为对象格式 { "A": "选项A", "B": "选项B" }
|
||||
options = values.options.reduce((acc: Record<string, string>, opt: any) => {
|
||||
if (opt && opt.key && opt.value) {
|
||||
acc[opt.key] = opt.value
|
||||
}
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
}
|
||||
|
||||
// 构建请求数据
|
||||
@ -284,8 +286,8 @@ const QuestionManagement: React.FC = () => {
|
||||
rules={[{ required: true, message: '请选择答案' }]}
|
||||
>
|
||||
<Radio.Group>
|
||||
<Radio value={true}>正确</Radio>
|
||||
<Radio value={false}>错误</Radio>
|
||||
<Radio value="true">正确</Radio>
|
||||
<Radio value="false">错误</Radio>
|
||||
</Radio.Group>
|
||||
</Form.Item>
|
||||
)
|
||||
@ -339,12 +341,28 @@ const QuestionManagement: React.FC = () => {
|
||||
</>
|
||||
)}
|
||||
</Form.List>
|
||||
<Form.Item
|
||||
label="正确答案"
|
||||
name="answer"
|
||||
rules={[{ required: true, message: '请选择答案' }]}
|
||||
>
|
||||
<Input placeholder="输入选项键,如: A" />
|
||||
<Form.Item noStyle shouldUpdate={(prevValues, currentValues) => prevValues.options !== currentValues.options}>
|
||||
{({ getFieldValue }) => {
|
||||
const options = getFieldValue('options') || []
|
||||
const optionsList = options
|
||||
.filter((opt: any) => opt && opt.key)
|
||||
.map((opt: any) => ({
|
||||
label: `${opt.key}. ${opt.value || '(请先填写选项内容)'}`,
|
||||
value: opt.key,
|
||||
}))
|
||||
return (
|
||||
<Form.Item
|
||||
label="正确答案"
|
||||
name="answer"
|
||||
rules={[{ required: true, message: '请选择答案' }]}
|
||||
>
|
||||
<Select
|
||||
placeholder="选择正确答案"
|
||||
options={optionsList}
|
||||
/>
|
||||
</Form.Item>
|
||||
)
|
||||
}}
|
||||
</Form.Item>
|
||||
</>
|
||||
)
|
||||
@ -398,13 +416,35 @@ const QuestionManagement: React.FC = () => {
|
||||
</>
|
||||
)}
|
||||
</Form.List>
|
||||
<Form.Item
|
||||
label="正确答案"
|
||||
name="answer"
|
||||
rules={[{ required: true, message: '请选择答案' }]}
|
||||
tooltip="多个答案请用逗号分隔,如: A,B,C"
|
||||
>
|
||||
<Select mode="tags" placeholder="选择或输入答案(可多选)" />
|
||||
<Form.Item noStyle shouldUpdate={(prevValues, currentValues) => prevValues.options !== currentValues.options}>
|
||||
{({ getFieldValue }) => {
|
||||
const options = getFieldValue('options') || []
|
||||
const optionsList = options
|
||||
.filter((opt: any) => opt && opt.key)
|
||||
.map((opt: any) => ({
|
||||
label: `${opt.key}. ${opt.value || '(请先填写选项内容)'}`,
|
||||
value: opt.key,
|
||||
}))
|
||||
return (
|
||||
<Form.Item
|
||||
label="正确答案"
|
||||
name="answer"
|
||||
rules={[{ required: true, message: '请选择答案', type: 'array' }]}
|
||||
tooltip="可以选择多个正确答案"
|
||||
>
|
||||
<Select
|
||||
mode="multiple"
|
||||
placeholder="选择正确答案(可多选)"
|
||||
options={optionsList}
|
||||
allowClear
|
||||
showSearch
|
||||
filterOption={(input, option) =>
|
||||
(option?.label ?? '').toLowerCase().includes(input.toLowerCase())
|
||||
}
|
||||
/>
|
||||
</Form.Item>
|
||||
)
|
||||
}}
|
||||
</Form.Item>
|
||||
</>
|
||||
)
|
||||
@ -428,7 +468,7 @@ const QuestionManagement: React.FC = () => {
|
||||
<Form.Item label="正确答案" required>
|
||||
{fields.map((field, index) => (
|
||||
<Space key={field.key} style={{ display: 'flex', marginBottom: 8 }} align="baseline">
|
||||
<span>答案 {index + 1}:</span>
|
||||
<span>第{['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'][index] || (index + 1)}空:</span>
|
||||
<Form.Item
|
||||
{...field}
|
||||
rules={[{ required: true, message: '请输入答案' }]}
|
||||
@ -443,7 +483,7 @@ const QuestionManagement: React.FC = () => {
|
||||
))}
|
||||
<Form.ErrorList errors={errors} />
|
||||
<Button type="dashed" onClick={() => add()} block icon={<PlusOutlined />}>
|
||||
添加答案
|
||||
添加空格
|
||||
</Button>
|
||||
</Form.Item>
|
||||
</>
|
||||
@ -585,7 +625,7 @@ const QuestionManagement: React.FC = () => {
|
||||
}
|
||||
// 为判断题设置默认答案
|
||||
else if (value === 'true-false') {
|
||||
form.setFieldsValue({ answer: true })
|
||||
form.setFieldsValue({ answer: 'true' })
|
||||
}
|
||||
// 为填空题设置默认答案数组
|
||||
else if (value === 'fill-in-blank') {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user