Skip to content

Commit

Permalink
feat: enable resolveFields to access parent data
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Nov 15, 2024
1 parent 35170f8 commit 196227b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,10 @@ const config = {

#### Args

| Prop | Example | Type |
| -------- | ------------------------------------------------------------------------- | ------ |
| `data` | `{ props: { title: "Hello, world" }, readOnly: {} }` | Object |
| `params` | `{ appState: {}, changed: {}, fields: {}, lastData: {}, lastFields: {} }` | Object |
| Prop | Example | Type |
| -------- | ------------------------------------------------------------------------------------- | ------ |
| `data` | `{ props: { title: "Hello, world" }, readOnly: {} }` | Object |
| `params` | `{ appState: {}, changed: {}, fields: {}, lastData: {}, lastFields: {}, parent: {} }` | Object |

##### `data.props`

Expand Down Expand Up @@ -461,6 +461,10 @@ The data object from the previous run of this function.

The last fields object created by the previous run of this function.

##### `params.parent`

The parent data object if this item is within a [DropZone](/docs/api-reference/components/drop-zone).

### `resolvePermissions(data, params)`

Dynamically set the [permissions](/docs/api-reference/permissions) for this component to toggle functionality. Can be used to control the permissions on a specific component instance. Inherits [`permissions`](#permissions). Supports asynchronous calls.
Expand Down
4 changes: 4 additions & 0 deletions packages/core/components/Puck/components/Fields/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getClassNameFactory } from "../../../../lib";
import { ReactNode, useCallback, useEffect, useMemo, useState } from "react";
import { ItemSelector } from "../../../../lib/get-item";
import { getChanged } from "../../../../lib/get-changed";
import { useParent } from "../../../../lib/use-parent";

const getClassName = getClassNameFactory("PuckFields", styles);

Expand All @@ -37,6 +38,7 @@ type ComponentOrRootData = Omit<ComponentData<any>, "type">;

const useResolvedFields = (): [FieldsType, boolean] => {
const { selectedItem, state, config } = useAppContext();
const parent = useParent();

const { data } = state;

Expand Down Expand Up @@ -101,6 +103,7 @@ const useResolvedFields = (): [FieldsType, boolean] => {
lastFields: resolvedFields,
lastData: lastData as ComponentData,
appState: state,
parent,
}
);
}
Expand All @@ -112,6 +115,7 @@ const useResolvedFields = (): [FieldsType, boolean] => {
lastFields: resolvedFields,
lastData: lastData as RootData,
appState: state,
parent,
});
}

Expand Down
19 changes: 19 additions & 0 deletions packages/core/lib/use-parent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useContext } from "react";
import { useAppContext } from "../components/Puck/context";
import { getItem, ItemSelector } from "./get-item";
import { dropZoneContext } from "../components/DropZone";
import { convertPathDataToBreadcrumbs } from "./use-breadcrumbs";

export const useParent = (itemSelector?: ItemSelector) => {
const { selectedItem, state } = useAppContext();
const { pathData } = useContext(dropZoneContext) || {};
const item = itemSelector ? getItem(itemSelector, state.data) : selectedItem;
const breadcrumbs = convertPathDataToBreadcrumbs(item, pathData, state.data);

const lastItem = breadcrumbs[breadcrumbs.length - 1];
const parent = lastItem?.selector
? getItem(lastItem.selector, state.data) ?? null
: null;

return parent || null;
};
1 change: 1 addition & 0 deletions packages/core/types/Config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type ComponentConfig<
lastFields: Fields<FieldProps>;
lastData: DataShape | null;
appState: AppState;
parent: ComponentData | null;
}
) => Promise<Fields<FieldProps>> | Fields<FieldProps>;
resolveData?: (
Expand Down

0 comments on commit 196227b

Please sign in to comment.