Convert single array values to values (#2506)
This commit is contained in:
parent
593d061224
commit
4349f19e24
2 changed files with 25 additions and 1 deletions
|
@ -111,4 +111,20 @@ describe("Tests the form convert util functions", () => {
|
|||
attributes: {},
|
||||
});
|
||||
});
|
||||
|
||||
it("convert single element arrays to string", () => {
|
||||
const given = {
|
||||
config: { group: ["one"], another: { nested: ["value"] } },
|
||||
};
|
||||
const setValue = jest.fn();
|
||||
|
||||
//when
|
||||
convertToFormValues(given, setValue);
|
||||
|
||||
//then
|
||||
expect(setValue).toHaveBeenCalledWith("config", {
|
||||
group: "one",
|
||||
another: { nested: "value" },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
10
src/util.ts
10
src/util.ts
|
@ -89,7 +89,15 @@ export const convertToFormValues = (
|
|||
if (key === "attributes" && isAttributesObject(value)) {
|
||||
setValue(key, arrayToKeyValue(value as Record<string, string[]>));
|
||||
} else if (key === "config" || key === "attributes") {
|
||||
setValue(key, !isEmpty(value) ? unflatten(value) : undefined);
|
||||
if (!isEmpty(value)) {
|
||||
const flattened: any = flatten(value, { safe: true });
|
||||
const convertedValues = Object.entries(flattened).map(([key, value]) =>
|
||||
Array.isArray(value) ? [key, value[0]] : [key, value]
|
||||
);
|
||||
setValue(key, unflatten(Object.fromEntries(convertedValues)));
|
||||
} else {
|
||||
setValue(key, undefined);
|
||||
}
|
||||
} else {
|
||||
setValue(key, value);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue