UNPKG

@premieroctet/next-admin

Version:

Next-Admin provides a customizable and turnkey admin dashboard for applications built with Next.js and powered by the Prisma ORM. It aims to simplify the development process by providing a turnkey admin system that can be easily integrated into your proje

101 lines (100 loc) 4.6 kB
import { Fragment, jsx, jsxs } from "react/jsx-runtime"; import { ChevronRightIcon } from "@heroicons/react/24/outline"; import external_clsx_default from "clsx"; import { useMemo, useState } from "react"; import { useConfig } from "../../context/ConfigContext.mjs"; import { useI18n } from "../../context/I18nContext.mjs"; import { contentTypeFromSchemaType, isFieldNullable, isSchemaPropertyScalarArray } from "../../utils/advancedSearch.mjs"; import { DropdownItem } from "../radix/Dropdown.mjs"; const AdvancedSearchDropdownItem = ({ property, schema, resource, path, onAddBlock, displayPath })=>{ const { options } = useConfig(); const { t } = useI18n(); const schemaDef = schema.definitions[resource]; const schemaProperty = schemaDef.properties[property]; const hasChildren = isSchemaPropertyScalarArray(schemaDef, property) ? false : schemaProperty?.__nextadmin?.relation || schemaProperty?.type === "array"; const [openChildren, setOpenChildren] = useState(false); const childResource = useMemo(()=>{ if (!hasChildren) return null; const modelRef = schemaProperty?.type === "array" ? schemaProperty?.items?.$ref : schemaProperty?.__nextadmin?.relation?.$ref; const model = modelRef?.split("/")?.at(-1); const schemaModel = schema.definitions[model]; if (!schemaModel) return null; return { model: model, properties: Object.keys(schemaModel.properties) }; }, [ hasChildren, schemaProperty, schema ]); const aliases = options?.model?.[resource]?.aliases; const displayedProperty = t(`model.${resource}.fields.${property}`, {}, aliases?.[property] ?? property); const onClick = (evt)=>{ if (hasChildren) { evt.preventDefault(); setOpenChildren((prev)=>!prev); } else onAddBlock({ type: "filter", path: [ path, property ].filter(Boolean).join("."), value: "", contentType: contentTypeFromSchemaType(schemaProperty), enum: schemaProperty?.enum?.filter(Boolean) ?? [], defaultValue: schemaProperty?.default, canHaveChildren: false, condition: "equals", id: crypto.randomUUID(), nullable: isFieldNullable(schemaProperty.type), displayPath: [ displayPath, displayedProperty ].filter(Boolean).join(" \u2192 ") }); }; return /*#__PURE__*/ jsxs(Fragment, { children: [ /*#__PURE__*/ jsx(DropdownItem, { className: "cursor-pointer rounded-md p-2", onClick: onClick, children: /*#__PURE__*/ jsxs("div", { className: "flex items-center justify-between", children: [ displayedProperty, hasChildren && /*#__PURE__*/ jsx(ChevronRightIcon, { className: external_clsx_default("h-5 w-5 transition-all", { "rotate-90 transform": openChildren }), "aria-hidden": "true", onClick: (e)=>{ e.stopPropagation(); setOpenChildren((prev)=>!prev); } }) ] }) }), childResource && openChildren && /*#__PURE__*/ jsx("div", { className: "border-l-nextadmin-border-strong dark:border-l-dark-nextadmin-border-strong ml-2 border-l pl-2", children: childResource.properties.map((childProperty)=>/*#__PURE__*/ jsx(AdvancedSearchDropdownItem, { property: childProperty, schema: schema, resource: childResource.model, path: [ path, property ].filter(Boolean).join("."), displayPath: [ displayPath, displayedProperty ].filter(Boolean).join(" \u2192 "), onAddBlock: onAddBlock }, childProperty)) }) ] }); }; const advancedSearch_AdvancedSearchDropdownItem = AdvancedSearchDropdownItem; export { advancedSearch_AdvancedSearchDropdownItem as default };