@trackunit/custom-field-components 0.0.741 → 0.0.743

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/index.cjs.js CHANGED
@@ -8,6 +8,7 @@ var irisAppRuntimeCore = require('@trackunit/iris-app-runtime-core');
8
8
  var reactFormComponents = require('@trackunit/react-form-components');
9
9
  var sharedUtils = require('@trackunit/shared-utils');
10
10
  var React = require('react');
11
+ var reactHookForm = require('react-hook-form');
11
12
  var uuid = require('uuid');
12
13
 
13
14
  function _interopNamespace(e) {
@@ -391,7 +392,7 @@ const useUnitTranslation = () => {
391
392
  * and functions for form registration and state management.
392
393
  * @returns {JSX.Element | null} - The JSX Element for the custom field or null if the field definition is not provided.
393
394
  */
394
- const useCustomFieldResolver = ({ field, definition, register, setValue, formState, formValue, unitPreference = "SI", language, fieldId, isEditable, }) => {
395
+ const useCustomFieldResolver = ({ field, definition, register, setValue, formState, formValue, unitPreference = "SI", language, fieldId, isEditable, control, }) => {
395
396
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
396
397
  const validation = { register, setValue, formState };
397
398
  const { unitTranslation } = useUnitTranslation();
@@ -410,38 +411,34 @@ const useCustomFieldResolver = ({ field, definition, register, setValue, formSta
410
411
  });
411
412
  const { getPhoneNumberValidationRules } = reactFormComponents.useGetPhoneValidationRules();
412
413
  const def = (_a = field === null || field === void 0 ? void 0 : field.definition) !== null && _a !== void 0 ? _a : definition;
413
- if (def === undefined || def.__typename === undefined) {
414
- return null;
415
- }
416
414
  const uniqueIdentifier = (_b = fieldId !== null && fieldId !== void 0 ? fieldId : def === null || def === void 0 ? void 0 : def.id) !== null && _b !== void 0 ? _b : "";
415
+ const value = useValue(uniqueIdentifier, numberValue, control, def, field);
417
416
  const key = (_c = def === null || def === void 0 ? void 0 : def.key) !== null && _c !== void 0 ? _c : uniqueIdentifier;
418
417
  const fieldError = uniqueIdentifier ? validation.formState.errors[uniqueIdentifier] : undefined;
419
418
  const errorMessage = fieldError ? fieldError.message + "" : undefined;
420
419
  const { description, title } = def !== null && def !== void 0 ? def : {};
421
420
  const isEditableCombined = (_e = (_d = isEditable !== null && isEditable !== void 0 ? isEditable : field === null || field === void 0 ? void 0 : field.valueEditable) !== null && _d !== void 0 ? _d : def === null || def === void 0 ? void 0 : def.uiEditable) !== null && _e !== void 0 ? _e : false;
421
+ if (def === undefined || def.__typename === undefined) {
422
+ return null;
423
+ }
422
424
  switch (def.__typename) {
423
425
  case "WebAddressFieldDefinition": {
424
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "WebAddressFieldValueAndDefinition" ? field.stringValue : null;
425
426
  const rules = getWebAddressValidationRules(def);
426
427
  return (jsxRuntime.jsx(reactFormComponents.TextField, Object.assign({ actions: !isEditableCombined && formValue && jsxRuntime.jsx(reactFormComponents.ActionButton, { type: "WEB_ADDRESS", value: formValue }), dataTestId: `${key}`, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title, placeholder: title !== null && title !== void 0 ? title : "", readOnly: !isEditableCombined }, validation.register(uniqueIdentifier, Object.assign(Object.assign({}, rules), { value })))));
427
428
  }
428
429
  case "EmailFieldDefinition": {
429
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "EmailFieldValueAndDefinition" ? field.stringValue : null;
430
430
  const rules = getEmailValidationRules(def);
431
431
  return (jsxRuntime.jsx(reactFormComponents.TextField, Object.assign({ actions: !isEditableCombined && formValue && jsxRuntime.jsx(reactFormComponents.ActionButton, { type: "EMAIL", value: formValue }), dataTestId: `${key}`, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title, placeholder: title !== null && title !== void 0 ? title : "", readOnly: !isEditableCombined }, validation.register(uniqueIdentifier, Object.assign(Object.assign({}, rules), { value })))));
432
432
  }
433
433
  case "PhoneNumberFieldDefinition": {
434
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "PhoneNumberFieldValueAndDefinition" ? field.stringValue : null;
435
434
  const rules = getPhoneNumberValidationRules(!Boolean(def.uiEditable));
436
- return (jsxRuntime.jsx(reactFormComponents.PhoneField, Object.assign({ dataTestId: `${key}`, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title, placeholder: title !== null && title !== void 0 ? title : "", readOnly: !isEditableCombined, value: value !== null && value !== void 0 ? value : "" }, validation.register(uniqueIdentifier, Object.assign(Object.assign({}, rules), { value })))));
435
+ return (jsxRuntime.jsx(reactFormComponents.PhoneField, Object.assign({ dataTestId: `${key}`, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title, placeholder: title !== null && title !== void 0 ? title : "", readOnly: !isEditableCombined, value: value }, validation.register(uniqueIdentifier, Object.assign({}, rules)))));
437
436
  }
438
437
  case "StringFieldDefinition": {
439
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "StringFieldValueAndDefinition" ? field.stringValue : null;
440
438
  const rules = getStringValidationRules(def);
441
439
  return (jsxRuntime.jsx(reactFormComponents.TextField, Object.assign({ dataTestId: `${key}`, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title, placeholder: title !== null && title !== void 0 ? title : "", readOnly: !isEditableCombined }, validation.register(uniqueIdentifier, Object.assign(Object.assign({}, rules), { value })))));
442
440
  }
443
441
  case "NumberFieldDefinition": {
444
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "NumberFieldValueAndDefinition" ? field.numberValue : null;
445
442
  const rules = getNumberValidationRules(def);
446
443
  const unit = unitPreference === "US_CUSTOMARY" && def.unitUs ? def.unitUs : def.unitSi;
447
444
  const numberProps = {
@@ -451,23 +448,16 @@ const useCustomFieldResolver = ({ field, definition, register, setValue, formSta
451
448
  return (jsxRuntime.jsx(reactFormComponents.NumberField, Object.assign({ dataTestId: `${key}`, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title, max: (_f = def.maximumNumber) !== null && _f !== void 0 ? _f : undefined, min: (_g = def.minimumNumber) !== null && _g !== void 0 ? _g : undefined, readOnly: !isEditableCombined, step: def.isInteger ? 1 : "any" }, validation.register(uniqueIdentifier, Object.assign(Object.assign({}, rules), { value })), numberProps)));
452
449
  }
453
450
  case "BooleanFieldDefinition": {
454
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "BooleanFieldValueAndDefinition" ? field.booleanValue : null;
455
451
  const rules = getBooleanValidationRules();
456
452
  return (jsxRuntime.jsx(BooleanCustomField, { dataTestId: `${key}`, defaultValue: value !== null && value !== void 0 ? value : undefined, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title !== null && title !== void 0 ? title : "", readOnly: !isEditableCombined, register: validation.register, setValue: validation.setValue, validationRules: rules }));
457
453
  }
458
454
  case "DropDownFieldDefinition": {
459
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "DropDownFieldValueAndDefinition" ? field.stringArrayValue : null;
460
455
  const rules = getDropdownValidationRules();
461
456
  return (jsxRuntime.jsx(DropdownCustomField, { allValues: (_h = def.allValues) !== null && _h !== void 0 ? _h : undefined, dataTestId: `${key}`, defaultValue: value !== null && value !== void 0 ? value : undefined, disabled: !isEditableCombined, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title || "", multiSelect: (_j = def.multiSelect) !== null && _j !== void 0 ? _j : false, register: validation.register, setValue: validation.setValue, validationRules: rules }));
462
457
  }
463
458
  case "DateFieldDefinition": {
464
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "DateFieldValueAndDefinition" ? field.dateValue : null;
465
459
  const rules = getDateValidationRules();
466
- let defaultValue;
467
- if (value !== undefined && value !== null) {
468
- defaultValue = new Date(value);
469
- }
470
- return (jsxRuntime.jsx(DateCustomField, { dataTestId: `${key}`, defaultValue: defaultValue, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title || "", readOnly: !isEditableCombined, register: validation.register, setValue: validation.setValue, title: title || "", validationRules: rules }));
460
+ return (jsxRuntime.jsx(DateCustomField, { dataTestId: `${key}`, defaultValue: value, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title || "", readOnly: !isEditableCombined, register: validation.register, setValue: validation.setValue, title: title || "", validationRules: rules }));
471
461
  }
472
462
  case "JsonFieldDefinition": {
473
463
  return null;
@@ -478,9 +468,6 @@ const useCustomFieldResolver = ({ field, definition, register, setValue, formSta
478
468
  placeholder: title !== null && title !== void 0 ? title : "",
479
469
  addonAfter: def.currency,
480
470
  };
481
- const value = numberValue === undefined || numberValue === null || numberValue === ""
482
- ? ""
483
- : parseFloat(Number(numberValue).toPrecision(4));
484
471
  return (jsxRuntime.jsx(reactFormComponents.NumberField, Object.assign({ dataTestId: key ? key : `monetaryField`, defaultValue: value, disabled: !isEditableCombined, helpText: errorMessage || description, id: uniqueIdentifier, isInvalid: errorMessage ? true : false, label: title, max: (_k = def.maximumNumber) !== null && _k !== void 0 ? _k : undefined, min: (_l = def.minimumNumber) !== null && _l !== void 0 ? _l : undefined, readOnly: !isEditableCombined, step: "any" }, validation.register(uniqueIdentifier, rules), { onChange: e => {
485
472
  setNumberValue(e.target.value === "" ? null : e.target.value);
486
473
  validation.register(uniqueIdentifier, rules).onChange(e);
@@ -490,6 +477,56 @@ const useCustomFieldResolver = ({ field, definition, register, setValue, formSta
490
477
  return sharedUtils.exhaustiveCheck(def);
491
478
  }
492
479
  };
480
+ const useValue = (uniqueIdentifier, numberValue, control, def, field) => {
481
+ // workaround that ensures the value in the phone number field is updated if the formState changes.
482
+ // Would probably be better to use PhoneFieldWithController, but doing that makes the form dirty on initial load.
483
+ const initialValue = (field === null || field === void 0 ? void 0 : field.__typename) === "PhoneNumberFieldValueAndDefinition" ? field.stringValue : null;
484
+ const watchedValue = reactHookForm.useWatch({ name: uniqueIdentifier, defaultValue: initialValue, control });
485
+ if (!def) {
486
+ return null;
487
+ }
488
+ switch (def.__typename) {
489
+ case "WebAddressFieldDefinition": {
490
+ return (field === null || field === void 0 ? void 0 : field.__typename) === "WebAddressFieldValueAndDefinition" ? field.stringValue : null;
491
+ }
492
+ case "EmailFieldDefinition": {
493
+ return (field === null || field === void 0 ? void 0 : field.__typename) === "EmailFieldValueAndDefinition" ? field.stringValue : null;
494
+ }
495
+ case "PhoneNumberFieldDefinition": {
496
+ return watchedValue;
497
+ }
498
+ case "StringFieldDefinition": {
499
+ return (field === null || field === void 0 ? void 0 : field.__typename) === "StringFieldValueAndDefinition" ? field.stringValue : null;
500
+ }
501
+ case "NumberFieldDefinition": {
502
+ return (field === null || field === void 0 ? void 0 : field.__typename) === "NumberFieldValueAndDefinition" ? field.numberValue : null;
503
+ }
504
+ case "BooleanFieldDefinition": {
505
+ return (field === null || field === void 0 ? void 0 : field.__typename) === "BooleanFieldValueAndDefinition" ? field.booleanValue : null;
506
+ }
507
+ case "DropDownFieldDefinition": {
508
+ return (field === null || field === void 0 ? void 0 : field.__typename) === "DropDownFieldValueAndDefinition" ? field.stringArrayValue : null;
509
+ }
510
+ case "DateFieldDefinition": {
511
+ const value = (field === null || field === void 0 ? void 0 : field.__typename) === "DateFieldValueAndDefinition" ? field.dateValue : null;
512
+ let defaultValue;
513
+ if (value !== undefined && value !== null) {
514
+ defaultValue = new Date(value);
515
+ }
516
+ return defaultValue;
517
+ }
518
+ case "JsonFieldDefinition": {
519
+ return null;
520
+ }
521
+ case "MonetaryFieldDefinition": {
522
+ return numberValue === undefined || numberValue === null || numberValue === ""
523
+ ? ""
524
+ : parseFloat(Number(numberValue).toPrecision(4));
525
+ }
526
+ default:
527
+ return sharedUtils.exhaustiveCheck(def);
528
+ }
529
+ };
493
530
  /**
494
531
  * CustomField is a React component that takes in the properties of a custom field and renders it.
495
532
  * It utilizes the `useCustomFieldResolver` hook to resolve the field into an input element
package/index.esm.js CHANGED
@@ -5,6 +5,7 @@ import { FormGroup, Checkbox, DateField, Select, useGetPhoneValidationRules, Num
5
5
  import { exhaustiveCheck } from '@trackunit/shared-utils';
6
6
  import * as React from 'react';
7
7
  import { useCallback, useEffect, createElement, useState } from 'react';
8
+ import { useWatch } from 'react-hook-form';
8
9
  import { v4 } from 'uuid';
9
10
 
10
11
  var defaultTranslations = {
@@ -368,7 +369,7 @@ const useUnitTranslation = () => {
368
369
  * and functions for form registration and state management.
369
370
  * @returns {JSX.Element | null} - The JSX Element for the custom field or null if the field definition is not provided.
370
371
  */
371
- const useCustomFieldResolver = ({ field, definition, register, setValue, formState, formValue, unitPreference = "SI", language, fieldId, isEditable, }) => {
372
+ const useCustomFieldResolver = ({ field, definition, register, setValue, formState, formValue, unitPreference = "SI", language, fieldId, isEditable, control, }) => {
372
373
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
373
374
  const validation = { register, setValue, formState };
374
375
  const { unitTranslation } = useUnitTranslation();
@@ -387,38 +388,34 @@ const useCustomFieldResolver = ({ field, definition, register, setValue, formSta
387
388
  });
388
389
  const { getPhoneNumberValidationRules } = useGetPhoneValidationRules();
389
390
  const def = (_a = field === null || field === void 0 ? void 0 : field.definition) !== null && _a !== void 0 ? _a : definition;
390
- if (def === undefined || def.__typename === undefined) {
391
- return null;
392
- }
393
391
  const uniqueIdentifier = (_b = fieldId !== null && fieldId !== void 0 ? fieldId : def === null || def === void 0 ? void 0 : def.id) !== null && _b !== void 0 ? _b : "";
392
+ const value = useValue(uniqueIdentifier, numberValue, control, def, field);
394
393
  const key = (_c = def === null || def === void 0 ? void 0 : def.key) !== null && _c !== void 0 ? _c : uniqueIdentifier;
395
394
  const fieldError = uniqueIdentifier ? validation.formState.errors[uniqueIdentifier] : undefined;
396
395
  const errorMessage = fieldError ? fieldError.message + "" : undefined;
397
396
  const { description, title } = def !== null && def !== void 0 ? def : {};
398
397
  const isEditableCombined = (_e = (_d = isEditable !== null && isEditable !== void 0 ? isEditable : field === null || field === void 0 ? void 0 : field.valueEditable) !== null && _d !== void 0 ? _d : def === null || def === void 0 ? void 0 : def.uiEditable) !== null && _e !== void 0 ? _e : false;
398
+ if (def === undefined || def.__typename === undefined) {
399
+ return null;
400
+ }
399
401
  switch (def.__typename) {
400
402
  case "WebAddressFieldDefinition": {
401
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "WebAddressFieldValueAndDefinition" ? field.stringValue : null;
402
403
  const rules = getWebAddressValidationRules(def);
403
404
  return (jsx(TextField, Object.assign({ actions: !isEditableCombined && formValue && jsx(ActionButton, { type: "WEB_ADDRESS", value: formValue }), dataTestId: `${key}`, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title, placeholder: title !== null && title !== void 0 ? title : "", readOnly: !isEditableCombined }, validation.register(uniqueIdentifier, Object.assign(Object.assign({}, rules), { value })))));
404
405
  }
405
406
  case "EmailFieldDefinition": {
406
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "EmailFieldValueAndDefinition" ? field.stringValue : null;
407
407
  const rules = getEmailValidationRules(def);
408
408
  return (jsx(TextField, Object.assign({ actions: !isEditableCombined && formValue && jsx(ActionButton, { type: "EMAIL", value: formValue }), dataTestId: `${key}`, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title, placeholder: title !== null && title !== void 0 ? title : "", readOnly: !isEditableCombined }, validation.register(uniqueIdentifier, Object.assign(Object.assign({}, rules), { value })))));
409
409
  }
410
410
  case "PhoneNumberFieldDefinition": {
411
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "PhoneNumberFieldValueAndDefinition" ? field.stringValue : null;
412
411
  const rules = getPhoneNumberValidationRules(!Boolean(def.uiEditable));
413
- return (jsx(PhoneField, Object.assign({ dataTestId: `${key}`, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title, placeholder: title !== null && title !== void 0 ? title : "", readOnly: !isEditableCombined, value: value !== null && value !== void 0 ? value : "" }, validation.register(uniqueIdentifier, Object.assign(Object.assign({}, rules), { value })))));
412
+ return (jsx(PhoneField, Object.assign({ dataTestId: `${key}`, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title, placeholder: title !== null && title !== void 0 ? title : "", readOnly: !isEditableCombined, value: value }, validation.register(uniqueIdentifier, Object.assign({}, rules)))));
414
413
  }
415
414
  case "StringFieldDefinition": {
416
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "StringFieldValueAndDefinition" ? field.stringValue : null;
417
415
  const rules = getStringValidationRules(def);
418
416
  return (jsx(TextField, Object.assign({ dataTestId: `${key}`, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title, placeholder: title !== null && title !== void 0 ? title : "", readOnly: !isEditableCombined }, validation.register(uniqueIdentifier, Object.assign(Object.assign({}, rules), { value })))));
419
417
  }
420
418
  case "NumberFieldDefinition": {
421
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "NumberFieldValueAndDefinition" ? field.numberValue : null;
422
419
  const rules = getNumberValidationRules(def);
423
420
  const unit = unitPreference === "US_CUSTOMARY" && def.unitUs ? def.unitUs : def.unitSi;
424
421
  const numberProps = {
@@ -428,23 +425,16 @@ const useCustomFieldResolver = ({ field, definition, register, setValue, formSta
428
425
  return (jsx(NumberField, Object.assign({ dataTestId: `${key}`, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title, max: (_f = def.maximumNumber) !== null && _f !== void 0 ? _f : undefined, min: (_g = def.minimumNumber) !== null && _g !== void 0 ? _g : undefined, readOnly: !isEditableCombined, step: def.isInteger ? 1 : "any" }, validation.register(uniqueIdentifier, Object.assign(Object.assign({}, rules), { value })), numberProps)));
429
426
  }
430
427
  case "BooleanFieldDefinition": {
431
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "BooleanFieldValueAndDefinition" ? field.booleanValue : null;
432
428
  const rules = getBooleanValidationRules();
433
429
  return (jsx(BooleanCustomField, { dataTestId: `${key}`, defaultValue: value !== null && value !== void 0 ? value : undefined, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title !== null && title !== void 0 ? title : "", readOnly: !isEditableCombined, register: validation.register, setValue: validation.setValue, validationRules: rules }));
434
430
  }
435
431
  case "DropDownFieldDefinition": {
436
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "DropDownFieldValueAndDefinition" ? field.stringArrayValue : null;
437
432
  const rules = getDropdownValidationRules();
438
433
  return (jsx(DropdownCustomField, { allValues: (_h = def.allValues) !== null && _h !== void 0 ? _h : undefined, dataTestId: `${key}`, defaultValue: value !== null && value !== void 0 ? value : undefined, disabled: !isEditableCombined, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title || "", multiSelect: (_j = def.multiSelect) !== null && _j !== void 0 ? _j : false, register: validation.register, setValue: validation.setValue, validationRules: rules }));
439
434
  }
440
435
  case "DateFieldDefinition": {
441
- const value = (field === null || field === void 0 ? void 0 : field.__typename) === "DateFieldValueAndDefinition" ? field.dateValue : null;
442
436
  const rules = getDateValidationRules();
443
- let defaultValue;
444
- if (value !== undefined && value !== null) {
445
- defaultValue = new Date(value);
446
- }
447
- return (jsx(DateCustomField, { dataTestId: `${key}`, defaultValue: defaultValue, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title || "", readOnly: !isEditableCombined, register: validation.register, setValue: validation.setValue, title: title || "", validationRules: rules }));
437
+ return (jsx(DateCustomField, { dataTestId: `${key}`, defaultValue: value, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title || "", readOnly: !isEditableCombined, register: validation.register, setValue: validation.setValue, title: title || "", validationRules: rules }));
448
438
  }
449
439
  case "JsonFieldDefinition": {
450
440
  return null;
@@ -455,9 +445,6 @@ const useCustomFieldResolver = ({ field, definition, register, setValue, formSta
455
445
  placeholder: title !== null && title !== void 0 ? title : "",
456
446
  addonAfter: def.currency,
457
447
  };
458
- const value = numberValue === undefined || numberValue === null || numberValue === ""
459
- ? ""
460
- : parseFloat(Number(numberValue).toPrecision(4));
461
448
  return (jsx(NumberField, Object.assign({ dataTestId: key ? key : `monetaryField`, defaultValue: value, disabled: !isEditableCombined, helpText: errorMessage || description, id: uniqueIdentifier, isInvalid: errorMessage ? true : false, label: title, max: (_k = def.maximumNumber) !== null && _k !== void 0 ? _k : undefined, min: (_l = def.minimumNumber) !== null && _l !== void 0 ? _l : undefined, readOnly: !isEditableCombined, step: "any" }, validation.register(uniqueIdentifier, rules), { onChange: e => {
462
449
  setNumberValue(e.target.value === "" ? null : e.target.value);
463
450
  validation.register(uniqueIdentifier, rules).onChange(e);
@@ -467,6 +454,56 @@ const useCustomFieldResolver = ({ field, definition, register, setValue, formSta
467
454
  return exhaustiveCheck(def);
468
455
  }
469
456
  };
457
+ const useValue = (uniqueIdentifier, numberValue, control, def, field) => {
458
+ // workaround that ensures the value in the phone number field is updated if the formState changes.
459
+ // Would probably be better to use PhoneFieldWithController, but doing that makes the form dirty on initial load.
460
+ const initialValue = (field === null || field === void 0 ? void 0 : field.__typename) === "PhoneNumberFieldValueAndDefinition" ? field.stringValue : null;
461
+ const watchedValue = useWatch({ name: uniqueIdentifier, defaultValue: initialValue, control });
462
+ if (!def) {
463
+ return null;
464
+ }
465
+ switch (def.__typename) {
466
+ case "WebAddressFieldDefinition": {
467
+ return (field === null || field === void 0 ? void 0 : field.__typename) === "WebAddressFieldValueAndDefinition" ? field.stringValue : null;
468
+ }
469
+ case "EmailFieldDefinition": {
470
+ return (field === null || field === void 0 ? void 0 : field.__typename) === "EmailFieldValueAndDefinition" ? field.stringValue : null;
471
+ }
472
+ case "PhoneNumberFieldDefinition": {
473
+ return watchedValue;
474
+ }
475
+ case "StringFieldDefinition": {
476
+ return (field === null || field === void 0 ? void 0 : field.__typename) === "StringFieldValueAndDefinition" ? field.stringValue : null;
477
+ }
478
+ case "NumberFieldDefinition": {
479
+ return (field === null || field === void 0 ? void 0 : field.__typename) === "NumberFieldValueAndDefinition" ? field.numberValue : null;
480
+ }
481
+ case "BooleanFieldDefinition": {
482
+ return (field === null || field === void 0 ? void 0 : field.__typename) === "BooleanFieldValueAndDefinition" ? field.booleanValue : null;
483
+ }
484
+ case "DropDownFieldDefinition": {
485
+ return (field === null || field === void 0 ? void 0 : field.__typename) === "DropDownFieldValueAndDefinition" ? field.stringArrayValue : null;
486
+ }
487
+ case "DateFieldDefinition": {
488
+ const value = (field === null || field === void 0 ? void 0 : field.__typename) === "DateFieldValueAndDefinition" ? field.dateValue : null;
489
+ let defaultValue;
490
+ if (value !== undefined && value !== null) {
491
+ defaultValue = new Date(value);
492
+ }
493
+ return defaultValue;
494
+ }
495
+ case "JsonFieldDefinition": {
496
+ return null;
497
+ }
498
+ case "MonetaryFieldDefinition": {
499
+ return numberValue === undefined || numberValue === null || numberValue === ""
500
+ ? ""
501
+ : parseFloat(Number(numberValue).toPrecision(4));
502
+ }
503
+ default:
504
+ return exhaustiveCheck(def);
505
+ }
506
+ };
470
507
  /**
471
508
  * CustomField is a React component that takes in the properties of a custom field and renders it.
472
509
  * It utilizes the `useCustomFieldResolver` hook to resolve the field into an input element
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/custom-field-components",
3
- "version": "0.0.741",
3
+ "version": "0.0.743",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -61,7 +61,7 @@ interface CustomFieldProps {
61
61
  * and functions for form registration and state management.
62
62
  * @returns {JSX.Element | null} - The JSX Element for the custom field or null if the field definition is not provided.
63
63
  */
64
- export declare const useCustomFieldResolver: ({ field, definition, register, setValue, formState, formValue, unitPreference, language, fieldId, isEditable, }: CustomFieldProps) => JSX.Element | null;
64
+ export declare const useCustomFieldResolver: ({ field, definition, register, setValue, formState, formValue, unitPreference, language, fieldId, isEditable, control, }: CustomFieldProps) => JSX.Element | null;
65
65
  /**
66
66
  * CustomField is a React component that takes in the properties of a custom field and renders it.
67
67
  * It utilizes the `useCustomFieldResolver` hook to resolve the field into an input element