Skip to content

Commit

Permalink
FormGroup: Add standalone mode
Browse files Browse the repository at this point in the history
Adds a 'standalone' mode that hides labels, hints and error messages. It
is designed to be used for forms with a single field.
  • Loading branch information
daniel-ac-martin committed Dec 10, 2024
1 parent 6ea139b commit 2ab229e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
9 changes: 9 additions & 0 deletions components/form-group/assets/FormGroup.scss
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
@import "@not-govuk/sass-base";
@import "govuk-frontend/dist/govuk/objects/_form-group";

.govuk-form-group {
&--standalone {
&.govuk-form-group--error {
padding-left: 0;
border-left-width: 0;
}
}
}
11 changes: 7 additions & 4 deletions components/form-group/src/FormGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FC, Fragment, HTMLAttributes, ReactNode, createElement as h } from 'react';
import { StandardProps, classBuilder } from '@not-govuk/component-helpers';
import { ErrorMessage } from '@not-govuk/error-message';
import { FieldSet, FieldSetProps } from '@not-govuk/fieldset';
import { FieldSet } from '@not-govuk/fieldset';
import { Hint } from '@not-govuk/hint';
import { Label } from '@not-govuk/label';

Expand All @@ -16,6 +16,7 @@ export type FormGroupProps = StandardProps & Omit<HTMLAttributes<HTMLDivElement>
hintId?: string
id: string
label: ReactNode
standalone?: boolean
};

export const FormGroup: FC<FormGroupProps> = ({
Expand All @@ -30,10 +31,12 @@ export const FormGroup: FC<FormGroupProps> = ({
hintId: _hintId,
id,
label,
standalone = false,
...attrs
}) => {
const classModifiers = [
error ? 'error' : undefined,
standalone ? 'standalone' : undefined,
...(Array.isArray(_classModifiers) ? _classModifiers : [_classModifiers])
];
const classes = classBuilder('govuk-form-group', classBlock, classModifiers, className);
Expand All @@ -49,8 +52,8 @@ export const FormGroup: FC<FormGroupProps> = ({

const children = (
<Fragment>
{ !hint ? null : <Hint id={hintId}>{hint}</Hint> }
{ !error ? null : <ErrorMessage id={errorId}>{error}</ErrorMessage>}
{ !hint ? null : <Hint id={hintId} hidden={standalone}>{hint}</Hint> }
{ !error ? null : <ErrorMessage id={errorId} hidden={standalone}>{error}</ErrorMessage> }
{_children}
</Fragment>
);
Expand All @@ -59,7 +62,7 @@ export const FormGroup: FC<FormGroupProps> = ({
<div id={id} {...attrs} className={classes()}>
{ fieldId ? (
<Fragment>
<Label htmlFor={fieldId}>{label}</Label>
<Label htmlFor={fieldId} hidden={standalone}>{label}</Label>
{children}
</Fragment>
) : (
Expand Down

0 comments on commit 2ab229e

Please sign in to comment.