@nwon/react-mui-hook-form 3.1.5 → 3.1.6

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.
Files changed (47) hide show
  1. package/package.json +1 -1
  2. package/dist/LoadingButton/LoadingButton.d.ts +0 -6
  3. package/dist/LoadingButton/LoadingButtonInterfaces.d.ts +0 -33
  4. package/dist/LoadingButton/getLoadingButtonState.d.ts +0 -20
  5. package/dist/LoadingButton/index.d.ts +0 -4
  6. package/dist/formFields/HookFormProvider.d.ts +0 -7
  7. package/dist/formFields/InputEmail/InputEmail.d.ts +0 -10
  8. package/dist/formFields/InputPasswordSet/InputPasswordSet.d.ts +0 -8
  9. package/dist/formFields/InputPasswordSet/InputPasswordSetInterfaces.d.ts +0 -18
  10. package/dist/formFields/TranslatedString.d.ts +0 -13
  11. package/dist/formFields/baseTypes/BaseInputProps.d.ts +0 -33
  12. package/dist/formFields/baseTypes/InputAutocomplete/InputAutocomplete.d.ts +0 -9
  13. package/dist/formFields/baseTypes/InputAutocomplete/InputAutocompleteInterfaces.d.ts +0 -24
  14. package/dist/formFields/baseTypes/InputAutocomplete/useSortedOptionsWithGroup.d.ts +0 -9
  15. package/dist/formFields/baseTypes/InputCheckbox/InputCheckbox.d.ts +0 -18
  16. package/dist/formFields/baseTypes/InputDatePicker/InputDatePicker.d.ts +0 -16
  17. package/dist/formFields/baseTypes/InputDateTimePicker/InputDateTimePicker.d.ts +0 -16
  18. package/dist/formFields/baseTypes/InputNumber/InputNumber.d.ts +0 -15
  19. package/dist/formFields/baseTypes/InputOutlinedInput/InputOutlinedInput.d.ts +0 -13
  20. package/dist/formFields/baseTypes/InputRadio/InputRadio.d.ts +0 -13
  21. package/dist/formFields/baseTypes/InputSelect/InputSelect.d.ts +0 -26
  22. package/dist/formFields/baseTypes/InputSelectFromEnum/InputSelectFromEnum.d.ts +0 -15
  23. package/dist/formFields/baseTypes/InputSelectFromEnum/InputSelectFromEnumInterfaces.d.ts +0 -10
  24. package/dist/formFields/baseTypes/InputSlider/InputSlider.d.ts +0 -13
  25. package/dist/formFields/baseTypes/InputSwitch/InputSwitch.d.ts +0 -11
  26. package/dist/formFields/baseTypes/InputTextField/InputTextField.d.ts +0 -13
  27. package/dist/formFields/baseTypes/InputTimePicker/InputTimePicker.d.ts +0 -16
  28. package/dist/formFields/baseTypes/InputToggleButton/InputToggleButton.d.ts +0 -12
  29. package/dist/formFields/baseTypes/InputToggleButtonGroup/InputToggleButtonGroup.d.ts +0 -34
  30. package/dist/formFields/baseTypes/useEffectFieldUnregisterOnUnmount.d.ts +0 -12
  31. package/dist/formFields/errorMessages.d.ts +0 -1
  32. package/dist/formFields/errors/FormFieldError.d.ts +0 -11
  33. package/dist/formFields/errors/FormFieldErrorDisplay.d.ts +0 -11
  34. package/dist/formFields/errors/getFormErrorForNestedKeys.d.ts +0 -14
  35. package/dist/formFields/errors/useFormFieldError.d.ts +0 -7
  36. package/dist/formFields/errors/useFormFieldHasError.d.ts +0 -6
  37. package/dist/formFields/formHelper/syncSubmit.d.ts +0 -15
  38. package/dist/formFields/formHelper/useHookForm.d.ts +0 -58
  39. package/dist/formFields/formHelper/useHookFormContext.d.ts +0 -9
  40. package/dist/formFields/formHelper/useMergedFormField.d.ts +0 -6
  41. package/dist/formFields/formHelper/useSortedOptionsWithGroup.d.ts +0 -5
  42. package/dist/formFields/formHelper/useTranslateString.d.ts +0 -8
  43. package/dist/formFields/index.d.ts +0 -49
  44. package/dist/formFields/inputTypes.d.ts +0 -76
  45. package/dist/packageExports.d.ts +0 -9
  46. package/dist/packageExports.js +0 -1772
  47. package/dist/packageExports.js.map +0 -1
