From 1e446a02400ca74174322191bd6ecbdad8cd7a1a Mon Sep 17 00:00:00 2001 From: keven1024 Date: Fri, 1 May 2026 11:55:23 +0800 Subject: [PATCH] refactor(front): simplify InputGroupField and KvInputGroupField components by removing unused state and streamlining item addition and update logic --- front/components/Field/InputGroupField.vue | 22 ++----- front/components/Field/KvInputGroupField.vue | 67 +++++--------------- 2 files changed, 21 insertions(+), 68 deletions(-) diff --git a/front/components/Field/InputGroupField.vue b/front/components/Field/InputGroupField.vue index 5c88db6..e9a1901 100644 --- a/front/components/Field/InputGroupField.vue +++ b/front/components/Field/InputGroupField.vue @@ -7,7 +7,6 @@ const props = defineProps<{ rules?: RuleExpression }>() const { value, setValue, errorMessage } = useField(props.name, props?.rules) -const addInput = ref('') diff --git a/front/components/Field/KvInputGroupField.vue b/front/components/Field/KvInputGroupField.vue index daa4acb..ef29f66 100644 --- a/front/components/Field/KvInputGroupField.vue +++ b/front/components/Field/KvInputGroupField.vue @@ -8,77 +8,44 @@ const props = defineProps<{ valuePlaceholder?: string }>() -const { value, setValue } = useField>(props.name) -const addKey = ref('') -const addValue = ref('') +const { value, setValue } = useField<[string, string][]>(props.name) -const currentValue = computed(() => (value.value && typeof value.value === 'object' && !Array.isArray(value.value) ? value.value : {})) - -const entries = computed(() => Object.entries(currentValue.value)) - -const removeItem = (key: string) => { - const nextValue = { ...currentValue.value } - delete nextValue[key] - setValue(nextValue) +const updateKey = (index: number, nextKey: string | number) => { + const next = [...(value.value ?? [])] + next[index] = [String(nextKey), next[index]?.[1] ?? ''] + setValue(next) } -const updateKey = (oldKey: string, nextKeyValue: string | number) => { - const nextKey = String(nextKeyValue).trim() - if (!nextKey || nextKey === oldKey) { - return - } - - const nextValue = { ...currentValue.value } - const oldValue = nextValue[oldKey] - delete nextValue[oldKey] - nextValue[nextKey] = oldValue - setValue(nextValue) -} - -const updateValue = (key: string, nextItemValue: string | number) => { - setValue({ ...currentValue.value, [key]: String(nextItemValue) }) -} - -const addItem = () => { - const nextKey = addKey.value.trim() - const nextValue = addValue.value.trim() - - if (!nextKey || !nextValue) { - return - } - - setValue({ ...currentValue.value, [nextKey]: nextValue }) - addKey.value = '' - addValue.value = '' +const updateValue = (index: number, nextVal: string | number) => { + const next = [...(value.value ?? [])] + next[index] = [next[index]?.[0] ?? '', String(nextVal)] + setValue(next) }