From 6cd336235d8abaebbcab0572cacc57675b0e57b6 Mon Sep 17 00:00:00 2001 From: Colin Regourd Date: Wed, 15 May 2024 14:17:44 +0200 Subject: [PATCH 1/2] Add slug inside breadcrumb --- .changeset/selfish-shoes-laugh.md | 5 +++++ packages/next-admin/src/components/Form.tsx | 4 +++- packages/next-admin/src/components/NextAdmin.tsx | 2 ++ packages/next-admin/src/types.ts | 1 + packages/next-admin/src/utils/props.ts | 8 ++++++++ packages/next-admin/src/utils/server.ts | 12 ++++++++++++ 6 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 .changeset/selfish-shoes-laugh.md diff --git a/.changeset/selfish-shoes-laugh.md b/.changeset/selfish-shoes-laugh.md new file mode 100644 index 00000000..25194dc6 --- /dev/null +++ b/.changeset/selfish-shoes-laugh.md @@ -0,0 +1,5 @@ +--- +"@premieroctet/next-admin": patch +--- + +Add slug inside breadcrumb (#263) diff --git a/packages/next-admin/src/components/Form.tsx b/packages/next-admin/src/components/Form.tsx index 00dffde0..fb253118 100644 --- a/packages/next-admin/src/components/Form.tsx +++ b/packages/next-admin/src/components/Form.tsx @@ -78,6 +78,7 @@ export type FormProps = { schema: any; dmmfSchema: readonly Prisma.DMMF.Field[]; resource: ModelName; + slug?: string; validation?: PropertyValidationError[]; action?: (formData: FormData) => Promise; title: string; @@ -107,6 +108,7 @@ const Form = ({ schema, dmmfSchema, resource, + slug, validation: validationProp, action, title, @@ -479,7 +481,7 @@ const Form = ({ if (edit && id) { breadcrumItems.push({ - label: id.toString(), + label: slug ?? id.toString(), href: `${basePath}/${slugify(resource)}/${id}`, current: true, }); diff --git a/packages/next-admin/src/components/NextAdmin.tsx b/packages/next-admin/src/components/NextAdmin.tsx index b17e480c..ce2f05a3 100644 --- a/packages/next-admin/src/components/NextAdmin.tsx +++ b/packages/next-admin/src/components/NextAdmin.tsx @@ -15,6 +15,7 @@ export function NextAdmin({ resource, schema, resources, + slug, message, error, total, @@ -79,6 +80,7 @@ export function NextAdmin({ return (
; resource?: ModelName; + slug?: string; /** * Page router only */ diff --git a/packages/next-admin/src/utils/props.ts b/packages/next-admin/src/utils/props.ts index 989fbf47..0d6ad93b 100644 --- a/packages/next-admin/src/utils/props.ts +++ b/packages/next-admin/src/utils/props.ts @@ -26,6 +26,7 @@ import { getResourceFromParams, getResourceIdFromParam, getResources, + getToStringForModel, transformData, transformSchema, } from "./server"; @@ -273,11 +274,18 @@ export async function getPropsFromParams({ } }); + const toStringFunction = getToStringForModel( + options?.model?.[resource] + ); + const slug = toStringFunction + ? toStringFunction(data) + : resourceId.toString(); data = transformData(data, resource, edit, options); return { ...defaultProps, resource, data, + slug, schema: deepCopySchema, dmmfSchema: dmmfSchema?.fields, customInputs, diff --git a/packages/next-admin/src/utils/server.ts b/packages/next-admin/src/utils/server.ts index d5943587..ecb5e05b 100644 --- a/packages/next-admin/src/utils/server.ts +++ b/packages/next-admin/src/utils/server.ts @@ -8,6 +8,7 @@ import { EditOptions, Enumeration, Field, + Model, ModelName, ModelWithoutRelationships, NextAdminOptions, @@ -78,6 +79,17 @@ export const getToStringForRelations = ( return toStringForRelations; }; +export const getToStringForModel = ( + options: Required["model"][M] +): ((item: Model) => string) | undefined => { + const nonCheckedToString = options?.toString; + const toStringForRelations = + nonCheckedToString && !isNativeFunction(nonCheckedToString) + ? nonCheckedToString + : undefined; + return toStringForRelations; +}; + /** * Order the fields in the schema according to the display option * From f2551a38676309536a8efb72be33315525119cb0 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 16 May 2024 09:46:50 +0000 Subject: [PATCH 2/2] Version Packages --- .changeset/selfish-shoes-laugh.md | 5 ----- packages/next-admin/CHANGELOG.md | 6 ++++++ packages/next-admin/package.json | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) delete mode 100644 .changeset/selfish-shoes-laugh.md diff --git a/.changeset/selfish-shoes-laugh.md b/.changeset/selfish-shoes-laugh.md deleted file mode 100644 index 25194dc6..00000000 --- a/.changeset/selfish-shoes-laugh.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@premieroctet/next-admin": patch ---- - -Add slug inside breadcrumb (#263) diff --git a/packages/next-admin/CHANGELOG.md b/packages/next-admin/CHANGELOG.md index 187002bf..8d3d3f61 100644 --- a/packages/next-admin/CHANGELOG.md +++ b/packages/next-admin/CHANGELOG.md @@ -1,5 +1,11 @@ # @premieroctet/next-admin +## 4.2.2 + +### Patch Changes + +- 6cd3362: Add slug inside breadcrumb (#263) + ## 4.2.1 ### Patch Changes diff --git a/packages/next-admin/package.json b/packages/next-admin/package.json index a1be3d52..3e0d8617 100644 --- a/packages/next-admin/package.json +++ b/packages/next-admin/package.json @@ -1,6 +1,6 @@ { "name": "@premieroctet/next-admin", - "version": "4.2.1", + "version": "4.2.2", "main": "./dist/index.js", "types": "./dist/index.d.ts", "license": "MIT",