@nwon/react-mui-hook-form 1.0.2 → 1.0.3

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 (32) 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 -11
  8. package/dist/formFields/InputPasswordSet/InputPasswordSet.d.ts +0 -8
  9. package/dist/formFields/InputPasswordSet/InputPasswordSetInterfaces.d.ts +0 -15
  10. package/dist/formFields/TranslatedString.d.ts +0 -13
  11. package/dist/formFields/baseTypes/InputAutocomplete/InputAutocomplete.d.ts +0 -8
  12. package/dist/formFields/baseTypes/InputAutocomplete/InputAutocompleteInterfaces.d.ts +0 -22
  13. package/dist/formFields/baseTypes/InputCheckbox/InputCheckbox.d.ts +0 -19
  14. package/dist/formFields/baseTypes/InputDateTimePicker/InputDateTimePicker.d.ts +0 -12
  15. package/dist/formFields/baseTypes/InputRadio/InputRadio.d.ts +0 -10
  16. package/dist/formFields/baseTypes/InputSelect/InputSelect.d.ts +0 -23
  17. package/dist/formFields/baseTypes/InputSelectFromEnum/InputSelectFromEnum.d.ts +0 -6
  18. package/dist/formFields/baseTypes/InputSelectFromEnum/InputSelectFromEnumInterfaces.d.ts +0 -10
  19. package/dist/formFields/baseTypes/InputText/InputText.d.ts +0 -14
  20. package/dist/formFields/errorMessages.d.ts +0 -1
  21. package/dist/formFields/formHelper/getFormError.d.ts +0 -11
  22. package/dist/formFields/formHelper/syncSubmit.d.ts +0 -15
  23. package/dist/formFields/formHelper/useHookForm.d.ts +0 -50
  24. package/dist/formFields/formHelper/useHookFormContext.d.ts +0 -9
  25. package/dist/formFields/formHelper/useMergedFormField.d.ts +0 -6
  26. package/dist/formFields/formHelper/useSortedOptionsWithGroup.d.ts +0 -5
  27. package/dist/formFields/formHelper/useTranslateString.d.ts +0 -8
  28. package/dist/formFields/index.d.ts +0 -28
  29. package/dist/formFields/inputTypes.d.ts +0 -76
  30. package/dist/packageExports.d.ts +0 -7
  31. package/dist/packageExports.js +0 -731
  32. package/dist/packageExports.js.map +0 -1
