Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order fields in form #108

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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