diff --git a/.changeset/pretty-melons-develop.md b/.changeset/pretty-melons-develop.md new file mode 100644 index 00000000..e36ff0f5 --- /dev/null +++ b/.changeset/pretty-melons-develop.md @@ -0,0 +1,5 @@ +--- +"@premieroctet/next-admin": patch +--- + +Order fields in form according to display options order diff --git a/packages/next-admin/src/utils/props.ts b/packages/next-admin/src/utils/props.ts index 8cf0e0a6..ff4c8ad7 100644 --- a/packages/next-admin/src/utils/props.ts +++ b/packages/next-admin/src/utils/props.ts @@ -20,6 +20,7 @@ import { getResourceFromParams, getResourceIdFromParam, getResources, + orderSchema, transformData, transformSchema, } from "./server"; @@ -160,8 +161,8 @@ export async function getPropsFromParams({ resource, searchParams, options, - ); + schema = orderSchema(schema, resource, options); const customInputs = isAppDir ? getCustomInputs(resource, options) diff --git a/packages/next-admin/src/utils/server.ts b/packages/next-admin/src/utils/server.ts index c37b9693..3b740778 100644 --- a/packages/next-admin/src/utils/server.ts +++ b/packages/next-admin/src/utils/server.ts @@ -58,6 +58,32 @@ export const getToStringForRelations = (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; + const display = edit?.display; + if (display) { + const properties = schema.definitions[modelName].properties; + const propertiesOrdered = {} as Record; + 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 * @@ -65,6 +91,8 @@ export const getToStringForRelations = (modelName: M, field * @param prisma * @param requestOptions * @param options + * + * @returns schema */ export const fillRelationInSchema = async ( schema: Schema,