# HG changeset patch
# User Frank Bessou <frank.bessou@logilab.fr>
# Date 1615383585 -3600
#      Wed Mar 10 14:39:45 2021 +0100
# Node ID 4d31c27cdf30cff76319cdc4d956dc73921920e7
# Parent  5651596f0201dad319818e3c3a7ac0509a5ce778
feat: Allow build objs to have default values

diff --git a/packages/ra-cubicweb/src/Schema.ts b/packages/ra-cubicweb/src/Schema.ts
--- a/packages/ra-cubicweb/src/Schema.ts
+++ b/packages/ra-cubicweb/src/Schema.ts
@@ -1,8 +1,35 @@
-export type BuildObj = {
-  type: "String" | "Date" | "Float" | "Int" | "Boolean";
+type BuildObjBase<T extends string> = {
+  type: T;
   required?: boolean;
 };
 
+interface StringBuildObj extends BuildObjBase<"String"> {
+  default?: string;
+}
+
+interface DateBuidObj extends BuildObjBase<"Date"> {
+  default?: Date | "NOW" | "TODAY";
+}
+
+interface FloatBuildObj extends BuildObjBase<"Float"> {
+  default?: number;
+}
+
+interface IntBuildObj extends BuildObjBase<"Int"> {
+  default?: number;
+}
+
+interface BooleanBuildObj extends BuildObjBase<"Boolean"> {
+  default?: boolean;
+}
+
+export type BuildObj =
+  | StringBuildObj
+  | DateBuidObj
+  | FloatBuildObj
+  | IntBuildObj
+  | BooleanBuildObj;
+
 export type Cardinality = "*" | "1" | "?" | "+";
 
 export type CardinalityPair = `${Cardinality}${Cardinality}`;
# HG changeset patch
# User Frank Bessou <frank.bessou@logilab.fr>
# Date 1615397584 -3600
#      Wed Mar 10 18:33:04 2021 +0100
# Node ID d7a1e5300ba6664c5ae5ae68b792abcc4453aa8d
# Parent  4d31c27cdf30cff76319cdc4d956dc73921920e7
feat: make City.name required

diff --git a/packages/demo/src/schema.ts b/packages/demo/src/schema.ts
--- a/packages/demo/src/schema.ts
+++ b/packages/demo/src/schema.ts
@@ -24,7 +24,7 @@
       postal_address: { type: "String" },
     },
     City: {
-      name: { type: "String" },
+      name: { type: "String", required: true },
       zip_code: { type: "Int" },
     },
     Person: {
# HG changeset patch
# User Frank Bessou <frank.bessou@logilab.fr>
# Date 1615397634 -3600
#      Wed Mar 10 18:33:54 2021 +0100
# Node ID c13c029edf856233988bdbe3fb42b9421bb42a6f
# Parent  d7a1e5300ba6664c5ae5ae68b792abcc4453aa8d
feat: Add country attribute to City

It defaults to "France".

diff --git a/packages/demo/src/App.tsx b/packages/demo/src/App.tsx
--- a/packages/demo/src/App.tsx
+++ b/packages/demo/src/App.tsx
@@ -109,6 +109,7 @@
       <TextField source="id" />
       <TextField source="name" />
       <NumberField source="zip_code" />
+      <TextField source="country" />
     </Datagrid>
   </List>
 );
@@ -119,6 +120,7 @@
       <TextField source="id" />
       <TextField source="name" />
       <NumberField source="zip_code" />
+      <TextField source="country" />
       <ReferenceArrayField
         label="Museum"
         reference="Museum"
diff --git a/packages/demo/src/schema.ts b/packages/demo/src/schema.ts
--- a/packages/demo/src/schema.ts
+++ b/packages/demo/src/schema.ts
@@ -26,6 +26,7 @@
     City: {
       name: { type: "String", required: true },
       zip_code: { type: "Int" },
+      country: { type: "String", default: "France" },
     },
     Person: {
       name: { type: "String" },
# HG changeset patch
# User Frank Bessou <frank.bessou@logilab.fr>
# Date 1615397683 -3600
#      Wed Mar 10 18:34:43 2021 +0100
# Node ID 79e4ae8409ae6299dbfec60806d00af35fd10e2e
# Parent  c13c029edf856233988bdbe3fb42b9421bb42a6f
feat: Add forms for City

diff --git a/packages/demo/src/App.tsx b/packages/demo/src/App.tsx
--- a/packages/demo/src/App.tsx
+++ b/packages/demo/src/App.tsx
@@ -114,6 +114,32 @@
   </List>
 );
 
