@unicef-polymer/etools-form-builder 2.1.0 → 2.1.2

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 (87) hide show
  1. package/LICENSE +674 -674
  2. package/README.md +1 -1
  3. package/dist/assets/translations.d.ts +6 -0
  4. package/dist/assets/translations.js +67 -0
  5. package/dist/form-attachments-popup/form-attachments-popup.d.ts +86 -85
  6. package/dist/form-attachments-popup/form-attachments-popup.helper.d.ts +8 -8
  7. package/dist/form-attachments-popup/form-attachments-popup.helper.js +14 -14
  8. package/dist/form-attachments-popup/form-attachments-popup.js +255 -248
  9. package/dist/form-attachments-popup/form-attachments-popup.tpl.d.ts +3 -3
  10. package/dist/form-attachments-popup/form-attachments-popup.tpl.js +91 -89
  11. package/dist/form-attachments-popup/index.d.ts +2 -2
  12. package/dist/form-attachments-popup/index.js +2 -2
  13. package/dist/form-fields/abstract-field-base.class.d.ts +33 -28
  14. package/dist/form-fields/abstract-field-base.class.js +194 -176
  15. package/dist/form-fields/field-renderer-component.d.ts +20 -20
  16. package/dist/form-fields/field-renderer-component.js +249 -249
  17. package/dist/form-fields/index.d.ts +12 -12
  18. package/dist/form-fields/index.js +12 -12
  19. package/dist/form-fields/repeatable-fields/repeatable-attachment-field.d.ts +17 -17
  20. package/dist/form-fields/repeatable-fields/repeatable-attachment-field.js +209 -208
  21. package/dist/form-fields/repeatable-fields/repeatable-base-field.d.ts +20 -20
  22. package/dist/form-fields/repeatable-fields/repeatable-base-field.js +124 -123
  23. package/dist/form-fields/repeatable-fields/repeatable-number-field.d.ts +10 -10
  24. package/dist/form-fields/repeatable-fields/repeatable-number-field.js +67 -66
  25. package/dist/form-fields/repeatable-fields/repeatable-scale-field.d.ts +15 -15
  26. package/dist/form-fields/repeatable-fields/repeatable-scale-field.js +108 -108
  27. package/dist/form-fields/repeatable-fields/repeatable-text-field.d.ts +8 -8
  28. package/dist/form-fields/repeatable-fields/repeatable-text-field.js +53 -53
  29. package/dist/form-fields/single-fields/attachment-field.d.ts +16 -16
  30. package/dist/form-fields/single-fields/attachment-field.js +98 -97
  31. package/dist/form-fields/single-fields/base-field.d.ts +11 -11
  32. package/dist/form-fields/single-fields/base-field.js +58 -57
  33. package/dist/form-fields/single-fields/boolean-field.d.ts +8 -8
  34. package/dist/form-fields/single-fields/boolean-field.js +49 -49
  35. package/dist/form-fields/single-fields/number-field.d.ts +10 -10
  36. package/dist/form-fields/single-fields/number-field.js +67 -66
  37. package/dist/form-fields/single-fields/scale-field.d.ts +18 -18
  38. package/dist/form-fields/single-fields/scale-field.js +108 -107
  39. package/dist/form-fields/single-fields/text-field.d.ts +8 -8
  40. package/dist/form-fields/single-fields/text-field.js +54 -54
  41. package/dist/form-groups/form-abstract-group.d.ts +65 -60
  42. package/dist/form-groups/form-abstract-group.js +375 -353
  43. package/dist/form-groups/form-card.d.ts +32 -32
  44. package/dist/form-groups/form-card.js +113 -108
  45. package/dist/form-groups/form-collapsed-card.d.ts +70 -70
  46. package/dist/form-groups/form-collapsed-card.js +254 -253
  47. package/dist/form-groups/index.d.ts +3 -3
  48. package/dist/form-groups/index.js +3 -3
  49. package/dist/index.d.ts +3 -3
  50. package/dist/index.js +3 -3
  51. package/dist/lib/additional-components/confirmation-dialog.d.ts +18 -16
  52. package/dist/lib/additional-components/confirmation-dialog.js +72 -65
  53. package/dist/lib/additional-components/etools-fb-card.d.ts +21 -16
  54. package/dist/lib/additional-components/etools-fb-card.js +238 -215
  55. package/dist/lib/styles/attachments.styles.d.ts +2 -2
  56. package/dist/lib/styles/attachments.styles.js +72 -72
  57. package/dist/lib/styles/card-styles.d.ts +2 -2
  58. package/dist/lib/styles/card-styles.js +154 -154
  59. package/dist/lib/styles/dialog.styles.d.ts +2 -2
  60. package/dist/lib/styles/dialog.styles.js +85 -85
  61. package/dist/lib/styles/elevation-styles.d.ts +9 -9
  62. package/dist/lib/styles/elevation-styles.js +43 -43
  63. package/dist/lib/styles/flex-layout-classes.d.ts +2 -2
  64. package/dist/lib/styles/flex-layout-classes.js +319 -319
  65. package/dist/lib/styles/form-builder-card.styles.d.ts +2 -2
  66. package/dist/lib/styles/form-builder-card.styles.js +49 -49
  67. package/dist/lib/styles/input-styles.d.ts +2 -2
  68. package/dist/lib/styles/input-styles.js +138 -138
  69. package/dist/lib/styles/page-layout-styles.d.ts +2 -2
  70. package/dist/lib/styles/page-layout-styles.js +201 -201
  71. package/dist/lib/styles/shared-styles.d.ts +2 -2
  72. package/dist/lib/styles/shared-styles.js +70 -70
  73. package/dist/lib/types/form-builder.interfaces.d.ts +83 -83
  74. package/dist/lib/types/form-builder.interfaces.js +0 -0
  75. package/dist/lib/types/form-builder.types.d.ts +59 -59
  76. package/dist/lib/types/form-builder.types.js +0 -0
  77. package/dist/lib/types/global.types.d.ts +4 -4
  78. package/dist/lib/types/global.types.js +0 -0
  79. package/dist/lib/utils/dialog.d.ts +10 -10
  80. package/dist/lib/utils/dialog.js +21 -21
  81. package/dist/lib/utils/fire-custom-event.d.ts +1 -1
  82. package/dist/lib/utils/fire-custom-event.js +7 -7
  83. package/dist/lib/utils/translate.d.ts +1 -0
  84. package/dist/lib/utils/translate.js +9 -0
  85. package/dist/lib/utils/validations.helper.d.ts +26 -26
  86. package/dist/lib/utils/validations.helper.js +42 -35
  87. package/package.json +56 -56
