Skip to content

Commit 18fe394

Browse files
committed
Allowing custom field to change other values
1 parent 24ee380 commit 18fe394

File tree

5 files changed

+174
-158
lines changed

5 files changed

+174
-158
lines changed

example/src/SampleApp/SampleApp.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ function SampleApp() {
114114
}, []);
115115

116116
const dataEnhancementPlugin = useDataEnhancementPlugin({
117-
apiKey: "AIzaSyDZ8Z7Z7Z7Z7Z7Z7Z7Z7Z7Z7Z7Z7Z7Z7Z7",
118117
getConfigForPath: async ({ path }) => {
119118
if (process.env.NODE_ENV !== "production")
120119
return true;

example/src/SampleApp/collections/test_collection.tsx

+158-154
Original file line numberDiff line numberDiff line change
@@ -176,155 +176,159 @@ export const testCollection = buildCollection({
176176
name: "Test entities",
177177
properties: {
178178

179-
specSheet: ({
180-
values,
181-
entityId
182-
}) => ({
183-
name: "Specs sheet",
184-
dataType: "array",
185-
of: {
186-
dataType: "string",
187-
storage: {
188-
mediaType: "image",
189-
storagePath: "products/" + entityId + "/pdf",
190-
acceptedFiles: ["application/pdf"],
191-
metadata: {
192-
cacheControl: "max-age=1000000"
193-
}
194-
}
195-
}
196-
}),
179+
// specSheet: ({
180+
// values,
181+
// entityId
182+
// }) => ({
183+
// name: "Specs sheet",
184+
// dataType: "array",
185+
// of: {
186+
// dataType: "string",
187+
// storage: {
188+
// mediaType: "image",
189+
// storagePath: "products/" + entityId + "/pdf",
190+
// acceptedFiles: ["application/pdf"],
191+
// metadata: {
192+
// cacheControl: "max-age=1000000"
193+
// }
194+
// }
195+
// }
196+
// }),
197197

198+
test_string: {
199+
dataType: "string",
200+
name: "Test string"
201+
},
198202
test_custom: {
199203
dataType: "string",
200204
name: "Test custom",
201205
fieldConfig: "test_custom_field"
202-
},
203-
map: {
204-
dataType: "map",
205-
properties: {
206-
sample: {
207-
name: "Sample",
208-
dataType: "string",
209-
validation: {
210-
required: true
211-
}
212-
},
213-
num: {
214-
name: "Num",
215-
dataType: "number",
216-
validation: {
217-
required: true,
218-
min: 5
219-
}
220-
}
221-
}
222-
},
223-
test_enum: buildProperty({
224-
dataType: "string",
225-
name: "Currency",
226-
enumValues: [
227-
{
228-
id: "EUR",
229-
label: "Euros",
230-
color: "blueDark"
231-
},
232-
{
233-
id: "DOL",
234-
label: "Dollars",
235-
color: {
236-
color: "#FFFFFF",
237-
text: "#333333"
238-
}
239-
}
240-
]
241-
}),
242-
background: {
243-
dataType: "number", // NB – this was string” above..
244-
name: "Colour",
245-
enumValues:
246-
[
247-
{
248-
id: 4281080974,
249-
label: " Blue ",
250-
color: "blueDarker"
251-
},
252-
{
253-
id: 4293947270,
254-
label: " Cyan ",
255-
color: "cyanDarker"
256-
}
257-
]
258-
},
259-
260-
test_date: {
261-
name: "Test date",
262-
dataType: "date",
263-
mode: "date"
264-
},
265-
name: {
266-
dataType: "string",
267-
name: "Name"
268-
},
269-
self_ref: {
270-
name: "Self ref",
271-
dataType: "reference",
272-
path: "test_entity"
273-
},
274-
self_refs: {
275-
dataType: "array",
276-
of: {
277-
dataType: "reference",
278-
name: "Self refs",
279-
path: "test_entity"
280-
// previewProperties: ["name","url_image"]
281-
}
282-
},
283-
url_image: {
284-
dataType: "string",
285-
name: "URL image",
286-
storage: {
287-
storagePath: "images",
288-
acceptedFiles: ["image/*"],
289-
storeUrl: true,
290-
fileName: async ({ file }) => {
291-
await new Promise(resolve => setTimeout(resolve, 100));
292-
return file.name;
293-
},
294-
metadata: {
295-
cacheControl: "max-age=1000000"
296-
}
297-
}
298-
},
299-
myArray: {
300-
name: "some array",
301-
dataType: "array",
302-
of: {
303-
dataType: "map",
304-
properties: {
305-
prop1: {
306-
dataType: "string",
307-
name: "prop1"
308-
},
309-
prop2: {
310-
dataType: "number",
311-
name: "prop2"
312-
}
313-
},
314-
defaultValue: {
315-
// this DOESN'T works as initial value :(
316-
prop1: "hello 2",
317-
prop2: 2
318-
}
319-
},
320-
defaultValue: [
321-
// this works as initial value :)
322-
{
323-
prop1: "hello 1",
324-
prop2: 1
325-
}
326-
]
327-
},
206+
}
207+
// map: {
208+
// dataType: "map",
209+
// properties: {
210+
// sample: {
211+
// name: "Sample",
212+
// dataType: "string",
213+
// validation: {
214+
// required: true
215+
// }
216+
// },
217+
// num: {
218+
// name: "Num",
219+
// dataType: "number",
220+
// validation: {
221+
// required: true,
222+
// min: 5
223+
// }
224+
// }
225+
// }
226+
// },
227+
// test_enum: buildProperty({
228+
// dataType: "string",
229+
// name: "Currency",
230+
// enumValues: [
231+
// {
232+
// id: "EUR",
233+
// label: "Euros",
234+
// color: "blueDark"
235+
// },
236+
// {
237+
// id: "DOL",
238+
// label: "Dollars",
239+
// color: {
240+
// color: "#FFFFFF",
241+
// text: "#333333"
242+
// }
243+
// }
244+
// ]
245+
// }),
246+
// background: {
247+
// dataType: "number", // NB – this was string” above..
248+
// name: "Colour",
249+
// enumValues:
250+
// [
251+
// {
252+
// id: 4281080974,
253+
// label: " Blue ",
254+
// color: "blueDarker"
255+
// },
256+
// {
257+
// id: 4293947270,
258+
// label: " Cyan ",
259+
// color: "cyanDarker"
260+
// }
261+
// ]
262+
// },
263+
//
264+
// test_date: {
265+
// name: "Test date",
266+
// dataType: "date",
267+
// mode: "date"
268+
// },
269+
// name: {
270+
// dataType: "string",
271+
// name: "Name"
272+
// },
273+
// self_ref: {
274+
// name: "Self ref",
275+
// dataType: "reference",
276+
// path: "test_entity"
277+
// },
278+
// self_refs: {
279+
// dataType: "array",
280+
// of: {
281+
// dataType: "reference",
282+
// name: "Self refs",
283+
// path: "test_entity"
284+
// // previewProperties: ["name","url_image"]
285+
// }
286+
// },
287+
// url_image: {
288+
// dataType: "string",
289+
// name: "URL image",
290+
// storage: {
291+
// storagePath: "images",
292+
// acceptedFiles: ["image/*"],
293+
// storeUrl: true,
294+
// fileName: async ({ file }) => {
295+
// await new Promise(resolve => setTimeout(resolve, 100));
296+
// return file.name;
297+
// },
298+
// metadata: {
299+
// cacheControl: "max-age=1000000"
300+
// }
301+
// }
302+
// },
303+
// myArray: {
304+
// name: "some array",
305+
// dataType: "array",
306+
// of: {
307+
// dataType: "map",
308+
// properties: {
309+
// prop1: {
310+
// dataType: "string",
311+
// name: "prop1"
312+
// },
313+
// prop2: {
314+
// dataType: "number",
315+
// name: "prop2"
316+
// }
317+
// },
318+
// defaultValue: {
319+
// // this DOESN'T works as initial value :(
320+
// prop1: "hello 2",
321+
// prop2: 2
322+
// }
323+
// },
324+
// defaultValue: [
325+
// // this works as initial value :)
326+
// {
327+
// prop1: "hello 1",
328+
// prop2: 1
329+
// }
330+
// ]
331+
// },
328332

329333
// impacts: {
330334
// name: "Impacts",
@@ -712,16 +716,16 @@ export const testCollection = buildCollection({
712716
// acceptedFiles: ["image/*"]
713717
// }
714718
// },
715-
created_on: {
716-
name: "Created on",
717-
dataType: "date",
718-
autoValue: "on_create"
719-
},
720-
updated_on: {
721-
name: "Updated on",
722-
dataType: "date",
723-
autoValue: "on_update"
724-
}
719+
// created_on: {
720+
// name: "Created on",
721+
// dataType: "date",
722+
// autoValue: "on_create"
723+
// },
724+
// updated_on: {
725+
// name: "Updated on",
726+
// dataType: "date",
727+
// autoValue: "on_update"
728+
// }
725729
// description: {
726730
// name: "Description",
727731
// dataType: "string",

example/src/SampleApp/custom_field/CustomColorTextField.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { TextField } from "@mui/material";
2+
import { Button, TextField } from "@mui/material";
33
import {
44
FieldDescription,
55
FieldProps,
@@ -14,6 +14,7 @@ export default function CustomColorTextField({
1414
property,
1515
value,
1616
setValue,
17+
setFieldValue,
1718
customProps,
1819
touched,
1920
error,

lib/src/form/PropertyFieldBinding.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,13 @@ function FieldInternal<T extends CMSType, CustomProps, M extends Record<string,
207207
propertyKey,
208208
value: value as T,
209209
initialValue,
210-
setValue: (value: T | null) => {
210+
setValue: (value: T | null, shouldValidate?: boolean) => {
211211
fieldProps.form.setFieldTouched(propertyKey, true, false);
212-
fieldProps.form.setFieldValue(propertyKey, value);
212+
fieldProps.form.setFieldValue(propertyKey, value, shouldValidate);
213+
},
214+
setFieldValue: (otherPropertyKey: string, value: T | null, shouldValidate?: boolean) => {
215+
fieldProps.form.setFieldTouched(propertyKey, true, false);
216+
fieldProps.form.setFieldValue(otherPropertyKey, value, shouldValidate);
213217
},
214218
error,
215219
touched,

lib/src/types/fields.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ export interface FieldProps<T extends CMSType = CMSType, CustomProps = any, M ex
3434
*/
3535
setValue: (value: T | null, shouldValidate?: boolean) => void;
3636

37+
/**
38+
* Set value of a different field directly
39+
* @param propertyKey
40+
* @param value
41+
* @param shouldValidate
42+
*/
43+
setFieldValue: (propertyKey:string, value: T | null, shouldValidate?: boolean) => void;
44+
3745
/**
3846
* Is the form currently submitting
3947
*/

0 commit comments

Comments
 (0)