@@ -1,731 +0,0 @@
1
- function ___$insertStyle(css) {
2
- if (!css || typeof window === 'undefined') {
3
- return;
4
- }
5
- const style = document.createElement('style');
6
- style.setAttribute('type', 'text/css');
7
- style.innerHTML = css;
8
- document.head.appendChild(style);
9
- return css;
10
- }
11
-
12
- import { LoadingButton as LoadingButton$1 } from '@mui/lab';
13
- import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
14
- import { useFormContext, FormProvider, useFormState, Controller, useForm } from 'react-hook-form';
15
- import { useFormControl, FormControl, InputLabel, OutlinedInput, FormHelperText, Autocomplete, TextField, FormControlLabel, Checkbox, Box, FormLabel, RadioGroup, Radio, Select, MenuItem, Stack, Tooltip, ListSubheader } from '@mui/material';
16
- import { uppercaseFirstLetter, assertTruthy, objectHasKey, rethrow } from '@nvon/baseline';
17
- import { useCallback, useMemo, useState, createElement } from 'react';
18
- import { t } from 'i18next';
19
- import { DateTimePicker } from '@mui/x-date-pickers';
20
- import { identity } from 'lodash-es';
21
-
22
- var LoadingButtonState;
23
- (function (LoadingButtonState) {
24
- LoadingButtonState["Idle"] = "idle";
25
- LoadingButtonState["Loading"] = "loading";
26
- LoadingButtonState["Success"] = "success";
27
- })(LoadingButtonState || (LoadingButtonState = {}));
28
-
29
- const LoadingButton = ({
30
- buttonText,
31
- buttonTextSuccess,
32
- loadingButtonProps,
33
- loadingButtonPropsPropsSuccess,
34
- buttonState,
35
- successIcon
36
- }) => {
37
- const isSuccess = buttonState === LoadingButtonState.Success;
38
- const propsToSpread = isSuccess ? loadingButtonPropsPropsSuccess : loadingButtonProps;
39
- return /*#__PURE__*/jsx(LoadingButton$1, {
40
- loading: buttonState === LoadingButtonState.Loading,
41
- disabled: buttonState !== LoadingButtonState.Idle,
42
- ...propsToSpread,
43
- children: buttonState === LoadingButtonState.Success ? /*#__PURE__*/jsxs(Fragment, {
44
- children: [successIcon ? successIcon : null, buttonTextSuccess ? buttonTextSuccess : buttonText]
45
- }) : buttonText
46
- });
47
- };
48
-
49
- /**
50
- * Get loading button state from react-query mutation status.
51
- */
52
- const loadingButtonStateFromReactQueryMutationStatus = mutationStatus => {
53
- const mapStatus = {
54
- idle: LoadingButtonState.Idle,
55
- error: LoadingButtonState.Idle,
56
- success: LoadingButtonState.Success,
57
- loading: LoadingButtonState.Loading
58
- };
59
- return mapStatus[mutationStatus];
60
- };
61
- /**
62
- * Get loading button state from a react-hook-form context.
63
- */
64
- const useLoadingButtonStateFromFormContext = () => {
65
- const formContext = useFormContext();
66
- return loadingButtonStateFromFormState(formContext.formState);
67
- };
68
- /**
69
- * Get loading button state from a react-hook-form state.
70
- */
71
- const loadingButtonStateFromFormState = (
72
- // we are not interested in the value types FormState holds
73
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
- formState) => {
75
- if (formState.isSubmitting) {
76
- return LoadingButtonState.Loading;
77
- }
78
- if (formState.isDirty) {
79
- return LoadingButtonState.Idle;
80
- }
81
- if (formState.isSubmitSuccessful) {
82
- return LoadingButtonState.Success;
83
- }
84
- return LoadingButtonState.Idle;
85
- };
86
-
87
- /**
88
- * Reexport of FormProvider.
89
- *
90
- * 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.
91
- */
92
- const HookFormProvider = FormProvider;
93
-
94
- 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";
95
-
96
- /**
97
- * Get the field errors for a (possibly nested) field name.
98
- *
99
- * Checking `formState.errors[fieldName]` only works if fieldName is not a path
100
- * (e.g. Does not include dots).
101
- *
102
- * Did not find a built-in way to do this … there might be one though.
103
- */
104
- const getFormError = (errors, fieldName) => {
105
- const nestedKeys = fieldName.split(".");
106
- //
107
- // To my best knowledge, this works as expected.
108
- //
109
- // It recursively walks the error object by the given keys
110
- // until the lowest level is reached, or undefined returned.
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 nestedKeys.reduce(
118
- // @ts-expect-error
119
- (errorObj, pathKey) => errorObj?.[pathKey], errors);
120
- /* eslint-enable */
121
- };
122
-
123
- /**
124
- * Return a function which turns our TranslatedString into a plain string.
125
- *
126
- * @throws Error if no t was provided but a translated string.
127
- */
128
- const useTranslateString = () => {
129
- if (!t) {
130
- throw Error("If you pass in a translated validate function you need to provide a t");
131
- }
132
- return useCallback(input => {
133
- return typeof input === "string" ? input : input(t);
134
- }, []);
135
- };
136
-
137
- /**
138
- * Merge a form field with defaults.
139
- */
140
- const useMergedFormField = (
141
- /**
142
- * Form fields, typically those passed into a component.
143
- *
144
- */
145
- formField, t,
146
- /**
147
- * Additional defaults. Duplicate values will be overridden by the formField.
148
- */
149
- formFieldDefaults) => {
150
- const translate = useTranslateString();
151
- const merged = useMemo(() => {
152
- const builtInDefaults = {
153
- label: uppercaseFirstLetter(formField.name),
154
- required: false,
155
- requiredErrorMessage: "Please fill in required information",
156
- validate: undefined
157
- };
158
- return {
159
- ...builtInDefaults,
160
- ...formFieldDefaults,
161
- ...formField
162
- };
163
- }, [formField, formFieldDefaults]);
164
- const withPlainStrings = useMemo(() => {
165
- // Translating the validate function is a bit tricky,
166
- // since it can be an object in both cases.
167
- const validate = (() => {
168
- if (typeof merged.validate === "object" && typeof merged.validate.translated === "function") {
169
- if (!t) {
170
- throw Error("If you pass in a translated validate function you need to provide a t");
171
- }
172
- return merged.validate.translated(t);
173
- }
174
- return merged.validate;
175
- })(); // This should be fine. Not sure why TS is complaining.
176
- return {
177
- ...merged,
178
- label: translate(merged.label, t),
179
- requiredErrorMessage: translate(merged.requiredErrorMessage, t),
180
- validate
181
- };
182
- }, [merged, t, translate]);
183
- return withPlainStrings;
184
- };
185
-
186
- const InputText = ({
187
- formField,
188
- textFieldProps
189
- }) => {
190
- const form = useFormContext();
191
- const muiFormControl = useFormControl();
192
- const {
193
- label,
194
- name,
195
- required,
196
- requiredErrorMessage,
197
- validate
198
- } = useMergedFormField(formField, formField.t);
199
- if (!form) {
200
- throw Error(formContextNull);
201
- }
202
- const {
203
- register,
204
- formState
205
- } = form;
206
- const error = getFormError(formState.errors, formField.name);
207
- return /*#__PURE__*/jsxs(FormControl, {
208
- error: !!error,
209
- required: required,
210
- disabled: muiFormControl?.disabled,
211
- children: [/*#__PURE__*/jsx(InputLabel, {
212
- required: required,
213
- children: label
214
- }), /*#__PURE__*/jsx(OutlinedInput, {
215
- ...register(name, {
216
- required: required ? requiredErrorMessage : false,
217
- validate,
218
- disabled: muiFormControl?.disabled
219
- }),
220
- required: required,
221
- label: label,
222
- ...textFieldProps
223
- }), error?.message && /*#__PURE__*/jsx(FormHelperText, {
224
- children: error?.message
225
- })]
226
- });
227
- };
228
-
229
- const InputEmail = props => {
230
- const merged = useMergedFormField(props.formField, props.formField.t, formDefaults);
231
- return /*#__PURE__*/jsx(InputText, {
232
- textFieldProps: {
233
- type: "email",
234
- ...props.textFieldProps
235
- },
236
- formField: merged
237
- });
238
- };
239
- const formDefaults = {
240
- required: true,
241
- validate: emailInput => {
242
- if (emailInput.match(/.@./)) {
243
- return true;
244
- }
245
- return "E-Mail address invalid";
246
- }
247
- };
248
-
249
- const InputPasswordSet = ({
250
- passwordField,
251
- passwordValidationField,
252
- doNotMatchError
253
- }) => {
254
- const {
255
- watch
256
- } = useFormContext();
257
- const passwordFieldMerged = useMergedFormField(passwordField, passwordField.t, {
258
- required: true
259
- });
260
- const validationFieldMerged = useMergedFormField(passwordValidationField, passwordValidationField.t, {
261
- required: true
262
- });
263
- const validatePasswordMatch = useCallback(value => {
264
- const passwordValue = watch(passwordFieldMerged.name);
265
- return value === passwordValue || doNotMatchError;
266
- }, [doNotMatchError, passwordFieldMerged.name, watch]);
267
- return /*#__PURE__*/jsxs(Fragment, {
268
- children: [/*#__PURE__*/jsx(InputText, {
269
- formField: passwordFieldMerged,
270
- textFieldProps: {
271
- type: "password"
272
- }
273
- }), /*#__PURE__*/jsx(InputText, {
274
- formField: {
275
- ...validationFieldMerged,
276
- validate: validatePasswordMatch,
277
- t: passwordValidationField.t
278
- },
279
- textFieldProps: {
280
- type: "password"
281
- }
282
- })]
283
- });
284
- };
285
-
286
- const InputAutocomplete = ({
287
- formField,
288
- selectableOptions,
289
- multiSelect,
290
- noOptions,
291
- autoCompleteProps
292
- }) => {
293
- const form = useFormContext();
294
- if (!form) {
295
- throw Error(formContextNull);
296
- }
297
- const {
298
- register,
299
- watch,
300
- formState,
301
- setValue
302
- } = form;
303
- const error = getFormError(formState.errors, formField.name);
304
- const muiFormControl = useFormControl();
305
- const {
306
- label,
307
- name,
308
- required,
309
- requiredErrorMessage,
310
- validate
311
- } = useMergedFormField(formField, formField.t);
312
- const value = watch(name);
313
- let valueToUse = value;
314
- if (!value) {
315
- valueToUse = multiSelect ? [] : "";
316
- }
317
- assertTruthy(["string", "number", "object"].includes(typeof valueToUse), "Value for select input has unexpected type");
318
- const props = register(name, {
319
- required: required ? requiredErrorMessage : false,
320
- validate,
321
- disabled: muiFormControl?.disabled
322
- });
323
- const currentValue = useMemo(() => multiSelect ? selectableOptions.filter(o => {
324
- if (Array.isArray(valueToUse)) {
325
- return valueToUse.includes(o.value);
326
- }
327
- return valueToUse;
328
- }) : valueToUse, [multiSelect, selectableOptions, valueToUse]);
329
- return /*#__PURE__*/jsxs(FormControl, {
330
- error: !!error,
331
- required: required,
332
- disabled: muiFormControl?.disabled,
333
- children: [/*#__PURE__*/jsx(Autocomplete, {
334
- options: selectableOptions,
335
- multiple: multiSelect,
336
- value: currentValue,
337
- label: label,
338
- ...props,
339
- getOptionLabel: o => {
340
- if (objectHasKey(o, "label")) {
341
- return o.label;
342
- }
343
- return "";
344
- },
345
- onChange: (e, selectedOption) => {
346
- if (Array.isArray(selectedOption)) {
347
- setValue(name, selectedOption.map(o => o.value));
348
- } else {
349
- setValue(name, selectedOption.value);
350
- }
351
- },
352
- noOptionsText: noOptions,
353
- renderInput: params => /*#__PURE__*/jsx(TextField, {
354
- ...params,
355
- label: label,
356
- variant: "outlined"
357
- })
358
- // need to parse this as any in order to get no typing errors
359
- // for multiselect
360
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
361
- ,
362
- ...autoCompleteProps
363
- }), error?.message && /*#__PURE__*/jsx(FormHelperText, {
364
- children: error?.message
365
- })]
366
- });
367
- };
368
-
369
- const InputCheckbox = ({
370
- formField,
371
- jsxLabel,
372
- checkboxProps,
373
- formControlLabelProps
374
- }) => {
375
- const {
376
- register,
377
- watch,
378
- setValue
379
- } = useFormContext();
380
- const {
381
- errors
382
- } = useFormState();
383
- const error = getFormError(errors, formField.name);
384
- const muiFormControl = useFormControl();
385
- const {
386
- label,
387
- name,
388
- required,
389
- requiredErrorMessage,
390
- validate
391
- } = useMergedFormField(formField);
392
- const changeEv = useCallback(e => {
393
- setValue(name, e.target.checked, {
394
- shouldValidate: true,
395
- shouldDirty: true,
396
- shouldTouch: true
397
- });
398
- }, [name, setValue]);
399
- const id = `rvo-input-checkbox-${name}`;
400
- const registered = register(name, {
401
- required: required ? requiredErrorMessage : false,
402
- onChange: changeEv,
403
- validate,
404
- disabled: muiFormControl?.disabled
405
- });
406
- const value = watch(name, false) ?? false;
407
- return /*#__PURE__*/jsxs(FormControl, {
408
- error: !!error,
409
- disabled: muiFormControl?.disabled,
410
- children: [/*#__PURE__*/jsx(FormControlLabel, {
411
- required: jsxLabel ? false : required // If jsxLabel is set, we don't want to show the asterisk.
412
- ,
413
- control: /*#__PURE__*/jsx(Checkbox, {
414
- checked: value,
415
- "aria-labelledby": id,
416
- ...registered,
417
- ...checkboxProps
418
- }),
419
- label: jsxLabel ?? label,
420
- ...formControlLabelProps
421
- }), error?.message && /*#__PURE__*/jsx(FormHelperText, {
422
- children: error?.message
423
- })]
424
- });
425
- };
426
-
427
- const InputDateTimePicker = ({
428
- formField,
429
- dateTimePickerProps
430
- }) => {
431
- const {
432
- formState,
433
- control
434
- } = useFormContext();
435
- const error = getFormError(formState.errors, formField.name);
436
- const muiFormControl = useFormControl();
437
- const {
438
- label,
439
- name,
440
- required
441
- } = useMergedFormField(formField);
442
- const [date, setDate] = useState(new Date());
443
- return /*#__PURE__*/jsx(Box, {
444
- children: /*#__PURE__*/jsxs(FormControl, {
445
- error: !!error,
446
- required: required,
447
- disabled: muiFormControl?.disabled,
448
- children: [/*#__PURE__*/jsx(Controller, {
449
- name: name,
450
- defaultValue: date,
451
- control: control,
452
- render: ({
453
- field: {
454
- onChange,
455
- ...restField
456
- }
457
- }) => /*#__PURE__*/jsx(DateTimePicker, {
458
- label: label,
459
- ampm: dateTimePickerProps?.ampm ? dateTimePickerProps.ampm : false,
460
- onChange: event => {
461
- onChange(event);
462
- if (event) {
463
- setDate(event);
464
- }
465
- },
466
- disabled: muiFormControl?.disabled,
467
- renderInput: params => /*#__PURE__*/jsx(TextField, {
468
- required: required,
469
- ...params
470
- }),
471
- ...restField
472
- })
473
- }), error?.message && /*#__PURE__*/jsx(FormHelperText, {
474
- children: error?.message
475
- })]
476
- })
477
- });
478
- };
479
-
480
- const InputRadio = ({
481
- formField,
482
- selectableOptions
483
- }) => {
484
- const {
485
- register,
486
- watch,
487
- setValue
488
- } = useFormContext();
489
- const {
490
- errors
491
- } = useFormState();
492
- const error = getFormError(errors, formField.name);
493
- const muiFormControl = useFormControl();
494
- const {
495
- label,
496
- name,
497
- required,
498
- requiredErrorMessage,
499
- validate
500
- } = useMergedFormField(formField);
501
- const changeEv = useCallback(e => {
502
- setValue(name, e.target.value, {
503
- shouldValidate: true,
504
- shouldDirty: true,
505
- shouldTouch: true
506
- });
507
- }, [name, setValue]);
508
- const value = watch(name) ?? "";
509
- assertTruthy(["string", "number"].includes(typeof value), "Value for select input has unexpected type");
510
- const id = `rvo-input-radio-${name}`;
511
- const registered = register(name, {
512
- required: required ? requiredErrorMessage : false,
513
- onChange: changeEv,
514
- validate,
515
- disabled: muiFormControl?.disabled
516
- });
517
- return /*#__PURE__*/jsxs(FormControl, {
518
- error: !!error,
519
- disabled: muiFormControl?.disabled,
520
- children: [/*#__PURE__*/jsx(FormLabel, {
521
- id: id,
522
- required: required,
523
- children: label
524
- }), /*#__PURE__*/jsx(RadioGroup, {
525
- value: value,
526
- "aria-labelledby": id,
527
- children: selectableOptions?.map(option => {
528
- return /*#__PURE__*/createElement(FormControlLabel, {
529
- ...registered,
530
- key: option.value,
531
- value: option.value,
532
- control: /*#__PURE__*/jsx(Radio, {}),
533
- label: option.label
534
- });
535
- })
536
- }), error?.message && /*#__PURE__*/jsx(FormHelperText, {
537
- children: error?.message
538
- })]
539
- });
540
- };
541
-
542
- /**
543
- * Sorts the options by group and value.
544
- */
545
- const useSortedOptionsWithGroup = selectableOptions => {
546
- return useMemo(() => selectableOptions.sort((a, b) => {
547
- if (a.group == undefined && !!b.group) {
548
- return -1;
549
- }
550
- if (a.group && b.group && a.group < b.group) {
551
- return -1;
552
- }
553
- if (a.group == b.group && b.value > a.value) {
554
- return 1;
555
- }
556
- return 0;
557
- }), [selectableOptions]);
558
- };
559
-
560
- const InputSelect = ({
561
- formField,
562
- selectableOptions,
563
- placeholderText,
564
- selectComponentProps,
565
- tooltipHolder
566
- }) => {
567
- const form = useFormContext();
568
- if (!form) {
569
- throw Error(formContextNull);
570
- }
571
- const {
572
- register,
573
- watch,
574
- formState
575
- } = form;
576
- const error = getFormError(formState.errors, formField.name);
577
- const muiFormControl = useFormControl();
578
- const {
579
- label,
580
- name,
581
- required,
582
- requiredErrorMessage,
583
- validate
584
- } = useMergedFormField(formField, formField.t);
585
- const value = watch(name) ?? "";
586
- assertTruthy(["string", "number"].includes(typeof value), "Value for select input has unexpected type");
587
- const id = `input-select-${name}`;
588
- const sortedOptions = useSortedOptionsWithGroup(selectableOptions);
589
- return /*#__PURE__*/jsxs(FormControl, {
590
- error: !!error,
591
- required: required,
592
- disabled: muiFormControl?.disabled,
593
- children: [/*#__PURE__*/jsx(InputLabel, {
594
- id: id,
595
- required: required,
596
- children: label
597
- }), /*#__PURE__*/jsxs(Select, {
598
- labelId: id,
599
- ...selectComponentProps,
600
- error: !!formState.errors[name],
601
- label: label,
602
- value: value,
603
- ...register(name, {
604
- required: required ? requiredErrorMessage : false,
605
- validate,
606
- disabled: muiFormControl?.disabled
607
- }),
608
- children: [/*#__PURE__*/jsx(MenuItem, {
609
- value: "",
610
- children: /*#__PURE__*/jsx("em", {
611
- children: placeholderText
612
- })
613
- }), sortedOptions?.map((option, index) => {
614
- const optionBefore = sortedOptions[index - 1];
615
- const showGroup = (optionBefore || index == 0) && option.group && optionBefore?.group !== option.group;
616
- const disabledText = typeof option.disabled === "string" ? option.disabled : undefined;
617
- const isDisabled = !!disabledText || !!option.disabled;
618
- const menuItem = /*#__PURE__*/jsx(MenuItem, {
619
- value: option.value,
620
- disabled: isDisabled,
621
- title: typeof option.disabled === "string" ? option.disabled : undefined,
622
- selected: value === option.value,
623
- children: option.label
624
- }, option.label);
625
- const wrappedMenuItem = disabledText ? /*#__PURE__*/jsxs(Box, {
626
- sx: {
627
- position: "relative"
628
- },
629
- children: [menuItem, /*#__PURE__*/jsx(Stack, {
630
- direction: "column",
631
- justifyContent: "center",
632
- spacing: 1,
633
- sx: {
634
- position: "absolute",
635
- right: th => th.spacing(0.5),
636
- top: 0,
637
- bottom: 0
638
- },
639
- children: tooltipHolder ? /*#__PURE__*/jsx(Tooltip, {
640
- title: disabledText,
641
- children: tooltipHolder
642
- }) : null
643
- })]
644
- }) : menuItem;
645
- return showGroup ? [/*#__PURE__*/jsx(ListSubheader, {
646
- children: option.group
647
- }, option.group), wrappedMenuItem] : wrappedMenuItem;
648
- })]
649
- }), error?.message && /*#__PURE__*/jsx(FormHelperText, {
650
- children: error?.message
651
- })]
652
- });
653
- };
654
-
655
- const InputSelectFromEnum = ({
656
- enumData,
657
- formField,
658
- placeholderText
659
- }) => {
660
- const translateFunction = formField.t;
661
- const options = useMemo(() => {
662
- return Object.entries(enumData.enumToUse).map(([, value]) => {
663
- const label = enumData.labelsForEnum[value];
664
- assertTruthy(label, "Could not find a label for value" + value);
665
- if (typeof label === "string") {
666
- return {
667
- label,
668
- value
669
- };
670
- }
671
- assertTruthy(translateFunction, "If you want to use translated labels you need to provide a translate function in the HookFormProvider.");
672
- return {
673
- label: label(translateFunction),
674
- value
675
- };
676
- });
677
- }, [enumData.enumToUse, enumData.labelsForEnum, translateFunction]);
678
- return /*#__PURE__*/jsx(InputSelect, {
679
- selectableOptions: options,
680
- formField: formField,
681
- placeholderText: placeholderText
682
- });
683
- };
684
-
685
- /**
686
- * Turn an async submitHandler into a synchronous one.
687
- *
688
- * Reasoning:
689
- * - we're often talking to the API during form submission. The onSubmit handler will be returning a Promise in this case.
690
- * - the `form` element, per definition, always expects a synchronous function.
691
- * - creating on inline function in every form is a bit clumsy, and does not look clean.
692
- *
693
- * Then/catch cases should be properly handled by the submit handler itself,
694
- * the catch supplied here is only a last safety net against an "unhandled promise rejection" error.
695
- */
696
- const syncSubmit = asyncSubmitHandler => {
697
- return e => {
698
- asyncSubmitHandler(e).catch(rethrow);
699
- };
700
- };
701
-
702
- /**
703
- * Wrapper around react-hook-form's useForm.
704
- *
705
- * Should behave like the original function, only exposing a "fieldName"
706
- * function on top which is used to validate field names.
707
- */
708
- const useHookForm = props => {
709
- const form = useForm(props);
710
- return {
711
- ...form,
712
- fieldName: identity
713
- };
714
- };
715
-
716
- /**
717
- * Wrapper around react-hook-form's useFormContext.
718
- *
719
- * Should behave like the original function, only exposing a "fieldName"
720
- * function on top which is used to validate field names.
721
- */
722
- const useHookFormContext = () => {
723
- const form = useFormContext();
724
- return {
725
- ...form,
726
- fieldName: identity
727
- };
728
- };
729
-
730
- export { HookFormProvider, InputAutocomplete, InputCheckbox, InputDateTimePicker, InputEmail, InputPasswordSet, InputRadio, InputSelect, InputSelectFromEnum, InputText, LoadingButton, LoadingButtonState, getFormError, loadingButtonStateFromFormState, loadingButtonStateFromReactQueryMutationStatus, syncSubmit, useHookForm, useHookFormContext, useLoadingButtonStateFromFormContext, useMergedFormField, useSortedOptionsWithGroup, useTranslateString };
731
- //# sourceMappingURL=packageExports.js.map