Skip to content

Commit 8165b60

Browse files
committed
Added alwaysApplyDefaultValues flag to collections
1 parent 39d8cd5 commit 8165b60

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

examples/example_pro/src/FirestoreApp/collections/test_collection.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export const testCollection = buildCollection<any>({
7575
path: "test_entity",
7676
customId: true,
7777
name: "Test entities",
78+
alwaysApplyDefaultValues: true,
7879
entityViews: [{
7980
key: "sec",
8081
name: "Secondary form",
@@ -122,6 +123,7 @@ export const testCollection = buildCollection<any>({
122123
dataType: "string",
123124
name: "Multiline",
124125
multiline: true,
126+
defaultValue: "Hello\nWorld"
125127
},
126128
size: {
127129
dataType: "map",

packages/firecms_core/src/form/EntityForm.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
getValueInPath,
2121
isHidden,
2222
isReadOnly,
23+
mergeDeep,
2324
resolveCollection,
2425
useDebouncedCallback
2526
} from "../util";
@@ -639,7 +640,11 @@ function getInitialEntityValues<M extends object>(
639640
});
640641
const properties = resolvedCollection.properties;
641642
if ((status === "existing" || status === "copy") && entity) {
642-
return entity.values ?? getDefaultValuesFor(properties);
643+
if (!collection.alwaysApplyDefaultValues) {
644+
return entity.values ?? getDefaultValuesFor(properties);
645+
} else {
646+
return mergeDeep(getDefaultValuesFor(properties), entity.values ?? {});
647+
}
643648
} else if (status === "new") {
644649
return getDefaultValuesFor(properties);
645650
} else {

packages/firecms_core/src/types/collections.ts

+7
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,13 @@ export interface EntityCollection<M extends Record<string, any> = any, USER exte
323323
* This prop has only effect if you are using the collection editor.
324324
*/
325325
editable?: boolean;
326+
327+
/**
328+
* If set to true, the default values of the properties will be applied
329+
* to the entity every time the entity is updated (not only when created).
330+
* Defaults to false.
331+
*/
332+
alwaysApplyDefaultValues?: boolean;
326333
}
327334

328335
/**

0 commit comments

Comments
 (0)