diff --git a/apps/example/e2e/utils.ts b/apps/example/e2e/utils.ts index d424be56..fa47b4a6 100644 --- a/apps/example/e2e/utils.ts +++ b/apps/example/e2e/utils.ts @@ -49,7 +49,7 @@ export const createItem = async ( await fillForm(model, page, dataTest); - await page.click('button[type="submit"]'); + await page.click('button[name="save_edit"]'); await page.waitForURL((url) => !url.pathname.endsWith("/new")); const url = new URL(page.url()); diff --git a/apps/example/options.tsx b/apps/example/options.tsx index f77846a6..8ca39a00 100644 --- a/apps/example/options.tsx +++ b/apps/example/options.tsx @@ -97,6 +97,10 @@ export const options: NextAdminOptions = { birthDate: { input: , }, + posts: { + display: "list", + orderField: "order", + }, avatar: { format: "file", handler: { @@ -166,13 +170,12 @@ export const options: NextAdminOptions = { format: "richtext-html", }, categories: { - optionFormatter: (category) => { + relationOptionFormatter: (category) => { return `${category.name} Cat.${category.id}`; }, display: "list", orderField: "order", relationshipSearchField: "category", - isExplicitManyToMany: true, }, }, display: [ @@ -196,6 +199,12 @@ export const options: NextAdminOptions = { }, edit: { display: ["name", "posts"], + fields: { + posts: { + display: "list", + relationshipSearchField: 'post' + }, + }, }, }, }, diff --git a/apps/example/prisma/json-schema/json-schema.json b/apps/example/prisma/json-schema/json-schema.json index 10104cb8..184485ae 100644 --- a/apps/example/prisma/json-schema/json-schema.json +++ b/apps/example/prisma/json-schema/json-schema.json @@ -109,6 +109,10 @@ "number", "null" ] + }, + "order": { + "type": "integer", + "default": 0 } }, "required": [ diff --git a/apps/example/prisma/migrations/20240524121923_order_posts/migration.sql b/apps/example/prisma/migrations/20240524121923_order_posts/migration.sql new file mode 100644 index 00000000..6f59e3cd --- /dev/null +++ b/apps/example/prisma/migrations/20240524121923_order_posts/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Post" ADD COLUMN "order" INTEGER NOT NULL DEFAULT 0; diff --git a/apps/example/prisma/schema.prisma b/apps/example/prisma/schema.prisma index ad9679a1..27740db3 100644 --- a/apps/example/prisma/schema.prisma +++ b/apps/example/prisma/schema.prisma @@ -45,6 +45,7 @@ model Post { authorId Int categories CategoriesOnPosts[] rate Decimal? @db.Decimal(5, 2) + order Int @default(0) } model Profile { diff --git a/packages/next-admin/src/actions/form.ts b/packages/next-admin/src/actions/form.ts index 1b4f9223..d824f859 100644 --- a/packages/next-admin/src/actions/form.ts +++ b/packages/next-admin/src/actions/form.ts @@ -81,14 +81,15 @@ export const submitForm = async ( // Validate validate(parsedFormData, fields); - const { formattedData, errors } = await formattedFormData( - formValues, - dmmfSchema?.fields!, - schema, - resource, - resourceId, - fields - ); + const { formattedData, complementaryFormattedData, errors } = + await formattedFormData( + formValues, + dmmfSchema?.fields!, + schema, + resource, + resourceId, + fields + ); if (errors.length) { return { @@ -138,6 +139,14 @@ export const submitForm = async ( data: formattedData, }); + // @ts-expect-error + await prisma[resource].update({ + where: { + [resourceIdField]: data[resourceIdField], + }, + data: complementaryFormattedData, + }); + return { created: true, createdId: data[resourceIdField], diff --git a/packages/next-admin/src/components/Form.tsx b/packages/next-admin/src/components/Form.tsx index 58a6f8f1..7121bc46 100644 --- a/packages/next-admin/src/components/Form.tsx +++ b/packages/next-admin/src/components/Form.tsx @@ -158,19 +158,15 @@ const Form = ({ } return ( -
+
{((edit && canEdit) || (!edit && canCreate)) && ( -
+
diff --git a/packages/next-admin/src/components/inputs/MultiSelect/MultiSelectDisplayList.tsx b/packages/next-admin/src/components/inputs/MultiSelect/MultiSelectDisplayList.tsx index 621fcedf..eed80d53 100644 --- a/packages/next-admin/src/components/inputs/MultiSelect/MultiSelectDisplayList.tsx +++ b/packages/next-admin/src/components/inputs/MultiSelect/MultiSelectDisplayList.tsx @@ -1,8 +1,8 @@ +import { DndContext, DragEndEvent } from "@dnd-kit/core"; +import { SortableContext } from "@dnd-kit/sortable"; import { RJSFSchema } from "@rjsf/utils"; import { Enumeration } from "../../../types"; import MultiSelectDisplayListItem from "./MultiSelectDisplayListItem"; -import { DndContext, DragEndEvent } from "@dnd-kit/core"; -import { SortableContext } from "@dnd-kit/sortable"; type Props = { formData: any; diff --git a/packages/next-admin/src/components/inputs/MultiSelect/MultiSelectDisplayListItem.tsx b/packages/next-admin/src/components/inputs/MultiSelect/MultiSelectDisplayListItem.tsx index 6d330eb5..cc705f2f 100644 --- a/packages/next-admin/src/components/inputs/MultiSelect/MultiSelectDisplayListItem.tsx +++ b/packages/next-admin/src/components/inputs/MultiSelect/MultiSelectDisplayListItem.tsx @@ -1,16 +1,16 @@ -import clsx from "clsx"; -import { Enumeration } from "../../../types"; -import Link from "next/link"; -import { useConfig } from "../../../context/ConfigContext"; -import { slugify } from "../../../utils/tools"; +import { useSortable } from "@dnd-kit/sortable"; +import { CSS } from "@dnd-kit/utilities"; import { ArrowTopRightOnSquareIcon, - Squares2X2Icon, + Bars2Icon, XMarkIcon, } from "@heroicons/react/24/outline"; -import { useSortable } from "@dnd-kit/sortable"; -import { CSS } from "@dnd-kit/utilities"; import { RJSFSchema } from "@rjsf/utils"; +import clsx from "clsx"; +import Link from "next/link"; +import { useConfig } from "../../../context/ConfigContext"; +import { Enumeration } from "../../../types"; +import { slugify } from "../../../utils/tools"; type Props = { item: Enumeration; @@ -46,7 +46,7 @@ const MultiSelectDisplayListItem = ({ return (
  • - +
  • )} {label} @@ -69,8 +69,9 @@ const MultiSelectDisplayListItem = ({
    diff --git a/packages/next-admin/src/components/inputs/SelectWidget.tsx b/packages/next-admin/src/components/inputs/SelectWidget.tsx index ba5c0dee..8727f741 100644 --- a/packages/next-admin/src/components/inputs/SelectWidget.tsx +++ b/packages/next-admin/src/components/inputs/SelectWidget.tsx @@ -43,7 +43,7 @@ const SelectWidget = ({ return (
    -
    +