@gen-epix/ui-core-form 4.0.2 → 4.1.0

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.
@@ -0,0 +1,73 @@
1
+ import { t as e } from "./FormUtil.js";
2
+ import { i as t, t as n } from "./index.esm.js";
3
+ import { FormFieldHelperText as r } from "../components/helpers/FormFieldHelperText/index.js";
4
+ import { useCallback as i, useId as a, useRef as o } from "react";
5
+ import { FormControl as s, FormHelperText as c, FormLabel as l } from "@mui/material";
6
+ import { jsx as u, jsxs as d } from "react/jsx-runtime";
7
+ import { TestIdUtil as f } from "@gen-epix/ui-core/utils/TestIdUtil";
8
+ import { FileDropZone as p } from "@gen-epix/ui-core-components/components/FileDropZone";
9
+ //#region src/components/fields/FileUpload/FileUpload.tsx
10
+ var m = ({ accept: m, disabled: h, infoMessage: g, label: _, multiple: v = !1, name: y, onChange: b, required: x, warningMessage: S }) => {
11
+ let C = a(), w = a(), { control: T, formState: { errors: E } } = t(), D = e.getFieldErrorMessage(E, y), O = o(null), k = !!D, A = i((e) => (t) => {
12
+ b && b(t), e(t);
13
+ }, [b]), j = i(({ field: { onBlur: e, onChange: t, ref: n, value: r } }) => (n({ focus: () => {
14
+ O?.current?.focus();
15
+ } }), /* @__PURE__ */ u(p, {
16
+ accept: m,
17
+ ariaDescribedBy: w,
18
+ disabled: h,
19
+ error: k,
20
+ files: r ?? null,
21
+ label: _,
22
+ labelledById: C,
23
+ multiple: v,
24
+ onBlur: e,
25
+ onFilesSelected: A(t),
26
+ ref: O
27
+ })), [
28
+ m,
29
+ w,
30
+ h,
31
+ k,
32
+ _,
33
+ C,
34
+ v,
35
+ A
36
+ ]);
37
+ return /* @__PURE__ */ d(s, {
38
+ error: k,
39
+ ...f.createAttributes("UploadButton", {
40
+ label: _,
41
+ name: y
42
+ }),
43
+ fullWidth: !0,
44
+ children: [
45
+ /* @__PURE__ */ u(l, {
46
+ component: "legend",
47
+ id: C,
48
+ required: x,
49
+ children: _
50
+ }),
51
+ /* @__PURE__ */ u(n, {
52
+ control: T,
53
+ defaultValue: null,
54
+ name: y,
55
+ render: j
56
+ }),
57
+ /* @__PURE__ */ u(c, {
58
+ id: w,
59
+ sx: { ml: 0 },
60
+ children: /* @__PURE__ */ u(r, {
61
+ errorMessage: D,
62
+ infoMessage: g,
63
+ noIndent: !0,
64
+ warningMessage: S
65
+ })
66
+ })
67
+ ]
68
+ });
69
+ };
70
+ //#endregion
71
+ export { m as t };
72
+
73
+ //# sourceMappingURL=FileUpload.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FileUpload.js","names":[],"sources":["../../src/components/fields/FileUpload/FileUpload.tsx"],"sourcesContent":["import type { ReactElement } from 'react';\nimport {\n useCallback,\n useId,\n useRef,\n} from 'react';\nimport {\n FormControl,\n FormHelperText,\n FormLabel,\n} from '@mui/material';\nimport {\n Controller,\n useFormContext,\n} from 'react-hook-form';\nimport type {\n FieldValues,\n Path,\n UseControllerReturn,\n} from 'react-hook-form';\nimport { TestIdUtil } from '@gen-epix/ui-core/utils/TestIdUtil';\nimport { FileDropZone } from '@gen-epix/ui-core-components/components/FileDropZone';\n\nimport { FormUtil } from '../../../utils/FormUtil';\nimport { FormFieldHelperText } from '../../helpers/FormFieldHelperText';\n\nexport type FileUploadProps<TFieldValues extends FieldValues, TName extends Path<TFieldValues> = Path<TFieldValues>> = {\n readonly accept: string;\n readonly disabled?: boolean;\n readonly infoMessage?: string;\n readonly label: string;\n readonly loading?: boolean; // NOT implemented\n readonly multiple?: boolean;\n readonly name: TName;\n readonly onChange?: (value: FileList) => void;\n readonly required?: boolean;\n readonly warningMessage?: string;\n};\n\nexport const FileUpload = <TFieldValues extends FieldValues, TName extends Path<TFieldValues> = Path<TFieldValues>>({\n accept,\n disabled,\n infoMessage,\n label,\n multiple = false,\n name,\n onChange: onChangeProp,\n required,\n warningMessage,\n}: FileUploadProps<TFieldValues, TName>): ReactElement => {\n const labelId = useId();\n const helperTextId = useId();\n const { control, formState: { errors } } = useFormContext<TFieldValues>();\n const errorMessage = FormUtil.getFieldErrorMessage(errors, name);\n\n const dropZoneRef = useRef<HTMLDivElement>(null);\n const hasError = !!errorMessage;\n\n const onFilesSelected = useCallback((onChange: UseControllerReturn<TFieldValues, TName>['field']['onChange']) =>\n (files: FileList) => {\n if (onChangeProp) {\n onChangeProp(files);\n }\n onChange(files);\n }\n , [onChangeProp]);\n\n const renderController = useCallback(({ field: { onBlur, onChange, ref, value } }: UseControllerReturn<TFieldValues, TName>) => {\n ref({\n focus: () => {\n dropZoneRef?.current?.focus();\n },\n });\n return (\n <FileDropZone\n accept={accept}\n ariaDescribedBy={helperTextId}\n disabled={disabled}\n error={hasError}\n files={value ?? null}\n label={label}\n labelledById={labelId}\n multiple={multiple}\n onBlur={onBlur}\n onFilesSelected={onFilesSelected(onChange)}\n ref={dropZoneRef}\n />\n );\n }, [accept, helperTextId, disabled, hasError, label, labelId, multiple, onFilesSelected]);\n\n return (\n <FormControl\n error={hasError}\n {...TestIdUtil.createAttributes('UploadButton', { label, name })}\n fullWidth\n >\n <FormLabel\n component={'legend'}\n id={labelId}\n required={required}\n >\n {label}\n </FormLabel>\n <Controller\n control={control}\n defaultValue={null}\n name={name}\n render={renderController}\n />\n <FormHelperText\n id={helperTextId}\n sx={{ ml: 0 }}\n >\n <FormFieldHelperText\n errorMessage={errorMessage}\n infoMessage={infoMessage}\n noIndent\n warningMessage={warningMessage}\n />\n </FormHelperText>\n </FormControl>\n );\n};\n"],"mappings":";;;;;;;;;AAuCA,IAAa,KAAuG,EAClH,WACA,aACA,gBACA,UACA,cAAW,IACX,SACA,UAAU,GACV,aACA,wBACwD;CACxD,IAAM,IAAU,EAAM,GAChB,IAAe,EAAM,GACrB,EAAE,YAAS,WAAW,EAAE,gBAAa,EAA6B,GAClE,IAAe,EAAS,qBAAqB,GAAQ,CAAI,GAEzD,IAAc,EAAuB,IAAI,GACzC,IAAW,CAAC,CAAC,GAEb,IAAkB,GAAa,OAClC,MAAoB;EAInB,AAHI,KACF,EAAa,CAAK,GAEpB,EAAS,CAAK;CAChB,GACA,CAAC,CAAY,CAAC,GAEV,IAAmB,GAAa,EAAE,OAAO,EAAE,WAAQ,aAAU,QAAK,kBACtE,EAAI,EACF,aAAa;EACX,GAAa,SAAS,MAAM;CAC9B,EACF,CAAC,GAEC,kBAAC,GAAD;EACU;EACR,iBAAiB;EACP;EACV,OAAO;EACP,OAAO,KAAS;EACT;EACP,cAAc;EACJ;EACF;EACR,iBAAiB,EAAgB,CAAQ;EACzC,KAAK;CACN,CAAA,IAEF;EAAC;EAAQ;EAAc;EAAU;EAAU;EAAO;EAAS;EAAU;CAAe,CAAC;CAExF,OACE,kBAAC,GAAD;EACE,OAAO;EACP,GAAI,EAAW,iBAAiB,gBAAgB;GAAE;GAAO;EAAK,CAAC;EAC/D,WAAA;EAHF,UAAA;GAKE,kBAAC,GAAD;IACE,WAAW;IACX,IAAI;IACM;IAET,UAAA;GACQ,CAAA;GACX,kBAAC,GAAD;IACW;IACT,cAAc;IACR;IACN,QAAQ;GACT,CAAA;GACD,kBAAC,GAAD;IACE,IAAI;IACJ,IAAI,EAAE,IAAI,EAAE;IAEZ,UAAA,kBAAC,GAAD;KACgB;KACD;KACb,UAAA;KACgB;IACjB,CAAA;GACa,CAAA;EACL;;AAEjB"}