feat: 支持评分字段的导入
This commit is contained in:
@@ -27,9 +27,12 @@ interface LevelDialogProps {
|
||||
onSubmit: (data: LevelFormData) => Promise<void>
|
||||
}
|
||||
|
||||
type FormState = Omit<LevelFormData, 'position'> & {
|
||||
type FormState = Omit<LevelFormData, 'position' | 'difficultyScore' | 'funScore'> & {
|
||||
/** 位置字段以字符串保存,便于处理"空输入"状态;提交时解析为数字 */
|
||||
position: string
|
||||
/** 评分字段以字符串保存;空串 = 未评分 */
|
||||
difficultyScore: string
|
||||
funScore: string
|
||||
}
|
||||
|
||||
const defaultFormState: FormState = {
|
||||
@@ -42,6 +45,8 @@ const defaultFormState: FormState = {
|
||||
hint1: '',
|
||||
hint2: '',
|
||||
hint3: '',
|
||||
difficultyScore: '',
|
||||
funScore: '',
|
||||
position: '',
|
||||
}
|
||||
|
||||
@@ -85,6 +90,9 @@ export function LevelDialog({
|
||||
hint1: level.hint1 || '',
|
||||
hint2: level.hint2 || '',
|
||||
hint3: level.hint3 || '',
|
||||
difficultyScore:
|
||||
level.difficultyScore != null ? String(level.difficultyScore) : '',
|
||||
funScore: level.funScore != null ? String(level.funScore) : '',
|
||||
position: String(defaultPos),
|
||||
})
|
||||
} else {
|
||||
@@ -126,6 +134,21 @@ export function LevelDialog({
|
||||
return
|
||||
}
|
||||
|
||||
// 评分:空串 → null;非空必须是 1-5 整数
|
||||
const parseScoreField = (raw: string): number | null | undefined => {
|
||||
const trimmed = raw.trim()
|
||||
if (trimmed === '') return null
|
||||
const n = Number(trimmed)
|
||||
if (!Number.isInteger(n) || n < 1 || n > 5) return undefined
|
||||
return n
|
||||
}
|
||||
const difficultyScore = parseScoreField(formData.difficultyScore)
|
||||
const funScore = parseScoreField(formData.funScore)
|
||||
if (difficultyScore === undefined || funScore === undefined) {
|
||||
setError('难度/有趣度评分必须是 1-5 之间的整数')
|
||||
return
|
||||
}
|
||||
|
||||
// 位置:留空视为"不改位置"(编辑)/ "追加末尾"(创建)
|
||||
let position: number | undefined
|
||||
const raw = formData.position.trim()
|
||||
@@ -153,6 +176,8 @@ export function LevelDialog({
|
||||
hint1: formData.hint1,
|
||||
hint2: formData.hint2,
|
||||
hint3: formData.hint3,
|
||||
difficultyScore,
|
||||
funScore,
|
||||
...(position !== undefined ? { position } : {}),
|
||||
}
|
||||
|
||||
@@ -316,6 +341,45 @@ export function LevelDialog({
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="difficultyScore">难度评分 (1-5,可选)</Label>
|
||||
<Input
|
||||
id="difficultyScore"
|
||||
type="number"
|
||||
min={1}
|
||||
max={5}
|
||||
step={1}
|
||||
value={formData.difficultyScore}
|
||||
onChange={(e) =>
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
difficultyScore: e.target.value,
|
||||
}))
|
||||
}
|
||||
placeholder="1-5"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="funScore">有趣度 (1-5,可选)</Label>
|
||||
<Input
|
||||
id="funScore"
|
||||
type="number"
|
||||
min={1}
|
||||
max={5}
|
||||
step={1}
|
||||
value={formData.funScore}
|
||||
onChange={(e) =>
|
||||
setFormData((prev) => ({
|
||||
...prev,
|
||||
funScore: e.target.value,
|
||||
}))
|
||||
}
|
||||
placeholder="1-5"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button
|
||||
type="button"
|
||||
|
||||
Reference in New Issue
Block a user