Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(form): replace use of context to apply field spacing #7243

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/__internal__/fieldset/fieldset.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
import ValidationIcon from "../validations/validation-icon.component";
import NewValidationContext from "../../components/carbon-provider/__internal__/new-validation.context";
import { InputGroupBehaviour, InputGroupContext } from "../input-behaviour";
import useFormSpacing from "../../hooks/__internal__/useFormSpacing";
import Help from "../../components/help";
import Typography from "../../components/typography";
import { filterStyledSystemMarginProps } from "../../style/utils";

export interface FieldsetProps extends MarginProps {
/** Role */
Expand Down Expand Up @@ -84,7 +84,7 @@ const Fieldset = ({
...rest
}: FieldsetProps) => {
const { validationRedesignOptIn } = useContext(NewValidationContext);
const marginProps = useFormSpacing(rest);
const marginProps = filterStyledSystemMarginProps(rest);
const [ref, setRef] = useState<HTMLFieldSetElement | null>(null);
const [isFocused, setFocus] = useState(false);

Expand Down
1 change: 1 addition & 0 deletions src/__internal__/fieldset/fieldset.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type StyledFieldsetProps = {

const StyledFieldset = styled.fieldset<StyledFieldsetProps>`
margin: 0;
margin-bottom: var(--fieldSpacing);
${margin}
border: none;
padding: 0;
Expand Down
4 changes: 2 additions & 2 deletions src/__internal__/form-field/form-field.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
} from "../../components/tabs/tab/__internal__/tab.context";
import useIsAboveBreakpoint from "../../hooks/__internal__/useIsAboveBreakpoint";
import { IconType } from "../../components/icon";
import useFormSpacing from "../../hooks/__internal__/useFormSpacing";
import { filterStyledSystemMarginProps } from "../../style/utils";

interface CommonFormFieldProps extends MarginProps, ValidationProps {
/** If true, the component will be disabled */
Expand Down Expand Up @@ -128,7 +128,7 @@
return Object.keys(validationProps).find(
(propName) => validationProps[propName],
);
}, [error, warning, info, disabled]);

Check warning on line 131 in src/__internal__/form-field/form-field.component.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook useMemo has a missing dependency: 'loading'. Either include it or remove the dependency array

invariant(
invalidValidationProp === undefined,
Expand All @@ -149,7 +149,7 @@

const { setError, setWarning, setInfo } =
useContext<TabContextProps>(TabContext);
const marginProps = useFormSpacing(rest);
const marginProps = filterStyledSystemMarginProps(rest);
const isMounted = useRef(false);

useLayoutEffect(() => {
Expand Down
1 change: 1 addition & 0 deletions src/__internal__/form-field/form-field.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { space } from "styled-system";
import { baseTheme } from "../../style/themes";

const FormFieldStyle = styled.div`
margin-bottom: var(--fieldSpacing);
& + & {
margin-top: 16px;
}
Expand Down

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions src/__internal__/form-spacing-provider/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Fieldset from "../../../__internal__/fieldset";
import { filterStyledSystemMarginProps } from "../../../style/utils";
import { TooltipProvider } from "../../../__internal__/tooltip-provider";
import { ValidationProps } from "../../../__internal__/validations";
import FormSpacingProvider from "../../../__internal__/form-spacing-provider";
import NewValidationContext from "../../carbon-provider/__internal__/new-validation.context";
import ValidationMessage from "../../../__internal__/validation-message/validation-message.component";
import Box from "../../box";
Expand Down Expand Up @@ -134,9 +133,7 @@ export const CheckboxGroup = ({
warning: !!warning,
}}
>
<FormSpacingProvider marginBottom={undefined}>
{children}
</FormSpacingProvider>
{children}
</CheckboxGroupContext.Provider>
</StyledCheckboxGroup>
</Box>
Expand Down Expand Up @@ -173,9 +170,7 @@ export const CheckboxGroup = ({
info: !!info,
}}
>
<FormSpacingProvider marginBottom={undefined}>
{children}
</FormSpacingProvider>
{children}
</CheckboxGroupContext.Provider>
</StyledCheckboxGroup>
</Fieldset>
Expand Down
4 changes: 2 additions & 2 deletions src/components/checkbox/checkbox.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import useIsAboveBreakpoint from "../../hooks/__internal__/useIsAboveBreakpoint"
import { TooltipProvider } from "../../__internal__/tooltip-provider";
import CheckboxGroupContext from "./checkbox-group/__internal__/checkbox-group.context";
import Logger from "../../__internal__/utils/logger";
import useFormSpacing from "../../hooks/__internal__/useFormSpacing";
import NewValidationContext from "../carbon-provider/__internal__/new-validation.context";
import { filterStyledSystemMarginProps } from "../../style/utils";

export interface CheckboxProps
extends CommonCheckableInputProps,
Expand Down Expand Up @@ -131,7 +131,7 @@ export const Checkbox = React.forwardRef(
: { info: contextInfo || info }),
};

const marginProps = useFormSpacing(rest);
const marginProps = filterStyledSystemMarginProps(rest);

const componentToRender = (
<CheckboxStyle
Expand Down
1 change: 1 addition & 0 deletions src/components/checkbox/checkbox.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface StyledCheckboxProps extends ValidationProps, MarginProps {
}

const StyledCheckbox = styled.div<StyledCheckboxProps>`
margin-bottom: var(--fieldSpacing);
${margin}
${({
disabled,
Expand Down
1 change: 1 addition & 0 deletions src/components/date-range/date-range.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface StyledDateRangeProps {
labelsInline?: boolean;
}
const StyledDateRange = styled.div<StyledDateRangeProps>`
margin-bottom: var(--fieldSpacing);
${margin}

& ${StyledDateInput} {
Expand Down
8 changes: 5 additions & 3 deletions src/components/date/date.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import {
import useLocale from "../../hooks/__internal__/useLocale";
import Logger from "../../__internal__/utils/logger";
import Events from "../../__internal__/utils/helpers/events";
import { filterOutStyledSystemSpacingProps } from "../../style/utils";
import {
filterOutStyledSystemSpacingProps,
filterStyledSystemMarginProps,
} from "../../style/utils";
import getFormatData from "./__internal__/date-formats";
import StyledDateInput from "./date.style";
import Textbox, { TextboxProps } from "../textbox";
Expand All @@ -31,7 +34,6 @@ import DateRangeContext, {
InputName,
} from "../date-range/__internal__/date-range.context";
import useClickAwayListener from "../../hooks/__internal__/useClickAwayListener";
import useFormSpacing from "../../hooks/__internal__/useFormSpacing";
import guid from "../../__internal__/utils/helpers/guid";
import tagComponent from "../../__internal__/utils/helpers/tags/tags";

Expand Down Expand Up @@ -492,7 +494,7 @@ export const DateInput = React.forwardRef<HTMLInputElement, DateInputProps>(
return value;
};

const marginProps = useFormSpacing(rest);
const marginProps = filterStyledSystemMarginProps(rest);

return (
<StyledDateInput
Expand Down
1 change: 1 addition & 0 deletions src/components/date/date.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface StyledDateInputProps
}

const StyledDateInput = styled.div<StyledDateInputProps>`
margin-bottom: var(--fieldSpacing);
${margin}

& ${StyledInputPresentation} {
Expand Down
13 changes: 3 additions & 10 deletions src/components/fieldset/fieldset.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import tagComponent, { TagProps } from "../../__internal__/utils/helpers/tags";

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

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

useEffect(() => {
Expand Down Expand Up @@ -56,13 +55,7 @@ export const Fieldset = ({
{legend}
</StyledLegend>
)}
{validationRedesignOptIn ? (
children
) : (
<FormSpacingProvider marginBottom={undefined}>
{children}
</FormSpacingProvider>
)}
{children}
</FieldsetStyle>
);
};
Expand Down
5 changes: 5 additions & 0 deletions src/components/fieldset/fieldset.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ export interface StyledFieldsetProps {

const FieldsetStyle = styled.fieldset<StyledFieldsetProps>`
margin: 0;
margin-bottom: var(--fieldSpacing);
${margin}
border: none;
padding: 0;

${({ newValidation }) =>
!newValidation &&
css`
& * {
--fieldSpacing: 0;
}

&&&& ${FormFieldStyle} {
margin-top: 0;
margin-bottom: -1px;
Expand Down
Loading
Loading