-
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: Let Form Error List add the error count to the document title (#…
…1309) Co-authored-by: Vincent Smedinga <[email protected]>
- Loading branch information
1 parent
acc002f
commit f18f03b
Showing
4 changed files
with
85 additions
and
1 deletion.
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
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
29 changes: 29 additions & 0 deletions
29
packages/react/src/FormErrorList/useAddErrorCountToDocumentTitle.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,29 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
export const useAddErrorCountToDocumentTitle = ( | ||
/** The list of error messages used to calculate the error count. */ | ||
errors: { id: string; label: string }[], | ||
/** The text following the error count. */ | ||
label = { | ||
plural: 'invoerfouten', | ||
singular: 'invoerfout', | ||
}, | ||
) => { | ||
const [documentTitle, setDocumentTitle] = useState<string>() | ||
|
||
useEffect(() => { | ||
setDocumentTitle(document.title) | ||
}, []) | ||
|
||
if (!documentTitle) return null | ||
|
||
if (errors.length === 1) { | ||
document.title = `(${errors.length} ${label.singular}) ${documentTitle}` | ||
} else if (errors.length > 1) { | ||
document.title = `(${errors.length} ${label.plural}) ${documentTitle}` | ||
} else { | ||
document.title = documentTitle | ||
} | ||
|
||
return null | ||
} |