Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ui/src/components/dynamics-form/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,12 @@ const render = (
const value = formFieldList.value
.map((item) => {
if (form_data[item.field] !== undefined) {
return { [item.field]: form_data[item.field] }
const v: any = item.option_list?.filter(i => i.value_field === form_data[item.field])
if (v?.length > 0) {
return { [item.field]: form_data[item.field] };
} else {
return { [item.field]: item.default_value };
}
}
if (item.show_default_value === true || item.show_default_value === undefined) {
return { [item.field]: item.default_value }
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.

The provided code has a few issues that can be addressed:

  1. Type Safety: The type assertion const v: any is unnecessary and could lead to runtime errors.
  2. Logic Simplification: Instead of filtering for an exact match, you might want to consider using strict equality (===) when comparing values.

Here's the revised version of the code with these improvements:

const render = (...):
 // ...

if (form_data[item.field] !== undefined && form_field_list.item.option_list.some(i => i.value_field === form_data[item.field])) {
  return { [item.field]: form_data[item.field] };
} else if (item.show_default_value === true || item.show_default_value === undefined) {
  return { [item.field]: item.default_value }
}

// ...

Key Changes:

  1. Removed Type Assertion: Removed any type assertion; typescript will infer the correct type based on conditions.
  2. Simplified Logic: Used .some() to check if there is at least one option with a matching value_field, rather than .filter(), which would require handling multiple matches separately.

This should make the code cleaner and more efficient while maintaining correctness.

Expand Down