Skip to content

Commit

Permalink
fix: provide updated props to resolveFields
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Jan 29, 2025
1 parent c22b3a9 commit b7ff689
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
24 changes: 14 additions & 10 deletions packages/core/lib/__tests__/use-resolved-fields.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ describe("use-resolved-fields", () => {
});
});

it("tracks lastFields when rerendering", async () => {
it("calls resolver with appropriate fields on props change", async () => {
const mockResolveFields = jest.fn().mockResolvedValue({
title: { type: "textarea" },
});
Expand All @@ -261,9 +261,13 @@ describe("use-resolved-fields", () => {
params = renderHook(() => useResolvedFields());
});

// update state and trigger re-render
useAppContextMock.mockReturnValue({
...context,
state: { ...context.state, data: { root: { foo: "bar" } } }, // trigger re-render
state: {
...context.state,
data: { ...context.state.data, root: { props: { foo: "bar" } } },
},
});

await act(() => {
Expand All @@ -275,20 +279,20 @@ describe("use-resolved-fields", () => {
expect(result.current[0]).toEqual({ title: { type: "textarea" } });
expect(result.current[1]).toBe(false);
expect(mockResolveFields).toHaveBeenCalledTimes(2);
expect(mockResolveFields).toHaveBeenCalledWith(
{ props: {}, readOnly: undefined },
expect(mockResolveFields.mock.calls[1]).toEqual([
{ props: { foo: "bar" }, readOnly: undefined },
{
appState: {
data: { content: [], root: { props: {} } },
data: { content: [], root: { props: { foo: "bar" } } }, // props changed
ui: { itemSelector: null },
},
changed: {},
changed: { foo: true }, // track changed
fields: { title: { type: "text" } },
lastData: {},
lastFields: { title: { type: "textarea" } },
lastData: { props: {}, readOnly: undefined },
lastFields: { title: { type: "textarea" } }, // track previous fields due to re-render
parent: null,
}
);
},
]);
});
});

Expand Down
18 changes: 14 additions & 4 deletions packages/core/lib/use-resolved-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ export const useResolvedFields = (): [FieldsType, boolean] => {
}
) => defaultFields;

const componentData: ComponentOrRootData = selectedItem
? selectedItem
: { props: rootProps, readOnly: data.root.readOnly };
const componentData: ComponentOrRootData = useMemo(
() =>
selectedItem
? selectedItem
: { props: rootProps, readOnly: data.root.readOnly },
[selectedItem, rootProps, data.root.readOnly]
);

const hasComponentResolver = selectedItem && componentConfig?.resolveFields;
const hasRootResolver = !selectedItem && config.root?.resolveFields;
Expand Down Expand Up @@ -131,7 +135,13 @@ export const useResolvedFields = (): [FieldsType, boolean] => {
}
}
setResolvedFields(defaultFields);
}, [defaultFields, state.ui.itemSelector, hasResolver, parent]);
}, [
defaultFields,
state.ui.itemSelector,
hasResolver,
parent,
resolveFields,
]);

useOnValueChange<ItemSelector | null>(
state.ui.itemSelector,
Expand Down

0 comments on commit b7ff689

Please sign in to comment.