Skip to content

Commit 7ee65a0

Browse files
committed
fix(form): replace use of context to apply field spacing
The use of form spacing context to apply spacing to our inputs within Form has caused issues for consumers using it in exported components in different projects using Carbon. This fix replaces context with a pure css solution to ensure margins are applied and consistent across projects. fix #7058
1 parent e22ebde commit 7ee65a0

39 files changed

+184
-374
lines changed

src/__internal__/fieldset/fieldset.component.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import {
1111
import ValidationIcon from "../validations/validation-icon.component";
1212
import NewValidationContext from "../../components/carbon-provider/__internal__/new-validation.context";
1313
import { InputGroupBehaviour, InputGroupContext } from "../input-behaviour";
14-
import useFormSpacing from "../../hooks/__internal__/useFormSpacing";
1514
import Help from "../../components/help";
1615
import Typography from "../../components/typography";
16+
import { filterStyledSystemMarginProps } from "../../style/utils";
1717

1818
export interface FieldsetProps extends MarginProps {
1919
/** Role */
@@ -84,7 +84,7 @@ const Fieldset = ({
8484
...rest
8585
}: FieldsetProps) => {
8686
const { validationRedesignOptIn } = useContext(NewValidationContext);
87-
const marginProps = useFormSpacing(rest);
87+
const marginProps = filterStyledSystemMarginProps(rest);
8888
const [ref, setRef] = useState<HTMLFieldSetElement | null>(null);
8989
const [isFocused, setFocus] = useState(false);
9090

src/__internal__/fieldset/fieldset.style.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type StyledFieldsetProps = {
88

99
const StyledFieldset = styled.fieldset<StyledFieldsetProps>`
1010
margin: 0;
11+
margin-bottom: var(--fieldSpacing);
1112
${margin}
1213
border: none;
1314
padding: 0;

src/__internal__/form-field/form-field.component.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import TabContext, {
1919
} from "../../components/tabs/tab/__internal__/tab.context";
2020
import useIsAboveBreakpoint from "../../hooks/__internal__/useIsAboveBreakpoint";
2121
import { IconType } from "../../components/icon";
22-
import useFormSpacing from "../../hooks/__internal__/useFormSpacing";
22+
import { filterStyledSystemMarginProps } from "../../style/utils";
2323

2424
interface CommonFormFieldProps extends MarginProps, ValidationProps {
2525
/** If true, the component will be disabled */
@@ -149,7 +149,7 @@ const FormField = ({
149149

150150
const { setError, setWarning, setInfo } =
151151
useContext<TabContextProps>(TabContext);
152-
const marginProps = useFormSpacing(rest);
152+
const marginProps = filterStyledSystemMarginProps(rest);
153153
const isMounted = useRef(false);
154154

155155
useLayoutEffect(() => {

src/__internal__/form-field/form-field.style.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { space } from "styled-system";
33
import { baseTheme } from "../../style/themes";
44

55
const FormFieldStyle = styled.div`
6+
margin-bottom: var(--fieldSpacing);
67
& + & {
78
margin-top: 16px;
89
}

src/__internal__/form-spacing-provider/form-spacing-provider.component.tsx

-20
This file was deleted.

src/__internal__/form-spacing-provider/form-spacing.context.ts

-9
This file was deleted.

src/__internal__/form-spacing-provider/index.ts

-3
This file was deleted.

src/components/checkbox/checkbox-group/checkbox-group.component.tsx

+2-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import Fieldset from "../../../__internal__/fieldset";
88
import { filterStyledSystemMarginProps } from "../../../style/utils";
99
import { TooltipProvider } from "../../../__internal__/tooltip-provider";
1010
import { ValidationProps } from "../../../__internal__/validations";
11-
import FormSpacingProvider from "../../../__internal__/form-spacing-provider";
1211
import NewValidationContext from "../../carbon-provider/__internal__/new-validation.context";
1312
import ValidationMessage from "../../../__internal__/validation-message/validation-message.component";
1413
import Box from "../../box";
@@ -134,9 +133,7 @@ export const CheckboxGroup = ({
134133
warning: !!warning,
135134
}}
136135
>
137-
<FormSpacingProvider marginBottom={undefined}>
138-
{children}
139-
</FormSpacingProvider>
136+
{children}
140137
</CheckboxGroupContext.Provider>
141138
</StyledCheckboxGroup>
142139
</Box>
@@ -173,9 +170,7 @@ export const CheckboxGroup = ({
173170
info: !!info,
174171
}}
175172
>
176-
<FormSpacingProvider marginBottom={undefined}>
177-
{children}
178-
</FormSpacingProvider>
173+
{children}
179174
</CheckboxGroupContext.Provider>
180175
</StyledCheckboxGroup>
181176
</Fieldset>

src/components/checkbox/checkbox.component.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import useIsAboveBreakpoint from "../../hooks/__internal__/useIsAboveBreakpoint"
1111
import { TooltipProvider } from "../../__internal__/tooltip-provider";
1212
import CheckboxGroupContext from "./checkbox-group/__internal__/checkbox-group.context";
1313
import Logger from "../../__internal__/utils/logger";
14-
import useFormSpacing from "../../hooks/__internal__/useFormSpacing";
1514
import NewValidationContext from "../carbon-provider/__internal__/new-validation.context";
15+
import { filterStyledSystemMarginProps } from "../../style/utils";
1616

1717
export interface CheckboxProps
1818
extends CommonCheckableInputProps,
@@ -131,7 +131,7 @@ export const Checkbox = React.forwardRef(
131131
: { info: contextInfo || info }),
132132
};
133133

134-
const marginProps = useFormSpacing(rest);
134+
const marginProps = filterStyledSystemMarginProps(rest);
135135

136136
const componentToRender = (
137137
<CheckboxStyle

src/components/checkbox/checkbox.style.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface StyledCheckboxProps extends ValidationProps, MarginProps {
2525
}
2626

2727
const StyledCheckbox = styled.div<StyledCheckboxProps>`
28+
margin-bottom: var(--fieldSpacing);
2829
${margin}
2930
${({
3031
disabled,

src/components/date-range/date-range.style.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface StyledDateRangeProps {
1010
labelsInline?: boolean;
1111
}
1212
const StyledDateRange = styled.div<StyledDateRangeProps>`
13+
margin-bottom: var(--fieldSpacing);
1314
${margin}
1415
1516
& ${StyledDateInput} {

src/components/date/date.component.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ import {
2222
import useLocale from "../../hooks/__internal__/useLocale";
2323
import Logger from "../../__internal__/utils/logger";
2424
import Events from "../../__internal__/utils/helpers/events";
25-
import { filterOutStyledSystemSpacingProps } from "../../style/utils";
25+
import {
26+
filterOutStyledSystemSpacingProps,
27+
filterStyledSystemMarginProps,
28+
} from "../../style/utils";
2629
import getFormatData from "./__internal__/date-formats";
2730
import StyledDateInput from "./date.style";
2831
import Textbox, { TextboxProps } from "../textbox";
@@ -31,7 +34,6 @@ import DateRangeContext, {
3134
InputName,
3235
} from "../date-range/__internal__/date-range.context";
3336
import useClickAwayListener from "../../hooks/__internal__/useClickAwayListener";
34-
import useFormSpacing from "../../hooks/__internal__/useFormSpacing";
3537
import guid from "../../__internal__/utils/helpers/guid";
3638
import tagComponent from "../../__internal__/utils/helpers/tags/tags";
3739

@@ -492,7 +494,7 @@ export const DateInput = React.forwardRef<HTMLInputElement, DateInputProps>(
492494
return value;
493495
};
494496

495-
const marginProps = useFormSpacing(rest);
497+
const marginProps = filterStyledSystemMarginProps(rest);
496498

497499
return (
498500
<StyledDateInput

src/components/date/date.style.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface StyledDateInputProps
2222
}
2323

2424
const StyledDateInput = styled.div<StyledDateInputProps>`
25+
margin-bottom: var(--fieldSpacing);
2526
${margin}
2627
2728
& ${StyledInputPresentation} {

src/components/fieldset/fieldset.component.tsx

+3-10
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import tagComponent, { TagProps } from "../../__internal__/utils/helpers/tags";
44

55
import { FieldsetStyle, StyledLegend } from "./fieldset.style";
66
import NewValidationContext from "../carbon-provider/__internal__/new-validation.context";
7-
import FormSpacingProvider from "../../__internal__/form-spacing-provider";
8-
import useFormSpacing from "../../hooks/__internal__/useFormSpacing";
7+
import { filterStyledSystemMarginProps } from "../../style/utils";
98

109
export interface FieldsetProps extends MarginProps, TagProps {
1110
/** Child elements */
@@ -26,7 +25,7 @@ export const Fieldset = ({
2625
...rest
2726
}: FieldsetProps) => {
2827
const [ref, setRef] = useState<HTMLFieldSetElement | null>(null);
29-
const marginProps = useFormSpacing(rest);
28+
const marginProps = filterStyledSystemMarginProps(rest);
3029
const { validationRedesignOptIn } = useContext(NewValidationContext);
3130

3231
useEffect(() => {
@@ -56,13 +55,7 @@ export const Fieldset = ({
5655
{legend}
5756
</StyledLegend>
5857
)}
59-
{validationRedesignOptIn ? (
60-
children
61-
) : (
62-
<FormSpacingProvider marginBottom={undefined}>
63-
{children}
64-
</FormSpacingProvider>
65-
)}
58+
{children}
6659
</FieldsetStyle>
6760
);
6861
};

src/components/fieldset/fieldset.style.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface StyledFieldsetProps {
1111

1212
const FieldsetStyle = styled.fieldset<StyledFieldsetProps>`
1313
margin: 0;
14+
margin-bottom: var(--fieldSpacing);
1415
${margin}
1516
border: none;
1617
padding: 0;

0 commit comments

Comments
 (0)