+const CityEdit = (props: ListProps) => {
+  const inputProps = formHelpers["City"].getInputProps;
+  return (
+    <Edit {...props}>
+      <SimpleForm>
+        <TextInput {...inputProps("name")} />
+        <NumberInput {...inputProps("zip_code")} />
+        <TextInput {...inputProps("country")} />
+      </SimpleForm>
+    </Edit>
+  );
+};
+
+const CityCreate = (props: ListProps) => {
+  const inputProps = formHelpers["City"].getInputProps;
+  return (
+    <Create {...props}>
+      <SimpleForm>
+        <TextInput {...inputProps("name")} />
+        <NumberInput {...inputProps("zip_code")} />
+        <TextInput {...inputProps("country")} />
+      </SimpleForm>
+    </Create>
+  );
+};
+
 const CityShow = (props: ShowProps) => (
   <Show {...props}>
     <SimpleShowLayout>
@@ -153,7 +179,13 @@
       edit={MuseumEdit}
       create={MuseumCreate}
     />
-    <Resource name="City" list={CityList} show={CityShow} />
+    <Resource
+      name="City"
+      list={CityList}
+      show={CityShow}
+      edit={CityEdit}
+      create={CityCreate}
+    />
   </Admin>
 );
 
# HG changeset patch
# User Frank Bessou <frank.bessou@logilab.fr>
# Date 1615397704 -3600
#      Wed Mar 10 18:35:04 2021 +0100
# Node ID b96548931ff80500fe32ebecec1c5e60570ee08b
# Parent  79e4ae8409ae6299dbfec60806d00af35fd10e2e
feat: Make formHelper.getInputProps handle default values

diff --git a/packages/ra-cubicweb/src/__tests__/createFormHelpers.ts b/packages/ra-cubicweb/src/__tests__/createFormHelpers.ts
--- a/packages/ra-cubicweb/src/__tests__/createFormHelpers.ts
+++ b/packages/ra-cubicweb/src/__tests__/createFormHelpers.ts
@@ -7,6 +7,7 @@
     User: {
       login: { type: "String", required: true } as const,
       address: { type: "String" } as const,
+      active: { type: "Boolean", default: true } as const,
     },
   },
   relationsDefinitions: [],
@@ -29,5 +30,15 @@
       const props = formHelpers.User.getInputProps("login");
       expect(isRequired(props.validate)).toBe(true);
     });
+
+    it("returns an 'initialValue' property when the attribute has a default value", () => {
+      const props = formHelpers.User.getInputProps("active");
+      expect(props.initialValue).toBe(true);
+    });
+
+    it("returns no 'initialValue' property when the attribute has no default value", () => {
+      const props = formHelpers.User.getInputProps("active");
+      expect(props.initialValue).toBe(true);
+    });
   });
 });
diff --git a/packages/ra-cubicweb/src/createFormHelpers.ts b/packages/ra-cubicweb/src/createFormHelpers.ts
--- a/packages/ra-cubicweb/src/createFormHelpers.ts
+++ b/packages/ra-cubicweb/src/createFormHelpers.ts
@@ -17,13 +17,16 @@
     entityType: SchemaEntityTypes<S>,
     attributeName: EntityAttributeNames<EntityType>
   ) {
-    const entitySchema = schema.entities[entityType];
+    const attributeSchema = schema.entities[entityType][attributeName];
     const props: InputProps = {
       source: attributeName,
     };
-    if (entitySchema[attributeName].required) {
+    if (attributeSchema.required) {
       props.validate = [required()];
     }
+    if (attributeSchema.default !== undefined) {
+      props.initialValue = attributeSchema.default;
+    }
     return props;
   }