@trackunit/custom-field-components 0.0.698 → 0.0.699
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 +4 -25
- package/index.esm.js +5 -26
- package/package.json +1 -2
- package/src/CustomField.d.ts +6 -2
package/index.cjs.js
CHANGED
|
@@ -9,7 +9,6 @@ var reactFormComponents = require('@trackunit/react-form-components');
|
|
|
9
9
|
var sharedUtils = require('@trackunit/shared-utils');
|
|
10
10
|
var React = require('react');
|
|
11
11
|
var uuid = require('uuid');
|
|
12
|
-
var libphonenumberJs = require('libphonenumber-js');
|
|
13
12
|
|
|
14
13
|
function _interopNamespace(e) {
|
|
15
14
|
if (e && e.__esModule) return e;
|
|
@@ -368,22 +367,6 @@ const getEmailValidationRules = (definition) => {
|
|
|
368
367
|
return definition.uiEditable
|
|
369
368
|
? Object.assign(Object.assign({}, pattern), defaultRules) : defaultRules;
|
|
370
369
|
};
|
|
371
|
-
/**
|
|
372
|
-
* Generates validation rules for a phone number field.
|
|
373
|
-
*
|
|
374
|
-
* @param {CommonValidationRulesDefinition} definition - The definition of the phone number field.
|
|
375
|
-
* @returns {ValidationRules} The generated validation rules.
|
|
376
|
-
*/
|
|
377
|
-
const getPhoneNumberValidationRules = (definition) => {
|
|
378
|
-
const defaultRules = {};
|
|
379
|
-
const pattern = {
|
|
380
|
-
validate: value => {
|
|
381
|
-
return value === "" || value === undefined || value === null || libphonenumberJs.isValidPhoneNumber(value);
|
|
382
|
-
},
|
|
383
|
-
};
|
|
384
|
-
return definition.uiEditable
|
|
385
|
-
? Object.assign(Object.assign({}, pattern), defaultRules) : defaultRules;
|
|
386
|
-
};
|
|
387
370
|
|
|
388
371
|
/**
|
|
389
372
|
* Returns the translation for a unit.
|
|
@@ -408,7 +391,7 @@ const useUnitTranslation = () => {
|
|
|
408
391
|
* and functions for form registration and state management.
|
|
409
392
|
* @returns {JSX.Element | null} - The JSX Element for the custom field or null if the field definition is not provided.
|
|
410
393
|
*/
|
|
411
|
-
const useCustomFieldResolver = ({ field, definition, register, setValue, formState, formValue, unitPreference = "SI", language, fieldId, isEditable, }) => {
|
|
394
|
+
const useCustomFieldResolver = ({ field, definition, register, setValue, formState, formValue, unitPreference = "SI", language, fieldId, isEditable, control, }) => {
|
|
412
395
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
413
396
|
const validation = { register, setValue, formState };
|
|
414
397
|
const { unitTranslation } = useUnitTranslation();
|
|
@@ -425,6 +408,7 @@ const useCustomFieldResolver = ({ field, definition, register, setValue, formSta
|
|
|
425
408
|
return "";
|
|
426
409
|
}
|
|
427
410
|
});
|
|
411
|
+
const { getPhoneNumberValidationRules } = reactFormComponents.useGetPhoneValidationRules();
|
|
428
412
|
const def = (_a = field === null || field === void 0 ? void 0 : field.definition) !== null && _a !== void 0 ? _a : definition;
|
|
429
413
|
if (def === undefined || def.__typename === undefined) {
|
|
430
414
|
return null;
|
|
@@ -448,13 +432,8 @@ const useCustomFieldResolver = ({ field, definition, register, setValue, formSta
|
|
|
448
432
|
}
|
|
449
433
|
case "PhoneNumberFieldDefinition": {
|
|
450
434
|
const value = (field === null || field === void 0 ? void 0 : field.__typename) === "PhoneNumberFieldValueAndDefinition" ? field.stringValue : undefined;
|
|
451
|
-
const rules = getPhoneNumberValidationRules(def);
|
|
452
|
-
return (jsxRuntime.jsx(reactFormComponents.
|
|
453
|
-
validation.setValue(uniqueIdentifier, props.phone, {
|
|
454
|
-
shouldDirty: true,
|
|
455
|
-
shouldValidate: true,
|
|
456
|
-
});
|
|
457
|
-
} })));
|
|
435
|
+
const rules = getPhoneNumberValidationRules(!Boolean(def.uiEditable));
|
|
436
|
+
return (jsxRuntime.jsx(reactFormComponents.PhoneFieldWithController, { control: control, controllerProps: { rules }, dataTestId: `${key}`, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title, name: key, placeholder: title !== null && title !== void 0 ? title : "", readOnly: !isEditableCombined, value: value !== null && value !== void 0 ? value : "" }));
|
|
458
437
|
}
|
|
459
438
|
case "StringFieldDefinition": {
|
|
460
439
|
const value = (field === null || field === void 0 ? void 0 : field.__typename) === "StringFieldValueAndDefinition" ? field.stringValue : undefined;
|
package/index.esm.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { jsx } from 'react/jsx-runtime';
|
|
2
2
|
import { registerTranslations, useNamespaceTranslation } from '@trackunit/i18n-library-translation';
|
|
3
3
|
import { CustomFieldRuntime } from '@trackunit/iris-app-runtime-core';
|
|
4
|
-
import { FormGroup, Checkbox, DateField, Select, NumberField, TextField,
|
|
4
|
+
import { FormGroup, Checkbox, DateField, Select, useGetPhoneValidationRules, NumberField, TextField, PhoneFieldWithController, ActionButton } from '@trackunit/react-form-components';
|
|
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
8
|
import { v4 } from 'uuid';
|
|
9
|
-
import { isValidPhoneNumber } from 'libphonenumber-js';
|
|
10
9
|
|
|
11
10
|
var defaultTranslations = {
|
|
12
11
|
"customfield.unit.acre": "acre",
|
|
@@ -345,22 +344,6 @@ const getEmailValidationRules = (definition) => {
|
|
|
345
344
|
return definition.uiEditable
|
|
346
345
|
? Object.assign(Object.assign({}, pattern), defaultRules) : defaultRules;
|
|
347
346
|
};
|
|
348
|
-
/**
|
|
349
|
-
* Generates validation rules for a phone number field.
|
|
350
|
-
*
|
|
351
|
-
* @param {CommonValidationRulesDefinition} definition - The definition of the phone number field.
|
|
352
|
-
* @returns {ValidationRules} The generated validation rules.
|
|
353
|
-
*/
|
|
354
|
-
const getPhoneNumberValidationRules = (definition) => {
|
|
355
|
-
const defaultRules = {};
|
|
356
|
-
const pattern = {
|
|
357
|
-
validate: value => {
|
|
358
|
-
return value === "" || value === undefined || value === null || isValidPhoneNumber(value);
|
|
359
|
-
},
|
|
360
|
-
};
|
|
361
|
-
return definition.uiEditable
|
|
362
|
-
? Object.assign(Object.assign({}, pattern), defaultRules) : defaultRules;
|
|
363
|
-
};
|
|
364
347
|
|
|
365
348
|
/**
|
|
366
349
|
* Returns the translation for a unit.
|
|
@@ -385,7 +368,7 @@ const useUnitTranslation = () => {
|
|
|
385
368
|
* and functions for form registration and state management.
|
|
386
369
|
* @returns {JSX.Element | null} - The JSX Element for the custom field or null if the field definition is not provided.
|
|
387
370
|
*/
|
|
388
|
-
const useCustomFieldResolver = ({ field, definition, register, setValue, formState, formValue, unitPreference = "SI", language, fieldId, isEditable, }) => {
|
|
371
|
+
const useCustomFieldResolver = ({ field, definition, register, setValue, formState, formValue, unitPreference = "SI", language, fieldId, isEditable, control, }) => {
|
|
389
372
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
390
373
|
const validation = { register, setValue, formState };
|
|
391
374
|
const { unitTranslation } = useUnitTranslation();
|
|
@@ -402,6 +385,7 @@ const useCustomFieldResolver = ({ field, definition, register, setValue, formSta
|
|
|
402
385
|
return "";
|
|
403
386
|
}
|
|
404
387
|
});
|
|
388
|
+
const { getPhoneNumberValidationRules } = useGetPhoneValidationRules();
|
|
405
389
|
const def = (_a = field === null || field === void 0 ? void 0 : field.definition) !== null && _a !== void 0 ? _a : definition;
|
|
406
390
|
if (def === undefined || def.__typename === undefined) {
|
|
407
391
|
return null;
|
|
@@ -425,13 +409,8 @@ const useCustomFieldResolver = ({ field, definition, register, setValue, formSta
|
|
|
425
409
|
}
|
|
426
410
|
case "PhoneNumberFieldDefinition": {
|
|
427
411
|
const value = (field === null || field === void 0 ? void 0 : field.__typename) === "PhoneNumberFieldValueAndDefinition" ? field.stringValue : undefined;
|
|
428
|
-
const rules = getPhoneNumberValidationRules(def);
|
|
429
|
-
return (jsx(
|
|
430
|
-
validation.setValue(uniqueIdentifier, props.phone, {
|
|
431
|
-
shouldDirty: true,
|
|
432
|
-
shouldValidate: true,
|
|
433
|
-
});
|
|
434
|
-
} })));
|
|
412
|
+
const rules = getPhoneNumberValidationRules(!Boolean(def.uiEditable));
|
|
413
|
+
return (jsx(PhoneFieldWithController, { control: control, controllerProps: { rules }, dataTestId: `${key}`, errorMessage: errorMessage, helpText: description, id: uniqueIdentifier, label: title, name: key, placeholder: title !== null && title !== void 0 ? title : "", readOnly: !isEditableCombined, value: value !== null && value !== void 0 ? value : "" }));
|
|
435
414
|
}
|
|
436
415
|
case "StringFieldDefinition": {
|
|
437
416
|
const value = (field === null || field === void 0 ? void 0 : field.__typename) === "StringFieldValueAndDefinition" ? field.stringValue : undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/custom-field-components",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.699",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
@@ -16,7 +16,6 @@
|
|
|
16
16
|
"@trackunit/iris-app-runtime-core": "*",
|
|
17
17
|
"@trackunit/iris-app-runtime-core-api": "*",
|
|
18
18
|
"react-select": "^5.8.0",
|
|
19
|
-
"libphonenumber-js": "1.10.36",
|
|
20
19
|
"@testing-library/react": "14.0.0",
|
|
21
20
|
"jest-fetch-mock": "^3.0.3",
|
|
22
21
|
"@trackunit/i18n-library-translation": "*"
|
package/src/CustomField.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { CustomFieldDefinition, CustomFieldValueAndDefinition } from "@trackunit/custom-field-api";
|
|
3
|
-
import { FieldValues, FormState, UseFormRegister, UseFormSetValue } from "react-hook-form";
|
|
3
|
+
import { Control, FieldValues, FormState, UseFormRegister, UseFormSetValue } from "react-hook-form";
|
|
4
4
|
import { UnitPreference } from "./UnitPreference";
|
|
5
5
|
/**
|
|
6
6
|
* CustomFieldProps interface defines the structure for the properties
|
|
@@ -47,6 +47,10 @@ interface CustomFieldProps {
|
|
|
47
47
|
* (Optional) An alternative identifier for the field, which overrides the default definition.key.
|
|
48
48
|
*/
|
|
49
49
|
fieldId?: string;
|
|
50
|
+
/**
|
|
51
|
+
* Instance of `Control` from `react-hook-form`.
|
|
52
|
+
*/
|
|
53
|
+
control: Control<any, any>;
|
|
50
54
|
}
|
|
51
55
|
/**
|
|
52
56
|
* A hook that resolves and returns a JSX Element for a given custom field.
|
|
@@ -57,7 +61,7 @@ interface CustomFieldProps {
|
|
|
57
61
|
* and functions for form registration and state management.
|
|
58
62
|
* @returns {JSX.Element | null} - The JSX Element for the custom field or null if the field definition is not provided.
|
|
59
63
|
*/
|
|
60
|
-
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;
|
|
61
65
|
/**
|
|
62
66
|
* CustomField is a React component that takes in the properties of a custom field and renders it.
|
|
63
67
|
* It utilizes the `useCustomFieldResolver` hook to resolve the field into an input element
|