eCourt-Doc-Edit/web/app/utils/fieldEditor.ts

44 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

/**
* Editor (รูปแบบช่องกรอก) ต่อ template field (FR-14) — pure util ให้ unit test ได้
* มิเรอร์ allow-list ของ backend (TemplateField.EditorsByType): text เลือก textbox/textarea ได้,
* number/date มีทางเดียวตาม type — เป็น presentation metadata เท่านั้น (validation ของค่ายังยึด type)
*/
import type { FieldEditor } from '~/types/api'
export const EDITORS_BY_TYPE: Record<string, FieldEditor[]> = {
text: ['textbox', 'textarea'],
number: ['number'],
date: ['date'],
}
/** editor เริ่มต้นของ type (ตัวแรกของรายการ) — type ที่ไม่รู้จัก → textbox */
export function defaultEditor(type: string): FieldEditor {
return EDITORS_BY_TYPE[type]?.[0] ?? 'textbox'
}
/** ตัวเลือก editor ที่เลือกได้ตาม type — ใช้ซ่อน select เมื่อมีทางเดียว (number/date) */
export function editorOptions(type: string): FieldEditor[] {
return EDITORS_BY_TYPE[type] ?? ['textbox']
}
/**
* หา editor ที่จะใช้ render จริงจาก field — editor ที่ถูกต้องตาม type → ใช้ค่านั้น,
* ไม่มี/ไม่รู้จัก/ไม่เข้ากับ type → default ของ type (degrade อ่อนโยน ไม่ throw)
*/
export function resolveEditor(field: { type: string; editor?: string | null }): FieldEditor {
const options = EDITORS_BY_TYPE[field.type] ?? ['textbox']
return options.includes(field.editor as FieldEditor)
? (field.editor as FieldEditor)
: options[0]
}
/**
* ค่าวันที่สำหรับ input[type=date] — รับเฉพาะรูป yyyy-MM-dd เท่านั้น
* ค่าจาก field sync ของ Word อาจเป็น ISO timestamp เต็ม (backend parse ได้ตาม FR-10) → ตัดเอาส่วนวันที่
* ค่าอื่นที่ไม่ใช่รูป ISO → คงเดิม ให้ validation ของฟอร์มจัดการต่อ
*/
export function dateInputValue(value: string): string {
return /^\d{4}-\d{2}-\d{2}/.test(value) ? value.slice(0, 10) : value
}