fix: 优化题库管理选项解析逻辑

- 调整选项解析条件判断顺序,先检查题型再检查选项数据
- 避免非选择题类型尝试解析不存在的选项字段
- 确保简答题和论述题不会误处理选项数据

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
燕陇琪 2025-11-08 07:06:07 +08:00
parent 60c2cd1406
commit 344ccd7a44

View File

@ -161,7 +161,8 @@ const QuestionManagement: React.FC = () => {
// 解析选项(仅选择题和多选题需要) // 解析选项(仅选择题和多选题需要)
let options: Record<string, string> | undefined let options: Record<string, string> | undefined
if (values.options && (values.type === 'multiple-choice' || values.type === 'multiple-selection')) { if (values.type === 'multiple-choice' || values.type === 'multiple-selection') {
if (values.options && values.options.length > 0) {
// 将数组格式转换为对象格式 { "A": "选项A", "B": "选项B" } // 将数组格式转换为对象格式 { "A": "选项A", "B": "选项B" }
options = values.options.reduce((acc: Record<string, string>, opt: any) => { options = values.options.reduce((acc: Record<string, string>, opt: any) => {
if (opt && opt.key && opt.value) { if (opt && opt.key && opt.value) {
@ -170,6 +171,7 @@ const QuestionManagement: React.FC = () => {
return acc return acc
}, {}) }, {})
} }
}
// 构建请求数据 // 构建请求数据
const data = { const data = {
@ -284,8 +286,8 @@ const QuestionManagement: React.FC = () => {
rules={[{ required: true, message: '请选择答案' }]} rules={[{ required: true, message: '请选择答案' }]}
> >
<Radio.Group> <Radio.Group>
<Radio value={true}></Radio> <Radio value="true"></Radio>
<Radio value={false}></Radio> <Radio value="false"></Radio>
</Radio.Group> </Radio.Group>
</Form.Item> </Form.Item>
) )
@ -339,12 +341,28 @@ const QuestionManagement: React.FC = () => {
</> </>
)} )}
</Form.List> </Form.List>
<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 <Form.Item
label="正确答案" label="正确答案"
name="answer" name="answer"
rules={[{ required: true, message: '请选择答案' }]} rules={[{ required: true, message: '请选择答案' }]}
> >
<Input placeholder="输入选项键,如: A" /> <Select
placeholder="选择正确答案"
options={optionsList}
/>
</Form.Item>
)
}}
</Form.Item> </Form.Item>
</> </>
) )
@ -398,13 +416,35 @@ const QuestionManagement: React.FC = () => {
</> </>
)} )}
</Form.List> </Form.List>
<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 <Form.Item
label="正确答案" label="正确答案"
name="answer" name="answer"
rules={[{ required: true, message: '请选择答案' }]} rules={[{ required: true, message: '请选择答案', type: 'array' }]}
tooltip="多个答案请用逗号分隔,如: A,B,C" tooltip="可以选择多个正确答案"
> >
<Select mode="tags" placeholder="选择或输入答案(可多选)" /> <Select
mode="multiple"
placeholder="选择正确答案(可多选)"
options={optionsList}
allowClear
showSearch
filterOption={(input, option) =>
(option?.label ?? '').toLowerCase().includes(input.toLowerCase())
}
/>
</Form.Item>
)
}}
</Form.Item> </Form.Item>
</> </>
) )
@ -428,7 +468,7 @@ const QuestionManagement: React.FC = () => {
<Form.Item label="正确答案" required> <Form.Item label="正确答案" required>
{fields.map((field, index) => ( {fields.map((field, index) => (
<Space key={field.key} style={{ display: 'flex', marginBottom: 8 }} align="baseline"> <Space key={field.key} style={{ display: 'flex', marginBottom: 8 }} align="baseline">
<span> {index + 1}:</span> <span>{['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'][index] || (index + 1)}:</span>
<Form.Item <Form.Item
{...field} {...field}
rules={[{ required: true, message: '请输入答案' }]} rules={[{ required: true, message: '请输入答案' }]}
@ -443,7 +483,7 @@ const QuestionManagement: React.FC = () => {
))} ))}
<Form.ErrorList errors={errors} /> <Form.ErrorList errors={errors} />
<Button type="dashed" onClick={() => add()} block icon={<PlusOutlined />}> <Button type="dashed" onClick={() => add()} block icon={<PlusOutlined />}>
</Button> </Button>
</Form.Item> </Form.Item>
</> </>
@ -585,7 +625,7 @@ const QuestionManagement: React.FC = () => {
} }
// 为判断题设置默认答案 // 为判断题设置默认答案
else if (value === 'true-false') { else if (value === 'true-false') {
form.setFieldsValue({ answer: true }) form.setFieldsValue({ answer: 'true' })
} }
// 为填空题设置默认答案数组 // 为填空题设置默认答案数组
else if (value === 'fill-in-blank') { else if (value === 'fill-in-blank') {