codeforlife 2.6.9 → 2.6.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [2.6.11](https://github.com/ocadotechnology/codeforlife-package-javascript/compare/v2.6.10...v2.6.11) (2025-02-25)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Portal frontend 40 ([#78](https://github.com/ocadotechnology/codeforlife-package-javascript/issues/78)) ([18082bb](https://github.com/ocadotechnology/codeforlife-package-javascript/commit/18082bbd60e2ec52dfaf072ca092c3812a25fab0))
7
+
8
+ ## [2.6.10](https://github.com/ocadotechnology/codeforlife-package-javascript/compare/v2.6.9...v2.6.10) (2025-02-25)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * non field errors ([dddf8d5](https://github.com/ocadotechnology/codeforlife-package-javascript/commit/dddf8d5d1990c201ef72eff74b2d4f260ce4c7a4))
14
+
1
15
  ## [2.6.9](https://github.com/ocadotechnology/codeforlife-package-javascript/compare/v2.6.8...v2.6.9) (2025-02-25)
2
16
 
3
17
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "codeforlife",
3
3
  "description": "Common frontend code",
4
4
  "private": false,
5
- "version": "2.6.9",
5
+ "version": "2.6.11",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "dev": "vite",
@@ -0,0 +1,54 @@
1
+ import { Button, type ButtonProps } from "@mui/material"
2
+ import { type FC, useEffect } from "react"
3
+ import { Download as DownloadIcon } from "@mui/icons-material"
4
+
5
+ export type DownloadFileButtonProps = ButtonProps & {
6
+ file:
7
+ | Blob
8
+ | MediaSource
9
+ | {
10
+ text: string
11
+ mimeType: "plain" | "csv"
12
+ name: string
13
+ charset?: string
14
+ extension?: string
15
+ }
16
+ }
17
+
18
+ const DownloadFileButton: FC<DownloadFileButtonProps> = ({
19
+ children = "Download",
20
+ endIcon = <DownloadIcon />,
21
+ file,
22
+ ...otherButtonProps
23
+ }) => {
24
+ let url: undefined | string = undefined
25
+ let anchorProps: undefined | { download?: string; href: string } = undefined
26
+ if ("mimeType" in file) {
27
+ let { text, mimeType, name, charset = "utf-8", extension } = file
28
+
29
+ if (!extension) extension = "." + { plain: "txt", csv: "csv" }[mimeType]
30
+
31
+ anchorProps = {
32
+ download: name + extension,
33
+ href: `data:text/${mimeType};charset=${charset},${encodeURIComponent(text)}`,
34
+ }
35
+ } else {
36
+ url = URL.createObjectURL(file)
37
+
38
+ anchorProps = { href: url }
39
+ }
40
+
41
+ useEffect(() => {
42
+ return () => {
43
+ if (url) URL.revokeObjectURL(url)
44
+ }
45
+ }, [url])
46
+
47
+ return (
48
+ <Button endIcon={endIcon} {...otherButtonProps} {...anchorProps}>
49
+ {children}
50
+ </Button>
51
+ )
52
+ }
53
+
54
+ export default DownloadFileButton
@@ -45,7 +45,7 @@ const NonFieldErrors: FC<NonFieldErrorsProps> = ({
45
45
  }
46
46
 
47
47
  export type FormErrors<Values> = FormikErrors<
48
- Omit<Values, "non_field_errors"> & { non_field_errors: string }
48
+ Omit<Values, "__all__"> & { __all__: string }
49
49
  >
50
50
 
51
51
  type _FormikProps<Values> = Omit<FormikProps<Values>, "errors"> & {
@@ -63,38 +63,38 @@ const BaseForm = <Values extends FormValues>({
63
63
  children,
64
64
  scrollIntoViewOptions = SCROLL_INTO_VIEW_OPTIONS,
65
65
  nonFieldErrorsProps,
66
- fieldRefs,
66
+ fieldRefs = [],
67
67
  ...otherFormikProps
68
68
  }: BaseFormProps<Values>) => (
69
69
  <Formik {...otherFormikProps}>
70
70
  {/* @ts-expect-error */}
71
71
  {(formik: _FormikProps<Values>) => {
72
- let nonFieldErrors: undefined | JSX.Element = undefined
73
- if (Object.keys(formik.errors).length) {
74
- if (typeof formik.errors.non_field_errors === "string") {
75
- nonFieldErrors = (
76
- <NonFieldErrors {...nonFieldErrorsProps}>
77
- {formik.errors.non_field_errors}
78
- </NonFieldErrors>
79
- )
80
- }
81
- // If a submission was attempted and refs to the fields were provided.
82
- else if (formik.isSubmitting && fieldRefs && fieldRefs.length) {
83
- const errorNames = getKeyPaths(formik.errors)
84
-
85
- const inputRef = fieldRefs.find(({ name }) =>
86
- errorNames.includes(name),
87
- )?.inputRef
88
-
89
- if (inputRef?.current) {
90
- inputRef.current.scrollIntoView(scrollIntoViewOptions)
91
- }
92
- }
72
+ const hasErrors = Boolean(Object.keys(formik.errors).length)
73
+ const hasNonFieldErrors =
74
+ hasErrors && typeof formik.errors.__all__ === "string"
75
+
76
+ // If a submission was attempted and refs to the fields were provided.
77
+ if (
78
+ hasErrors &&
79
+ !hasNonFieldErrors &&
80
+ formik.isSubmitting &&
81
+ fieldRefs.length
82
+ ) {
83
+ const errorNames = getKeyPaths(formik.errors)
84
+
85
+ const input = fieldRefs.find(({ name }) => errorNames.includes(name))
86
+ ?.inputRef.current
87
+
88
+ if (input) input.scrollIntoView(scrollIntoViewOptions)
93
89
  }
94
90
 
95
91
  return (
96
92
  <>
97
- {nonFieldErrors}
93
+ {hasNonFieldErrors && (
94
+ <NonFieldErrors {...nonFieldErrorsProps}>
95
+ {formik.errors.__all__ as string}
96
+ </NonFieldErrors>
97
+ )}
98
98
  <FormikForm>
99
99
  {typeof children === "function" ? children(formik) : children}
100
100
  </FormikForm>
@@ -6,6 +6,8 @@ export * from "./CopyIconButton"
6
6
  export { default as CopyIconButton } from "./CopyIconButton"
7
7
  export * from "./Countdown"
8
8
  export { default as Countdown } from "./Countdown"
9
+ export * from "./DownloadFileButton"
10
+ export { default as DownloadFileButton } from "./DownloadFileButton"
9
11
  export * from "./ElevatedAppBar"
10
12
  export { default as ElevatedAppBar } from "./ElevatedAppBar"
11
13
  export * from "./Image"