Skip to content

Commit

Permalink
Allow passing a function as child to BlocksFinalForm (#3054)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasdax98 authored Jan 10, 2025
1 parent 575f1a7 commit 5583c9c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-bananas-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@comet/admin": minor
---

Export `renderFinalFormChildren` helper
5 changes: 5 additions & 0 deletions .changeset/mighty-vans-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@comet/blocks-admin": patch
---

Allow passing a function as child to `BlocksFinalForm`
4 changes: 2 additions & 2 deletions packages/admin/admin/src/FinalForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { MutableRefObject, PropsWithChildren, useCallback, useContext, useEffect
import { AnyObject, Form, FormRenderProps, FormSpy, RenderableProps } from "react-final-form";
import { useIntl } from "react-intl";

import { renderComponent } from "./finalFormRenderComponent";
import { FinalFormContext, FinalFormContextProvider } from "./form/FinalFormContextProvider";
import { messages } from "./messages";
import { renderFinalFormChildren } from "./renderFinalFormChildren";
import { RouterPrompt } from "./router/Prompt";
import { useSubRoutePrefix } from "./router/SubRoute";
import { Savable, useSaveBoundaryApi } from "./saveBoundary/SaveBoundary";
Expand Down Expand Up @@ -217,7 +217,7 @@ export function FinalForm<FormValues = AnyObject, InitialFormValues = Partial<Fo
<RouterPromptIf formApi={formRenderProps.form} doSave={doSave} subRoutePath={subRoutePath}>
<form onSubmit={submit}>
<div>
{renderComponent<FormValues, InitialFormValues>(
{renderFinalFormChildren<FormValues, InitialFormValues>(
{
children: props.children,
component: props.component,
Expand Down
1 change: 1 addition & 0 deletions packages/admin/admin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ export { MenuItemRouterLink, MenuItemRouterLinkProps } from "./mui/menu/ItemRout
export { Menu, MenuProps } from "./mui/menu/Menu";
export { MenuClassKey } from "./mui/menu/Menu.styles";
export { MuiThemeProvider } from "./mui/ThemeProvider";
export { renderFinalFormChildren } from "./renderFinalFormChildren";
export { RouterBrowserRouter } from "./router/BrowserRouter";
export { RouterConfirmationDialog, RouterConfirmationDialogClassKey, RouterConfirmationDialogProps } from "./router/ConfirmationDialog";
export { RouterContext } from "./router/Context";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createElement } from "react";
import { FormRenderProps, RenderableProps } from "react-final-form";

// Render children like final-form does.
export function renderComponent<FormValues = Record<string, any>, InitialFormValues = Partial<FormValues>>(
export function renderFinalFormChildren<FormValues = Record<string, any>, InitialFormValues = Partial<FormValues>>(
props: RenderableProps<FormRenderProps<FormValues, InitialFormValues>>,
formRenderProps: FormRenderProps<FormValues, InitialFormValues>,
) {
Expand Down
4 changes: 2 additions & 2 deletions packages/admin/admin/src/table/TableFilterFinalForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Component, ReactNode } from "react";
import { Form, FormProps, FormRenderProps } from "react-final-form";
import { FormattedMessage } from "react-intl";

import { renderComponent } from "../finalFormRenderComponent";
import { renderFinalFormChildren } from "../renderFinalFormChildren";
import { IFilterApi } from "./useTableQueryFilter";

type Props<FilterValues = AnyObject> = Omit<FormProps<FilterValues>, "onSubmit" | "initialValues"> & {
Expand Down Expand Up @@ -66,7 +66,7 @@ export class TableFilterFinalForm<FilterValues = AnyObject> extends Component<Pr
</Grid>
)}
<Grid item xs={12}>
{renderComponent(this.props, formRenderProps)}
{renderFinalFormChildren(this.props, formRenderProps)}
</Grid>
</Grid>
</form>
Expand Down
17 changes: 13 additions & 4 deletions packages/admin/blocks-admin/src/form/BlocksFinalForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FinalFormContextProvider, FinalFormContextProviderProps } from "@comet/admin";
import { AnyObject, Form, FormProps, FormSpy } from "react-final-form";
import { FinalFormContextProvider, FinalFormContextProviderProps, renderFinalFormChildren } from "@comet/admin";
import { AnyObject, Form, FormProps, FormRenderProps, FormSpy } from "react-final-form";

interface AutoSaveSpyProps<FormValues> {
onSubmit: FormProps<FormValues>["onSubmit"];
Expand Down Expand Up @@ -34,11 +34,20 @@ const finalFormContextValues: Omit<FinalFormContextProviderProps, "children"> =
export function BlocksFinalForm<FormValues = AnyObject>({ onSubmit, children, ...rest }: FormProps<FormValues>): JSX.Element {
return <Form {...rest} render={renderForm} onSubmit={noop} />;

function renderForm() {
function renderForm(props: FormRenderProps<FormValues, Partial<FormValues>>) {
return (
<>
<AutosaveSpy<FormValues> onSubmit={onSubmit} />
<FinalFormContextProvider {...finalFormContextValues}>{children}</FinalFormContextProvider>
<FinalFormContextProvider {...finalFormContextValues}>
{renderFinalFormChildren(
{
children: children,
component: rest.component,
render: rest.render,
},
props,
)}
</FinalFormContextProvider>
</>
);
}
Expand Down

0 comments on commit 5583c9c

Please sign in to comment.