Skip to content

feat: internationalize dynamic forms - #7956

Merged
wangdan-fit2cloud merged 1 commit into
v3from
pr@v3@feat_model_i18n1
Nov 16, 2026
Merged

wangdan-fit2cloud merged 1 commit into
v3from
pr@v3@feat_model_i18n1

Conversation

@shaohuzhang1

Copy link
Copy Markdown
Contributor

feat: internationalize dynamic forms

TreeSelect: 'dynamicForm.types.treeSelect',
}
export type DynamicFormInputType = (typeof DYNAMIC_FORM_TYPES)[number]['value']
export type DynamicFormTypeOption = (typeof DYNAMIC_FORM_TYPES)[number]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  • dynamicFormTypeOptions
  • referenceOptionsHelp
  • DYNAMIC_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_TYPES

2. ⚠️ 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 const gives exact literal types — good. But DynamicFormInputType resolves 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>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Potential Issues

  1. getTypeLabel in 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 a Map<string, string> (or computing labels once when inputFields changes) 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 }}
  2. 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 the computed approach above, make sure t is reactive inside computed (it is with useI18n), otherwise labels won't update on language change.

  3. No fallback warning on missing definition – Silently falling back to raw inputType may hide misconfigurations. Consider a dev-only console.warn when no definition is found, or at least ensure DYNAMIC_FORM_TYPES stays complete.

  4. 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-form is a breaking change for other consumers.

  5. useScope: 'global' – Correct as written for global messages, but double-check that labelKeys exist in the global message catalog (not only a component/local scope). Otherwise t will emit missing-key warnings.

  6. Type of inputType – Signature is (inputType: string). If row.input_type can be undefined/null, .find simply won't match and you'll render undefined. Consider typing it as string | undefined and handling the empty case explicitly (e.g., render —).

Style / Minor

  1. 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.

  2. 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')"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Key issues in this diff:

  1. rules placement fix (good change): Moving :rules from <el-input-number> to <el-form-item> is correct — Element Plus validates rules at the el-form-item level, not on the input component. The original code would not have enforced validation properly. This is a legitimate bug fix.

  2. Removed style="width: 100%": This is the main concern.

    • el-input-number has a default fixed width (~150px). Without width: 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.
  3. 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.

  4. Redundancy check: required attribute plus an explicit :rules with required: true is duplicative. Either keep required and rely on the implicit rule, or drop required and keep explicit rules (explicit rules are preferable for custom messages). Not a bug, but minor redundancy.

  5. align="left" on el-input-number: Verify this prop exists in your Element Plus version — align is supported in newer versions, but on older ones it's ignored silently.

  6. prop="limit" vs model binding: Ensure formValue.limit maps correctly to prop="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 required or the explicit rule to avoid duplication.
  • Add a trailing newline / consistent formatting for both el-form-item blocks.
  • Confirm align prop support in your Element Plus version.

@wangdan-fit2cloud
wangdan-fit2cloud merged commit 2de7ade into v3 Nov 16, 2026
3 of 4 checks passed
@wangdan-fit2cloud
wangdan-fit2cloud deleted the pr@v3@feat_model_i18n1 branch November 16, 2026 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants