Skip to content

Commit ec7a218

Browse files
authored
fix: resolve the issue of potentially rendering empty data in the Array (#8378)
#### What type of PR is this? /kind bug /area ui /milestone 2.23.x #### What this PR does / why we need it: 解决 Array 中数据渲染可能为空的问题,这个问题应该是由 #8360 引入的 #### Does this PR introduce a user-facing change? ```release-note None ```
1 parent ea3c474 commit ec7a218

1 file changed

Lines changed: 21 additions & 18 deletions

File tree

ui/src/formkit/inputs/array/ArrayInput.vue

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { getNode, type FormKitNode, type FormKitProps } from "@formkit/core";
33
import { undefine } from "@formkit/utils";
44
import { IconClose, VButton } from "@halo-dev/components";
55
import { utils } from "@halo-dev/ui-shared";
6-
import { isNil } from "es-toolkit";
7-
import { cloneDeepWith, get, toArray } from "es-toolkit/compat";
6+
import { isNil, isNotNil } from "es-toolkit";
7+
import { cloneDeepWith, get } from "es-toolkit/compat";
88
import objectHash from "object-hash";
99
import { onMounted, ref, toRaw, watch } from "vue";
1010
import { VueDraggable } from "vue-draggable-plus";
@@ -95,7 +95,7 @@ onMounted(async () => {
9595
9696
type FormattedItemLabel =
9797
| {
98-
type: "text" | "image" | "color";
98+
type: Exclude<ArrayItemLabelType, "iconify">;
9999
value: string;
100100
}
101101
| {
@@ -108,7 +108,7 @@ type FormattedItemLabel =
108108
const parseItemLabel = async (
109109
itemLabel: ArrayItemLabel,
110110
item: Record<string, unknown>
111-
): Promise<FormattedItemLabel | FormattedItemLabel[] | undefined> => {
111+
): Promise<FormattedItemLabel[] | undefined> => {
112112
if (!itemLabel.label) {
113113
return;
114114
}
@@ -119,20 +119,24 @@ const parseItemLabel = async (
119119
const node = hiddenChildrenFormKit.value?.at(path);
120120
121121
if (!node) {
122-
return {
123-
type: itemLabel.type,
124-
value: String(value ?? ""),
125-
} as FormattedItemLabel;
122+
return [
123+
{
124+
type: itemLabel.type,
125+
value: String(value ?? ""),
126+
} as FormattedItemLabel,
127+
];
126128
}
127129
const renderedValue = await renderItemLabelValue(node, value);
128130
const castRenderedValueArray = Array.isArray(renderedValue)
129131
? renderedValue
130132
: [renderedValue];
131133
if (castRenderedValueArray.length === 0) {
132-
return {
133-
type: itemLabel.type,
134-
value: String(value ?? ""),
135-
} as FormattedItemLabel;
134+
return [
135+
{
136+
type: itemLabel.type,
137+
value: String(value ?? ""),
138+
} as FormattedItemLabel,
139+
];
136140
}
137141
return castRenderedValueArray.map((renderedValue) => {
138142
return {
@@ -155,12 +159,11 @@ const formatItemLabel = async (
155159
});
156160
const itemLabels = props.node.props.itemLabels ?? defaultItemLabel;
157161
if (itemLabels.length > 0) {
158-
const results = (
159-
await Promise.all(itemLabels.map((label) => parseItemLabel(label, item)))
160-
).flatMap(toArray);
161-
return results.filter(
162-
(itemLabel): itemLabel is FormattedItemLabel => !!itemLabel?.value
163-
);
162+
const results = await Promise.all<FormattedItemLabel[][]>(
163+
itemLabels.map((label: ArrayItemLabel) => parseItemLabel(label, item))
164+
).then((results) => results.flat());
165+
166+
return results.filter(isNotNil);
164167
}
165168
return [];
166169
};

0 commit comments

Comments
 (0)