[Glitch] Allow displaying field status (error, warning, info) under form fields
Port 890b2673fc to glitch-soc
Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
1 parent
3afb58ad18
commit
8ec882df0c
30 files changed
+330
-52
No files matched your search
@@ -3,14 +3,19 @@
|
||||
import type { ReactNode, FC } from 'react';
|
||||
import { createContext, useId } from 'react';
|
||||
|
||||
import { A11yLiveRegion } from 'flavours/glitch/components/a11y_live_region';
|
||||
import type { FieldStatus } from 'flavours/glitch/components/callout_inline';
|
||||
import { CalloutInline } from 'flavours/glitch/components/callout_inline';
|
||||
|
||||
import classes from './fieldset.module.scss';
|
||||
import { getFieldStatus } from './form_field_wrapper';
|
||||
import formFieldWrapperClasses from './form_field_wrapper.module.scss';
|
||||
|
||||
interface FieldsetProps {
|
||||
legend: ReactNode;
|
||||
hint?: ReactNode;
|
||||
name?: string;
|
||||
hasError?: boolean;
|
||||
status?: FieldStatus | FieldStatus['variant'];
|
||||
layout?: 'vertical' | 'horizontal';
|
||||
children: ReactNode;
|
||||
}
|
||||
@@ -26,22 +31,33 @@ export const Fieldset: FC<FieldsetProps> = ({
|
||||
legend,
|
||||
hint,
|
||||
name,
|
||||
hasError,
|
||||
status,
|
||||
layout,
|
||||
children,
|
||||
}) => {
|
||||
const uniqueId = useId();
|
||||
const labelId = `${uniqueId}-label`;
|
||||
const hintId = `${uniqueId}-hint`;
|
||||
const statusId = `${uniqueId}-status`;
|
||||
const fieldsetName = name || `${uniqueId}-fieldset-name`;
|
||||
const hasHint = !!hint;
|
||||
|
||||
const fieldStatus = getFieldStatus(status);
|
||||
const hasStatusMessage = !!fieldStatus?.message;
|
||||
|
||||
const descriptionIds = [
|
||||
hasHint ? hintId : '',
|
||||
hasStatusMessage ? statusId : '',
|
||||
]
|
||||
.filter((id) => !!id)
|
||||
.join(' ');
|
||||
|
||||
return (
|
||||
<fieldset
|
||||
className={classes.fieldset}
|
||||
data-has-error={hasError}
|
||||
data-has-error={status === 'error'}
|
||||
aria-labelledby={labelId}
|
||||
aria-describedby={hintId}
|
||||
aria-describedby={descriptionIds}
|
||||
>
|
||||
<div className={formFieldWrapperClasses.labelWrapper}>
|
||||
<div id={labelId} className={formFieldWrapperClasses.label}>
|
||||
@@ -59,6 +75,11 @@ export const Fieldset: FC<FieldsetProps> = ({
|
||||
{children}
|
||||
</FieldsetNameContext.Provider>
|
||||
</div>
|
||||
|
||||
{/* Live region must be rendered even when empty */}
|
||||
<A11yLiveRegion className={classes.status} id={statusId}>
|
||||
{hasStatusMessage && <CalloutInline {...fieldStatus} />}
|
||||
</A11yLiveRegion>
|
||||
</fieldset>
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user