Skip to content

Commit

Permalink
fix(form): allow form-footer content to be responsive
Browse files Browse the repository at this point in the history
This fix allows the form-footer content to be responsive, the buttons and validation message will
wrap in smaller screen sizes. When buttons are `fullWidth` the `rightSideButtons` will now render
underneath the `saveButton` instead of above.

fix #6658
  • Loading branch information
nuria1110 committed Oct 2, 2024
1 parent f73d0d5 commit c7d55e1
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 91 deletions.
32 changes: 16 additions & 16 deletions src/components/form/__internal__/form-summary.style.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import styled, { css } from "styled-components";
import StyledIcon from "../../icon/icon.style";
import StyledButton from "../../button/button.style";

type StyledFormSummaryProps = {
showSummary?: boolean;
Expand All @@ -9,12 +8,11 @@ type StyledFormSummaryProps = {

export const StyledFormSummary = styled.div<StyledFormSummaryProps>`
display: inline-flex;
flex-wrap: wrap;
align-items: center;
font-size: 13px;
justify-content: end;
font-size: var(--fontSizes100);
font-weight: 500;
margin: -8px;
padding: 8px;
white-space: nowrap;
${({ fullWidth }) =>
fullWidth &&
Expand All @@ -25,21 +23,27 @@ export const StyledFormSummary = styled.div<StyledFormSummaryProps>`
justify-content: flex-start;
`}
${({ showSummary }) =>
${({ showSummary, fullWidth }) =>
showSummary &&
css`
background-color: var(--colorsUtilityMajor025);
border: solid var(--borderWidth100) var(--colorsActionMinor250);
border-radius: var(--borderRadius100);
margin: calc(-1 * var(--sizing100)) 0;
padding: var(--sizing100);
gap: var(--sizing125);
${fullWidth &&
css`
margin: 0 calc(-1 * var(--sizing100));
`}
`}
${StyledButton} {
margin-right: 0;
}
`;

export const StyledMessagePrefix = styled.div`
&:first-of-type {
margin-left: 4px;
margin-left: var(--sizing100);
}
margin-right: 4px;
`;

export type StyledInternalSummaryProps = {
Expand All @@ -49,10 +53,7 @@ export type StyledInternalSummaryProps = {
export const StyledInternalSummary = styled.div<StyledInternalSummaryProps>`
display: flex;
align-items: center;
margin-right: 8px;
&:last-of-type {
margin-right: 16px;
}
gap: var(--sizing100);
${({ type }) =>
type === "warning" &&
css`
Expand All @@ -65,7 +66,6 @@ export const StyledInternalSummary = styled.div<StyledInternalSummaryProps>`
`}
${StyledIcon} {
margin-right: 4px;
${({ type }) =>
type === "warning" &&
css`
Expand Down
47 changes: 45 additions & 2 deletions src/components/form/form-test.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default {
},
};

export const DefaultWithStickyFooter = () => (
export const DefaultWithStickyFooter = ({ ...props }) => (
<Form
onSubmit={() => console.log("submit")}
leftSideButtons={
Expand All @@ -48,7 +48,7 @@ export const DefaultWithStickyFooter = () => (
Save
</Button>
}
stickyFooter
{...props}
>
<Tabs mb={2}>
<Tab
Expand All @@ -72,6 +72,12 @@ export const DefaultWithStickyFooter = () => (
);

DefaultWithStickyFooter.storyName = "default";
DefaultWithStickyFooter.args = {
stickyFooter: true,
errorCount: 0,
warningCount: 0,
buttonAlignment: "right",
};

export const FormAlignmentCustomMarginsTextInputs = () => {
return (
Expand Down Expand Up @@ -485,3 +491,40 @@ MarginTest.parameters = {
},
themeProvider: { chromatic: { theme: "sage" } },
};

export const FullWidthWithLeftAndRight = () => {
return (
<Form
fullWidthButtons
onSubmit={() => console.log("submit")}
leftSideButtons={
<Button onClick={() => console.log("cancel")} fullWidth>
Cancel
</Button>
}
saveButton={
<Button buttonType="primary" type="submit" fullWidth>
Save
</Button>
}
rightSideButtons={
<Button onClick={() => console.log("other")} fullWidth>
Other
</Button>
}
errorCount={3}
warningCount={2}
>
<Textbox label="Textbox" />
<Textbox label="Textbox" />
</Form>
);
};

FullWidthWithLeftAndRight.storyName = "left and right fullWith buttons";
FullWidthWithLeftAndRight.parameters = {
chromatic: {
disableSnapshot: false,
},
themeProvider: { chromatic: { theme: "sage" }, viewports: [1200, 900, 320] },
};
42 changes: 7 additions & 35 deletions src/components/form/form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
StyledFormFooter,
StyledLeftButtons,
StyledRightButtons,
StyledFullWidthButtons,
} from "./form.style";
import { FormButtonAlignment, formSpacing } from "./form.config";
import FormSpacingProvider from "../../__internal__/form-spacing-provider";
Expand Down Expand Up @@ -106,14 +105,15 @@ export const Form = ({
{children}
</FormSpacingProvider>
</StyledFormContent>
{!fullWidthButtons && renderFooter && (
{renderFooter && (
<StyledFormFooter
data-element="form-footer"
data-role="form-footer"
className={classNames}
ref={formFooterRef}
stickyFooter={stickyFooter}
buttonAlignment={buttonAlignment}
fullWidthButtons={fullWidthButtons}
isInModal={isInModal}
{...footerPadding}
>
Expand All @@ -126,7 +126,11 @@ export const Form = ({
</StyledLeftButtons>
)}

<FormSummary errorCount={errorCount} warningCount={warningCount}>
<FormSummary
fullWidth={fullWidthButtons}
errorCount={errorCount}
warningCount={warningCount}
>
{saveButton}
</FormSummary>

Expand All @@ -140,38 +144,6 @@ export const Form = ({
)}
</StyledFormFooter>
)}
{fullWidthButtons && renderFooter && (
<StyledFormFooter
data-element="form-footer"
data-role="form-footer"
className={classNames}
ref={formFooterRef}
stickyFooter={stickyFooter}
buttonAlignment={buttonAlignment}
fullWidthButtons={fullWidthButtons}
{...footerPadding}
>
{leftSideButtons && (
<StyledLeftButtons fullWidthButtons={fullWidthButtons}>
{leftSideButtons}
</StyledLeftButtons>
)}
{rightSideButtons && (
<StyledRightButtons fullWidthButtons={fullWidthButtons}>
{rightSideButtons}
</StyledRightButtons>
)}
<StyledFullWidthButtons>
<FormSummary
fullWidth={fullWidthButtons}
errorCount={errorCount}
warningCount={warningCount}
>
{saveButton}
</FormSummary>
</StyledFullWidthButtons>
</StyledFormFooter>
)}
</StyledForm>
);
};
Expand Down
33 changes: 14 additions & 19 deletions src/components/form/form.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable no-console */
import React, { useState } from "react";
import { Meta, StoryObj } from "@storybook/react";
import { allModes } from "../../../.storybook/modes";
import isChromatic from "../../../.storybook/isChromatic";
import generateStyledSystemProps from "../../../.storybook/utils/styled-system-props";

Expand Down Expand Up @@ -36,11 +35,6 @@ const meta: Meta<typeof Form> = {
},
parameters: {
controls: { disable: true },
chromatic: {
modes: {
desktop: allModes.chromatic,
},
},
},
decorators: [
(Story) => (
Expand Down Expand Up @@ -94,7 +88,7 @@ export const WithFullWidthButtons: Story = () => (
onSubmit={() => console.log("submit")}
stickyFooter
leftSideButtons={
<Button mb={3} onClick={() => console.log("cancel")} fullWidth>
<Button onClick={() => console.log("cancel")} fullWidth>
Cancel
</Button>
}
Expand Down Expand Up @@ -179,6 +173,9 @@ export const WithErrorsSummary: Story = () => (
</Form>
);
WithErrorsSummary.storyName = "With Errors Summary";
WithErrorsSummary.parameters = {
chromatic: { viewports: [1200, 900, 320] },
};

export const WithWarningsSummary: Story = () => (
<Form
Expand All @@ -197,6 +194,9 @@ export const WithWarningsSummary: Story = () => (
</Form>
);
WithWarningsSummary.storyName = "With Warnings Summary";
WithWarningsSummary.parameters = {
chromatic: { viewports: [1200, 900, 320] },
};

export const WithBothErrorsAndWarningsSummary: Story = () => (
<Form
Expand All @@ -217,16 +217,17 @@ export const WithBothErrorsAndWarningsSummary: Story = () => (
);
WithBothErrorsAndWarningsSummary.storyName =
"With Both Errors and Warnings Summary";
WithBothErrorsAndWarningsSummary.parameters = {
chromatic: { viewports: [1200, 900, 320] },
};

export const WithAdditionalButtons: Story = () => (
<Form
onSubmit={() => console.log("submit")}
leftSideButtons={
<>
<Button onClick={() => console.log("cancel")}>Other</Button>
<Button onClick={() => console.log("cancel")} ml={2}>
Cancel
</Button>
<Button onClick={() => console.log("cancel")}>Cancel</Button>
</>
}
saveButton={
Expand All @@ -237,9 +238,7 @@ export const WithAdditionalButtons: Story = () => (
rightSideButtons={
<>
<Button onClick={() => console.log("cancel")}>Reset</Button>
<Button onClick={() => console.log("cancel")} ml={2}>
Other
</Button>
<Button onClick={() => console.log("cancel")}>Other</Button>
</>
}
>
Expand All @@ -254,9 +253,7 @@ export const WithButtonsAlignedToTheLeft: Story = () => (
leftSideButtons={
<>
<Button onClick={() => console.log("cancel")}>Other</Button>
<Button onClick={() => console.log("cancel")} ml={2}>
Cancel
</Button>
<Button onClick={() => console.log("cancel")}>Cancel</Button>
</>
}
saveButton={
Expand All @@ -267,9 +264,7 @@ export const WithButtonsAlignedToTheLeft: Story = () => (
rightSideButtons={
<>
<Button onClick={() => console.log("cancel")}>Reset</Button>
<Button onClick={() => console.log("cancel")} ml={2}>
Other
</Button>
<Button onClick={() => console.log("cancel")}>Other</Button>
</>
}
buttonAlignment="left"
Expand Down
24 changes: 6 additions & 18 deletions src/components/form/form.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import styled, { css } from "styled-components";
import { space, padding } from "styled-system";
import StyledFormField from "../../__internal__/form-field/form-field.style";

import StyledButton from "../button/button.style";
import baseTheme from "../../style/themes/base";
import { FormButtonAlignment } from "./form.config";
import StyledSearch from "../search/search.style";
Expand Down Expand Up @@ -33,6 +32,8 @@ interface ButtonProps extends StyledFormContentProps {
export const StyledFormFooter = styled.div<ButtonProps>`
align-items: center;
display: flex;
flex-wrap: wrap;
gap: var(--sizing200);
${({ buttonAlignment }) =>
buttonAlignment === "right" &&
Expand Down Expand Up @@ -119,30 +120,17 @@ export const StyledForm = styled.form<StyledFormProps>`

export const StyledRightButtons = styled.div<ButtonProps>`
display: flex;
${({ fullWidthButtons }) =>
fullWidthButtons ? `margin-left: 0px;` : `margin-left: 16px;`}
${({ buttonAlignment }) => buttonAlignment === "left" && "flex-grow: 1"};
gap: var(--sizing200);
${StyledButton}:last-child {
margin-right: 0;
}
`;

export const StyledFullWidthButtons = styled.div`
width: 100%;
display: flex;
${({ buttonAlignment }) => buttonAlignment === "left" && "flex-grow: 1;"}
`;

export const StyledLeftButtons = styled.div<ButtonProps>`
display: flex;
justify-content: flex-end;
${({ fullWidthButtons }) =>
fullWidthButtons ? `margin-right: 0px;` : `margin-right: 16px;`}
${({ buttonAlignment }) => buttonAlignment === "right" && "flex-grow: 1"};
gap: var(--sizing200);
${StyledButton}:last-child {
margin-right: 0;
}
${({ buttonAlignment }) => buttonAlignment === "right" && "flex-grow: 1;"}
`;

StyledForm.defaultProps = {
Expand Down
2 changes: 1 addition & 1 deletion src/components/form/form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ test("form summary and footer have correct styles when the `fullWidthButtons` pr
});

// for coverage: stickyFooter prop styles are covered by Chromatic and Playwright
test("has the corrrect styles when the `stickyFooter` prop is set", () => {
test("has the correct styles when the `stickyFooter` prop is set", () => {
render(
<Form
aria-label="form-example"
Expand Down

0 comments on commit c7d55e1

Please sign in to comment.