@@ -1,1772 +0,0 @@
1
- import { CheckBoxOutlineBlank, CheckBox } from '@mui/icons-material';
2
- import { FormHelperText, Checkbox, FormControl, Autocomplete, TextField, FormControlLabel, Box, InputLabel, OutlinedInput, FormLabel, RadioGroup, Radio, Select, MenuItem, Typography, Stack, Tooltip, ListSubheader, Slider, Switch, ToggleButton, ToggleButtonGroup } from '@mui/material';
3
- import { useFormControl } from '@mui/material/FormControl';
4
- import { filterNoUndefinedNoNull, uppercaseFirstLetter, assertTruthy, objectHasKey, rethrow } from '@nvon/baseline';
5
- import { useCallback, useMemo, useState, useEffect } from 'react';
6
- import { useFormContext, useWatch, Controller, useForm, useFieldArray, FormProvider } from 'react-hook-form';
7
- import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
8
- import { identity } from 'lodash-es';
9
- import { t } from 'i18next';
10
- import { DatePicker, DateTimePicker, TimePicker } from '@mui/x-date-pickers';
11
- import dayjs from 'dayjs';
12
- import { NumericFormat } from 'react-number-format';
13
- import { LoadingButton as LoadingButton$1 } from '@mui/lab';
14
-
15
- const formContextNull = "FormContext is null. This should not happen. You may need to use the HookFormProvider instead of the FormProvider exported by react-hook-form";
16
-
17
- const FormFieldErrorDisplay = ({
18
- error,
19
- formHelperTextProps
20
- }) => {
21
- if (Array.isArray(error)) {
22
- return /*#__PURE__*/jsx(FormHelperText, {
23
- ...formHelperTextProps,
24
- error: true,
25
- children: error.map(e => e.message).filter(filterNoUndefinedNoNull).join(", ")
26
- });
27
- }
28
- if (error?.message) {
29
- return /*#__PURE__*/jsx(FormHelperText, {
30
- ...formHelperTextProps,
31
- error: true,
32
- children: error?.message
33
- });
34
- }
35
- return null;
36
- };
37
-
38
- /**
39
- * Wrapper around react-hook-form's useFormContext.
40
- *
41
- * Should behave like the original function, only exposing a "fieldName"
42
- * function on top which is used to validate field names.
43
- */
44
- const useHookFormContext = () => {
45
- const form = useFormContext();
46
- return {
47
- ...form,
48
- fieldName: identity
49
- };
50
- };
51
-
52
- /**
53
- * Get the field errors for a (possibly nested) field name.
54
- *
55
- * Checking `formState.errors[fieldName]` only works if fieldName is not a path
56
- * (e.g. Does not include dots).
57
- *
58
- * Did not find a built-in way to do this … there might be one though.
59
- *
60
- * Supports both dot notation (e.g., "response.receipts.0.field") and
61
- * bracket notation (e.g., "response.receipts[0].field") for array fields.
62
- */
63
- const getFormErrorForNestedKeys = (errors, fieldName) => {
64
- // Parse the field name to handle both dot notation and bracket notation
65
- // e.g., "response.receipts.0.field" or "response.receipts[0].field"
66
- const keys = [];
67
- let currentKey = "";
68
- let i = 0;
69
- while (i < fieldName.length) {
70
- const char = fieldName[i];
71
- if (char === "[") {
72
- // Save the current key if any
73
- if (currentKey) {
74
- keys.push(currentKey);
75
- currentKey = "";
76
- }
77
- // Find the closing bracket
78
- i++; // Skip the opening bracket
79
- let bracketContent = "";
80
- while (i < fieldName.length && fieldName[i] !== "]") {
81
- bracketContent += fieldName[i];
82
- i++;
83
- }
84
- // Parse the bracket content as a number if possible, otherwise as string
85
- const numIndex = Number(bracketContent);
86
- keys.push(isNaN(numIndex) ? bracketContent : numIndex);
87
- i++; // Skip the closing bracket
88
- } else if (char === ".") {
89
- // Save the current key if any
90
- if (currentKey) {
91
- keys.push(currentKey);
92
- currentKey = "";
93
- }
94
- i++; // Skip the dot
95
- } else {
96
- currentKey += char;
97
- i++;
98
- }
99
- }
100
- // Don't forget the last key
101
- if (currentKey) {
102
- keys.push(currentKey);
103
- }
104
- //
105
- // To my best knowledge, this works as expected.
106
- //
107
- // It recursively walks the error object by the given keys
108
- // until the lowest level is reached, or undefined returned.
109
- //
110
- // Handles both object properties and array indices.
111
- //
112
- // Typescript is just not very happy with the dynamic nature of this,
113
- // hence the errors.
114
- //
115
- /* eslint-disable @typescript-eslint/ban-ts-comment,@typescript-eslint/no-unsafe-return */
116
- // @ts-expect-error
117
- return keys.reduce((errorObj, pathKey) => {
118
- if (errorObj === undefined || errorObj === null) {
119
- return undefined;
120
- }
121
- // Handle array access
122
- if (typeof pathKey === "number") {
123
- return Array.isArray(errorObj) ? errorObj[pathKey] :
124
- // @ts-expect-error
125
- errorObj?.[pathKey];
126
- }
127
- // Handle object property access
128
- // @ts-expect-error
129
- return errorObj?.[pathKey];
130
- }, errors);
131
- /* eslint-enable */
132
- };
133
-
134
- /**
135
- * Returns the error for a given field name.
136
- *
137
- * @throws Error when FormContext is null.
138
- */
139
- const useFormFieldError = fieldName => {
140
- const form = useHookFormContext();
141
- if (!form) {
142
- throw Error(formContextNull);
143
- }
144
- const {
145
- formState
146
- } = form;
147
- const errors = getFormErrorForNestedKeys(formState.errors, fieldName);
148
- return errors;
149
- };
150
-
151
- const FormFieldError = ({
152
- fieldName,
153
- formHelperTextProps
154
- }) => {
155
- const error = useFormFieldError(fieldName);
156
- return /*#__PURE__*/jsx(FormFieldErrorDisplay, {
157
- error: error,
158
- formHelperTextProps: formHelperTextProps
159
- });
160
- };
161
-
162
- /**
163
- * Returns whether or not a field has an error.
164
- *
165
- * @throws Error when FormContext is null.
166
- */
167
- const useFormFieldHasError = fieldName => !!useFormFieldError(fieldName);
168
-
169
- /**
170
- * Return a function which turns our TranslatedString into a plain string.
171
- *
172
- * @throws Error if no t was provided but a translated string.
173
- */
174
- const useTranslateString = () => {
175
- if (!t) {
176
- throw Error("If you pass in a translated validate function you need to provide a t");
177
- }
178
- return useCallback(input => {
179
- return typeof input === "string" ? input : input(t);
180
- }, []);
181
- };
182
-
183
- /**
184
- * Merge a form field with defaults.
185
- */
186
- const useMergedFormField = (
187
- /**
188
- * Form fields, typically those passed into a component.
189
- *
190
- */
191
- formField, t,
192
- /**
193
- * Additional defaults. Duplicate values will be overridden by the formField.
194
- */
195
- formFieldDefaults) => {
196
- const translate = useTranslateString();
197
- const merged = useMemo(() => {
198
- const builtInDefaults = {
199
- label: uppercaseFirstLetter(formField.name),
200
- required: false,
201
- requiredErrorMessage: "Please fill in required information",
202
- validate: undefined
203
- };
204
- return {
205
- ...builtInDefaults,
206
- ...formFieldDefaults,
207
- ...formField
208
- };
209
- }, [formField, formFieldDefaults]);
210
- const withPlainStrings = useMemo(() => {
211
- // Translating the validate function is a bit tricky,
212
- // since it can be an object in both cases.
213
- const validate = (() => {
214
- if (typeof merged.validate === "object" && typeof merged.validate.translated === "function") {
215
- if (!t) {
216
- throw Error("If you pass in a translated validate function you need to provide a t");
217
- }
218
- return merged.validate.translated(t);
219
- }
220
- return merged.validate;
221
- })(); // This should be fine. Not sure why TS is complaining.
222
- return {
223
- ...merged,
224
- label: translate(merged.label, t),
225
- requiredErrorMessage: translate(merged.requiredErrorMessage, t),
226
- validate
227
- };
228
- }, [merged, t, translate]);
229
- return withPlainStrings;
230
- };
231
-
232
- /**
233
- * An effect that unregisters a field from the form when the component unmounts.
234
- *
235
- * @throws Error when FormContext is null.
236
- */
237
- const useEffectFieldUnregisterOnUnmount = ({
238
- name,
239
- unregisterOptions,
240
- isUnregisteringOnUnmount
241
- }) => {
242
- const form = useHookFormContext();
243
- /**
244
- * Default options for unregistering the field.
245
- *
246
- * Needed to wrap this in a state in order to prevent infinite rerenders.
247
- */
248
- const [options] = useState(unregisterOptions);
249
- if (!form) {
250
- throw Error(formContextNull);
251
- }
252
- const {
253
- unregister
254
- } = form;
255
- useEffect(() => {
256
- return () => {
257
- if (isUnregisteringOnUnmount) {
258
- unregister(name, options);
259
- }
260
- };
261
- }, [isUnregisteringOnUnmount, name, unregister, options]);
262
- };
263
-
264
- var GroupLabelSorting;
265
- (function (GroupLabelSorting) {
266
- GroupLabelSorting["Alphabetical"] = "alphabetical";
267
- GroupLabelSorting["GroupMemberCountAsc"] = "groupMemberCountAsc";
268
- })(GroupLabelSorting || (GroupLabelSorting = {}));
269
- /**
270
- * Sorts option in the desired order.
271
- */
272
- const useSortedOptionsWithGroup$1 = (selectableOptions, sorting = GroupLabelSorting.Alphabetical) => {
273
- return useMemo(() => {
274
- if (sorting === GroupLabelSorting.GroupMemberCountAsc) {
275
- const groupCounts = selectableOptions.reduce((acc, option) => {
276
- if (option.group) {
277
- acc[option.group] = (acc[option.group] || 0) + 1;
278
- }
279
- return acc;
280
- }, {});
281
- return selectableOptions.sort((a, b) => {
282
- if (a.group == undefined && !!b.group) {
283
- return -1;
284
- }
285
- if (a.group && b.group) {
286
- const groupA = groupCounts[a.group];
287
- const groupB = groupCounts[b.group];
288
- if (groupA && groupB && groupA < groupB) {
289
- return -1;
290
- } else if (groupCounts[a.group] === groupCounts[b.group]) {
291
- if (b.label > a.label) {
292
- return -1;
293
- }
294
- return 1;
295
- }
296
- return 1;
297
- }
298
- if (b.label > a.label) {
299
- return -1;
300
- }
301
- return 1;
302
- });
303
- }
304
- return selectableOptions.sort((a, b) => {
305
- if (a.group == undefined && !!b.group) {
306
- return -1;
307
- }
308
- if (a.group && b.group && a.group < b.group) {
309
- return -1;
310
- }
311
- if (a.group == b.group && b.label > a.label) {
312
- return 1;
313
- }
314
- return 0;
315
- });
316
- }, [selectableOptions, sorting]);
317
- };
318
-
319
- const icon = /*#__PURE__*/jsx(CheckBoxOutlineBlank, {
320
- fontSize: "small"
321
- });
322
- const checkedIcon = /*#__PURE__*/jsx(CheckBox, {
323
- fontSize: "small"
324
- });
325
- const defaultNonMultiselectValue = "";
326
- /**
327
- * Renders a MUI autocomplete input that is embedded in a react hook form context.
328
- *
329
- * @throws Error when FormContext is null.
330
- */
331
- const InputAutocomplete = ({
332
- formField,
333
- selectableOptions,
334
- noOptions,
335
- autoCompleteProps,
336
- checkboxesForMultiselect: renderCheckboxes,
337
- groupSorting,
338
- inputTextFieldProps,
339
- unregisterOptions,
340
- isUnregisteringOnUnmount
341
- }) => {
342
- const form = useHookFormContext();
343
- if (!form) {
344
- throw Error(formContextNull);
345
- }
346
- const {
347
- register,
348
- control,
349
- setValue
350
- } = form;
351
- const hasError = useFormFieldHasError(formField.name);
352
- const muiFormControl = useFormControl();
353
- const {
354
- label,
355
- name,
356
- required,
357
- requiredErrorMessage,
358
- validate
359
- } = useMergedFormField(formField, formField.t);
360
- useEffectFieldUnregisterOnUnmount({
361
- name,
362
- unregisterOptions,
363
- isUnregisteringOnUnmount
364
- });
365
- const multiSelect = !!autoCompleteProps?.multiple;
366
- const value = useWatch({
367
- name,
368
- control,
369
- exact: true
370
- });
371
- const {
372
- value: valueToUse,
373
- hasValue
374
- } = useMemo(() => {
375
- if (value) {
376
- return {
377
- value,
378
- hasValue: true
379
- };
380
- }
381
- return {
382
- value: multiSelect ? [] : defaultNonMultiselectValue,
383
- hasValue: false
384
- };
385
- }, [multiSelect, value]);
386
- const adjustedSelectableOptions = useMemo(() => {
387
- if (hasValue || multiSelect) {
388
- return selectableOptions;
389
- }
390
- return [...selectableOptions, {
391
- value: defaultNonMultiselectValue,
392
- label: ""
393
- }];
394
- }, [hasValue, multiSelect, selectableOptions]);
395
- const sortedOptions = useSortedOptionsWithGroup$1(adjustedSelectableOptions, groupSorting);
396
- assertTruthy(["string", "number", "object"].includes(typeof valueToUse), `Value (Field: ${name}, value: ${valueToUse?.toString() || "undefined"}) for select input has unexpected type`);
397
- const isRequired = required || muiFormControl?.required;
398
- const isDisabled = muiFormControl?.disabled || autoCompleteProps?.disabled;
399
- const props = register(name, {
400
- required: isRequired ? requiredErrorMessage : false,
401
- validate,
402
- disabled: isDisabled
403
- });
404
- const currentValue = useMemo(() => {
405
- return multiSelect ? adjustedSelectableOptions.filter(o => {
406
- if (Array.isArray(valueToUse)) {
407
- return valueToUse.includes(o.value);
408
- }
409
- return adjustedSelectableOptions.find(option => option.value === valueToUse);
410
- }) : adjustedSelectableOptions.find(option => option.value === valueToUse);
411
- }, [multiSelect, adjustedSelectableOptions, valueToUse]);
412
- const renderOption = useMemo(() => {
413
- if (autoCompleteProps?.renderOption) {
414
- return autoCompleteProps.renderOption;
415
- }
416
- return multiSelect && renderCheckboxes ? (liProps, option) => {
417
- return /*#__PURE__*/jsxs("li", {
418
- ...liProps,
419
- children: [/*#__PURE__*/jsx(Checkbox, {
420
- icon: icon,
421
- checkedIcon: checkedIcon,
422
- sx: {
423
- mr: 2
424
- },
425
- checked: Array.isArray(currentValue) ? !!currentValue.find(item => item.value === option.value) : false
426
- }), option.label]
427
- });
428
- } : undefined;
429
- }, [autoCompleteProps?.renderOption, currentValue, multiSelect, renderCheckboxes]);
430
- return /*#__PURE__*/jsxs(FormControl, {
431
- error: hasError,
432
- required: isRequired,
433
- disabled: isDisabled,
434
- children: [/*#__PURE__*/jsx(Autocomplete, {
435
- options: sortedOptions,
436
- value: currentValue,
437
- ...props,
438
- required: isRequired,
439
- label: label,
440
- renderOption: renderOption,
441
- multiple: multiSelect,
442
- getOptionLabel: o => {
443
- if (objectHasKey(o, "label")) {
444
- return o.label;
445
- }
446
- return "";
447
- },
448
- onChange: (e, selectedOption) => {
449
- if (Array.isArray(selectedOption)) {
450
- setValue(name, selectedOption.map(o => o.value), {
451
- shouldDirty: true,
452
- shouldValidate: true,
453
- shouldTouch: true
454
- });
455
- } else if (selectedOption) {
456
- setValue(name, selectedOption.value, {
457
- shouldDirty: true,
458
- shouldValidate: true,
459
- shouldTouch: true
460
- });
461
- } else {
462
- setValue(name, undefined, {
463
- shouldDirty: true,
464
- shouldValidate: false,
465
- shouldTouch: true
466
- });
467
- }
468
- },
469
- noOptionsText: noOptions,
470
- filterOptions: (options, {
471
- inputValue
472
- }) => {
473
- return options.filter(o => {
474
- // Filter out the default empty option
475
- if (o.value === defaultNonMultiselectValue) {
476
- return false;
477
- }
478
- // If no input, show all non-default options
479
- if (!inputValue) {
480
- return true;
481
- }
482
- // Filter based on input text (case-insensitive)
483
- return o.label.toLowerCase().includes(inputValue.toLowerCase());
484
- });
485
- },
486
- renderInput: params => {
487
- return /*#__PURE__*/jsx(TextField, {
488
- ...params,
489
- label: label,
490
- variant: "outlined",
491
- ...inputTextFieldProps,
492
- required: isRequired,
493
- disabled: isDisabled,
494
- error: hasError
495
- });
496
- },
497
- groupBy: option => option.group || "Choose"
498
- // need to parse this as any in order to get no typing errors
499
- // for multiselect
500
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
501
- ,
502
- ...autoCompleteProps
503
- }), /*#__PURE__*/jsx(FormFieldError, {
504
- fieldName: formField.name
505
- })]
506
- });
507
- };
508
-
509
- const InputCheckbox = ({
510
- formField,
511
- jsxLabel,
512
- checkboxProps,
513
- formControlLabelProps,
514
- unregisterOptions,
515
- isUnregisteringOnUnmount
516
- }) => {
517
- const {
518
- control,
519
- setValue
520
- } = useHookFormContext();
521
- const hasError = useFormFieldHasError(formField.name);
522
- const muiFormControl = useFormControl();
523
- const {
524
- label,
525
- name,
526
- required,
527
- requiredErrorMessage,
528
- validate
529
- } = useMergedFormField(formField);
530
- useEffectFieldUnregisterOnUnmount({
531
- name,
532
- unregisterOptions,
533
- isUnregisteringOnUnmount
534
- });
535
- const id = `hook-form-input-checkbox-${name}`;
536
- const disabled = muiFormControl?.disabled || checkboxProps?.disabled;
537
- const isRequired = required || muiFormControl?.required;
538
- return /*#__PURE__*/jsxs(FormControl, {
539
- error: hasError,
540
- disabled: disabled,
541
- required: required,
542
- children: [/*#__PURE__*/jsx(FormControlLabel, {
543
- required: jsxLabel ? false : isRequired // If jsxLabel is set, we don't want to show the asterisk.
544
- ,
545
- disabled: disabled,
546
- label: jsxLabel ?? label,
547
- sx: {
548
- color: t => hasError ? t.palette.error.main : undefined
549
- },
550
- control: /*#__PURE__*/jsx(Controller, {
551
- name: name,
552
- control: control,
553
- rules: {
554
- required: isRequired ? requiredErrorMessage : false,
555
- validate
556
- },
557
- render: ({
558
- field: {
559
- value
560
- }
561
- }) => {
562
- return /*#__PURE__*/jsx(Checkbox, {
563
- checked: !!value,
564
- "aria-labelledby": id,
565
- onChange: (e, val) => {
566
- setValue(name, val, {
567
- shouldValidate: true,
568
- shouldDirty: true,
569
- shouldTouch: true
570
- });
571
- },
572
- ...checkboxProps,
573
- disabled: disabled
574
- });
575
- }
576
- }),
577
- ...formControlLabelProps
578
- }), /*#__PURE__*/jsx(FormFieldError, {
579
- fieldName: formField.name
580
- })]
581
- });
582
- };
583
-
584
- const InputDatePicker = ({
585
- formField,
586
- datePickerProps,
587
- unregisterOptions,
588
- isUnregisteringOnUnmount
589
- }) => {
590
- const {
591
- control
592
- } = useHookFormContext();
593
- const hasError = useFormFieldHasError(formField.name);
594
- const muiFormControl = useFormControl();
595
- const {
596
- label,
597
- name,
598
- required,
599
- requiredErrorMessage
600
- } = useMergedFormField(formField);
601
- useEffectFieldUnregisterOnUnmount({
602
- name,
603
- unregisterOptions,
604
- isUnregisteringOnUnmount
605
- });
606
- const [date, setDate] = useState(dayjs());
607
- const isRequired = required || muiFormControl?.required;
608
- const isDisabled = muiFormControl?.disabled || datePickerProps?.disabled;
609
- return /*#__PURE__*/jsx(Box, {
610
- children: /*#__PURE__*/jsxs(FormControl, {
611
- error: hasError,
612
- required: isRequired,
613
- disabled: isDisabled,
614
- children: [/*#__PURE__*/jsx(Controller, {
615
- name: name,
616
- defaultValue: date,
617
- control: control,
618
- rules: {
619
- required: isRequired ? requiredErrorMessage : false
620
- },
621
- render: ({
622
- field: {
623
- onChange,
624
- ...restField
625
- }
626
- }) => /*#__PURE__*/jsx(DatePicker, {
627
- label: label,
628
- onChange: event => {
629
- onChange(event);
630
- if (event) {
631
- setDate(event);
632
- }
633
- },
634
- disabled: isDisabled,
635
- slotProps: {
636
- textField: {
637
- required: isRequired,
638
- disabled: isDisabled,
639
- error: hasError
640
- }
641
- },
642
- ...restField,
643
- ...datePickerProps
644
- })
645
- }), /*#__PURE__*/jsx(FormFieldError, {
646
- fieldName: formField.name
647
- })]
648
- })
649
- });
650
- };
651
-
652
- const InputDateTimePicker = ({
653
- formField,
654
- dateTimePickerProps,
655
- unregisterOptions,
656
- isUnregisteringOnUnmount
657
- }) => {
658
- const {
659
- control
660
- } = useHookFormContext();
661
- const hasError = useFormFieldHasError(formField.name);
662
- const muiFormControl = useFormControl();
663
- const {
664
- label,
665
- name,
666
- required,
667
- requiredErrorMessage
668
- } = useMergedFormField(formField);
669
- useEffectFieldUnregisterOnUnmount({
670
- name,
671
- unregisterOptions,
672
- isUnregisteringOnUnmount
673
- });
674
- const [date, setDate] = useState(dayjs());
675
- const isRequired = required || muiFormControl?.required;
676
- const isDisabled = muiFormControl?.disabled || dateTimePickerProps?.disabled;
677
- return /*#__PURE__*/jsx(Box, {
678
- children: /*#__PURE__*/jsxs(FormControl, {
679
- error: hasError,
680
- required: isRequired,
681
- disabled: isDisabled,
682
- children: [/*#__PURE__*/jsx(Controller, {
683
- name: name,
684
- defaultValue: date,
685
- control: control,
686
- rules: {
687
- required: isRequired ? requiredErrorMessage : false
688
- },
689
- render: ({
690
- field: {
691
- onChange,
692
- ...restField
693
- }
694
- }) => /*#__PURE__*/jsx(DateTimePicker, {
695
- label: label,
696
- ampm: dateTimePickerProps?.ampm ? dateTimePickerProps.ampm : false,
697
- onChange: event => {
698
- onChange(event);
699
- if (event) {
700
- setDate(event);
701
- }
702
- },
703
- disabled: isDisabled,
704
- required: isRequired,
705
- slotProps: {
706
- textField: {
707
- required: isRequired,
708
- disabled: isDisabled,
709
- error: hasError
710
- }
711
- },
712
- ...restField,
713
- ...dateTimePickerProps
714
- })
715
- }), /*#__PURE__*/jsx(FormFieldError, {
716
- fieldName: formField.name
717
- })]
718
- })
719
- });
720
- };
721
-
722
- const InputNumber = ({
723
- formField,
724
- textFieldProps,
725
- numericFormatProps,
726
- unregisterOptions,
727
- isUnregisteringOnUnmount
728
- }) => {
729
- const form = useHookFormContext();
730
- const {
731
- control
732
- } = form;
733
- const muiFormControl = useFormControl();
734
- const {
735
- label,
736
- name,
737
- required,
738
- requiredErrorMessage,
739
- validate
740
- } = useMergedFormField(formField, formField.t);
741
- useEffectFieldUnregisterOnUnmount({
742
- name,
743
- unregisterOptions,
744
- isUnregisteringOnUnmount
745
- });
746
- if (!form) {
747
- throw Error(formContextNull);
748
- }
749
- const hasError = useFormFieldHasError(formField.name);
750
- const isRequired = required || muiFormControl?.required;
751
- const isDisabled = muiFormControl?.disabled || textFieldProps?.disabled;
752
- return /*#__PURE__*/jsxs(FormControl, {
753
- error: hasError,
754
- required: required,
755
- disabled: isDisabled,
756
- children: [/*#__PURE__*/jsx(Controller, {
757
- name: name,
758
- control: control,
759
- rules: {
760
- required: isRequired ? requiredErrorMessage : false,
761
- validate
762
- },
763
- render: props => {
764
- const {
765
- ref,
766
- ...rest
767
- } = props.field;
768
- return (
769
- /*#__PURE__*/
770
- // @ts-expect-error TextFieldProps is not compatible with NumberFormatBaseProps
771
- jsx(NumericFormat, {
772
- ...rest,
773
- customInput: TextField,
774
- onValueChange: target => {
775
- rest.onChange(target.value);
776
- },
777
- label: label,
778
- onChange: undefined,
779
- onBlur: props.field.onBlur,
780
- error: !!props.fieldState.error,
781
- ...textFieldProps,
782
- ...numericFormatProps,
783
- disabled: isDisabled,
784
- required: isRequired
785
- })
786
- );
787
- }
788
- }), /*#__PURE__*/jsx(FormFieldError, {
789
- fieldName: formField.name
790
- })]
791
- });
792
- };
793
-
794
- const InputOutlinedInput = ({
795
- formField,
796
- outlinedInputProps,
797
- unregisterOptions,
798
- isUnregisteringOnUnmount
799
- }) => {
800
- const form = useHookFormContext();
801
- const muiFormControl = useFormControl();
802
- const {
803
- label,
804
- name,
805
- required,
806
- requiredErrorMessage,
807
- validate
808
- } = useMergedFormField(formField, formField.t);
809
- useEffectFieldUnregisterOnUnmount({
810
- name,
811
- unregisterOptions,
812
- isUnregisteringOnUnmount
813
- });
814
- if (!form) {
815
- throw Error(formContextNull);
816
- }
817
- const {
818
- control
819
- } = form;
820
- const hasError = useFormFieldHasError(formField.name);
821
- const isRequired = required || muiFormControl?.required;
822
- const isDisabled = muiFormControl?.disabled || outlinedInputProps?.disabled;
823
- return /*#__PURE__*/jsxs(FormControl, {
824
- error: hasError,
825
- required: required,
826
- disabled: muiFormControl?.disabled,
827
- children: [/*#__PURE__*/jsx(InputLabel, {
828
- required: isRequired,
829
- disabled: isDisabled,
830
- children: label
831
- }), /*#__PURE__*/jsx(Controller, {
832
- name: name,
833
- control: control,
834
- rules: {
835
- required: isRequired ? requiredErrorMessage : false,
836
- validate
837
- },
838
- render: ({
839
- field: {
840
- value,
841
- onBlur,
842
- onChange
843
- }
844
- }) => {
845
- return /*#__PURE__*/jsx(OutlinedInput, {
846
- value: value,
847
- onChange: onChange,
848
- onBlur: onBlur,
849
- label: label,
850
- ...outlinedInputProps,
851
- required: isRequired,
852
- disabled: isDisabled
853
- });
854
- }
855
- }), /*#__PURE__*/jsx(FormFieldError, {
856
- fieldName: formField.name
857
- })]
858
- });
859
- };
860
-
861
- const InputRadio = ({
862
- formField,
863
- selectableOptions,
864
- formControlLabelProps,
865
- radioGroupProps,
866
- unregisterOptions,
867
- isUnregisteringOnUnmount
868
- }) => {
869
- const {
870
- control
871
- } = useHookFormContext();
872
- const hasError = useFormFieldHasError(formField.name);
873
- const muiFormControl = useFormControl();
874
- const {
875
- label,
876
- name,
877
- required,
878
- requiredErrorMessage,
879
- validate
880
- } = useMergedFormField(formField);
881
- useEffectFieldUnregisterOnUnmount({
882
- name,
883
- unregisterOptions,
884
- isUnregisteringOnUnmount
885
- });
886
- const id = `hook-form-input-radio-${name}`;
887
- const isRequired = required || muiFormControl?.required;
888
- const isDisabled = muiFormControl?.disabled || formControlLabelProps?.disabled;
889
- return /*#__PURE__*/jsxs(FormControl, {
890
- error: hasError,
891
- disabled: muiFormControl?.disabled,
892
- children: [/*#__PURE__*/jsx(FormLabel, {
893
- id: id,
894
- required: isRequired,
895
- disabled: isDisabled,
896
- error: hasError,
897
- sx: {
898
- color: t => hasError ? t.palette.error.main : undefined
899
- },
900
- children: label
901
- }), /*#__PURE__*/jsx(Controller, {
902
- name: name,
903
- control: control,
904
- rules: {
905
- required: isRequired ? requiredErrorMessage : false,
906
- validate
907
- },
908
- render: ({
909
- field: {
910
- onChange,
911
- value,
912
- onBlur
913
- }
914
- }) => {
915
- return /*#__PURE__*/jsx(RadioGroup, {
916
- value: value,
917
- "aria-labelledby": id,
918
- onChange: onChange,
919
- onBlur: onBlur,
920
- ...radioGroupProps,
921
- children: selectableOptions?.map(option => {
922
- return /*#__PURE__*/jsx(FormControlLabel, {
923
- value: option.value,
924
- control: /*#__PURE__*/jsx(Radio, {}),
925
- label: option.label,
926
- ...formControlLabelProps,
927
- disabled: isDisabled
928
- }, typeof option.value === "boolean" ? option.value.toString() : option.value);
929
- })
930
- });
931
- }
932
- }), /*#__PURE__*/jsx(FormFieldError, {
933
- fieldName: formField.name
934
- })]
935
- });
936
- };
937
-
938
- /**
939
- * Sorts the options by group and value.
940
- */
941
- const useSortedOptionsWithGroup = selectableOptions => {
942
- return useMemo(() => selectableOptions.sort((a, b) => {
943
- if (a.group == undefined && !!b.group) {
944
- return -1;
945
- }
946
- if (a.group && b.group && a.group < b.group) {
947
- return -1;
948
- }
949
- if (a.group == b.group && b.value > a.value) {
950
- return 1;
951
- }
952
- return 0;
953
- }), [selectableOptions]);
954
- };
955
-
956
- const InputSelect = ({
957
- formField,
958
- selectableOptions,
959
- optionsTitle,
960
- selectComponentProps,
961
- tooltipHolder,
962
- unregisterOptions,
963
- isUnregisteringOnUnmount
964
- }) => {
965
- const form = useHookFormContext();
966
- if (!form) {
967
- throw Error(formContextNull);
968
- }
969
- const {
970
- control
971
- } = form;
972
- const muiFormControl = useFormControl();
973
- const {
974
- label,
975
- name,
976
- required,
977
- requiredErrorMessage,
978
- validate
979
- } = useMergedFormField(formField, formField.t);
980
- useEffectFieldUnregisterOnUnmount({
981
- name,
982
- unregisterOptions,
983
- isUnregisteringOnUnmount
984
- });
985
- const defaultValue = useMemo(() => selectComponentProps?.multiple ? [] : "", [selectComponentProps?.multiple]);
986
- const id = `input-select-${name}`;
987
- const adjustedOptions = useMemo(() => selectComponentProps?.multiple || Array.isArray(defaultValue) ? selectableOptions : [...selectableOptions, {
988
- value: defaultValue,
989
- label: ""
990
- }], [defaultValue, selectComponentProps?.multiple, selectableOptions]);
991
- const sortedOptions = useSortedOptionsWithGroup(adjustedOptions);
992
- const hasErrors = useFormFieldHasError(formField.name);
993
- const isRequired = required || muiFormControl?.required;
994
- const isDisabled = muiFormControl?.disabled || selectComponentProps?.disabled;
995
- return /*#__PURE__*/jsxs(FormControl, {
996
- error: hasErrors,
997
- required: isRequired,
998
- disabled: isDisabled,
999
- children: [/*#__PURE__*/jsx(InputLabel, {
1000
- id: id,
1001
- required: isRequired,
1002
- disabled: isDisabled,
1003
- error: hasErrors,
1004
- children: label
1005
- }), /*#__PURE__*/jsx(Controller, {
1006
- name: name,
1007
- control: control,
1008
- rules: {
1009
- required: isRequired ? requiredErrorMessage : false,
1010
- validate
1011
- },
1012
- render: ({
1013
- field: {
1014
- onChange,
1015
- onBlur,
1016
- value
1017
- },
1018
- fieldState: {
1019
- error
1020
- }
1021
- }) => {
1022
- const valueToUse = value || defaultValue;
1023
- return /*#__PURE__*/jsxs(Select, {
1024
- labelId: id,
1025
- ...selectComponentProps,
1026
- label: label,
1027
- value: valueToUse,
1028
- error: !!error,
1029
- onChange: onChange,
1030
- onBlur: onBlur,
1031
- disabled: isDisabled,
1032
- required: isRequired,
1033
- children: [optionsTitle && /*#__PURE__*/jsx(MenuItem, {
1034
- value: defaultValue,
1035
- disabled: true,
1036
- children: /*#__PURE__*/jsx("em", {
1037
- children: optionsTitle
1038
- })
1039
- }), sortedOptions?.filter(o => o.value !== "").map((option, index) => {
1040
- const optionBefore = sortedOptions[index - 1];
1041
- const showGroup = (optionBefore || index == 0) && option.group && optionBefore?.group !== option.group;
1042
- const disabledText = typeof option.disabled === "string" ? option.disabled : undefined;
1043
- const isDisabledToUse = !!disabledText || !!option.disabled || isDisabled;
1044
- const menuItem = /*#__PURE__*/jsx(MenuItem, {
1045
- value: typeof option.value === "boolean" ? option.value.toString() : option.value,
1046
- disabled: isDisabledToUse,
1047
- title: typeof option.disabled === "string" ? option.disabled : undefined,
1048
- selected: typeof option.value === "boolean" ? option.value.toString() === value : value === option.value,
1049
- children: /*#__PURE__*/jsx(Typography, {
1050
- dangerouslySetInnerHTML: {
1051
- __html: option.label
1052
- }
1053
- })
1054
- }, option.label);
1055
- const wrappedMenuItem = disabledText ? /*#__PURE__*/jsxs(Box, {
1056
- sx: {
1057
- position: "relative"
1058
- },
1059
- children: [menuItem, /*#__PURE__*/jsx(Stack, {
1060
- direction: "column",
1061
- justifyContent: "center",
1062
- spacing: 1,
1063
- sx: {
1064
- position: "absolute",
1065
- right: th => th.spacing(0.5),
1066
- top: 0,
1067
- bottom: 0
1068
- },
1069
- children: tooltipHolder ? /*#__PURE__*/jsx(Tooltip, {
1070
- title: disabledText,
1071
- children: tooltipHolder
1072
- }) : null
1073
- })]
1074
- }) : menuItem;
1075
- return showGroup ? [/*#__PURE__*/jsx(ListSubheader, {
1076
- children: option.group
1077
- }, option.group), wrappedMenuItem] : wrappedMenuItem;
1078
- })]
1079
- });
1080
- }
1081
- }), /*#__PURE__*/jsx(FormFieldError, {
1082
- fieldName: formField.name
1083
- })]
1084
- });
1085
- };
1086
-
1087
- const InputSelectFromEnum = props => {
1088
- const {
1089
- enumData,
1090
- formField
1091
- } = props;
1092
- const translateFunction = formField.t;
1093
- const options = useMemo(() => {
1094
- return Object.entries(enumData.enumToUse).map(([key, value]) => {
1095
- const label = enumData.labelsForEnum[key];
1096
- assertTruthy(label, "Could not find a label for value: " + value + " in Field " + formField.name);
1097
- if (typeof label === "string") {
1098
- return {
1099
- label,
1100
- value
1101
- };
1102
- }
1103
- assertTruthy(translateFunction, `If you want to use translated labels for field (name: ${formField.name}) you need to provide a translate function in the HookFormProvider.`);
1104
- return {
1105
- label: label(translateFunction),
1106
- value
1107
- };
1108
- }).filter(option => {
1109
- if (props.limitOptions) {
1110
- return props.limitOptions.includes(option.value);
1111
- }
1112
- return true;
1113
- });
1114
- }, [enumData.enumToUse, enumData.labelsForEnum, formField.name, translateFunction, props.limitOptions]);
1115
- return props.autocomplete ? /*#__PURE__*/jsx(InputAutocomplete, {
1116
- noOptions: props.autocomplete.noOptionText,
1117
- ...props,
1118
- selectableOptions: options,
1119
- formField: formField
1120
- }) : /*#__PURE__*/jsx(InputSelect, {
1121
- ...props,
1122
- selectableOptions: options,
1123
- formField: formField
1124
- });
1125
- };
1126
-
1127
- const InputSlider = ({
1128
- formField,
1129
- sliderProps,
1130
- unregisterOptions,
1131
- isUnregisteringOnUnmount
1132
- }) => {
1133
- const form = useHookFormContext();
1134
- const muiFormControl = useFormControl();
1135
- const {
1136
- label,
1137
- name,
1138
- required,
1139
- requiredErrorMessage,
1140
- validate
1141
- } = useMergedFormField(formField, formField.t);
1142
- useEffectFieldUnregisterOnUnmount({
1143
- name,
1144
- unregisterOptions,
1145
- isUnregisteringOnUnmount
1146
- });
1147
- if (!form) {
1148
- throw Error(formContextNull);
1149
- }
1150
- const {
1151
- control
1152
- } = form;
1153
- const hasError = useFormFieldHasError(formField.name);
1154
- const isRequired = required || muiFormControl?.required;
1155
- const isDisabled = muiFormControl?.disabled || sliderProps?.disabled;
1156
- return /*#__PURE__*/jsxs(FormControl, {
1157
- error: hasError,
1158
- required: isRequired,
1159
- disabled: isDisabled,
1160
- children: [/*#__PURE__*/jsx(Controller, {
1161
- name: name,
1162
- control: control,
1163
- defaultValue: 0,
1164
- rules: {
1165
- required: isRequired ? requiredErrorMessage : false,
1166
- validate
1167
- },
1168
- render: ({
1169
- field: {
1170
- onChange,
1171
- value,
1172
- onBlur
1173
- }
1174
- }) => {
1175
- return /*#__PURE__*/jsx(Slider, {
1176
- value: value || 0,
1177
- onChange: onChange,
1178
- onBlur: onBlur,
1179
- required: isRequired,
1180
- label: label,
1181
- component: sliderProps?.component || "span",
1182
- max: typeof sliderProps?.max === "number" ? sliderProps.max : undefined,
1183
- min: typeof sliderProps?.min === "number" ? sliderProps.min : undefined,
1184
- ...sliderProps,
1185
- disabled: isDisabled
1186
- });
1187
- }
1188
- }), /*#__PURE__*/jsx(FormFieldError, {
1189
- fieldName: name
1190
- })]
1191
- });
1192
- };
1193
-
1194
- const InputSwitch = ({
1195
- formField,
1196
- switchProps,
1197
- formControlLabelProps,
1198
- unregisterOptions,
1199
- isUnregisteringOnUnmount
1200
- }) => {
1201
- const {
1202
- control,
1203
- setValue
1204
- } = useHookFormContext();
1205
- const hasError = useFormFieldHasError(formField.name);
1206
- const muiFormControl = useFormControl();
1207
- const {
1208
- label,
1209
- name,
1210
- required,
1211
- requiredErrorMessage,
1212
- validate
1213
- } = useMergedFormField(formField);
1214
- useEffectFieldUnregisterOnUnmount({
1215
- name,
1216
- unregisterOptions,
1217
- isUnregisteringOnUnmount
1218
- });
1219
- const isRequired = required || muiFormControl?.required;
1220
- const isDisabled = muiFormControl?.disabled || switchProps?.disabled;
1221
- return /*#__PURE__*/jsxs(FormControl, {
1222
- error: hasError,
1223
- disabled: isDisabled,
1224
- required: isRequired,
1225
- children: [/*#__PURE__*/jsx(FormControlLabel, {
1226
- label: label,
1227
- disabled: isDisabled,
1228
- required: isRequired,
1229
- sx: {
1230
- color: t => hasError ? t.palette.error.main : undefined
1231
- },
1232
- control: /*#__PURE__*/jsx(Controller, {
1233
- name: name,
1234
- control: control,
1235
- rules: {
1236
- required: isRequired ? requiredErrorMessage : false,
1237
- validate
1238
- },
1239
- render: ({
1240
- field: {
1241
- value
1242
- }
1243
- }) => {
1244
- return /*#__PURE__*/jsx(Switch, {
1245
- checked: !!value,
1246
- onChange: (e, val) => {
1247
- setValue(name, val, {
1248
- shouldValidate: true,
1249
- shouldDirty: true,
1250
- shouldTouch: true
1251
- });
1252
- },
1253
- ...switchProps
1254
- });
1255
- }
1256
- }),
1257
- ...formControlLabelProps
1258
- }), /*#__PURE__*/jsx(FormFieldError, {
1259
- fieldName: formField.name
1260
- })]
1261
- });
1262
- };
1263
-
1264
- const InputTextField = ({
1265
- formField,
1266
- textFieldProps,
1267
- unregisterOptions,
1268
- isUnregisteringOnUnmount
1269
- }) => {
1270
- const form = useHookFormContext();
1271
- const {
1272
- control
1273
- } = form;
1274
- const muiFormControl = useFormControl();
1275
- const {
1276
- label,
1277
- name,
1278
- required,
1279
- requiredErrorMessage,
1280
- validate
1281
- } = useMergedFormField(formField, formField.t);
1282
- useEffectFieldUnregisterOnUnmount({
1283
- name,
1284
- unregisterOptions,
1285
- isUnregisteringOnUnmount
1286
- });
1287
- if (!form) {
1288
- throw Error(formContextNull);
1289
- }
1290
- const hasError = useFormFieldHasError(formField.name);
1291
- const isRequired = required || muiFormControl?.required;
1292
- const isDisabled = muiFormControl?.disabled || textFieldProps?.disabled;
1293
- return /*#__PURE__*/jsxs(FormControl, {
1294
- error: hasError,
1295
- required: isRequired,
1296
- disabled: isDisabled,
1297
- children: [/*#__PURE__*/jsx(Controller, {
1298
- name: name,
1299
- control: control,
1300
- rules: {
1301
- required: isRequired ? requiredErrorMessage : false,
1302
- validate
1303
- },
1304
- render: ({
1305
- field: {
1306
- onChange,
1307
- value
1308
- },
1309
- fieldState: {
1310
- error
1311
- }
1312
- }) => {
1313
- return /*#__PURE__*/jsx(TextField, {
1314
- error: !!error,
1315
- onChange: onChange,
1316
- value: value,
1317
- label: label,
1318
- ...textFieldProps,
1319
- required: isRequired,
1320
- disabled: isDisabled
1321
- });
1322
- }
1323
- }), /*#__PURE__*/jsx(FormFieldError, {
1324
- fieldName: formField.name
1325
- })]
1326
- });
1327
- };
1328
-
1329
- const InputTimePicker = ({
1330
- formField,
1331
- timePickerProps,
1332
- unregisterOptions,
1333
- isUnregisteringOnUnmount
1334
- }) => {
1335
- const {
1336
- control
1337
- } = useHookFormContext();
1338
- const hasError = useFormFieldHasError(formField.name);
1339
- const muiFormControl = useFormControl();
1340
- const {
1341
- label,
1342
- name,
1343
- required,
1344
- requiredErrorMessage
1345
- } = useMergedFormField(formField);
1346
- useEffectFieldUnregisterOnUnmount({
1347
- name,
1348
- unregisterOptions,
1349
- isUnregisteringOnUnmount
1350
- });
1351
- const [date, setDate] = useState(dayjs());
1352
- const isRequired = required || muiFormControl?.required;
1353
- const isDisabled = muiFormControl?.disabled || timePickerProps?.disabled;
1354
- return /*#__PURE__*/jsx(Box, {
1355
- children: /*#__PURE__*/jsxs(FormControl, {
1356
- error: hasError,
1357
- required: isRequired,
1358
- disabled: isDisabled,
1359
- children: [/*#__PURE__*/jsx(Controller, {
1360
- name: name,
1361
- defaultValue: date,
1362
- control: control,
1363
- rules: {
1364
- required: isRequired ? requiredErrorMessage : false
1365
- },
1366
- render: ({
1367
- field: {
1368
- onChange,
1369
- ...restField
1370
- }
1371
- }) => /*#__PURE__*/jsx(TimePicker, {
1372
- label: label,
1373
- onChange: event => {
1374
- onChange(event);
1375
- if (event) {
1376
- setDate(event);
1377
- }
1378
- },
1379
- disabled: isDisabled,
1380
- required: isRequired,
1381
- slotProps: {
1382
- textField: {
1383
- required: isRequired,
1384
- disabled: isDisabled,
1385
- error: hasError
1386
- }
1387
- },
1388
- ...restField,
1389
- ...timePickerProps
1390
- })
1391
- }), /*#__PURE__*/jsx(FormFieldError, {
1392
- fieldName: formField.name
1393
- })]
1394
- })
1395
- });
1396
- };
1397
-
1398
- const InputToggleButton = ({
1399
- formField,
1400
- toggleButtonProps,
1401
- formControlLabelProps,
1402
- children,
1403
- unregisterOptions,
1404
- isUnregisteringOnUnmount
1405
- }) => {
1406
- const {
1407
- register,
1408
- control,
1409
- setValue
1410
- } = useHookFormContext();
1411
- const hasError = useFormFieldHasError(formField.name);
1412
- const muiFormControl = useFormControl();
1413
- const {
1414
- label,
1415
- name,
1416
- required,
1417
- requiredErrorMessage,
1418
- validate
1419
- } = useMergedFormField(formField);
1420
- useEffectFieldUnregisterOnUnmount({
1421
- name,
1422
- unregisterOptions,
1423
- isUnregisteringOnUnmount
1424
- });
1425
- const id = `hook-form-input-toggle-button-${name}`;
1426
- const valueToUse = !!useWatch({
1427
- name: name,
1428
- control: control,
1429
- exact: true
1430
- });
1431
- assertTruthy(typeof valueToUse === "boolean", `Value (Field: ${name}, value: ${valueToUse?.toString() || "undefined"}) for a InputToggleButton should be undefined or boolean`);
1432
- const onClick = useCallback(
1433
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1434
- e => {
1435
- setValue(name, !valueToUse, {
1436
- shouldValidate: true,
1437
- shouldDirty: true,
1438
- shouldTouch: true
1439
- });
1440
- }, [name, setValue, valueToUse]);
1441
- const isRequired = required || muiFormControl?.required;
1442
- const isDisabled = muiFormControl?.disabled || toggleButtonProps?.disabled;
1443
- const registered = register(name, {
1444
- required: isRequired ? requiredErrorMessage : false,
1445
- validate,
1446
- disabled: muiFormControl?.disabled
1447
- });
1448
- return /*#__PURE__*/jsxs(FormControl, {
1449
- error: hasError,
1450
- disabled: isDisabled,
1451
- required: isRequired,
1452
- children: [/*#__PURE__*/jsx(FormControlLabel, {
1453
- required: isRequired,
1454
- label: label,
1455
- labelPlacement: "top",
1456
- disabled: isDisabled,
1457
- control: /*#__PURE__*/jsx(ToggleButton, {
1458
- value: valueToUse,
1459
- selected: valueToUse,
1460
- "aria-labelledby": id,
1461
- onClick: onClick,
1462
- ...registered,
1463
- ...toggleButtonProps,
1464
- children: children
1465
- }),
1466
- sx: {
1467
- alignItems: "flex-start",
1468
- ml: 0,
1469
- color: t => hasError ? t.palette.error.main : undefined,
1470
- ...formControlLabelProps?.sx
1471
- },
1472
- ...formControlLabelProps
1473
- }), /*#__PURE__*/jsx(FormFieldError, {
1474
- fieldName: formField.name
1475
- })]
1476
- });
1477
- };
1478
-
1479
- const InputToggleButtonGroup = ({
1480
- formField,
1481
- toggleButtonGroupProps,
1482
- stackProps,
1483
- typographyProps,
1484
- buttons,
1485
- enforceValueSelected = false,
1486
- unregisterOptions,
1487
- isUnregisteringOnUnmount
1488
- }) => {
1489
- const {
1490
- register,
1491
- setValue,
1492
- control
1493
- } = useHookFormContext();
1494
- const hasError = useFormFieldHasError(formField.name);
1495
- const muiFormControl = useFormControl();
1496
- const {
1497
- label: labelText,
1498
- name,
1499
- required,
1500
- requiredErrorMessage,
1501
- validate
1502
- } = useMergedFormField(formField);
1503
- useEffectFieldUnregisterOnUnmount({
1504
- name,
1505
- unregisterOptions,
1506
- isUnregisteringOnUnmount
1507
- });
1508
- const changeEv = useCallback((_, value) => {
1509
- const onSetValue = () => setValue(name, value, {
1510
- shouldValidate: true,
1511
- shouldDirty: true,
1512
- shouldTouch: true
1513
- });
1514
- if (enforceValueSelected) {
1515
- if (toggleButtonGroupProps?.exclusive === true && value !== null) {
1516
- onSetValue();
1517
- } else if (toggleButtonGroupProps?.exclusive === false || toggleButtonGroupProps?.exclusive === undefined && value?.length) {
1518
- onSetValue();
1519
- }
1520
- } else {
1521
- onSetValue();
1522
- }
1523
- }, [enforceValueSelected, name, setValue, toggleButtonGroupProps?.exclusive]);
1524
- const id = `hook-form-input-toggle-button-group-${name}`;
1525
- const registered = register(name, {
1526
- required: required ? requiredErrorMessage : false,
1527
- validate,
1528
- disabled: muiFormControl?.disabled
1529
- });
1530
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
1531
- const valueFromForm = useWatch({
1532
- name: name,
1533
- control: control,
1534
- exact: true
1535
- });
1536
- const isRequired = required || muiFormControl?.required;
1537
- const isDisabled = muiFormControl?.disabled || toggleButtonGroupProps?.disabled;
1538
- const toggleButtonGroup = /*#__PURE__*/jsx(ToggleButtonGroup, {
1539
- ...registered,
1540
- ...toggleButtonGroupProps,
1541
- onChange: changeEv
1542
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
1543
- ,
1544
- value: valueFromForm,
1545
- children: buttons.map((button, i) => /*#__PURE__*/jsx(ToggleButton
1546
- // eslint-disable-next-line react/no-array-index-key
1547
- , {
1548
- value: button.value,
1549
- "aria-labelledby": id,
1550
- ...button.toggleButtonProps,
1551
- disabled: isDisabled,
1552
- children: button.icon
1553
- }, `${button.value.toString()}-${i}`))
1554
- });
1555
- const label = /*#__PURE__*/jsx(Typography, {
1556
- component: "span",
1557
- ...typographyProps,
1558
- sx: {
1559
- color: t => {
1560
- if (hasError) {
1561
- return t.palette.error.main;
1562
- }
1563
- return isDisabled ? t.palette.text.disabled : undefined;
1564
- }
1565
- },
1566
- children: labelText
1567
- });
1568
- return /*#__PURE__*/jsxs(FormControl, {
1569
- error: hasError,
1570
- disabled: isDisabled,
1571
- required: isRequired,
1572
- children: [formField.label ? /*#__PURE__*/jsxs(Stack, {
1573
- ...stackProps,
1574
- children: [isRequired ? /*#__PURE__*/jsxs(Stack, {
1575
- id: id,
1576
- component: "label",
1577
- sx: {
1578
- display: "block"
1579
- },
1580
- children: [label, /*#__PURE__*/jsxs(Box, {
1581
- component: "span",
1582
- "aria-hidden": true,
1583
- sx: {
1584
- color: hasError ? t => t.palette.error.main : undefined
1585
- },
1586
- children: ["\u2009", "*"]
1587
- })]
1588
- }) : label, toggleButtonGroup]
1589
- }) : toggleButtonGroup, /*#__PURE__*/jsx(FormFieldError, {
1590
- fieldName: formField.name
1591
- })]
1592
- });
1593
- };
1594
-
1595
- /**
1596
- * Turn an async submitHandler into a synchronous one.
1597
- *
1598
- * Reasoning:
1599
- * - we're often talking to the API during form submission. The onSubmit handler will be returning a Promise in this case.
1600
- * - the `form` element, per definition, always expects a synchronous function.
1601
- * - creating on inline function in every form is a bit clumsy, and does not look clean.
1602
- *
1603
- * Then/catch cases should be properly handled by the submit handler itself,
1604
- * the catch supplied here is only a last safety net against an "unhandled promise rejection" error.
1605
- */
1606
- const syncSubmit = asyncSubmitHandler => {
1607
- return e => {
1608
- asyncSubmitHandler(e).catch(rethrow);
1609
- };
1610
- };
1611
-
1612
- /**
1613
- * Wrapper around react-hook-form's useForm.
1614
- *
1615
- * Should behave like the original function, only exposing a "fieldName"
1616
- * function on top which is used to validate field names.
1617
- */
1618
- const useHookForm = props => {
1619
- const form = useForm(props);
1620
- return {
1621
- ...form,
1622
- fieldName: identity
1623
- };
1624
- };
1625
- const useHookFormFieldArray = useFieldArray;
1626
-
1627
- /**
1628
- * Reexport of FormProvider.
1629
- *
1630
- * This became necessary as context is not available when the FormProvider is initialized outside of the package. Most likely this is some kind of instance mismatch.
1631
- */
1632
- const HookFormProvider = FormProvider;
1633
-
1634
- const InputEmail = props => {
1635
- const merged = useMergedFormField(props.formField, props.formField.t, formDefaults);
1636
- const commonProps = useMemo(() => ({
1637
- textFieldProps: {
1638
- type: "email",
1639
- autoComplete: "email",
1640
- ...props.textFieldProps
1641
- },
1642
- formField: merged
1643
- }), [merged, props.textFieldProps]);
1644
- return props.isUnregisteringOnUnmount ? /*#__PURE__*/jsx(InputTextField, {
1645
- ...commonProps,
1646
- isUnregisteringOnUnmount: props.isUnregisteringOnUnmount,
1647
- unregisterOptions: props.unregisterOptions
1648
- }) : /*#__PURE__*/jsx(InputTextField, {
1649
- ...commonProps,
1650
- isUnregisteringOnUnmount: props.isUnregisteringOnUnmount
1651
- });
1652
- };
1653
- const formDefaults = {
1654
- required: true,
1655
- validate: emailInput => {
1656
- if (emailInput && emailInput.match(/.@./)) {
1657
- return true;
1658
- }
1659
- return "E-Mail address invalid";
1660
- }
1661
- };
1662
-
1663
- const InputPasswordSet = ({
1664
- passwordField,
1665
- passwordValidationField,
1666
- doNotMatchError,
1667
- passwordValidationTextFieldProps,
1668
- passwordTextFieldProps
1669
- }) => {
1670
- const {
1671
- watch
1672
- } = useHookFormContext();
1673
- const passwordFieldMerged = useMergedFormField(passwordField, passwordField.t, {
1674
- required: true
1675
- });
1676
- const validationFieldMerged = useMergedFormField(passwordValidationField, passwordValidationField.t, {
1677
- required: true
1678
- });
1679
- const validatePasswordMatch = useCallback(value => {
1680
- const passwordValue = watch(passwordFieldMerged.name);
1681
- return value === passwordValue || doNotMatchError;
1682
- }, [doNotMatchError, passwordFieldMerged.name, watch]);
1683
- return /*#__PURE__*/jsxs(Fragment, {
1684
- children: [/*#__PURE__*/jsx(InputTextField, {
1685
- formField: passwordFieldMerged,
1686
- textFieldProps: {
1687
- type: "password",
1688
- autoComplete: "new-password",
1689
- ...passwordTextFieldProps
1690
- }
1691
- }), /*#__PURE__*/jsx(InputTextField, {
1692
- formField: {
1693
- ...validationFieldMerged,
1694
- validate: validatePasswordMatch,
1695
- t: passwordValidationField.t
1696
- },
1697
- textFieldProps: {
1698
- type: "password",
1699
- autoComplete: "new-password",
1700
- ...passwordValidationTextFieldProps
1701
- }
1702
- })]
1703
- });
1704
- };
1705
-
1706
- var LoadingButtonState;
1707
- (function (LoadingButtonState) {
1708
- LoadingButtonState["Idle"] = "idle";
1709
- LoadingButtonState["Loading"] = "loading";
1710
- LoadingButtonState["Success"] = "success";
1711
- })(LoadingButtonState || (LoadingButtonState = {}));
1712
-
1713
- const LoadingButton = ({
1714
- buttonText,
1715
- buttonTextSuccess,
1716
- loadingButtonProps,
1717
- loadingButtonPropsPropsSuccess,
1718
- buttonState,
1719
- successIcon
1720
- }) => {
1721
- const isSuccess = buttonState === LoadingButtonState.Success;
1722
- const propsToSpread = isSuccess ? loadingButtonPropsPropsSuccess : loadingButtonProps;
1723
- return /*#__PURE__*/jsx(LoadingButton$1, {
1724
- loading: buttonState === LoadingButtonState.Loading,
1725
- disabled: buttonState !== LoadingButtonState.Idle,
1726
- ...propsToSpread,
1727
- children: buttonState === LoadingButtonState.Success ? /*#__PURE__*/jsxs(Fragment, {
1728
- children: [successIcon ? successIcon : null, buttonTextSuccess ? buttonTextSuccess : buttonText]
1729
- }) : buttonText
1730
- });
1731
- };
1732
-
1733
- /**
1734
- * Get loading button state from react-query mutation status.
1735
- */
1736
- const loadingButtonStateFromReactQueryMutationStatus = mutationStatus => {
1737
- const mapStatus = {
1738
- idle: LoadingButtonState.Idle,
1739
- error: LoadingButtonState.Idle,
1740
- success: LoadingButtonState.Success,
1741
- loading: LoadingButtonState.Loading
1742
- };
1743
- return mapStatus[mutationStatus];
1744
- };
1745
- /**
1746
- * Get loading button state from a react-hook-form context.
1747
- */
1748
- const useLoadingButtonStateFromFormContext = () => {
1749
- const formContext = useHookFormContext();
1750
- return loadingButtonStateFromFormState(formContext.formState);
1751
- };
1752
- /**
1753
- * Get loading button state from a react-hook-form state.
1754
- */
1755
- const loadingButtonStateFromFormState = (
1756
- // we are not interested in the value types FormState holds
1757
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1758
- formState) => {
1759
- if (formState.isSubmitting) {
1760
- return LoadingButtonState.Loading;
1761
- }
1762
- if (formState.isDirty) {
1763
- return LoadingButtonState.Idle;
1764
- }
1765
- if (formState.isSubmitSuccessful) {
1766
- return LoadingButtonState.Success;
1767
- }
1768
- return LoadingButtonState.Idle;
1769
- };
1770
-
1771
- export { FormFieldError, FormFieldErrorDisplay, HookFormProvider, InputAutocomplete, InputCheckbox, InputDatePicker, InputDateTimePicker, InputEmail, InputNumber, InputOutlinedInput, InputPasswordSet, InputRadio, InputSelect, InputSelectFromEnum, InputSlider, InputSwitch, InputOutlinedInput as InputText, InputTextField, InputTimePicker, InputToggleButton, InputToggleButtonGroup, LoadingButton, LoadingButtonState, getFormErrorForNestedKeys, loadingButtonStateFromFormState, loadingButtonStateFromReactQueryMutationStatus, syncSubmit, useFormFieldError, useFormFieldHasError, useHookForm, useHookFormContext, useHookFormFieldArray, useLoadingButtonStateFromFormContext, useMergedFormField, useSortedOptionsWithGroup, useTranslateString };
1772
- //# sourceMappingURL=packageExports.js.map