Skip to content

Commit

Permalink
Merge pull request #111 from premieroctet/fix/search-select
Browse files Browse the repository at this point in the history
Fix enum search select
  • Loading branch information
cregourd authored Jan 15, 2024
2 parents 4cac944 + 821baf5 commit 470cd9a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-apricots-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@premieroctet/next-admin": patch
---

Add limit of 20 items on select for relationship
5 changes: 5 additions & 0 deletions .changeset/fifty-mice-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@premieroctet/next-admin": patch
---

Fix search in enumeration selector
15 changes: 9 additions & 6 deletions packages/next-admin/src/utils/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
transformData,
transformSchema,
} from "./server";
import { cloneDeep } from "lodash";

export type GetPropsFromParamsParams = {
params?: string[];
Expand Down Expand Up @@ -154,15 +155,17 @@ export async function getPropsFromParams({
const edit = options?.model?.[resource]?.edit as EditOptions<
typeof resource
>;
schema = transformSchema(schema, resource, edit);
schema = await fillRelationInSchema(
schema,

let deepCopySchema = cloneDeep(schema);
deepCopySchema = transformSchema(deepCopySchema, resource, edit);
deepCopySchema = await fillRelationInSchema(
deepCopySchema,
prisma,
resource,
searchParams,
options,
);
schema = orderSchema(schema, resource, options);
deepCopySchema = orderSchema(deepCopySchema, resource, options);

const customInputs = isAppDir
? getCustomInputs(resource, options)
Expand All @@ -188,7 +191,7 @@ export async function getPropsFromParams({
...defaultProps,
resource,
data,
schema,
schema: deepCopySchema,
dmmfSchema: dmmfSchema?.fields,
customInputs,
actions: isAppDir ? actions : undefined,
Expand All @@ -199,7 +202,7 @@ export async function getPropsFromParams({
return {
...defaultProps,
resource,
schema,
schema: deepCopySchema,
dmmfSchema: dmmfSchema?.fields,
customInputs,
};
Expand Down
9 changes: 9 additions & 0 deletions packages/next-admin/src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,14 @@ export const fillRelationInSchema = async (
fieldValue.enum = fieldValue.enum?.map((item) =>
typeof item !== "object" ? { label: item, value: item } : item
);
const search = requestOptions[`${fieldName}search`];
if (search) {
fieldValue.enum = fieldValue.enum?.filter((item: any) =>
item.label.toLowerCase().includes(search.toLowerCase())
);
}
}

if (fieldValue?.default) {
fieldValue.default = typeof fieldValue.default !== "object" ? { label: fieldValue.default, value: fieldValue.default } : fieldValue.default;
}
Expand Down Expand Up @@ -160,6 +167,7 @@ export const fillRelationInSchema = async (
// @ts-expect-error
.findMany({
where,
take: 20,
})
.then((data: any[]) =>
data.forEach((item) => {
Expand Down Expand Up @@ -195,6 +203,7 @@ export const fillRelationInSchema = async (
// @ts-expect-error
.findMany({
where,
take: 20,
})
.then((data: any[]) =>
data.forEach((item) => {
Expand Down

0 comments on commit 470cd9a

Please sign in to comment.