@gen-epix/ui-core-form 4.0.2 → 4.2.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.
- package/dist/_chunks/Autocomplete.js +86 -84
- package/dist/_chunks/Autocomplete.js.map +1 -1
- package/dist/_chunks/DatePicker.js +26 -26
- package/dist/_chunks/FileUpload.js +73 -0
- package/dist/_chunks/FileUpload.js.map +1 -0
- package/dist/_chunks/FormUtil.js +4 -1
- package/dist/_chunks/FormUtil.js.map +1 -1
- package/dist/_chunks/RichTextEditor.js +7658 -4335
- package/dist/_chunks/RichTextEditor.js.map +1 -1
- package/dist/_chunks/TransferList.js +59 -59
- package/dist/_chunks/TransferList.js.map +1 -1
- package/dist/components/fields/Autocomplete/Autocomplete.d.ts +6 -3
- package/dist/components/fields/CheckboxGroup/index.js +14 -14
- package/dist/components/fields/DateRangePicker/index.js +39 -39
- package/dist/components/fields/DateRangePicker/index.js.map +1 -1
- package/dist/components/fields/{UploadButton/UploadButton.d.ts → FileUpload/FileUpload.d.ts} +2 -2
- package/dist/components/fields/FileUpload/index.d.ts +1 -0
- package/dist/components/fields/FileUpload/index.js +2 -0
- package/dist/components/fields/NumberField/index.js +29 -29
- package/dist/components/fields/NumberField/index.js.map +1 -1
- package/dist/components/fields/NumberRangeInput/index.js +34 -34
- package/dist/components/fields/RadioGroup/index.js +7 -7
- package/dist/components/fields/Select/index.js +32 -19
- package/dist/components/fields/Select/index.js.map +1 -1
- package/dist/components/fields/Switch/Switch.d.ts +3 -2
- package/dist/components/fields/Switch/index.js +50 -40
- package/dist/components/fields/Switch/index.js.map +1 -1
- package/dist/components/fields/TextField/index.js +19 -19
- package/dist/components/fields/ToggleButtonGroup/index.js +7 -7
- package/dist/components/helpers/GenericForm/GenericForm.d.ts +1 -1
- package/dist/components/helpers/GenericForm/index.js +42 -32
- package/dist/components/helpers/GenericForm/index.js.map +1 -1
- package/dist/hooks/useIsFormFieldRequiredFromSchema/index.js +5 -4
- package/dist/hooks/useIsFormFieldRequiredFromSchema/index.js.map +1 -1
- package/dist/models/form.d.ts +31 -19
- package/dist/models/form.js.map +1 -1
- package/package.json +84 -81
- package/dist/_chunks/UploadButton.js +0 -91
- package/dist/_chunks/UploadButton.js.map +0 -1
- package/dist/_chunks/index.esm.js +0 -398
- package/dist/_chunks/index.esm.js.map +0 -1
- package/dist/components/fields/UploadButton/index.d.ts +0 -1
- package/dist/components/fields/UploadButton/index.js +0 -2
|
@@ -1,68 +1,78 @@
|
|
|
1
1
|
import { n as e } from "../../../_chunks/rolldown-runtime.js";
|
|
2
2
|
import { t } from "../../../_chunks/FormUtil.js";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { jsx as
|
|
10
|
-
import { TestIdUtil as
|
|
3
|
+
import { FormFieldHelperText as n } from "../../helpers/FormFieldHelperText/index.js";
|
|
4
|
+
import { FormFieldLoadingIndicator as r } from "../../helpers/FormFieldLoadingIndicator/index.js";
|
|
5
|
+
import { t as i } from "../../../_chunks/classnames.js";
|
|
6
|
+
import { useCallback as a, useRef as o } from "react";
|
|
7
|
+
import { Controller as s, useFormContext as c } from "react-hook-form";
|
|
8
|
+
import { Box as l, FormControl as u, FormControlLabel as d, FormHelperText as f, Switch as p } from "@mui/material";
|
|
9
|
+
import { jsx as m, jsxs as h } from "react/jsx-runtime";
|
|
10
|
+
import { TestIdUtil as g } from "@gen-epix/ui-core/utils/TestIdUtil";
|
|
11
11
|
//#region src/components/fields/Switch/Switch.tsx
|
|
12
|
-
var
|
|
13
|
-
let { control:
|
|
14
|
-
|
|
15
|
-
}, [
|
|
16
|
-
|
|
17
|
-
} }), /* @__PURE__ */
|
|
18
|
-
...
|
|
19
|
-
label:
|
|
20
|
-
name:
|
|
12
|
+
var _ = /* @__PURE__ */ e(i(), 1), v = ({ disabled: e = !1, icon: i, infoMessage: v, label: y, loading: b = !1, name: x, onChange: S, warningMessage: C }) => {
|
|
13
|
+
let { control: w, formState: { errors: T } } = c(), E = t.getFieldErrorMessage(T, x), D = o(null), O = !!C && !E, k = a((e) => (t, n) => {
|
|
14
|
+
S && S(n), e(n);
|
|
15
|
+
}, [S]), A = a(({ field: { onBlur: t, onChange: a, ref: o, value: s } }) => (o({ focus: () => {
|
|
16
|
+
D?.current?.focus();
|
|
17
|
+
} }), /* @__PURE__ */ h(u, {
|
|
18
|
+
...g.createAttributes("Select", {
|
|
19
|
+
label: y,
|
|
20
|
+
name: x
|
|
21
21
|
}),
|
|
22
22
|
fullWidth: !0,
|
|
23
23
|
children: [
|
|
24
|
-
/* @__PURE__ */
|
|
25
|
-
control: /* @__PURE__ */ p
|
|
24
|
+
/* @__PURE__ */ m(d, {
|
|
25
|
+
control: /* @__PURE__ */ m(p, {
|
|
26
26
|
checked: !!s,
|
|
27
27
|
color: "primary",
|
|
28
|
-
|
|
28
|
+
size: "small",
|
|
29
|
+
slotProps: { input: { ref: D } }
|
|
29
30
|
}),
|
|
30
31
|
disabled: e,
|
|
31
|
-
label:
|
|
32
|
+
label: i ? /* @__PURE__ */ h(l, {
|
|
33
|
+
component: "span",
|
|
34
|
+
sx: {
|
|
35
|
+
alignItems: "center",
|
|
36
|
+
display: "flex",
|
|
37
|
+
gap: .5
|
|
38
|
+
},
|
|
39
|
+
children: [i, y]
|
|
40
|
+
}) : y,
|
|
32
41
|
onBlur: t,
|
|
33
|
-
onChange:
|
|
42
|
+
onChange: k(a)
|
|
34
43
|
}),
|
|
35
|
-
/* @__PURE__ */
|
|
36
|
-
className: (0,
|
|
44
|
+
/* @__PURE__ */ m(f, {
|
|
45
|
+
className: (0, _.default)({ "Mui-warning": O }),
|
|
37
46
|
sx: { ml: 0 },
|
|
38
|
-
children: /* @__PURE__ */
|
|
39
|
-
errorMessage:
|
|
40
|
-
infoMessage:
|
|
47
|
+
children: /* @__PURE__ */ m(n, {
|
|
48
|
+
errorMessage: E,
|
|
49
|
+
infoMessage: v,
|
|
41
50
|
noIndent: !0,
|
|
42
|
-
warningMessage:
|
|
51
|
+
warningMessage: C
|
|
43
52
|
})
|
|
44
53
|
}),
|
|
45
|
-
!!
|
|
54
|
+
!!b && /* @__PURE__ */ m(r, {})
|
|
46
55
|
]
|
|
47
56
|
})), [
|
|
48
57
|
e,
|
|
49
|
-
o,
|
|
50
|
-
w,
|
|
51
|
-
E,
|
|
52
|
-
_,
|
|
53
58
|
v,
|
|
59
|
+
E,
|
|
60
|
+
O,
|
|
61
|
+
i,
|
|
54
62
|
y,
|
|
55
|
-
|
|
56
|
-
x
|
|
63
|
+
b,
|
|
64
|
+
x,
|
|
65
|
+
k,
|
|
66
|
+
C
|
|
57
67
|
]);
|
|
58
|
-
return /* @__PURE__ */
|
|
59
|
-
control:
|
|
68
|
+
return /* @__PURE__ */ m(s, {
|
|
69
|
+
control: w,
|
|
60
70
|
defaultValue: null,
|
|
61
|
-
name:
|
|
62
|
-
render:
|
|
71
|
+
name: x,
|
|
72
|
+
render: A
|
|
63
73
|
});
|
|
64
74
|
};
|
|
65
75
|
//#endregion
|
|
66
|
-
export {
|
|
76
|
+
export { v as Switch };
|
|
67
77
|
|
|
68
78
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../../../src/components/fields/Switch/Switch.tsx"],"sourcesContent":["import type {\n ReactElement,\n SyntheticEvent,\n} from 'react';\nimport {\n useCallback,\n useRef,\n} from 'react';\nimport {\n FormControl,\n FormControlLabel,\n FormHelperText,\n Switch as MuiSwitch,\n} from '@mui/material';\nimport type {\n ControllerRenderProps,\n FieldValues,\n Path,\n UseControllerReturn,\n} from 'react-hook-form';\nimport {\n Controller,\n useFormContext,\n} from 'react-hook-form';\nimport classnames from 'classnames';\nimport { TestIdUtil } from '@gen-epix/ui-core/utils/TestIdUtil';\n\nimport { FormUtil } from '../../../utils/FormUtil';\nimport { FormFieldHelperText } from '../../helpers/FormFieldHelperText';\nimport { FormFieldLoadingIndicator } from '../../helpers/FormFieldLoadingIndicator';\n\nexport type SwitchProps<TFieldValues extends FieldValues, TName extends Path<TFieldValues>> = {\n readonly disabled?: boolean;\n readonly infoMessage?: string;\n readonly label: string;\n readonly loading?: boolean;\n readonly name: TName;\n readonly onChange?: (value: boolean) => void;\n readonly required?: boolean;\n readonly warningMessage?: string;\n};\n\nexport const Switch = <TFieldValues extends FieldValues, TName extends Path<TFieldValues> = Path<TFieldValues>>({\n disabled = false,\n infoMessage,\n label,\n loading = false,\n name,\n onChange: onChangeProp,\n warningMessage,\n}: SwitchProps<TFieldValues, TName>): ReactElement => {\n const { control, formState: { errors } } = useFormContext<TFieldValues>();\n const errorMessage = FormUtil.getFieldErrorMessage(errors, name);\n const inputRef = useRef<HTMLInputElement>(null);\n\n const hasError = !!errorMessage;\n const hasWarning = !!warningMessage && !hasError;\n\n const onMuiSwitchChange = useCallback((onChange: ControllerRenderProps<TFieldValues, TName>['onChange']) =>\n (_event: SyntheticEvent, checked: boolean) => {\n if (onChangeProp) {\n onChangeProp(checked);\n }\n onChange(checked);\n }\n , [onChangeProp]);\n\n const renderController = useCallback(({ field: { onBlur, onChange, ref, value } }: UseControllerReturn<TFieldValues, TName>) => {\n ref({\n focus: () => {\n inputRef?.current?.focus();\n },\n });\n return (\n <FormControl\n {...TestIdUtil.createAttributes('Select', { label, name })}\n fullWidth\n >\n <FormControlLabel\n control={(\n <MuiSwitch\n checked={!!value}\n color={'primary'}\n slotProps={{\n input: {\n ref: inputRef,\n },\n }}\n />\n )}\n disabled={disabled}\n label={label}\n onBlur={onBlur}\n onChange={onMuiSwitchChange(onChange)}\n />\n <FormHelperText\n className={classnames({ 'Mui-warning': hasWarning })}\n sx={{ ml: 0 }}\n >\n <FormFieldHelperText\n errorMessage={errorMessage}\n infoMessage={infoMessage}\n noIndent\n warningMessage={warningMessage}\n />\n </FormHelperText>\n { !!loading && <FormFieldLoadingIndicator />}\n </FormControl>\n\n );\n }, [disabled, infoMessage, errorMessage, hasWarning, label, loading, name, onMuiSwitchChange, warningMessage]);\n\n return (\n\n <Controller\n control={control}\n defaultValue={null}\n name={name}\n render={renderController}\n />\n\n );\n};\n"],"mappings":";;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../../src/components/fields/Switch/Switch.tsx"],"sourcesContent":["import type {\n ReactElement,\n ReactNode,\n SyntheticEvent,\n} from 'react';\nimport {\n useCallback,\n useRef,\n} from 'react';\nimport {\n Box,\n FormControl,\n FormControlLabel,\n FormHelperText,\n Switch as MuiSwitch,\n} from '@mui/material';\nimport type {\n ControllerRenderProps,\n FieldValues,\n Path,\n UseControllerReturn,\n} from 'react-hook-form';\nimport {\n Controller,\n useFormContext,\n} from 'react-hook-form';\nimport classnames from 'classnames';\nimport { TestIdUtil } from '@gen-epix/ui-core/utils/TestIdUtil';\n\nimport { FormUtil } from '../../../utils/FormUtil';\nimport { FormFieldHelperText } from '../../helpers/FormFieldHelperText';\nimport { FormFieldLoadingIndicator } from '../../helpers/FormFieldLoadingIndicator';\n\nexport type SwitchProps<TFieldValues extends FieldValues, TName extends Path<TFieldValues>> = {\n readonly disabled?: boolean;\n readonly icon?: ReactNode;\n readonly infoMessage?: string;\n readonly label: string;\n readonly loading?: boolean;\n readonly name: TName;\n readonly onChange?: (value: boolean) => void;\n readonly required?: boolean;\n readonly warningMessage?: string;\n};\n\nexport const Switch = <TFieldValues extends FieldValues, TName extends Path<TFieldValues> = Path<TFieldValues>>({\n disabled = false,\n icon,\n infoMessage,\n label,\n loading = false,\n name,\n onChange: onChangeProp,\n warningMessage,\n}: SwitchProps<TFieldValues, TName>): ReactElement => {\n const { control, formState: { errors } } = useFormContext<TFieldValues>();\n const errorMessage = FormUtil.getFieldErrorMessage(errors, name);\n const inputRef = useRef<HTMLInputElement>(null);\n\n const hasError = !!errorMessage;\n const hasWarning = !!warningMessage && !hasError;\n\n const onMuiSwitchChange = useCallback((onChange: ControllerRenderProps<TFieldValues, TName>['onChange']) =>\n (_event: SyntheticEvent, checked: boolean) => {\n if (onChangeProp) {\n onChangeProp(checked);\n }\n onChange(checked);\n }\n , [onChangeProp]);\n\n const renderController = useCallback(({ field: { onBlur, onChange, ref, value } }: UseControllerReturn<TFieldValues, TName>) => {\n ref({\n focus: () => {\n inputRef?.current?.focus();\n },\n });\n return (\n <FormControl\n {...TestIdUtil.createAttributes('Select', { label, name })}\n fullWidth\n >\n <FormControlLabel\n control={(\n <MuiSwitch\n checked={!!value}\n color={'primary'}\n size={'small'}\n slotProps={{\n input: {\n ref: inputRef,\n },\n }}\n />\n )}\n disabled={disabled}\n label={icon ? (\n <Box\n component={'span'}\n sx={{ alignItems: 'center', display: 'flex', gap: 0.5 }}\n >\n {icon}\n {label}\n </Box>\n ) : label}\n onBlur={onBlur}\n onChange={onMuiSwitchChange(onChange)}\n />\n <FormHelperText\n className={classnames({ 'Mui-warning': hasWarning })}\n sx={{ ml: 0 }}\n >\n <FormFieldHelperText\n errorMessage={errorMessage}\n infoMessage={infoMessage}\n noIndent\n warningMessage={warningMessage}\n />\n </FormHelperText>\n { !!loading && <FormFieldLoadingIndicator />}\n </FormControl>\n\n );\n }, [disabled, infoMessage, errorMessage, hasWarning, icon, label, loading, name, onMuiSwitchChange, warningMessage]);\n\n return (\n\n <Controller\n control={control}\n defaultValue={null}\n name={name}\n render={renderController}\n />\n\n );\n};\n"],"mappings":";;;;;;;;;;;mCA6Ca,KAAmG,EAC9G,cAAW,IACX,SACA,gBACA,UACA,aAAU,IACV,SACA,UAAU,GACV,wBACoD;CACpD,IAAM,EAAE,YAAS,WAAW,EAAE,gBAAa,EAA6B,GAClE,IAAe,EAAS,qBAAqB,GAAQ,CAAI,GACzD,IAAW,EAAyB,IAAI,GAGxC,IAAa,CAAC,CAAC,KAAkB,CADpB,GAGb,IAAoB,GAAa,OACpC,GAAwB,MAAqB;EAI5C,AAHI,KACF,EAAa,CAAO,GAEtB,EAAS,CAAO;CAClB,GACA,CAAC,CAAY,CAAC,GAEV,IAAmB,GAAa,EAAE,OAAO,EAAE,WAAQ,aAAU,QAAK,kBACtE,EAAI,EACF,aAAa;EACX,GAAU,SAAS,MAAM;CAC3B,EACF,CAAC,GAEC,kBAAC,GAAD;EACE,GAAI,EAAW,iBAAiB,UAAU;GAAE;GAAO;EAAK,CAAC;EACzD,WAAA;EAFF,UAAA;GAIE,kBAAC,GAAD;IACE,SACE,kBAAC,GAAD;KACE,SAAS,CAAC,CAAC;KACX,OAAO;KACP,MAAM;KACN,WAAW,EACT,OAAO,EACL,KAAK,EACP,EACF;IACD,CAAA;IAEO;IACV,OAAO,IACL,kBAAC,GAAD;KACE,WAAW;KACX,IAAI;MAAE,YAAY;MAAU,SAAS;MAAQ,KAAK;KAAI;KAFxD,UAAA,CAIG,GACA,CACE;IACH,CAAA,IAAA;IACI;IACR,UAAU,EAAkB,CAAQ;GACrC,CAAA;GACD,kBAAC,GAAD;IACE,YAAA,GAAW,EAAA,QAAA,CAAW,EAAE,eAAe,EAAW,CAAC;IACnD,IAAI,EAAE,IAAI,EAAE;IAEZ,UAAA,kBAAC,GAAD;KACgB;KACD;KACb,UAAA;KACgB;IACjB,CAAA;GACa,CAAA;GACd,CAAC,CAAC,KAAW,kBAAC,GAAD,CAA4B,CAAA;EAChC;MAGd;EAAC;EAAU;EAAa;EAAc;EAAY;EAAM;EAAO;EAAS;EAAM;EAAmB;CAAc,CAAC;CAEnH,OAEE,kBAAC,GAAD;EACW;EACT,cAAc;EACR;EACN,QAAQ;CACT,CAAA;AAGL"}
|
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
import { n as e } from "../../../_chunks/rolldown-runtime.js";
|
|
2
2
|
import { t } from "../../../_chunks/FormUtil.js";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
3
|
+
import { FormFieldHelperText as n } from "../../helpers/FormFieldHelperText/index.js";
|
|
4
|
+
import { FormFieldLoadingIndicator as r } from "../../helpers/FormFieldLoadingIndicator/index.js";
|
|
5
|
+
import { t as i } from "../../../_chunks/classnames.js";
|
|
6
|
+
import { useCallback as a, useRef as o } from "react";
|
|
7
|
+
import { Controller as s, useFormContext as c } from "react-hook-form";
|
|
8
8
|
import { useTranslation as l } from "react-i18next";
|
|
9
9
|
import { FormControl as u, IconButton as d, InputAdornment as f, TextField as p } from "@mui/material";
|
|
10
10
|
import m from "@mui/icons-material/Clear";
|
|
11
11
|
import { jsx as h, jsxs as g } from "react/jsx-runtime";
|
|
12
12
|
import { TestIdUtil as _ } from "@gen-epix/ui-core/utils/TestIdUtil";
|
|
13
13
|
//#region src/components/fields/TextField/TextField.tsx
|
|
14
|
-
var v = /* @__PURE__ */ e(
|
|
15
|
-
let { t: A } = l(), { control: j, formState: { errors: M } } =
|
|
14
|
+
var v = /* @__PURE__ */ e(i(), 1), y = ({ autocomplete: e, disabled: i = !1, infoMessage: y, label: b, loading: x = !1, multiline: S = !1, name: C, onChange: w, placeholder: T, required: E = !1, rows: D = 3, type: O = "text", warningMessage: k }) => {
|
|
15
|
+
let { t: A } = l(), { control: j, formState: { errors: M } } = c(), N = t.getFieldErrorMessage(M, C), P = !!N, F = !!k && !P, I = o(null), L = a((e) => (t) => {
|
|
16
16
|
let n = t.target.value;
|
|
17
17
|
w && w(n), e(n);
|
|
18
|
-
}, [w]), R =
|
|
19
|
-
|
|
18
|
+
}, [w]), R = a(({ field: { onBlur: t, onChange: r, ref: a, value: o } }) => {
|
|
19
|
+
a({ focus: () => {
|
|
20
20
|
I?.current?.focus();
|
|
21
21
|
} });
|
|
22
22
|
let s = () => {
|
|
23
|
-
|
|
23
|
+
r("");
|
|
24
24
|
};
|
|
25
25
|
return /* @__PURE__ */ h(p, {
|
|
26
|
-
disabled:
|
|
26
|
+
disabled: i || x,
|
|
27
27
|
error: P,
|
|
28
|
-
helperText: /* @__PURE__ */ h(
|
|
28
|
+
helperText: /* @__PURE__ */ h(n, {
|
|
29
29
|
errorMessage: N,
|
|
30
30
|
infoMessage: y,
|
|
31
31
|
warningMessage: k
|
|
@@ -34,14 +34,14 @@ var v = /* @__PURE__ */ e(o(), 1), y = ({ autocomplete: e, disabled: o = !1, inf
|
|
|
34
34
|
label: b,
|
|
35
35
|
multiline: S,
|
|
36
36
|
onBlur: t,
|
|
37
|
-
onChange: L(
|
|
37
|
+
onChange: L(r),
|
|
38
38
|
placeholder: T,
|
|
39
39
|
rows: D,
|
|
40
40
|
slotProps: {
|
|
41
41
|
formHelperText: { className: (0, v.default)({ "Mui-warning": F }) },
|
|
42
42
|
input: {
|
|
43
43
|
className: (0, v.default)({ "Mui-warning": F }),
|
|
44
|
-
endAdornment:
|
|
44
|
+
endAdornment: i ? void 0 : /* @__PURE__ */ h(f, {
|
|
45
45
|
position: "end",
|
|
46
46
|
children: /* @__PURE__ */ h(d, {
|
|
47
47
|
..._.createAttributes("TextField-reset"),
|
|
@@ -59,14 +59,14 @@ var v = /* @__PURE__ */ e(o(), 1), y = ({ autocomplete: e, disabled: o = !1, inf
|
|
|
59
59
|
},
|
|
60
60
|
inputLabel: {
|
|
61
61
|
className: (0, v.default)({ "Mui-warning": F }),
|
|
62
|
-
required: E && !
|
|
62
|
+
required: E && !i
|
|
63
63
|
}
|
|
64
64
|
},
|
|
65
|
-
value:
|
|
65
|
+
value: o ?? "",
|
|
66
66
|
variant: "outlined"
|
|
67
67
|
});
|
|
68
68
|
}, [
|
|
69
|
-
|
|
69
|
+
i,
|
|
70
70
|
x,
|
|
71
71
|
y,
|
|
72
72
|
P,
|
|
@@ -94,12 +94,12 @@ var v = /* @__PURE__ */ e(o(), 1), y = ({ autocomplete: e, disabled: o = !1, inf
|
|
|
94
94
|
"&:hover button, &:focus-within button": { display: "initial" },
|
|
95
95
|
button: { display: "none" }
|
|
96
96
|
},
|
|
97
|
-
children: [/* @__PURE__ */ h(
|
|
97
|
+
children: [/* @__PURE__ */ h(s, {
|
|
98
98
|
control: j,
|
|
99
99
|
defaultValue: "",
|
|
100
100
|
name: C,
|
|
101
101
|
render: R
|
|
102
|
-
}), !!x && /* @__PURE__ */ h(
|
|
102
|
+
}), !!x && /* @__PURE__ */ h(r, {})]
|
|
103
103
|
});
|
|
104
104
|
};
|
|
105
105
|
//#endregion
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { t as e } from "../../../_chunks/FormUtil.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { FormFieldHelperText as t } from "../../helpers/FormFieldHelperText/index.js";
|
|
3
|
+
import { useCallback as n, useRef as r } from "react";
|
|
4
|
+
import { Controller as i, useFormContext as a } from "react-hook-form";
|
|
5
5
|
import { FormControl as o, FormHelperText as s, ToggleButton as c, ToggleButtonGroup as l } from "@mui/material";
|
|
6
6
|
import { jsx as u, jsxs as d } from "react/jsx-runtime";
|
|
7
7
|
import { TestIdUtil as f } from "@gen-epix/ui-core/utils/TestIdUtil";
|
|
8
8
|
//#region src/components/fields/ToggleButtonGroup/ToggleButtonGroup.tsx
|
|
9
9
|
var p = ({ disabled: p, infoMessage: m, name: h, onChange: g, options: _, required: v, warningMessage: y }) => {
|
|
10
|
-
let { control: b, formState: { errors: x } } =
|
|
10
|
+
let { control: b, formState: { errors: x } } = a(), S = e.getFieldErrorMessage(x, h), C = r(null), w = !!S, T = n((e) => (t, n) => {
|
|
11
11
|
v && n === null || (g && g(n), e(n));
|
|
12
|
-
}, [g, v]), E =
|
|
12
|
+
}, [g, v]), E = n(({ field: { onBlur: e, onChange: t, ref: n, value: r } }) => (n({ focus: () => {
|
|
13
13
|
C?.current?.focus();
|
|
14
14
|
} }), /* @__PURE__ */ u(l, {
|
|
15
15
|
color: "primary",
|
|
@@ -35,14 +35,14 @@ var p = ({ disabled: p, infoMessage: m, name: h, onChange: g, options: _, requir
|
|
|
35
35
|
error: w,
|
|
36
36
|
...f.createAttributes("ToggleButtonGroup", { name: h }),
|
|
37
37
|
fullWidth: !0,
|
|
38
|
-
children: [/* @__PURE__ */ u(
|
|
38
|
+
children: [/* @__PURE__ */ u(i, {
|
|
39
39
|
control: b,
|
|
40
40
|
defaultValue: null,
|
|
41
41
|
name: h,
|
|
42
42
|
render: E
|
|
43
43
|
}), /* @__PURE__ */ u(s, {
|
|
44
44
|
sx: { ml: 0 },
|
|
45
|
-
children: /* @__PURE__ */ u(
|
|
45
|
+
children: /* @__PURE__ */ u(t, {
|
|
46
46
|
errorMessage: S,
|
|
47
47
|
infoMessage: m,
|
|
48
48
|
noIndent: !0,
|
|
@@ -12,7 +12,7 @@ export type GenericFormProps<TFormFields extends FieldValues> = {
|
|
|
12
12
|
readonly onSubmit?: SubmitEventHandler<HTMLFormElement>;
|
|
13
13
|
readonly renderField?: (definition: FormFieldDefinition<TFormFields>, element: ReactElement) => ReactElement;
|
|
14
14
|
readonly renderGroup?: (definition: FormGroupDefinition, children: ReactElement) => ReactElement;
|
|
15
|
-
readonly schema
|
|
15
|
+
readonly schema?: ObjectSchema<TFormFields, TFormFields>;
|
|
16
16
|
readonly wrapForm?: (children: ReactElement) => ReactElement;
|
|
17
17
|
};
|
|
18
18
|
export declare const GenericForm: <TFormFields extends FieldValues>({ disableAll, formFieldDefinitions, formGroupDefinitions, formId, formMethods, onSubmit, renderField, renderGroup, schema, wrapForm, }: GenericFormProps<TFormFields>) => import("react").JSX.Element;
|
|
@@ -1,82 +1,82 @@
|
|
|
1
1
|
import { t as e } from "../../../_chunks/FormUtil.js";
|
|
2
2
|
import { FORM_FIELD_DEFINITION_TYPE as t } from "../../../models/form.js";
|
|
3
|
-
import { n } from "../../../
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { t as a } from "../../../_chunks/
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import { t as u } from "../../../_chunks/
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
3
|
+
import { useIsFormFieldRequiredFromSchema as n } from "../../../hooks/useIsFormFieldRequiredFromSchema/index.js";
|
|
4
|
+
import { n as r } from "../../../_chunks/RichTextEditor.js";
|
|
5
|
+
import { t as i } from "../../../_chunks/TransferList.js";
|
|
6
|
+
import { t as a } from "../../../_chunks/Autocomplete.js";
|
|
7
|
+
import { Select as o } from "../../fields/Select/index.js";
|
|
8
|
+
import { TextField as s } from "../../fields/TextField/index.js";
|
|
9
|
+
import { NumberField as c } from "../../fields/NumberField/index.js";
|
|
10
|
+
import { t as l } from "../../../_chunks/DatePicker.js";
|
|
11
|
+
import { t as u } from "../../../_chunks/FileUpload.js";
|
|
12
|
+
import { RadioGroup as d } from "../../fields/RadioGroup/index.js";
|
|
13
|
+
import { Switch as f } from "../../fields/Switch/index.js";
|
|
14
|
+
import { Fragment as p, useCallback as m, useMemo as h } from "react";
|
|
15
|
+
import { FormProvider as g } from "react-hook-form";
|
|
16
16
|
import { useTranslation as _ } from "react-i18next";
|
|
17
17
|
import { Alert as v, AlertTitle as y, Box as b, Button as x, Typography as S } from "@mui/material";
|
|
18
18
|
import { Fragment as C, jsx as w, jsxs as T } from "react/jsx-runtime";
|
|
19
19
|
//#region src/components/helpers/GenericForm/GenericForm.tsx
|
|
20
20
|
var E = ({ disableAll: E, formFieldDefinitions: D, formGroupDefinitions: O, formId: k, formMethods: A, onSubmit: j, renderField: M, renderGroup: N, schema: P, wrapForm: F }) => {
|
|
21
|
-
let { t: I } = _(), L =
|
|
21
|
+
let { t: I } = _(), L = h(() => e.createBooleanOptions(I), [I]), R = n(P, A.getValues), z = m((e) => {
|
|
22
22
|
switch (e.definition) {
|
|
23
|
-
case t.AUTOCOMPLETE: return /* @__PURE__ */ w(
|
|
23
|
+
case t.AUTOCOMPLETE: return /* @__PURE__ */ w(a, {
|
|
24
24
|
...e,
|
|
25
25
|
disabled: e.disabled || E,
|
|
26
26
|
required: R(e.name)
|
|
27
27
|
});
|
|
28
|
-
case t.BOOLEAN: return /* @__PURE__ */ w(
|
|
28
|
+
case t.BOOLEAN: return /* @__PURE__ */ w(o, {
|
|
29
29
|
...e,
|
|
30
30
|
disabled: e.disabled || E,
|
|
31
31
|
options: L,
|
|
32
32
|
required: R(e.name)
|
|
33
33
|
});
|
|
34
|
-
case t.BOOLEAN_SWITCH: return /* @__PURE__ */ w(
|
|
34
|
+
case t.BOOLEAN_SWITCH: return /* @__PURE__ */ w(f, {
|
|
35
35
|
...e,
|
|
36
36
|
disabled: e.disabled || E,
|
|
37
37
|
required: R(e.name)
|
|
38
38
|
});
|
|
39
|
-
case t.DATE: return /* @__PURE__ */ w(
|
|
39
|
+
case t.DATE: return /* @__PURE__ */ w(l, {
|
|
40
40
|
...e,
|
|
41
41
|
disabled: e.disabled || E,
|
|
42
42
|
required: R(e.name)
|
|
43
43
|
});
|
|
44
|
-
case t.FILE: return /* @__PURE__ */ w(
|
|
44
|
+
case t.FILE: return /* @__PURE__ */ w(u, {
|
|
45
45
|
...e,
|
|
46
46
|
disabled: e.disabled || E,
|
|
47
47
|
required: R(e.name)
|
|
48
48
|
});
|
|
49
|
-
case t.NUMBER: return /* @__PURE__ */ w(
|
|
49
|
+
case t.NUMBER: return /* @__PURE__ */ w(c, {
|
|
50
50
|
...e,
|
|
51
51
|
disabled: e.disabled || E,
|
|
52
52
|
required: R(e.name)
|
|
53
53
|
});
|
|
54
|
-
case t.RADIO_GROUP: return /* @__PURE__ */ w(
|
|
54
|
+
case t.RADIO_GROUP: return /* @__PURE__ */ w(d, {
|
|
55
55
|
...e,
|
|
56
56
|
disabled: e.disabled || E,
|
|
57
57
|
required: R(e.name)
|
|
58
58
|
});
|
|
59
|
-
case t.RICH_TEXT: return /* @__PURE__ */ w(
|
|
59
|
+
case t.RICH_TEXT: return /* @__PURE__ */ w(r, {
|
|
60
60
|
...e,
|
|
61
61
|
disabled: e.disabled || E,
|
|
62
62
|
required: R(e.name)
|
|
63
63
|
});
|
|
64
|
-
case t.SELECT: return /* @__PURE__ */ w(
|
|
64
|
+
case t.SELECT: return /* @__PURE__ */ w(o, {
|
|
65
65
|
...e,
|
|
66
66
|
disabled: e.disabled || E,
|
|
67
67
|
required: R(e.name)
|
|
68
68
|
});
|
|
69
|
-
case t.SWITCH: return /* @__PURE__ */ w(
|
|
69
|
+
case t.SWITCH: return /* @__PURE__ */ w(f, {
|
|
70
70
|
...e,
|
|
71
71
|
disabled: e.disabled || E,
|
|
72
72
|
required: R(e.name)
|
|
73
73
|
});
|
|
74
|
-
case t.TEXTFIELD: return /* @__PURE__ */ w(
|
|
74
|
+
case t.TEXTFIELD: return /* @__PURE__ */ w(s, {
|
|
75
75
|
...e,
|
|
76
76
|
disabled: e.disabled || E,
|
|
77
77
|
required: R(e.name)
|
|
78
78
|
});
|
|
79
|
-
case t.TRANSFER_LIST: return /* @__PURE__ */ w(
|
|
79
|
+
case t.TRANSFER_LIST: return /* @__PURE__ */ w(i, {
|
|
80
80
|
...e,
|
|
81
81
|
disabled: e.disabled || E,
|
|
82
82
|
required: R(e.name)
|
|
@@ -87,10 +87,18 @@ var E = ({ disableAll: E, formFieldDefinitions: D, formGroupDefinitions: O, form
|
|
|
87
87
|
L,
|
|
88
88
|
E,
|
|
89
89
|
R
|
|
90
|
-
]), B =
|
|
90
|
+
]), B = m((e) => M ? /* @__PURE__ */ T(p, { children: [
|
|
91
|
+
e.contentBefore,
|
|
92
|
+
M(e, z(e)),
|
|
93
|
+
e.contentAfter
|
|
94
|
+
] }, e.name) : /* @__PURE__ */ T(b, {
|
|
91
95
|
sx: { marginY: 1 },
|
|
92
|
-
children:
|
|
93
|
-
|
|
96
|
+
children: [
|
|
97
|
+
e.contentBefore,
|
|
98
|
+
z(e),
|
|
99
|
+
e.contentAfter
|
|
100
|
+
]
|
|
101
|
+
}, e.name), [M, z]), V = h(() => {
|
|
94
102
|
if (!O?.length) return /* @__PURE__ */ w(C, { children: D.map(B) });
|
|
95
103
|
let e = /* @__PURE__ */ new Map();
|
|
96
104
|
for (let t of O) e.set(t.groupKey, []);
|
|
@@ -100,9 +108,10 @@ var E = ({ disableAll: E, formFieldDefinitions: D, formGroupDefinitions: O, form
|
|
|
100
108
|
let n = e.get(t.groupKey) ?? [];
|
|
101
109
|
if (!n.length) return null;
|
|
102
110
|
let r = /* @__PURE__ */ w(C, { children: n.map(B) });
|
|
103
|
-
return N ? /* @__PURE__ */ w(
|
|
111
|
+
return N ? /* @__PURE__ */ w(p, { children: N(t, r) }, t.groupKey) : /* @__PURE__ */ T(b, {
|
|
104
112
|
sx: { marginY: 2 },
|
|
105
113
|
children: [
|
|
114
|
+
t.contentBefore,
|
|
106
115
|
/* @__PURE__ */ w(S, {
|
|
107
116
|
variant: "subtitle1",
|
|
108
117
|
children: t.label
|
|
@@ -129,7 +138,8 @@ var E = ({ disableAll: E, formFieldDefinitions: D, formGroupDefinitions: O, form
|
|
|
129
138
|
})
|
|
130
139
|
})]
|
|
131
140
|
}, e.message)),
|
|
132
|
-
r
|
|
141
|
+
r,
|
|
142
|
+
t.contentAfter
|
|
133
143
|
]
|
|
134
144
|
}, t.groupKey);
|
|
135
145
|
}), t.map(B)] });
|
|
@@ -139,7 +149,7 @@ var E = ({ disableAll: E, formFieldDefinitions: D, formGroupDefinitions: O, form
|
|
|
139
149
|
N,
|
|
140
150
|
B
|
|
141
151
|
]);
|
|
142
|
-
return /* @__PURE__ */ w(
|
|
152
|
+
return /* @__PURE__ */ w(g, {
|
|
143
153
|
...A,
|
|
144
154
|
children: /* @__PURE__ */ w(b, {
|
|
145
155
|
autoComplete: "off",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../../../src/components/helpers/GenericForm/GenericForm.tsx"],"sourcesContent":["import type {\n ReactElement,\n SubmitEventHandler,\n} from 'react';\nimport {\n Fragment,\n useCallback,\n useMemo,\n} from 'react';\nimport { useTranslation } from 'react-i18next';\nimport type {\n FieldValues,\n Path,\n UseFormReturn,\n} from 'react-hook-form';\nimport { FormProvider } from 'react-hook-form';\nimport {\n Alert,\n AlertTitle,\n Box,\n Button,\n Typography,\n} from '@mui/material';\nimport type { ObjectSchema } from 'yup';\n\nimport type {\n FormFieldDefinition,\n FormGroupDefinition,\n} from '../../../models/form';\nimport { FORM_FIELD_DEFINITION_TYPE } from '../../../models/form';\nimport { FormUtil } from '../../../utils/FormUtil';\nimport { RichTextEditor } from '../../fields/RichTextEditor';\nimport { TransferList } from '../../fields/TransferList';\nimport type { AutocompleteProps } from '../../fields/Autocomplete';\nimport { Autocomplete } from '../../fields/Autocomplete';\nimport type { SelectProps } from '../../fields/Select';\nimport { Select } from '../../fields/Select';\nimport { TextField } from '../../fields/TextField';\nimport { NumberField } from '../../fields/NumberField';\nimport { DatePicker } from '../../fields/DatePicker';\nimport { UploadButton } from '../../fields/UploadButton/UploadButton';\nimport { RadioGroup } from '../../fields/RadioGroup';\nimport { Switch } from '../../fields/Switch';\nimport { useIsFormFieldRequiredFromSchema } from '../../../hooks/useIsFormFieldRequiredFromSchema';\n\nexport type GenericFormProps<TFormFields extends FieldValues> = {\n readonly defaultFormValues?: Partial<TFormFields>;\n readonly disableAll?: boolean;\n readonly formFieldDefinitions: FormFieldDefinition<TFormFields>[];\n readonly formGroupDefinitions?: FormGroupDefinition[];\n readonly formId?: string;\n readonly formMethods: UseFormReturn<TFormFields>;\n readonly onSubmit?: SubmitEventHandler<HTMLFormElement>;\n readonly renderField?: (definition: FormFieldDefinition<TFormFields>, element: ReactElement) => ReactElement;\n readonly renderGroup?: (definition: FormGroupDefinition, children: ReactElement) => ReactElement;\n readonly schema: ObjectSchema<TFormFields, TFormFields>;\n readonly wrapForm?: (children: ReactElement) => ReactElement;\n};\n\nexport const GenericForm = <TFormFields extends FieldValues>({\n disableAll,\n formFieldDefinitions,\n formGroupDefinitions,\n formId,\n formMethods,\n onSubmit,\n renderField,\n renderGroup,\n schema,\n wrapForm,\n}: GenericFormProps<TFormFields>) => {\n const { t } = useTranslation();\n\n const booleanOptions = useMemo(() => FormUtil.createBooleanOptions(t), [t]);\n\n const isFormFieldRequired = useIsFormFieldRequiredFromSchema(schema, formMethods.getValues);\n\n const renderFormFieldDefinition = useCallback((formFieldDefinition: FormFieldDefinition<TFormFields>) => {\n switch (formFieldDefinition.definition) {\n case FORM_FIELD_DEFINITION_TYPE.AUTOCOMPLETE:\n return (\n <Autocomplete\n {...formFieldDefinition as AutocompleteProps<TFormFields, Path<TFormFields>, false>}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.BOOLEAN:\n return (\n <Select\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n options={booleanOptions}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.BOOLEAN_SWITCH:\n return (\n <Switch\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.DATE:\n return (\n <DatePicker\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.FILE:\n return (\n <UploadButton\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.NUMBER:\n return (\n <NumberField\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.RADIO_GROUP:\n return (\n <RadioGroup\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.RICH_TEXT:\n return (\n <RichTextEditor\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.SELECT:\n return (\n <Select\n {...formFieldDefinition as SelectProps<TFormFields, Path<TFormFields>, false>}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.SWITCH:\n return (\n <Switch\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.TEXTFIELD:\n return (\n <TextField\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.TRANSFER_LIST:\n return (\n <TransferList\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n default:\n throw Error(`Unsupported form field definition: ${formFieldDefinition.definition}`);\n }\n }, [booleanOptions, disableAll, isFormFieldRequired]);\n\n\n const renderSingleField = useCallback((formFieldDefinition: FormFieldDefinition<TFormFields>) => {\n if (renderField) {\n return (\n <Fragment key={formFieldDefinition.name}>\n {renderField(formFieldDefinition, renderFormFieldDefinition(formFieldDefinition))}\n </Fragment>\n );\n }\n return (\n <Box\n key={formFieldDefinition.name}\n sx={{\n marginY: 1,\n }}\n >\n {renderFormFieldDefinition(formFieldDefinition)}\n </Box>\n );\n }, [renderField, renderFormFieldDefinition]);\n\n const formFieldsContent = useMemo(() => {\n if (!formGroupDefinitions?.length) {\n return (\n <>\n {formFieldDefinitions.map(renderSingleField)}\n </>\n );\n }\n\n const groupFieldsMap = new Map<string, FormFieldDefinition<TFormFields>[]>();\n for (const group of formGroupDefinitions) {\n groupFieldsMap.set(group.groupKey, []);\n }\n\n const ungroupedFields: FormFieldDefinition<TFormFields>[] = [];\n for (const field of formFieldDefinitions) {\n if (field.groupKey && groupFieldsMap.has(field.groupKey)) {\n groupFieldsMap.get(field.groupKey)?.push(field);\n } else {\n ungroupedFields.push(field);\n }\n }\n\n return (\n <>\n {formGroupDefinitions.map(group => {\n const groupFields = groupFieldsMap.get(group.groupKey) ?? [];\n if (!groupFields.length) {\n return null;\n }\n const groupContent = (\n <>\n {groupFields.map(renderSingleField)}\n </>\n );\n if (renderGroup) {\n return (\n <Fragment key={group.groupKey}>\n {renderGroup(group, groupContent)}\n </Fragment>\n );\n }\n return (\n <Box\n key={group.groupKey}\n sx={{ marginY: 2 }}\n >\n <Typography variant={'subtitle1'}>\n {group.label}\n </Typography>\n {group.description && (\n <Typography\n color={'text.secondary'}\n variant={'body2'}\n >\n {group.description}\n </Typography>\n )}\n {group.messages?.map((msg) => (\n <Alert\n key={msg.message}\n severity={msg.severity}\n sx={{ '& .MuiAlert-message': { width: '100%' } }}\n >\n <AlertTitle>\n {msg.message}\n </AlertTitle>\n {msg.buttonLabel && msg.onButtonClick && (\n <Box sx={{ display: 'flex', justifyContent: 'flex-end' }}>\n <Button\n color={'inherit'}\n onClick={msg.onButtonClick}\n size={'small'}\n variant={'text'}\n >\n {msg.buttonLabel}\n </Button>\n </Box>\n )}\n </Alert>\n ))}\n {groupContent}\n </Box>\n );\n })}\n {ungroupedFields.map(renderSingleField)}\n </>\n );\n }, [formFieldDefinitions, formGroupDefinitions, renderGroup, renderSingleField]);\n\n return (\n <FormProvider {...formMethods}>\n <Box\n autoComplete={'off'}\n component={'form'}\n id={formId}\n onSubmit={onSubmit}\n >\n {wrapForm ? wrapForm(formFieldsContent) : formFieldsContent}\n </Box>\n </FormProvider>\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA2DA,IAAa,KAAgD,EAC3D,eACA,yBACA,yBACA,WACA,gBACA,aACA,gBACA,gBACA,WACA,kBACmC;CACnC,IAAM,EAAE,SAAM,EAAe,GAEvB,IAAiB,QAAc,EAAS,qBAAqB,CAAC,GAAG,CAAC,CAAC,CAAC,GAEpE,IAAsB,EAA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C3D,IAAI,CAAC,EAAY,QACf,OAAO;GAET,IAAM,IACJ,kBAAA,GAAA,EAAA,UACG,EAAY,IAAI,CAAiB,EAClC,CAAA;GASJ,OAPI,IAEA,kBAAC,GAAD,EAAA,UACG,EAAY,GAAO,CAAY,EACxB,GAFK,EAAM,QAEX,IAIZ,kBAAC,GAAD;IAEE,IAAI,EAAE,SAAS,EAAE;IAFnB,UAAA;KAIE,kBAAC,GAAD;MAAY,SAAS;MAClB,UAAA,EAAM;KACG,CAAA;KACX,EAAM,eACL,kBAAC,GAAD;MACE,OAAO;MACP,SAAS;MAER,UAAA,EAAM;KACG,CAAA;KAEb,EAAM,UAAU,KAAK,MACpB,kBAAC,GAAD;MAEE,UAAU,EAAI;MACd,IAAI,EAAE,uBAAuB,EAAE,OAAO,OAAO,EAAE;MAHjD,UAAA,CAKE,kBAAC,GAAD,EAAA,UACG,EAAI,QACK,CAAA,GACX,EAAI,eAAe,EAAI,iBACtB,kBAAC,GAAD;OAAK,IAAI;QAAE,SAAS;QAAQ,gBAAgB;OAAW;OACrD,UAAA,kBAAC,GAAD;QACE,OAAO;QACP,SAAS,EAAI;QACb,MAAM;QACN,SAAS;QAER,UAAA,EAAI;OACC,CAAA;MACL,CAAA,CAEF;KAnBA,GAAA,EAAI,OAmBJ,CACR;KACA;IACE;GAtCE,GAAA,EAAM,QAsCR;EAET,CAAC,GACA,EAAgB,IAAI,CAAiB,CACtC,EAAA,CAAA;CAEN,GAAG;EAAC;EAAsB;EAAsB;EAAa;CAAiB,CAAC;CAE/E,OACE,kBAAC,GAAD;EAAc,GAAI;EAChB,UAAA,kBAAC,GAAD;GACE,cAAc;GACd,WAAW;GACX,IAAI;GACM;GAET,UAAA,IAAW,EAAS,CAAiB,IAAI;EACvC,CAAA;CACO,CAAA;AAElB"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../../src/components/helpers/GenericForm/GenericForm.tsx"],"sourcesContent":["import type {\n ReactElement,\n SubmitEventHandler,\n} from 'react';\nimport {\n Fragment,\n useCallback,\n useMemo,\n} from 'react';\nimport { useTranslation } from 'react-i18next';\nimport type {\n FieldValues,\n Path,\n UseFormReturn,\n} from 'react-hook-form';\nimport { FormProvider } from 'react-hook-form';\nimport {\n Alert,\n AlertTitle,\n Box,\n Button,\n Typography,\n} from '@mui/material';\nimport type { ObjectSchema } from 'yup';\n\nimport type {\n FormFieldDefinition,\n FormGroupDefinition,\n} from '../../../models/form';\nimport { FORM_FIELD_DEFINITION_TYPE } from '../../../models/form';\nimport { FormUtil } from '../../../utils/FormUtil';\nimport { RichTextEditor } from '../../fields/RichTextEditor';\nimport { TransferList } from '../../fields/TransferList';\nimport type { AutocompleteProps } from '../../fields/Autocomplete';\nimport { Autocomplete } from '../../fields/Autocomplete';\nimport type { SelectProps } from '../../fields/Select';\nimport { Select } from '../../fields/Select';\nimport { TextField } from '../../fields/TextField';\nimport { NumberField } from '../../fields/NumberField';\nimport { DatePicker } from '../../fields/DatePicker';\nimport { FileUpload } from '../../fields/FileUpload/FileUpload';\nimport { RadioGroup } from '../../fields/RadioGroup';\nimport { Switch } from '../../fields/Switch';\nimport { useIsFormFieldRequiredFromSchema } from '../../../hooks/useIsFormFieldRequiredFromSchema';\n\nexport type GenericFormProps<TFormFields extends FieldValues> = {\n readonly defaultFormValues?: Partial<TFormFields>;\n readonly disableAll?: boolean;\n readonly formFieldDefinitions: FormFieldDefinition<TFormFields>[];\n readonly formGroupDefinitions?: FormGroupDefinition[];\n readonly formId?: string;\n readonly formMethods: UseFormReturn<TFormFields>;\n readonly onSubmit?: SubmitEventHandler<HTMLFormElement>;\n readonly renderField?: (definition: FormFieldDefinition<TFormFields>, element: ReactElement) => ReactElement;\n readonly renderGroup?: (definition: FormGroupDefinition, children: ReactElement) => ReactElement;\n readonly schema?: ObjectSchema<TFormFields, TFormFields>;\n readonly wrapForm?: (children: ReactElement) => ReactElement;\n};\n\nexport const GenericForm = <TFormFields extends FieldValues>({\n disableAll,\n formFieldDefinitions,\n formGroupDefinitions,\n formId,\n formMethods,\n onSubmit,\n renderField,\n renderGroup,\n schema,\n wrapForm,\n}: GenericFormProps<TFormFields>) => {\n const { t } = useTranslation();\n\n const booleanOptions = useMemo(() => FormUtil.createBooleanOptions(t), [t]);\n\n const isFormFieldRequired = useIsFormFieldRequiredFromSchema(schema, formMethods.getValues);\n\n const renderFormFieldDefinition = useCallback((formFieldDefinition: FormFieldDefinition<TFormFields>) => {\n switch (formFieldDefinition.definition) {\n case FORM_FIELD_DEFINITION_TYPE.AUTOCOMPLETE:\n return (\n <Autocomplete\n {...formFieldDefinition as AutocompleteProps<TFormFields, Path<TFormFields>, false>}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.BOOLEAN:\n return (\n <Select\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n options={booleanOptions}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.BOOLEAN_SWITCH:\n return (\n <Switch\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.DATE:\n return (\n <DatePicker\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.FILE:\n return (\n <FileUpload\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.NUMBER:\n return (\n <NumberField\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.RADIO_GROUP:\n return (\n <RadioGroup\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.RICH_TEXT:\n return (\n <RichTextEditor\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.SELECT:\n return (\n <Select\n {...formFieldDefinition as SelectProps<TFormFields, Path<TFormFields>, false>}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.SWITCH:\n return (\n <Switch\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.TEXTFIELD:\n return (\n <TextField\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n case FORM_FIELD_DEFINITION_TYPE.TRANSFER_LIST:\n return (\n <TransferList\n {...formFieldDefinition}\n disabled={formFieldDefinition.disabled || disableAll}\n required={isFormFieldRequired(formFieldDefinition.name)}\n />\n );\n default:\n throw Error(`Unsupported form field definition: ${formFieldDefinition.definition}`);\n }\n }, [booleanOptions, disableAll, isFormFieldRequired]);\n\n\n const renderSingleField = useCallback((formFieldDefinition: FormFieldDefinition<TFormFields>) => {\n if (renderField) {\n return (\n <Fragment key={formFieldDefinition.name}>\n {formFieldDefinition.contentBefore}\n {renderField(formFieldDefinition, renderFormFieldDefinition(formFieldDefinition))}\n {formFieldDefinition.contentAfter}\n </Fragment>\n );\n }\n return (\n <Box\n key={formFieldDefinition.name}\n sx={{\n marginY: 1,\n }}\n >\n {formFieldDefinition.contentBefore}\n {renderFormFieldDefinition(formFieldDefinition)}\n {formFieldDefinition.contentAfter}\n </Box>\n );\n }, [renderField, renderFormFieldDefinition]);\n\n const formFieldsContent = useMemo(() => {\n if (!formGroupDefinitions?.length) {\n return (\n <>\n {formFieldDefinitions.map(renderSingleField)}\n </>\n );\n }\n\n const groupFieldsMap = new Map<string, FormFieldDefinition<TFormFields>[]>();\n for (const group of formGroupDefinitions) {\n groupFieldsMap.set(group.groupKey, []);\n }\n\n const ungroupedFields: FormFieldDefinition<TFormFields>[] = [];\n for (const field of formFieldDefinitions) {\n if (field.groupKey && groupFieldsMap.has(field.groupKey)) {\n groupFieldsMap.get(field.groupKey)?.push(field);\n } else {\n ungroupedFields.push(field);\n }\n }\n\n return (\n <>\n {formGroupDefinitions.map(group => {\n const groupFields = groupFieldsMap.get(group.groupKey) ?? [];\n if (!groupFields.length) {\n return null;\n }\n const groupContent = (\n <>\n {groupFields.map(renderSingleField)}\n </>\n );\n if (renderGroup) {\n return (\n <Fragment key={group.groupKey}>\n {renderGroup(group, groupContent)}\n </Fragment>\n );\n }\n return (\n <Box\n key={group.groupKey}\n sx={{ marginY: 2 }}\n >\n {group.contentBefore}\n <Typography variant={'subtitle1'}>\n {group.label}\n </Typography>\n {group.description && (\n <Typography\n color={'text.secondary'}\n variant={'body2'}\n >\n {group.description}\n </Typography>\n )}\n {group.messages?.map((msg) => (\n <Alert\n key={msg.message}\n severity={msg.severity}\n sx={{ '& .MuiAlert-message': { width: '100%' } }}\n >\n <AlertTitle>\n {msg.message}\n </AlertTitle>\n {msg.buttonLabel && msg.onButtonClick && (\n <Box sx={{ display: 'flex', justifyContent: 'flex-end' }}>\n <Button\n color={'inherit'}\n onClick={msg.onButtonClick}\n size={'small'}\n variant={'text'}\n >\n {msg.buttonLabel}\n </Button>\n </Box>\n )}\n </Alert>\n ))}\n {groupContent}\n {group.contentAfter}\n </Box>\n );\n })}\n {ungroupedFields.map(renderSingleField)}\n </>\n );\n }, [formFieldDefinitions, formGroupDefinitions, renderGroup, renderSingleField]);\n\n return (\n <FormProvider {...formMethods}>\n <Box\n autoComplete={'off'}\n component={'form'}\n id={formId}\n onSubmit={onSubmit}\n >\n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;EAET,CAAC,GACA,EAAgB,IAAI,CAAiB,CACtC,EAAA,CAAA;CAEN,GAAG;EAAC;EAAsB;EAAsB;EAAa;CAAiB,CAAC;CAE/E,OACE,kBAAC,GAAD;EAAc,GAAI;EAChB,UAAA,kBAAC,GAAD;GACE,cAAc;GACd,WAAW;GACX,IAAI;GACM;GAET,UAAA,IAAW,EAAS,CAAiB,IAAI;EACvC,CAAA;CACO,CAAA;AAElB"}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useCallback as e } from "react";
|
|
2
|
+
import { set as t } from "react-hook-form";
|
|
3
3
|
//#region src/hooks/useIsFormFieldRequiredFromSchema/useIsFormFieldRequiredFromSchema.ts
|
|
4
|
-
var n = (n, r) =>
|
|
4
|
+
var n = (n, r) => e((e) => {
|
|
5
|
+
if (!n) return !1;
|
|
5
6
|
try {
|
|
6
7
|
let i = structuredClone(r());
|
|
7
|
-
return
|
|
8
|
+
return t(i, e, void 0), n.validateSyncAt(e, i, {
|
|
8
9
|
abortEarly: !0,
|
|
9
10
|
strict: !1
|
|
10
11
|
}), !1;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../../src/hooks/useIsFormFieldRequiredFromSchema/useIsFormFieldRequiredFromSchema.ts"],"sourcesContent":["import { useCallback } from 'react';\nimport { set } from 'react-hook-form';\nimport type {\n FieldValues,\n Path,\n UseFormGetValues,\n} from 'react-hook-form';\nimport type {\n ObjectSchema,\n ValidationError,\n} from 'yup';\n\nconst useIsFormFieldRequiredFromSchema = <TFormFields extends FieldValues, TName extends Path<TFormFields>>(schema: ObjectSchema<TFormFields, TFormFields>, getFormValues: UseFormGetValues<TFormFields>): (fieldName: TName) => boolean => {\n return useCallback((fieldName: TName) => {\n try {\n const values = structuredClone(getFormValues());\n set(values, fieldName, undefined);\n\n schema.validateSyncAt(fieldName, values, {\n abortEarly: true,\n strict: false,\n });\n return false;\n } catch (error) {\n return (error as ValidationError)?.type === 'required' || (error as ValidationError)?.type === 'optionality';\n }\n }, [getFormValues, schema]);\n};\n\nexport {\n useIsFormFieldRequiredFromSchema,\n};\n"],"mappings":";;;AAYA,IAAM,KAAsG,GAAgD,MACnJ,GAAa,MAAqB;CACvC,IAAI;EACF,IAAM,IAAS,gBAAgB,EAAc,CAAC;EAO9C,OANA,EAAI,GAAQ,GAAW,KAAA,CAAS,GAEhC,EAAO,eAAe,GAAW,GAAQ;GACvC,YAAY;GACZ,QAAQ;EACV,CAAC,GACM;CACT,SAAS,GAAO;EACd,OAAQ,GAA2B,SAAS,cAAe,GAA2B,SAAS;CACjG;AACF,GAAG,CAAC,GAAe,CAAM,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../src/hooks/useIsFormFieldRequiredFromSchema/useIsFormFieldRequiredFromSchema.ts"],"sourcesContent":["import { useCallback } from 'react';\nimport { set } from 'react-hook-form';\nimport type {\n FieldValues,\n Path,\n UseFormGetValues,\n} from 'react-hook-form';\nimport type {\n ObjectSchema,\n ValidationError,\n} from 'yup';\n\nconst useIsFormFieldRequiredFromSchema = <TFormFields extends FieldValues, TName extends Path<TFormFields>>(schema: ObjectSchema<TFormFields, TFormFields>, getFormValues: UseFormGetValues<TFormFields>): (fieldName: TName) => boolean => {\n return useCallback((fieldName: TName) => {\n if (!schema) {\n return false;\n }\n\n try {\n const values = structuredClone(getFormValues());\n set(values, fieldName, undefined);\n\n schema.validateSyncAt(fieldName, values, {\n abortEarly: true,\n strict: false,\n });\n return false;\n } catch (error) {\n return (error as ValidationError)?.type === 'required' || (error as ValidationError)?.type === 'optionality';\n }\n }, [getFormValues, schema]);\n};\n\nexport {\n useIsFormFieldRequiredFromSchema,\n};\n"],"mappings":";;;AAYA,IAAM,KAAsG,GAAgD,MACnJ,GAAa,MAAqB;CACvC,IAAI,CAAC,GACH,OAAO;CAGT,IAAI;EACF,IAAM,IAAS,gBAAgB,EAAc,CAAC;EAO9C,OANA,EAAI,GAAQ,GAAW,KAAA,CAAS,GAEhC,EAAO,eAAe,GAAW,GAAQ;GACvC,YAAY;GACZ,QAAQ;EACV,CAAC,GACM;CACT,SAAS,GAAO;EACd,OAAQ,GAA2B,SAAS,cAAe,GAA2B,SAAS;CACjG;AACF,GAAG,CAAC,GAAe,CAAM,CAAC"}
|