Repository navigation
feat: internationalize dynamic forms - #7956
Conversation
| TreeSelect: 'dynamicForm.types.treeSelect', | ||
| } | ||
| export type DynamicFormInputType = (typeof DYNAMIC_FORM_TYPES)[number]['value'] | ||
| export type DynamicFormTypeOption = (typeof DYNAMIC_FORM_TYPES)[number] |
There was a problem hiding this comment.
Code Review
Summary
The diff consolidates a duplicated mapping (dynamicFormTypeOptions + DYNAMIC_FORM_TYPE_MESSAGE_KEYS) into a single ordered list using i18n keys directly. This is generally a good refactor.
Issues & Potential Problems
1. ⚠️ Removed exports may break callers (breaking change)
The following were deleted without any compatibility shim:
dynamicFormTypeOptionsreferenceOptionsHelpDYNAMIC_FORM_TYPE_MESSAGE_KEYS
Any file still importing them will fail to compile/run. Verify with a repo-wide search, or re-export aliases for a deprecation period:
/** @deprecated use DYNAMIC_FORM_TYPES */
export const dynamicFormTypeOptions = DYNAMIC_FORM_TYPES2. ⚠️ Loss of the label field changes consumer expectations
Previously options carried a label string; now they carry only labelKey. Any UI code that rendered option.label must be updated to t(option.labelKey). Consider documenting this in the PR, or keep a helper:
export const getTypeLabel = (t: TFunction, value: DynamicFormInputType) =>
t(DYNAMIC_FORM_TYPES.find(x => x.value === value)?.labelKey ?? value)3. ⚠️ referenceOptionsHelp removal is unaccounted for
This was user-facing help text (the JSON format description). If it's moved elsewhere, fine — but ensure it wasn't just dropped. If it lives in i18n now, confirm translations exist for all locales.
4. Minor: DynamicFormTypeOption semantics changed
It previously included { label, value }; now { value, labelKey }. External consumers depending on .label will silently get undefined (if typed as any) rather than a compile error — worth auditing.
5. Optimization / consistency notes
as constgives exact literal types — good. ButDynamicFormInputTyperesolves to the union of all literals, which is nice and keeps values type-safe end-to-end.- If you want an O(1) lookup map, derive it once at module scope:
const TYPE_LABEL_KEYS = Object.fromEntries(
DYNAMIC_FORM_TYPES.map(o => [o.value, o.labelKey])
) as Record<DynamicFormInputType, string>- Ensure every referenced i18n key (
dynamicForm.types.*) actually exists in all locale files — a typo here degrades gracefully at runtime but is easy to miss in tests.
6. Naming
DYNAMIC_FORM_TYPES vs previous dynamicFormTypeOptions — the new name is cleaner, but consider documenting the migration in the CHANGELOG given the public export rename.
Verdict
Refactor is sound and removes duplication. Main risks are silent breaking changes to removed exports and the label → labelKey contract shift. Add aliases or verify no external usage before merging.
| </template> | ||
| </el-table-column> | ||
| <el-table-column label="必填" width="55"> | ||
| <template #default="{ row }"><el-switch :model-value="Boolean(row.required)" disabled size="small" /></template> |
There was a problem hiding this comment.
Review
Potential Issues
-
getTypeLabelin template causes extra function calls – It's called on every render for each row. This is usually fine for small tables, but if the list grows large, consider precomputing aMap<string, string>(or computing labels once wheninputFieldschanges) rather than a linear.find()per cell per render.const typeLabelMap = computed(() => { const map = new Map<string, string>() for (const d of DYNAMIC_FORM_TYPES) map.set(d.value, t(d.labelKey)) return map }) // template: {{ typeLabelMap.get(row.input_type) ?? row.input_type }}
-
i18n reactivity –
t(definition.labelKey)returns a string at call time. Because it's invoked inside a function within the template, it is re-evaluated on re-render, so locale switches should work. But if you switch to thecomputedapproach above, make suretis reactive insidecomputed(it is withuseI18n), otherwise labels won't update on language change. -
No fallback warning on missing definition – Silently falling back to raw
inputTypemay hide misconfigurations. Consider a dev-onlyconsole.warnwhen no definition is found, or at least ensureDYNAMIC_FORM_TYPESstays complete. -
Renamed import (
dynamicFormTypeOptions→DYNAMIC_FORM_TYPES) – Verify the old export isn't still used elsewhere in the codebase; removing/renaming a public export from@/components/mk-dynamics-formis a breaking change for other consumers. -
useScope: 'global'– Correct as written for global messages, but double-check thatlabelKeys exist in the global message catalog (not only a component/local scope). Otherwisetwill emit missing-key warnings. -
Type of
inputType– Signature is(inputType: string). Ifrow.input_typecan beundefined/null,.findsimply won't match and you'll renderundefined. Consider typing it asstring | undefinedand handling the empty case explicitly (e.g., render—).
Style / Minor
-
Template reformatting – The diff mixes logic changes with pure formatting churn (multi-line
<span>, table columns). Splitting formatting-only changes into a separate commit makes review easier. -
Repeated
formatDefaultValue(row)calls – Called twice per row (title + content). If it does any non-trivial work (e.g., JSON.stringify of large values), compute it once via a scoped variable using<template #default="{ row }">+ a small computed/helper, or cache results.
No Observed Bugs
The core refactor is sound: extracting the tag label lookup into getTypeLabel improves readability, i18n integration looks correct, and the v-if/v-else label logic is preserved. The main actionable items are the per-row find performance note and confirming the renamed export in item 4.
| <el-input-number v-model="formValue.max_file_size" :min="0" controls-position="right" align="left" /> | ||
| </el-form-item> | ||
| <el-form-item | ||
| :label="t('dynamicForm.fields.fileTypes.label')" |
There was a problem hiding this comment.
Key issues in this diff:
-
rulesplacement fix (good change): Moving:rulesfrom<el-input-number>to<el-form-item>is correct — Element Plus validates rules at theel-form-itemlevel, not on the input component. The original code would not have enforced validation properly. This is a legitimate bug fix. -
Removed
style="width: 100%": This is the main concern.el-input-numberhas a default fixed width (~150px). Withoutwidth: 100%, the input will no longer stretch to fill the form item, causing inconsistent layout versus other fields.- If the intent was styling cleanup, there's no replacement (no class or CSS). Verify UI consistency; consider restoring via a class instead of inline style for maintainability.
-
Inconsistency between the two fields: The first field now uses multi-line formatting with rules moved up, while the second keeps the single-line input but also lost its width style. Both lost width styling without any substitute — apply consistently.
-
Redundancy check:
requiredattribute plus an explicit:ruleswithrequired: trueis duplicative. Either keeprequiredand rely on the implicit rule, or droprequiredand keep explicit rules (explicit rules are preferable for custom messages). Not a bug, but minor redundancy. -
align="left"onel-input-number: Verify this prop exists in your Element Plus version —alignis supported in newer versions, but on older ones it's ignored silently. -
prop="limit"vs model binding: EnsureformValue.limitmaps correctly toprop="limit"and the underlying form model key matches, otherwise validation won't trigger.
Suggestions:
- Restore full-width behavior via a scoped class (e.g.,
class="full-width") rather than inline styles, or confirm the narrower width is intentional. - Remove either
requiredor the explicit rule to avoid duplication. - Add a trailing newline / consistent formatting for both
el-form-itemblocks. - Confirm
alignprop support in your Element Plus version.
feat: internationalize dynamic forms