feat: add admin options for entities and app shell

Introduced `BkndAdminEntitiesOptions` and `BkndAdminAppShellOptions` for advanced customization of entity actions, headers, footers, and app shell user menu. Updated related components, hooks, and types for seamless integration with the new configuration options.
This commit is contained in:
dswbx
2025-09-19 11:36:31 +02:00
parent 95d114ea68
commit 26d1f2b583
15 changed files with 234 additions and 35 deletions

View File

@@ -0,0 +1,12 @@
import { useBknd } from "ui/client/BkndProvider";
import type { DropdownProps } from "ui/components/overlay/Dropdown";
export type BkndAdminAppShellOptions = {
userMenu?: DropdownProps["items"];
};
export function useAppShellAdminOptions() {
const { options } = useBknd();
const userMenu = options?.appShell?.userMenu ?? [];
return { userMenu };
}

View File

@@ -0,0 +1,85 @@
import type { DB, Field } from "bknd";
import type { ReactNode } from "react";
import type { Entity } from "data/entities";
import { useBknd } from "ui/client/BkndProvider";
import type { DropdownProps } from "ui/components/overlay/Dropdown";
import type { ButtonProps } from "ui/components/buttons/Button";
export type BkndAdminEntityContext = "list" | "create" | "update";
export type BkndAdminEntitiesOptions = {
[E in keyof DB]?: BkndAdminEntityOptions<E>;
};
export type BkndAdminEntityOptions<E extends keyof DB | string> = {
/**
* Header to be rendered depending on the context
*/
header?: (
context: BkndAdminEntityContext,
entity: Entity,
data?: DB[E],
) => ReactNode | void | undefined;
/**
* Footer to be rendered depending on the context
*/
footer?: (
context: BkndAdminEntityContext,
entity: Entity,
data?: DB[E],
) => ReactNode | void | undefined;
/**
* Actions to be rendered depending on the context
*/
actions?: (
context: BkndAdminEntityContext,
entity: Entity,
data?: DB[E],
) => {
/**
* Primary actions are always visible
*/
primary?: (ButtonProps | undefined | null | false)[];
/**
* Context actions are rendered in a dropdown
*/
context?: DropdownProps["items"];
};
/**
* Field UI overrides
*/
fields?: {
[F in keyof DB[E]]?: BkndAdminEntityFieldOptions<E>;
};
};
export type BkndAdminEntityFieldOptions<E extends keyof DB | string> = {
/**
* Override the rendering of a certain field
*/
render?: (
context: BkndAdminEntityContext,
entity: Entity,
field: Field,
ctx: {
data?: DB[E];
value?: DB[E][keyof DB[E]];
handleChange: (value: any) => void;
},
) => ReactNode | void | undefined;
};
export function useEntityAdminOptions(entity: Entity, context: BkndAdminEntityContext, data?: any) {
const b = useBknd();
const opts = b.options?.entities?.[entity.name];
const footer = opts?.footer?.(context, entity, data) ?? null;
const header = opts?.header?.(context, entity, data) ?? null;
const actions = opts?.actions?.(context, entity, data);
return {
footer,
header,
field: (name: string) => opts?.fields?.[name],
actions,
};
}

View File

@@ -0,0 +1,2 @@
export * from "./entities";
export * from "./app-shell";