@@ -1,83 +1,83 @@
1
- import { TemplateResult } from 'lit-element';
2
- import { BlueprintField, BlueprintGroup, BlueprintMetadata } from './form-builder.types';
3
- import { GenericObject } from './global.types';
4
- /** Component for rendering BlueprintGroup with group.extra.type.includes('abstract') or group.name === 'root' */
5
- export interface IFormBuilderAbstractGroup {
6
- groupStructure: BlueprintGroup;
7
- metadata: BlueprintMetadata;
8
- parentGroupName: string;
9
- readonly: boolean;
10
- value: GenericObject;
11
- /**
12
- * Updates groupValue object by this.value[fieldName] = event.details.value
13
- * @param event - comes from event emitted by rendered BlueprintField or BlueprintGroup child of current Group
14
- * @param fieldName - name field of BlueprintField or BlueprintGroup child of current Group
15
- *
16
- * stopsPropagation of current value-changed event it will be replaced with new event
17
- * Emits value-changed event with updated groupValue
18
- */
19
- valueChanged(event: CustomEvent, fieldName: string): void;
20
- /**
21
- * Returns template for BlueprintField structure depending on their styling
22
- *
23
- * without styling (empty array) - standard appearance (label and helper text in left column field element in right)
24
- * styling.includes('wide') - field takes all parent width
25
- * styling.includes('additional') - gray background
26
- *
27
- * Pass value to field as this.value[BlueprintField.name]
28
- * Rendered field must emit value-changed event
29
- * Use it to update current group value:
30
- * @value-changed="${(event) => this.valueChanged(event, BlueprintField.name)}"
31
- */
32
- renderField(fieldStructure: BlueprintField): TemplateResult;
33
- /**
34
- * Returns template for BlueprintGroup structure depending on their styling
35
- *
36
- * styling.includes('card') && styling.includes('collapse') - render as IFormBuilderCollapsedCard
37
- * styling.includes('card') - render as IFormBuilderCard
38
- * styling.includes('abstract') - render as IFormBuilderAbstractGroup recursively
39
- *
40
- * Pass groupValue to group as this.value[BlueprintGroup.name]
41
- * Rendered group must emit value-changed event
42
- * Use it to update current group value:
43
- * @value-changed="${(event) => this.valueChanged(event, BlueprintGroup.name)}"
44
- */
45
- renderGroup(groupStructure: BlueprintGroup, groupValue?: GenericObject): TemplateResult | TemplateResult[];
46
- }
47
- export interface IFormBuilderCard extends IFormBuilderAbstractGroup {
48
- /**
49
- * Overrides parent method.
50
- * It must doing the same but not sending new event, only stop current.
51
- * value-changed will be send latter
52
- */
53
- valueChanged(event: CustomEvent, fieldName: string): void;
54
- /**
55
- * Show toastr if card has errors, returns;
56
- * Emits value-changed event for updating changed value
57
- */
58
- saveChanges(): void;
59
- }
60
- /**
61
- * Component for rendering BlueprintGroup as DataCollectionCard.
62
- * Computes Card title using parentGroupName mapping + currentGroup.name
63
- * Allows to render attachment group and handles their logic
64
- */
65
- export interface IFormBuilderCollapsedCard extends IFormBuilderAbstractGroup, IFormBuilderCard {
66
- /**
67
- * Extend renderGroup() method. It must handle additional type:
68
- *
69
- * styling.includes('floating_attachments') - render group as attachment button
70
- *
71
- * ---
72
- * if (styling.includes('floating_attachments')) { ...render attachment button... }
73
- * else { super.renderGroup(groupStructure); }
74
- *
75
- */
76
- renderGroup(groupStructure: BlueprintGroup, groupValue?: GenericObject): TemplateResult | TemplateResult[];
77
- /**
78
- * Use openDialog method. Call valueChanged() on popup resolve with confirmed === true
79
- */
80
- openAttachmentsPopup(): void;
81
- cancelEdit(): void;
82
- startEdit(): void;
83
- }
1
+ import { TemplateResult } from 'lit-element';
2
+ import { BlueprintField, BlueprintGroup, BlueprintMetadata } from './form-builder.types';
3
+ import { GenericObject } from './global.types';
4
+ /** Component for rendering BlueprintGroup with group.extra.type.includes('abstract') or group.name === 'root' */
5
+ export interface IFormBuilderAbstractGroup {
6
+ groupStructure: BlueprintGroup;
7
+ metadata: BlueprintMetadata;
8
+ parentGroupName: string;
9
+ readonly: boolean;
10
+ value: GenericObject;
11
+ /**
12
+ * Updates groupValue object by this.value[fieldName] = event.details.value
13
+ * @param event - comes from event emitted by rendered BlueprintField or BlueprintGroup child of current Group
14
+ * @param fieldName - name field of BlueprintField or BlueprintGroup child of current Group
15
+ *
16
+ * stopsPropagation of current value-changed event it will be replaced with new event
17
+ * Emits value-changed event with updated groupValue
18
+ */
19
+ valueChanged(event: CustomEvent, fieldName: string): void;
20
+ /**
21
+ * Returns template for BlueprintField structure depending on their styling
22
+ *
23
+ * without styling (empty array) - standard appearance (label and helper text in left column field element in right)
24
+ * styling.includes('wide') - field takes all parent width
25
+ * styling.includes('additional') - gray background
26
+ *
27
+ * Pass value to field as this.value[BlueprintField.name]
28
+ * Rendered field must emit value-changed event
29
+ * Use it to update current group value:
30
+ * @value-changed="${(event) => this.valueChanged(event, BlueprintField.name)}"
31
+ */
32
+ renderField(fieldStructure: BlueprintField): TemplateResult;
33
+ /**
34
+ * Returns template for BlueprintGroup structure depending on their styling
35
+ *
36
+ * styling.includes('card') && styling.includes('collapse') - render as IFormBuilderCollapsedCard
37
+ * styling.includes('card') - render as IFormBuilderCard
38
+ * styling.includes('abstract') - render as IFormBuilderAbstractGroup recursively
39
+ *
40
+ * Pass groupValue to group as this.value[BlueprintGroup.name]
41
+ * Rendered group must emit value-changed event
42
+ * Use it to update current group value:
43
+ * @value-changed="${(event) => this.valueChanged(event, BlueprintGroup.name)}"
44
+ */
45
+ renderGroup(groupStructure: BlueprintGroup, groupValue?: GenericObject): TemplateResult | TemplateResult[];
46
+ }
47
+ export interface IFormBuilderCard extends IFormBuilderAbstractGroup {
48
+ /**
49
+ * Overrides parent method.
50
+ * It must doing the same but not sending new event, only stop current.
51
+ * value-changed will be send latter
52
+ */
53
+ valueChanged(event: CustomEvent, fieldName: string): void;
54
+ /**
55
+ * Show toastr if card has errors, returns;
56
+ * Emits value-changed event for updating changed value
57
+ */
58
+ saveChanges(): void;
59
+ }
60
+ /**
61
+ * Component for rendering BlueprintGroup as DataCollectionCard.
62
+ * Computes Card title using parentGroupName mapping + currentGroup.name
63
+ * Allows to render attachment group and handles their logic
64
+ */
65
+ export interface IFormBuilderCollapsedCard extends IFormBuilderAbstractGroup, IFormBuilderCard {
66
+ /**
67
+ * Extend renderGroup() method. It must handle additional type:
68
+ *
69
+ * styling.includes('floating_attachments') - render group as attachment button
70
+ *
71
+ * ---
72
+ * if (styling.includes('floating_attachments')) { ...render attachment button... }
73
+ * else { super.renderGroup(groupStructure); }
74
+ *
75
+ */
76
+ renderGroup(groupStructure: BlueprintGroup, groupValue?: GenericObject): TemplateResult | TemplateResult[];
77
+ /**
78
+ * Use openDialog method. Call valueChanged() on popup resolve with confirmed === true
79
+ */
80
+ openAttachmentsPopup(): void;
81
+ cancelEdit(): void;
82
+ startEdit(): void;
83
+ }
File without changes
@@ -1,59 +1,59 @@
1
- import { FieldValidator } from '../utils/validations.helper';
2
- import { GenericObject } from './global.types';
3
- /** JSON file from backend */
4
- export declare type ChecklistFormJson = {
5
- blueprint: {
6
- code: string;
7
- metadata: BlueprintMetadata;
8
- structure: BlueprintGroup;
9
- title: string;
10
- };
11
- value: GenericObject;
12
- };
13
- /** --------- */
14
- export declare type BlueprintGroup = {
15
- type: 'group';
16
- extra: GenericObject;
17
- styling: string[];
18
- name: string;
19
- repeatable: boolean;
20
- required: boolean;
21
- title: string | null;
22
- children: (BlueprintGroup | BlueprintField | Information)[];
23
- };
24
- export declare type BlueprintField = {
25
- type: 'field';
26
- extra: GenericObject;
27
- styling: string[];
28
- name: string;
29
- repeatable: boolean;
30
- required: boolean;
31
- input_type: 'text' | 'likert_scale' | 'bool' | 'number' | 'number-integer' | 'number-float' | 'file';
32
- label: string;
33
- validations: string[];
34
- help_text: string;
35
- placeholder: string;
36
- default_value: null;
37
- options_key: null;
38
- };
39
- export declare type Information = {
40
- type: 'information';
41
- extra: GenericObject;
42
- styling: string[];
43
- text: string;
44
- };
45
- /** Metadata for blueprint structure */
46
- export declare type BlueprintMetadata = {
47
- options: GenericObject<BlueprintMetadataOptions>;
48
- validations: GenericObject<FieldValidator>;
49
- offline_enabled: boolean;
50
- allow_multiple_responses: boolean;
51
- };
52
- export declare type BlueprintMetadataOptions = {
53
- option_type: string;
54
- values: BlueprintMetadataOption[];
55
- };
56
- export declare type BlueprintMetadataOption = {
57
- value: string;
58
- label: string;
59
- };
1
+ import { FieldValidator } from '../utils/validations.helper';
2
+ import { GenericObject } from './global.types';
3
+ /** JSON file from backend */
4
+ export declare type ChecklistFormJson = {
5
+ blueprint: {
6
+ code: string;
7
+ metadata: BlueprintMetadata;
8
+ structure: BlueprintGroup;
9
+ title: string;
10
+ };
11
+ value: GenericObject;
12
+ };
13
+ /** --------- */
14
+ export declare type BlueprintGroup = {
15
+ type: 'group';
16
+ extra: GenericObject;
17
+ styling: string[];
18
+ name: string;
19
+ repeatable: boolean;
20
+ required: boolean;
21
+ title: string | null;
22
+ children: (BlueprintGroup | BlueprintField | Information)[];
23
+ };
24
+ export declare type BlueprintField = {
25
+ type: 'field';
26
+ extra: GenericObject;
27
+ styling: string[];
28
+ name: string;
29
+ repeatable: boolean;
30
+ required: boolean;
31
+ input_type: 'text' | 'likert_scale' | 'bool' | 'number' | 'number-integer' | 'number-float' | 'file';
32
+ label: string;
33
+ validations: string[];
34
+ help_text: string;
35
+ placeholder: string;
36
+ default_value: null;
37
+ options_key: null;
38
+ };
39
+ export declare type Information = {
40
+ type: 'information';
41
+ extra: GenericObject;
42
+ styling: string[];
43
+ text: string;
44
+ };
45
+ /** Metadata for blueprint structure */
46
+ export declare type BlueprintMetadata = {
47
+ options: GenericObject<BlueprintMetadataOptions>;
48
+ validations: GenericObject<FieldValidator>;
49
+ offline_enabled: boolean;
50
+ allow_multiple_responses: boolean;
51
+ };
52
+ export declare type BlueprintMetadataOptions = {
53
+ option_type: string;
54
+ values: BlueprintMetadataOption[];
55
+ };
56
+ export declare type BlueprintMetadataOption = {
57
+ value: string;
58
+ label: string;
59
+ };
File without changes
@@ -1,4 +1,4 @@
1
- export declare type GenericObject<T = any> = {
2
- [key: string]: T;
3
- };
4
- export declare type Callback = (...args: any) => void;
1
+ export declare type GenericObject<T = any> = {
2
+ [key: string]: T;
3
+ };
4
+ export declare type Callback = (...args: any) => void;
File without changes
@@ -1,10 +1,10 @@
1
- export interface IDialog<D> {
2
- dialog: string;
3
- dialogData?: D;
4
- readonly?: boolean;
5
- }
6
- export interface IDialogResponse<R> {
7
- response?: R;
8
- confirmed: boolean;
9
- }
10
- export declare function openDialog<D, R = any>({ dialog, dialogData, readonly }: IDialog<D>): Promise<IDialogResponse<R>>;
1
+ export interface IDialog<D> {
2
+ dialog: string;
3
+ dialogData?: D;
4
+ readonly?: boolean;
5
+ }
6
+ export interface IDialogResponse<R> {
7
+ response?: R;
8
+ confirmed: boolean;
9
+ }
10
+ export declare function openDialog<D, R = any>({ dialog, dialogData, readonly }: IDialog<D>): Promise<IDialogResponse<R>>;
@@ -1,21 +1,21 @@
1
- export function openDialog({ dialog, dialogData, readonly }) {
2
- return new Promise((resolve, reject) => {
3
- const dialogElement = document.createElement(dialog);
4
- const body = document.querySelector('body');
5
- if (body) {
6
- body.appendChild(dialogElement);
7
- }
8
- else {
9
- reject(new Error('Body not exist'));
10
- }
11
- dialogElement.dialogData = dialogData;
12
- if (readonly) {
13
- dialogElement.readonly = readonly;
14
- }
15
- dialogElement.addEventListener('response', (e) => {
16
- const event = e;
17
- resolve(event.detail);
18
- dialogElement.remove();
19
- });
20
- });
21
- }
1
+ export function openDialog({ dialog, dialogData, readonly }) {
2
+ return new Promise((resolve, reject) => {
3
+ const dialogElement = document.createElement(dialog);
4
+ const body = document.querySelector('body');
5
+ if (body) {
6
+ body.appendChild(dialogElement);
7
+ }
8
+ else {
9
+ reject(new Error('Body not exist'));
10
+ }
11
+ dialogElement.dialogData = dialogData;
12
+ if (readonly) {
13
+ dialogElement.readonly = readonly;
14
+ }
15
+ dialogElement.addEventListener('response', (e) => {
16
+ const event = e;
17
+ resolve(event.detail);
18
+ dialogElement.remove();
19
+ });
20
+ });
21
+ }
@@ -1 +1 @@
1
- export declare function fireEvent(el: HTMLElement, eventName: string, detail?: any): void;
1
+ export declare function fireEvent(el: HTMLElement, eventName: string, detail?: any): void;
@@ -1,7 +1,7 @@
1
- export function fireEvent(el, eventName, detail) {
2
- el.dispatchEvent(new CustomEvent(eventName, {
3
- detail,
4
- bubbles: true,
5
- composed: true
6
- }));
7
- }
1
+ export function fireEvent(el, eventName, detail) {
2
+ el.dispatchEvent(new CustomEvent(eventName, {
3
+ detail,
4
+ bubbles: true,
5
+ composed: true
6
+ }));
7
+ }
@@ -0,0 +1 @@
1
+ export declare function getTranslation(lang: string, key: string): string;
@@ -0,0 +1,9 @@
1
+ import translations from '../../assets/translations';
2
+ export function getTranslation(lang, key) {
3
+ try {
4
+ return translations[lang][key];
5
+ }
6
+ catch (error) {
7
+ return translations.en[key];
8
+ }
9
+ }
@@ -1,26 +1,26 @@
1
- export declare enum Validations {
2
- MAX_LENGTH = "max_length",
3
- REGEX = "regex",
4
- LOWER_THAN = "lt",
5
- GREATER_THAN = "gt"
6
- }
7
- export declare type MaxLengthValidation = {
8
- name: Validations.MAX_LENGTH;
9
- [Validations.MAX_LENGTH]: number;
10
- };
11
- export declare type RegexValidation = {
12
- name: Validations.REGEX;
13
- [Validations.REGEX]: string;
14
- };
15
- export declare type GreaterValidation = {
16
- name: Validations.GREATER_THAN;
17
- threshold: number;
18
- allow_equality: boolean;
19
- };
20
- export declare type LowerValidation = {
21
- name: Validations.LOWER_THAN;
22
- threshold: number;
23
- allow_equality: boolean;
24
- };
25
- export declare type FieldValidator = MaxLengthValidation | RegexValidation | GreaterValidation | LowerValidation;
26
- export declare function validate(validators: FieldValidator[], value: any): string | null;
1
+ export declare enum Validations {
2
+ MAX_LENGTH = "max_length",
3
+ REGEX = "regex",
4
+ LOWER_THAN = "lt",
5
+ GREATER_THAN = "gt"
6
+ }
7
+ export declare type MaxLengthValidation = {
8
+ name: Validations.MAX_LENGTH;
9
+ [Validations.MAX_LENGTH]: number;
10
+ };
11
+ export declare type RegexValidation = {
12
+ name: Validations.REGEX;
13
+ [Validations.REGEX]: string;
14
+ };
15
+ export declare type GreaterValidation = {
16
+ name: Validations.GREATER_THAN;
17
+ threshold: number;
18
+ allow_equality: boolean;
19
+ };
20
+ export declare type LowerValidation = {
21
+ name: Validations.LOWER_THAN;
22
+ threshold: number;
23
+ allow_equality: boolean;
24
+ };
25
+ export declare type FieldValidator = MaxLengthValidation | RegexValidation | GreaterValidation | LowerValidation;
26
+ export declare function validate(validators: FieldValidator[], value: any, language: string): string | null;
@@ -1,35 +1,42 @@
1
- export var Validations;
2
- (function (Validations) {
3
- Validations["MAX_LENGTH"] = "max_length";
4
- Validations["REGEX"] = "regex";
5
- Validations["LOWER_THAN"] = "lt";
6
- Validations["GREATER_THAN"] = "gt";
7
- })(Validations || (Validations = {}));
8
- export function validate(validators, value) {
9
- let message = null;
10
- for (const validator of validators) {
11
- message = checkValidation(validator, value);
12
- if (message) {
13
- break;
14
- }
15
- }
16
- return message;
17
- }
18
- function checkValidation(validation, value) {
19
- switch (validation.name) {
20
- case Validations.MAX_LENGTH:
21
- const maxLength = Number(validation[Validations.MAX_LENGTH]) + 1;
22
- return String(value).length < maxLength ? null : `Text must be less than ${maxLength} character`;
23
- case Validations.REGEX:
24
- const regex = new RegExp(`^${validation[Validations.REGEX]}$`);
25
- return regex.test(String(value)) ? null : `Doesn't match allowed pattern`;
26
- case Validations.GREATER_THAN:
27
- const greaterThan = Number(validation.threshold) - Number(validation.allow_equality);
28
- return Number(value) > greaterThan ? null : `Number must be greater than ${greaterThan}`;
29
- case Validations.LOWER_THAN:
30
- const lowerThan = Number(validation.threshold) + Number(validation.allow_equality);
31
- return Number(value) < lowerThan ? null : `Number must be lower than ${lowerThan}`;
32
- default:
33
- return null;
34
- }
35
- }
1
+ import { getTranslation } from './translate';
2
+ export var Validations;
3
+ (function (Validations) {
4
+ Validations["MAX_LENGTH"] = "max_length";
5
+ Validations["REGEX"] = "regex";
6
+ Validations["LOWER_THAN"] = "lt";
7
+ Validations["GREATER_THAN"] = "gt";
8
+ })(Validations || (Validations = {}));
9
+ export function validate(validators, value, language) {
10
+ let message = null;
11
+ for (const validator of validators) {
12
+ message = checkValidation(validator, value, language);
13
+ if (message) {
14
+ break;
15
+ }
16
+ }
17
+ return message;
18
+ }
19
+ function checkValidation(validation, value, language) {
20
+ switch (validation.name) {
21
+ case Validations.MAX_LENGTH:
22
+ const maxLength = Number(validation[Validations.MAX_LENGTH]) + 1;
23
+ return String(value).length < maxLength
24
+ ? null
25
+ : getTranslation(language, 'TEXT_MUST_BE_LESS_CHARS').replace('{0}', String(maxLength));
26
+ case Validations.REGEX:
27
+ const regex = new RegExp(`^${validation[Validations.REGEX]}$`);
28
+ return regex.test(String(value)) ? null : getTranslation(language, 'DOES_NOT_MATCH_PATTERN');
29
+ case Validations.GREATER_THAN:
30
+ const greaterThan = Number(validation.threshold) - Number(validation.allow_equality);
31
+ return Number(value) > greaterThan
32
+ ? null
33
+ : getTranslation(language, 'NUMBER_MUST_BE_GREATER_THAN').replace('{0}', String(greaterThan));
34
+ case Validations.LOWER_THAN:
35
+ const lowerThan = Number(validation.threshold) + Number(validation.allow_equality);
36
+ return Number(value) < lowerThan
37
+ ? null
38
+ : getTranslation(language, 'NUMBER_MUST_BE_LOWER_THAN').replace('{0}', String(lowerThan));
39
+ default:
40
+ return null;
41
+ }
42
+ }
package/package.json CHANGED
@@ -1,56 +1,56 @@
1
- {
2
- "name": "@unicef-polymer/etools-form-builder",
3
- "description": "Etools FM Form Builder components",
4
- "version": "2.1.0",
5
- "contributors": [
6
- "eTools Team"
7
- ],
8
- "license": "BSD-3-Clause",
9
- "repository": "https://github.com/unicef-polymer/fm-form-builder",
10
- "main": "dist/index.js",
11
- "types": "dist/index.d.ts",
12
- "files": [
13
- "dist"
14
- ],
15
- "scripts": {
16
- "build": "tsc --skipLibCheck",
17
- "lint": "eslint src/",
18
- "prepare": "rm -rf ./dist && npm run build",
19
- "prepublishOnly": "npm run lint"
20
- },
21
- "dependencies": {
22
- "@polymer/iron-collapse": "^3.0.1",
23
- "@polymer/iron-icons": "^3.0.1",
24
- "@polymer/paper-button": "^3.0.1",
25
- "@polymer/paper-input": "^3.2.1",
26
- "@polymer/paper-radio-button": "^3.0.1",
27
- "@polymer/paper-radio-group": "^3.0.1",
28
- "@polymer/paper-toast": "^3.0.1",
29
- "@polymer/paper-toggle-button": "^3.0.1",
30
- "@types/ramda": "^0.26.43",
31
- "@unicef-polymer/etools-dialog": "^6.0.3",
32
- "@unicef-polymer/etools-dropdown": "^3.9.2",
33
- "@unicef-polymer/etools-upload": "^4.3.0",
34
- "@webcomponents/webcomponentsjs": "^2.6.0",
35
- "lit-element": "^2.5.1",
36
- "lit-translate": "^1.2.1",
37
- "ramda": "^0.27.1",
38
- "web-animations-js": "^2.3.2"
39
- },
40
- "devDependencies": {
41
- "@typescript-eslint/eslint-plugin": "^3.10.1",
42
- "@typescript-eslint/parser": "^3.10.1",
43
- "acorn": "^8.0.4",
44
- "eslint": "^7.14.0",
45
- "eslint-config-prettier": "^6.15.0",
46
- "eslint-plugin-html": "^6.1.1",
47
- "eslint-plugin-lit": "^1.2.4",
48
- "eslint-plugin-prettier": "^3.1.3",
49
- "minimist": ">=0.2.1",
50
- "prettier": "~2.2.0",
51
- "typescript": "^3.9.7",
52
- "lodash": ">=4.17.21",
53
- "ansi-regex": ">=5.0.1",
54
- "glob-parent": ">=5.1.2"
55
- }
56
- }
1
+ {
2
+ "name": "@unicef-polymer/etools-form-builder",
3
+ "description": "Etools FM Form Builder components",
4
+ "version": "2.1.2",
5
+ "contributors": [
6
+ "eTools Team"
7
+ ],
8
+ "license": "BSD-3-Clause",
9
+ "repository": "https://github.com/unicef-polymer/fm-form-builder",
10
+ "main": "dist/index.js",
11
+ "types": "dist/index.d.ts",
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "scripts": {
16
+ "build": "tsc --skipLibCheck",
17
+ "lint": "eslint src/",
18
+ "prepare": "npm run build",
19
+ "prepublishOnly": "npm run lint"
20
+ },
21
+ "dependencies": {
22
+ "@polymer/iron-collapse": "^3.0.1",
23
+ "@polymer/iron-icons": "^3.0.1",
24
+ "@polymer/paper-button": "^3.0.1",
25
+ "@polymer/paper-input": "^3.2.1",
26
+ "@polymer/paper-radio-button": "^3.0.1",
27
+ "@polymer/paper-radio-group": "^3.0.1",
28
+ "@polymer/paper-toast": "^3.0.1",
29
+ "@polymer/paper-toggle-button": "^3.0.1",
30
+ "@types/ramda": "^0.26.43",
31
+ "@unicef-polymer/etools-dialog": "^6.0.4",
32
+ "@unicef-polymer/etools-dropdown": "^3.9.7",
33
+ "@unicef-polymer/etools-upload": "^4.4.1",
34
+ "@webcomponents/webcomponentsjs": "^2.6.0",
35
+ "lit-element": "^2.5.1",
36
+ "lit-translate": "^1.2.1",
37
+ "ramda": "^0.27.1",
38
+ "web-animations-js": "^2.3.2"
39
+ },
40
+ "devDependencies": {
41
+ "@typescript-eslint/eslint-plugin": "^3.10.1",
42
+ "@typescript-eslint/parser": "^3.10.1",
43
+ "acorn": "^8.0.4",
44
+ "eslint": "^7.14.0",
45
+ "eslint-config-prettier": "^6.15.0",
46
+ "eslint-plugin-html": "^6.1.1",
47
+ "eslint-plugin-lit": "^1.2.4",
48
+ "eslint-plugin-prettier": "^3.1.3",
49
+ "minimist": ">=0.2.1",
50
+ "prettier": "~2.2.0",
51
+ "typescript": "^3.9.7",
52
+ "lodash": ">=4.17.21",
53
+ "ansi-regex": ">=5.0.1",
54
+ "glob-parent": ">=5.1.2"
55
+ }
56
+ }