fix: 修复题库管理新建题目时answer字段未初始化的bug
- 将编辑题目时的表单初始化逻辑应用到新建题目 - 新建时正确设置默认的type、content、answer和options字段 - 题型切换时根据不同题型设置相应的默认answer值 - 确保所有题型都有正确的初始状态 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
51c85b41a5
commit
60c2cd1406
@ -106,10 +106,24 @@ const QuestionManagement: React.FC = () => {
|
|||||||
} else {
|
} else {
|
||||||
setEditingQuestion(null)
|
setEditingQuestion(null)
|
||||||
form.resetFields()
|
form.resetFields()
|
||||||
// 新建时设置默认值
|
|
||||||
|
// 新建时设置默认值 - 参照编辑逻辑
|
||||||
|
const defaultType = 'multiple-choice'
|
||||||
|
|
||||||
|
// 设置默认答案值
|
||||||
|
let defaultAnswer: any = ''
|
||||||
|
let defaultOptions: Array<{ key: string; value: string }> = []
|
||||||
|
|
||||||
|
// 为单选和多选题设置默认选项
|
||||||
|
if (defaultType === 'multiple-choice' || defaultType === 'multiple-selection') {
|
||||||
|
defaultOptions = [{ key: 'A', value: '' }, { key: 'B', value: '' }]
|
||||||
|
}
|
||||||
|
|
||||||
form.setFieldsValue({
|
form.setFieldsValue({
|
||||||
type: 'multiple-choice',
|
type: defaultType,
|
||||||
options: [{ key: 'A', value: '' }, { key: 'B', value: '' }],
|
content: '',
|
||||||
|
answer: defaultAnswer,
|
||||||
|
options: defaultOptions,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
setModalVisible(true)
|
setModalVisible(true)
|
||||||
@ -548,7 +562,8 @@ const QuestionManagement: React.FC = () => {
|
|||||||
layout="vertical"
|
layout="vertical"
|
||||||
initialValues={{
|
initialValues={{
|
||||||
type: 'multiple-choice',
|
type: 'multiple-choice',
|
||||||
options: [{ key: 'A', value: '' }, { key: 'B', value: '' }]
|
options: [{ key: 'A', value: '' }, { key: 'B', value: '' }],
|
||||||
|
answer: '', // 添加默认answer字段
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Form.Item
|
<Form.Item
|
||||||
@ -564,13 +579,22 @@ const QuestionManagement: React.FC = () => {
|
|||||||
// 为单选和多选题设置默认选项
|
// 为单选和多选题设置默认选项
|
||||||
if (value === 'multiple-choice' || value === 'multiple-selection') {
|
if (value === 'multiple-choice' || value === 'multiple-selection') {
|
||||||
form.setFieldsValue({
|
form.setFieldsValue({
|
||||||
options: [{ key: 'A', value: '' }, { key: 'B', value: '' }]
|
options: [{ key: 'A', value: '' }, { key: 'B', value: '' }],
|
||||||
|
answer: value === 'multiple-choice' ? '' : [], // 单选空字符串,多选空数组
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
// 为判断题设置默认答案
|
||||||
|
else if (value === 'true-false') {
|
||||||
|
form.setFieldsValue({ answer: true })
|
||||||
|
}
|
||||||
// 为填空题设置默认答案数组
|
// 为填空题设置默认答案数组
|
||||||
if (value === 'fill-in-blank') {
|
else if (value === 'fill-in-blank') {
|
||||||
form.setFieldsValue({ answer: [''] })
|
form.setFieldsValue({ answer: [''] })
|
||||||
}
|
}
|
||||||
|
// 为简答题和论述题设置默认空字符串
|
||||||
|
else if (value === 'short-answer' || value === 'ordinary-essay' || value === 'management-essay') {
|
||||||
|
form.setFieldsValue({ answer: '' })
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{questionTypes.map(type => (
|
{questionTypes.map(type => (
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user