ding-react-admin 2.2.2 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -6,8 +6,8 @@ export type ToFormDataOptions = {
|
|
|
6
6
|
skipExistingUploadUrls?: boolean;
|
|
7
7
|
};
|
|
8
8
|
/**
|
|
9
|
-
* Flatten a save payload into `FormData` using
|
|
10
|
-
* and arrays (e.g. `lines[0]
|
|
9
|
+
* Flatten a save payload into `FormData` using Django REST Framework HTML form
|
|
10
|
+
* keys for nested fields and arrays (e.g. `lines[0]label`, `address.city`).
|
|
11
11
|
*/
|
|
12
12
|
export declare function toFormData(data: Record<string, unknown>, options?: ToFormDataOptions): FormData;
|
|
13
13
|
//# sourceMappingURL=toFormData.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toFormData.d.ts","sourceRoot":"","sources":["../../../../src/crud/utils/toFormData.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;;OAGG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;CAClC,CAAC;
|
|
1
|
+
{"version":3,"file":"toFormData.d.ts","sourceRoot":"","sources":["../../../../src/crud/utils/toFormData.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;;OAGG;IACH,sBAAsB,CAAC,EAAE,OAAO,CAAC;CAClC,CAAC;AAiEF;;;GAGG;AACH,wBAAgB,UAAU,CACxB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,CAAC,EAAE,iBAAiB,GAC1B,QAAQ,CAWV"}
|
package/package.json
CHANGED
|
@@ -8,6 +8,14 @@ export type ToFormDataOptions = {
|
|
|
8
8
|
skipExistingUploadUrls?: boolean;
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
+
/** DRF `parse_html_list` expects `lines[0]label`; nested dicts use `address.city`. */
|
|
12
|
+
function nestedObjectKey(parentKey: string, childKey: string): string {
|
|
13
|
+
if (/\[[0-9]+\]$/.test(parentKey)) {
|
|
14
|
+
return `${parentKey}${childKey}`;
|
|
15
|
+
}
|
|
16
|
+
return `${parentKey}.${childKey}`;
|
|
17
|
+
}
|
|
18
|
+
|
|
11
19
|
function appendFormDataValue(
|
|
12
20
|
formData: FormData,
|
|
13
21
|
key: string,
|
|
@@ -50,7 +58,12 @@ function appendFormDataValue(
|
|
|
50
58
|
for (const [childKey, childValue] of Object.entries(
|
|
51
59
|
value as Record<string, unknown>,
|
|
52
60
|
)) {
|
|
53
|
-
appendFormDataValue(
|
|
61
|
+
appendFormDataValue(
|
|
62
|
+
formData,
|
|
63
|
+
nestedObjectKey(key, childKey),
|
|
64
|
+
childValue,
|
|
65
|
+
options,
|
|
66
|
+
);
|
|
54
67
|
}
|
|
55
68
|
return;
|
|
56
69
|
}
|
|
@@ -59,8 +72,8 @@ function appendFormDataValue(
|
|
|
59
72
|
}
|
|
60
73
|
|
|
61
74
|
/**
|
|
62
|
-
* Flatten a save payload into `FormData` using
|
|
63
|
-
* and arrays (e.g. `lines[0]
|
|
75
|
+
* Flatten a save payload into `FormData` using Django REST Framework HTML form
|
|
76
|
+
* keys for nested fields and arrays (e.g. `lines[0]label`, `address.city`).
|
|
64
77
|
*/
|
|
65
78
|
export function toFormData(
|
|
66
79
|
data: Record<string, unknown>,
|
|
@@ -102,15 +102,24 @@ describe("toFormData", () => {
|
|
|
102
102
|
expect(fd.get("title")).toBe("Hello");
|
|
103
103
|
});
|
|
104
104
|
|
|
105
|
-
it("encodes inline rows with
|
|
105
|
+
it("encodes inline rows with Django REST Framework HTML keys", () => {
|
|
106
106
|
const file = new File(["x"], "line.png", { type: "image/png" });
|
|
107
107
|
const fd = toFormData({
|
|
108
108
|
lines: [{ id: 10, label: "A", photo: file }],
|
|
109
109
|
});
|
|
110
110
|
|
|
111
|
-
expect(fd.get("lines[0]
|
|
112
|
-
expect(fd.get("lines[0]
|
|
113
|
-
expect(fd.get("lines[0]
|
|
111
|
+
expect(fd.get("lines[0]id")).toBe("10");
|
|
112
|
+
expect(fd.get("lines[0]label")).toBe("A");
|
|
113
|
+
expect(fd.get("lines[0]photo")).toBe(file);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("encodes nested objects with dotted keys", () => {
|
|
117
|
+
const fd = toFormData({
|
|
118
|
+
address: { city: "Lahore", zip: 54000 },
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
expect(fd.get("address.city")).toBe("Lahore");
|
|
122
|
+
expect(fd.get("address.zip")).toBe("54000");
|
|
114
123
|
});
|
|
115
124
|
});
|
|
116
125
|
|