Skip to content

Commit

Permalink
fix: inline forms layout
Browse files Browse the repository at this point in the history
- fixes node name edit forms layout when displayed in the title
  • Loading branch information
petermakowski committed Mar 20, 2024
1 parent 6dc7b2e commit b9b0ec5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
render,
screen,
renderWithBrowserRouter,
renderWithMockStore,
} from "@/testing/utils";

const mockStore = configureStore<RootState>();
Expand Down Expand Up @@ -497,4 +498,21 @@ describe("FormikFormContent", () => {

expect(screen.getByTestId("footer")).toBeInTheDocument();
});

it("renders inline form correctly when inline prop is true", () => {
renderWithMockStore(
<Formik initialValues={{}} onSubmit={vi.fn()}>
<FormikFormContent aria-label="inline form" inline>
<Field aria-label="test field" name="testField" />
</FormikFormContent>
</Formik>
);

expect(screen.getByRole("form", { name: "inline form" })).toHaveClass(
"p-form--inline"
);
expect(
screen.getByRole("textbox", { name: "test field" })
).toBeInTheDocument();
});
});
70 changes: 40 additions & 30 deletions src/app/base/components/FormikFormContent/FormikFormContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ const FormikFormContent = <V extends object, E = null>({
}, [onSuccess, hasErrors, hasSaved, values]);

// Send an analytics event when form is saved.

useSendAnalyticsWhen(
saved,
onSaveAnalytics.category,
Expand All @@ -167,44 +168,53 @@ const FormikFormContent = <V extends object, E = null>({
}
}, [navigate, savedRedirect, saved]);

const renderFormContent = () => (
<>
{!!nonFieldError && (
<Notification severity="negative" title="Error:">
{nonFieldError}
</Notification>
)}
{typeof children === "function" ? children(formikContext) : children}
</>
);

const renderFormButtons = () => (
<FormikFormButtons
{...buttonsProps}
buttonsBehavior={buttonsBehavior}
cancelDisabled={cancelDisabled === false ? false : saving}
inline={inline}
saved={saved}
saving={saving}
submitDisabled={loading || saving || formDisabled || submitDisabled}
/>
);

return (
<Form
aria-label={ariaLabel}
className={className}
inline={inline}
onSubmit={handleSubmit}
>
{/* collapse content section when there's no content */}
<ContentSection className={!children ? "u-no-padding--top" : undefined}>
<ContentSection.Content>
{!!nonFieldError && (
<Notification severity="negative" title="Error:">
{nonFieldError}
</Notification>
)}
{typeof children === "function"
? children({ ...formikContext })
: children}
</ContentSection.Content>
<ContentSection.Footer
className={!children ? "u-no-margin--top" : undefined}
>
{editable && (
<FormikFormButtons
{...buttonsProps}
buttonsBehavior={buttonsBehavior}
cancelDisabled={cancelDisabled === false ? false : saving}
inline={inline}
saved={saved}
saving={saving}
submitDisabled={
loading || saving || formDisabled || submitDisabled
}
/>
)}
{inline ? (
<>
{renderFormContent()}
{editable && renderFormButtons()}
{footer}
</ContentSection.Footer>
</ContentSection>
</>
) : (
<ContentSection className={!children ? "u-no-padding--top" : undefined}>
<ContentSection.Content>{renderFormContent()}</ContentSection.Content>
<ContentSection.Footer
className={!children ? "u-no-margin--top" : undefined}
>
{editable && renderFormButtons()}
{footer}
</ContentSection.Footer>
</ContentSection>
)}
</Form>
);
};
Expand Down

0 comments on commit b9b0ec5

Please sign in to comment.