-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add focus on initial render to Form Error List (#1328)
Co-authored-by: Vincent Smedinga <[email protected]>
- Loading branch information
1 parent
1b9c269
commit 09387b7
Showing
8 changed files
with
140 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/css/src/components/form-error-list/form-error-list.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.ams-form-error-list { | ||
outline-offset: var(--ams-form-error-list-outline-offset); | ||
|
||
// In Chromium browsers, the outline overlaps with the border in this component. | ||
// We're not sure why, but to fix this we double the offset for Chromium browsers here. | ||
@supports (contain: paint) and (not (-moz-appearance: none)) { | ||
outline-offset: calc(var(--ams-form-error-list-outline-offset) * 2); | ||
|
||
// Reset for Safari | ||
@supports (font: -apple-system-body) { | ||
outline-offset: var(--ams-form-error-list-outline-offset); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/react/src/FormErrorList/FormErrorListWithErrors.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @license EUPL-1.2+ | ||
* Copyright Gemeente Amsterdam | ||
*/ | ||
|
||
import clsx from 'clsx' | ||
import { forwardRef, useEffect, useImperativeHandle, useRef } from 'react' | ||
import type { Dispatch, ForwardedRef, SetStateAction } from 'react' | ||
import type { FormErrorListProps } from './FormErrorList' | ||
import { Alert } from '../Alert' | ||
import { LinkList } from '../LinkList' | ||
|
||
type FormErrorListWithErrorsProps = Omit<FormErrorListProps, 'errorCountLabel'> & { | ||
/** Whether the component has set focus once. */ | ||
hasFocusedOnce: boolean | ||
/** Callback to let parent component know whether focus has been set once. */ | ||
setHasFocusedOnce: Dispatch<SetStateAction<boolean>> | ||
} | ||
|
||
export const FormErrorListWithErrors = forwardRef( | ||
( | ||
{ | ||
className, | ||
errors, | ||
focusOnRender = true, | ||
hasFocusedOnce, | ||
heading = 'Verbeter de fouten voor u verder gaat', | ||
headingLevel = 2, | ||
setHasFocusedOnce, | ||
...restProps | ||
}: FormErrorListWithErrorsProps, | ||
ref: ForwardedRef<HTMLDivElement>, | ||
) => { | ||
const innerRef = useRef<HTMLDivElement>(null) | ||
|
||
// use a passed ref if it's there, otherwise use innerRef | ||
useImperativeHandle(ref, () => innerRef.current as HTMLDivElement) | ||
|
||
useEffect(() => { | ||
if (innerRef.current && focusOnRender && !hasFocusedOnce) { | ||
innerRef.current.focus() | ||
setHasFocusedOnce(true) | ||
} | ||
}, [innerRef]) | ||
|
||
return ( | ||
<Alert | ||
{...restProps} | ||
className={clsx('ams-form-error-list', className)} | ||
heading={heading} | ||
headingLevel={headingLevel} | ||
ref={innerRef} | ||
severity="error" | ||
tabIndex={-1} | ||
> | ||
<LinkList> | ||
{errors.map(({ id, label }) => ( | ||
<LinkList.Link href={id} key={`${id}-${label}`}> | ||
{label} | ||
</LinkList.Link> | ||
))} | ||
</LinkList> | ||
</Alert> | ||
) | ||
}, | ||
) | ||
|
||
FormErrorListWithErrors.displayName = 'FormErrorListWithErrors' |
7 changes: 7 additions & 0 deletions
7
proprietary/tokens/src/components/ams/form-error-list.tokens.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"ams": { | ||
"form-error-list": { | ||
"outline-offset": { "value": "{ams.focus.outline-offset}" } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters