Skip to content

Commit

Permalink
Order fields on form
Browse files Browse the repository at this point in the history
  • Loading branch information
cregourd committed Jan 12, 2024
1 parent ce1c30c commit c962865
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/pretty-melons-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@premieroctet/next-admin": patch
---

Order fields in form according to display options order
3 changes: 2 additions & 1 deletion packages/next-admin/src/utils/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
getResourceFromParams,
getResourceIdFromParam,
getResources,
orderSchema,
transformData,
transformSchema,
} from "./server";
Expand Down Expand Up @@ -160,8 +161,8 @@ export async function getPropsFromParams({
resource,
searchParams,
options,

);
schema = orderSchema(schema, resource, options);

const customInputs = isAppDir
? getCustomInputs(resource, options)
Expand Down
28 changes: 28 additions & 0 deletions packages/next-admin/src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,41 @@ export const getToStringForRelations = <M extends ModelName>(modelName: M, field
return toStringForRelations;
}

/**
* Order the fields in the schema according to the display option
*
* @param schema
* @param resource
* @param options
*
* @returns schema
*/
export const orderSchema = (schema: Schema, resource: ModelName, options?: NextAdminOptions) => {
const modelName = resource;
const model = models.find((model) => model.name === modelName);
if (!model) return schema;
const edit = options?.model?.[modelName]?.edit as EditOptions<typeof modelName>;
const display = edit?.display;
if (display) {
const properties = schema.definitions[modelName].properties;
const propertiesOrdered = {} as Record<string, any>;
display.forEach((property) => {
propertiesOrdered[property] = properties[property];
});
schema.definitions[modelName].properties = propertiesOrdered;
}
return schema;
}

/**
* Fill fields with relations with the values of the related model, and inject them into the schema
*
* @param schema
* @param prisma
* @param requestOptions
* @param options
*
* @returns schema
*/
export const fillRelationInSchema = async (
schema: Schema,
Expand Down

0 comments on commit c962865

Please sign in to comment.