@bolttech/form-engine 0.1.0

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 (61) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +1172 -0
  3. package/index.js +2 -0
  4. package/index.js.map +1 -0
  5. package/package.json +50 -0
  6. package/src/adapters/react/Field.d.ts +4 -0
  7. package/src/adapters/react/Form.d.ts +5 -0
  8. package/src/adapters/react/Submit.d.ts +3 -0
  9. package/src/adapters/react/asFormField.d.ts +14 -0
  10. package/src/adapters/react/context.d.ts +5 -0
  11. package/src/adapters/react/index.d.ts +6 -0
  12. package/src/adapters/react/types.d.ts +185 -0
  13. package/src/adapters/react/useForceUpdate.d.ts +3 -0
  14. package/src/adapters/react/useForm.d.ts +51 -0
  15. package/src/adapters/react/useFormGroup.d.ts +15 -0
  16. package/src/core/apis/formatters.d.ts +16 -0
  17. package/src/core/apis/index.d.ts +4 -0
  18. package/src/core/apis/masks.d.ts +3 -0
  19. package/src/core/apis/validations.d.ts +11 -0
  20. package/src/core/apis/varOps.d.ts +4 -0
  21. package/src/core/constants/events.d.ts +23 -0
  22. package/src/core/constants/index.d.ts +5 -0
  23. package/src/core/constants/observer.d.ts +7 -0
  24. package/src/core/events/ObserverError.d.ts +7 -0
  25. package/src/core/events/events.types.d.ts +27 -0
  26. package/src/core/events/index.d.ts +2 -0
  27. package/src/core/handlers/common/templating.d.ts +2 -0
  28. package/src/core/handlers/field/api.d.ts +4 -0
  29. package/src/core/handlers/field/blur.d.ts +2 -0
  30. package/src/core/handlers/field/change.d.ts +2 -0
  31. package/src/core/handlers/field/clearFields.d.ts +4 -0
  32. package/src/core/handlers/field/data.d.ts +3 -0
  33. package/src/core/handlers/field/filter.d.ts +2 -0
  34. package/src/core/handlers/field/focus.d.ts +2 -0
  35. package/src/core/handlers/field/formatters.d.ts +4 -0
  36. package/src/core/handlers/field/htmlEventParser.d.ts +6 -0
  37. package/src/core/handlers/field/masks.d.ts +4 -0
  38. package/src/core/handlers/field/mount.d.ts +2 -0
  39. package/src/core/handlers/field/validations.d.ts +4 -0
  40. package/src/core/handlers/field/visibilityConditions.d.ts +4 -0
  41. package/src/core/handlers/flows.d.ts +36 -0
  42. package/src/core/handlers/form/hooks.d.ts +3 -0
  43. package/src/core/handlers/form/steps.d.ts +2 -0
  44. package/src/core/handlers/form/templating.d.ts +2 -0
  45. package/src/core/handlers/form/validate.d.ts +2 -0
  46. package/src/core/handlers/form/visibilityConditions.d.ts +4 -0
  47. package/src/core/index.d.ts +3 -0
  48. package/src/core/managers/Base.d.ts +19 -0
  49. package/src/core/managers/Factory.d.ts +52 -0
  50. package/src/core/managers/Field.d.ts +31 -0
  51. package/src/core/managers/Form.d.ts +32 -0
  52. package/src/core/managers/Scope.d.ts +12 -0
  53. package/src/core/managers/index.d.ts +1 -0
  54. package/src/core/types/index.d.ts +621 -0
  55. package/src/core/utils/credit-card.d.ts +16 -0
  56. package/src/core/utils/index.d.ts +4 -0
  57. package/src/core/utils/object.d.ts +24 -0
  58. package/src/core/utils/string.d.ts +3 -0
  59. package/src/index.d.ts +3 -0
  60. package/types.js +2 -0
  61. package/types.js.map +1 -0
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Encapsulates in a given object, at a given path the provided value
3
+ *
4
+ * @example
5
+ * ORIGINAL object
6
+ *
7
+ * encapsulateIn({maintain: 'spread_me'}, 'a.b.c','test')
8
+ *
9
+ * RESULT
10
+ *
11
+ * {
12
+ * maintain: 'spread_me',
13
+ * c: 'test
14
+ * }
15
+ * }
16
+ *
17
+ * @param origin - The original object where the new value will be appended
18
+ * @param path - The path at which the new value will be placed
19
+ * @param value - The new value
20
+ * @returns One new object with the new value at the provided path merged with the given object
21
+ */
22
+ declare const encapsulateIn: (origin: Record<string, unknown>, path: string, value: string) => Record<string, unknown>;
23
+ declare const getValueByPath: (object?: Record<string, any>, path?: string) => string | number | boolean | Record<string, unknown>;
24
+ export { encapsulateIn, getValueByPath };
@@ -0,0 +1,3 @@
1
+ declare function hasJsonStructure(str: any): boolean;
2
+ declare const hasTemplate: (value: string) => boolean;
3
+ export { hasJsonStructure, hasTemplate };
package/src/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export type { TComponent, TField, TSchema, TFormRefActions, TFormValues, THookPayload, TEventsKeys, TObserverData, TScope, } from '@core/types';
2
+ export { TAvailableHooks, CoreEvents, ALL_NAMESPACE_EVENTS, BUILD_EVENT } from '@core';
3
+ export { Form, FormProvider, useForm, asFormField } from '@adapters/react';
package/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";var e;Object.defineProperty(exports,"__esModule",{value:!0}),exports.TAvailableHooks=void 0,(e=exports.TAvailableHooks||(exports.TAvailableHooks={})).preMount="preMount",e.preUnmount="preUnmount",e.preSubmit="preSubmit",e.postSubmit="postSubmit";
2
+ //# sourceMappingURL=types.js.map
package/types.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sources":["../../../libs/form-engine/src/core/types/index.ts"],"sourcesContent":["import { ICustomValidationValue } from '@core/apis/validations';\nimport { EEVents } from '@core/constants';\nimport { TEventInformation } from '@core/events';\n\nexport type TObserverData = {\n data: any;\n event: TEventsKeys;\n namespace: string;\n payload: any;\n};\n\nexport type TObservable = (data: any, unsubscribe: () => void) => any;\n\nexport type TEvents = Record<string, TObservable[]>;\n\nexport interface IObservable {\n events: TEvents;\n}\n\nexport type TScopeNamespaces =\n | 'global'\n | 'api'\n | 'hooks'\n | 'fields'\n | 'configs';\n\nexport type TSetGlobalScope = {\n namespace: TScopeNamespaces;\n key?: string;\n data: any;\n merge?: boolean;\n};\nexport type TScope = {\n global?: any;\n api?: any;\n hooks?: any;\n configs?: TConfigs & {\n disable?: boolean;\n enableLogging?: boolean;\n };\n fields?: any;\n};\n\nexport type TMapper = Record<string, Record<string, unknown>>;\n\nexport type TMaskGeneric = {\n to: number;\n from: number;\n mask: string;\n}[];\n\ntype TCurrencyMask = {\n locale: string;\n currency: string;\n};\n\nexport type TComponentMasks = {\n generic?: TMaskGeneric;\n cardNumber?: boolean;\n hideCardNumber?: boolean;\n cardMask?: boolean;\n cardDate?: boolean;\n currencyMask?: TCurrencyMask;\n feinMask?: boolean;\n replaceAll?: string | number;\n};\n\ntype TFormMessages = Record<\n string,\n { name: string; value?: any; values?: string[]; required?: boolean }\n>;\n\nexport type TChildrenOptions = {\n childrenScope?: string[];\n blurredChildren?: string[];\n changedChildren?: string[];\n scopeBlurredChildren?: boolean;\n scopeChangedChildren?: boolean;\n};\n\nexport type TFieldData = Record<string, TField>;\n\nexport type TFormRefActions = {\n submit(): void;\n stepForward(): TFormValues;\n stepBack(): TFormValues;\n validateForm(opts?: TChildrenOptions): Promise<TFormValues>;\n values(\n opts: Pick<\n TChildrenOptions,\n 'scopeBlurredChildren' | 'scopeChangedChildren' | 'childrenScope'\n >,\n ): TFormValues;\n};\n\nexport type THookPayload = {\n setScope(data: TSetGlobalScope): void;\n};\n\nexport type THooks = {\n preMount?: () => Record<string, unknown>;\n preUnmount?: () => Record<string, unknown>;\n preSubmit?: (formData: TFormValues) => Record<string, unknown>;\n posSubmit?: (formData: TFormValues) => Record<string, unknown>;\n};\n\nexport type TIVars = Record<string, unknown>;\n\nexport type TEventsKeys = keyof typeof EEVents;\nexport enum TAvailableHooks {\n 'preMount' = 'preMount',\n 'preUnmount' = 'preUnmount',\n 'preSubmit' = 'preSubmit',\n 'postSubmit' = 'postSubmit',\n}\nexport type TField = {\n value?: string | number | boolean;\n errors?: TErrors;\n visible?: boolean;\n failedErrorMessages?: string[];\n mask?: string;\n changed?: boolean;\n blured?: boolean;\n focused?: boolean;\n name: string;\n mounted: boolean;\n metadata?: any;\n ignore?: boolean;\n schemaLocation?: {\n step: number;\n depth: number;\n index: number;\n };\n};\n\ntype TPathError = Omit<TSchemaValidation, 'path'> & {\n path?: string;\n paths?: string[];\n preventUnMountValidation?: boolean;\n};\n\nexport type TValidationDateFormats =\n | 'MMDDYYYY'\n | 'DDMMYYYY'\n | 'YYYYMMDD'\n | 'YYYYDDMM'\n | 'timestamp';\nexport type TValidationDateOperators = '<' | '>' | '===' | '>=' | '<=' | '!==';\n\nexport type TVAvailableValitationConditionsSet = {\n forceDefinedOrigin?: boolean;\n forceDefinedTarget?: boolean;\n origin?: string | number | boolean;\n condition: TValidationDateOperators;\n target?: string | number | boolean;\n};\n\nexport type TVAvailableValitationConditions = {\n rule: 'and' | 'or';\n set: TVAvailableValitationConditionsSet[];\n};\n\nexport type TVAvailableValidations = {\n /**\n * Validation based on conditions\n *\n * @example - Compare own field to two. Origin and target default to field value\n * ```\n * conditions: {\n rule: 'and',\n set: [\n {\n condition: '===',\n target: '2',\n },\n {\n origin: '2',\n condition: '===',\n },\n ],\n },\n * ```\n * @example - Binded to Postcode field value. Must be greater than or equal two\n * ```\n * conditions: {\n rule: 'or',\n set: [\n {\n origin: '${fields.postcode.value}',\n condition: '>',\n target: '2',\n },\n {\n origin: '${fields.postcode.value}',\n condition: '===',\n target: '2',\n },\n ],\n },\n * ```\n * @example - Binded to Postcode field value. Must be equal to two\n * ```\n * conditions: {\n rule: 'or',\n set: [\n {\n origin: '${fields.postcode.value}',\n condition: '===',\n target: '2',\n },\n ],\n },\n * ```\n */\n conditions?: TVAvailableValitationConditions;\n /**\n * Dates validations\n *\n * @example - Dates should be different\n * ```\n * date: {\n operator: '!==',\n origin: {\n format: 'DDMMYYYY',\n },\n target: {\n format: 'DDMMYYYY',\n value: '10/10/2001',\n },\n },\n * ```\n *\n * @example - Compare only valid dates using intervals\n * ```\n * date: {\n * onlyValidDate: true,\n * operator: '<',\n * origin: {\n * format: 'DD/MM/YYYY',\n * intervals: {\n * years: 1,\n * },\n * },\n * },\n * ```\n *\n * @example - Should have at max 85 years\n * ```\n * {\n operator: '>',\n origin: {\n format: 'DDMMYYYY',\n value: eightyFiveYearsDate,\n intervals: {\n years: 85,\n },\n },\n }\n * }\n *\n * ```\n */\n date?: {\n /**\n * Flag to force only valid dates. Valid dates must be of min length of 8\n */\n onlyValidDate?: boolean;\n /**\n * List of operations you can do\n * - between origin and target dates\n * - Betweeen origin and intervals\n */\n operator: TValidationDateOperators;\n /**\n * The origin configurations\n */\n origin: {\n /**\n * Origin date value\n */\n value?: string | number;\n /**\n * The available date formats\n */\n format: TValidationDateFormats;\n /**\n * Intervals to compare with the original date.\n *\n * It will use todays date for comparison.\n *\n * @example\n *\n * origin date = 10/10/2022\n * interval.year = 1\n * operator = '==='\n *\n * It will compare (10/10/2022 + 1) with the current date and check if they are the same\n */\n intervals?: {\n years?: number;\n months?: number;\n days?: number;\n };\n };\n target?: {\n value: string | number;\n format: TValidationDateFormats;\n };\n };\n /**\n * Allow to define a maximum length for the input to have no error\n */\n length?: number;\n /**\n * Will look into the input length and send an error if if not greater than this value\n */\n greaterThan?: number | string;\n regex?: string;\n maxLength?: number;\n minLength?: number;\n required?: boolean;\n onlyLetters?: boolean;\n value?: string | number | boolean;\n notEmpty?: boolean;\n callback?(value: string | number): { fail: boolean; errorMessage?: string };\n numericRange?: { start: number | string; end: number | string };\n isNumber?: boolean;\n hasNoExtraSpaces?: boolean;\n email?: boolean;\n lessThan?: number | string;\n sequentialNumber?: boolean;\n repeatedNumbers?: boolean;\n url?: boolean;\n path?: TPathError;\n isCreditCard?: string[];\n isCreditCardAndLength?: string[];\n isCreditCodeMatch?: { numberCard: string; availableOptions: string[] };\n customValidation?: ICustomValidationValue[];\n notAllowSpaces?: true;\n isInTheList?: string[] | number[] | string;\n fields?: {\n rule: 'every';\n set: {\n bind: string;\n fieldName: string;\n validations: Omit<TVAvailableValidations, 'fields'>;\n }[];\n };\n};\n\nexport type TGenericValidationRule = {\n [key: string]: TVAvailableValidations;\n};\n\nexport type TSchemaValidation = TVAvailableValidations | TGenericValidationRule;\n\nexport type TStepData = {\n navigated: boolean;\n index: number;\n data: Record<\n number,\n Pick<\n TFormValues,\n 'fields' | 'erroredFields' | 'formatted' | 'filteredFields'\n >\n >;\n currentStepSchema?: TComponent;\n isValid: boolean;\n numSteps?: number;\n};\n\nexport type TFormValues = {\n predictableErroredFields: string[];\n erroredFields: string[];\n fields: TFieldData;\n formatted: Record<string, unknown>;\n filteredFields?: Record<string, unknown>;\n form: {\n steps: TStepData;\n isValid: boolean;\n messages: string[];\n scope: TScope;\n };\n};\n\nexport type TError = {\n value: unknown;\n message?: string;\n fail: boolean;\n};\n\nexport type TErrors = Record<string, TError> | undefined;\n\n// TODO - object with the available errors has props\nexport type TErrorMessages = Record<string, string>;\n\nexport type TSchemaValidations = TSchemaHandler<TSchemaValidation>;\n\ntype TFormLevelSchemaHandler<T> = Partial<\n Record<\n | EEVents.ON_FIELD_CHANGE\n | EEVents.ON_FORM_REHYDRATE\n | EEVents.ON_FORM_MOUNT\n | EEVents.ON_SCOPE_CHANGE,\n T\n >\n>;\n\ntype TSchemaHandler<T> = Partial<\n Record<\n | EEVents.ON_FIELD_REHYDRATE\n | EEVents.ON_FIELD_CHANGE\n | EEVents.ON_FIELD_BLUR\n | EEVents.ON_FIELD_MOUNT\n | EEVents.ON_FIELD_FOCUS,\n T\n >\n>;\n\nexport type TSchemaVisibilityConditions = {\n /**\n * The validations that will say if the target field will be visible or not\n *\n * Those validations will run against your field (the one that has this directive)\n */\n validations: TSchemaValidation;\n /**\n * Target field that will have the visibility toggled\n */\n fieldName?: string;\n /**\n * Target fields that will have the visibility toggled\n */\n fieldNames?: string[];\n}[];\n\nexport type TSchemaClearFields = {\n /**\n * Validations to run for the target field\n */\n validations?: TSchemaValidation;\n /**\n * target fields that will be used in the validation specified\n */\n fields: string[];\n /**\n * The cleared values on the fields in case they do no pass the validation\n */\n clearedValue: string | number | boolean;\n}[];\n\nexport type TSchemaApi = {\n /**\n * Specify a debounce time in miliseconds for your call\n */\n debounceTime?: number;\n method: 'GET' | 'POST' | 'PUT' | 'PATCH';\n url: string;\n headers?: HeadersInit | undefined;\n body?: Record<string, any>;\n scope?: string;\n blockRequestWhenInvalid?: boolean;\n preConditions?: TSchemaValidation;\n}[];\n\nexport type TSchemaMasks = { cleanMask?: boolean } & TComponentMasks;\n\nexport type TFormEventDirectives = {\n visibilityConditions: TSchemaVisibilityConditions;\n};\n\nexport type TEventReducedSchema = {\n api: TSchemaApi;\n clearFields: TSchemaClearFields;\n formatters: TSchemaFormatters;\n masks: TSchemaMasks;\n validations: TSchemaValidation;\n visibilityConditions: TSchemaVisibilityConditions;\n};\n\nexport type TComponent = {\n /**\n * This name will be used latter to corelate the field with the value and you will be able to read it\n *\n * You can also mount here complex objects like\n *\n * name={a.b.c}\n *\n * that will result in\n * a: {\n * b: {\n * c: 'field_value'\n * }\n * }\n */\n name: string;\n /**\n * A component name that should map to the one you gave in the from mappings\n *\n * @example - For React\n * ```\n * const mappings: TMapper = {\n * input: { component: Input },\n * formGroup: { component: FormGroup },\n * };\n * ```\n *\n * The component name for the Input element will be \"input\" ending up like\n * ```\n * {\n * component: 'input'\n * }\n * ```\n */\n component: string;\n /**\n * You can attach metadata to your component that will be forwarded to you after on your frontend\n * callback methods link `onData`, `onBlur` etc\n */\n metadata?: Record<string, any>;\n /**\n * This property allows you to specify a virtual fieldName that can be in several fields\n *\n * USAGE - You have two fields but you want to have the value stored only under one name\n *\n */\n group?: string;\n wrapper?: new () => React.Component;\n children?: TComponent[];\n /**\n * Field error messages in case any validation fails\n *\n * @example - Set a default error message for all the validations\n *\n * ```\n * {\n * errorMessages: {\n * default: 'default error message'\n * }\n * }\n * ```\n *\n * @example - Set error message for a given validation\n *\n * ```\n * {\n * validations: {\n * ON_FIELD_CHANGE: {\n * required: true\n * }\n * },\n * errorMessages: {\n * required: 'This field is required'\n * }\n * }\n * ```\n *\n */\n errorMessages?: TErrorMessages;\n type?: 'text' | 'number' | string;\n /**\n * Allow you to set a initial state for this field\n */\n state?: {\n /**\n * Hide the field\n */\n hidden?: boolean;\n /**\n * Ignore value from field\n */\n ignoreValue?: boolean;\n };\n /**\n * This key lets you inject props directly into the component\n */\n props?: Record<string, unknown>;\n /**\n * Setup validations to be used in the field.\n *\n * You can setup validations in several life-cycle methods\n *\n * @example - Required validation\n * ```\n * validations: {\n ON_FIELD_CHANGE: {\n required: true,\n },\n },\n * ```\n */\n validations?: TSchemaHandler<\n Pick<TEventReducedSchema, 'validations'>['validations']\n >;\n filter?: Pick<TEventReducedSchema, 'validations'>['validations'];\n /**\n * Allows you to specify the conditions a given field will be visible\n * what will run when this field meets the specified life-cycle\n */\n visibilityConditions?: TSchemaHandler<\n Pick<TEventReducedSchema, 'visibilityConditions'>['visibilityConditions']\n >;\n /**\n * Will clear target fields in case they do not pass with the specified validations\n */\n clearFields?: TSchemaHandler<\n Pick<TEventReducedSchema, 'clearFields'>['clearFields']\n >;\n api?: TSchemaHandler<Pick<TEventReducedSchema, 'api'>['api']>;\n masks?: Partial<\n Record<\n | EEVents.ON_FIELD_BLUR\n | EEVents.ON_FIELD_MOUNT\n | EEVents.ON_FIELD_FOCUS\n | EEVents.ON_FIELD_CHANGE,\n Pick<TEventReducedSchema, 'masks'>['masks']\n >\n >;\n formatters?: TSchemaHandler<\n Pick<TEventReducedSchema, 'formatters'>['formatters']\n >;\n};\n\nexport type TStep = {\n component: string;\n name: string;\n children: TComponent[];\n props?: Record<string, string>;\n};\n\nexport type TComponentPropsMapping = {\n getValue?: string;\n setValue?: string;\n onBlur?: string;\n onFocus?: string;\n setErrorMessage?: string;\n setErrorState?: string;\n};\n\nexport type TPropsMapping = Record<string, TComponentPropsMapping>;\n\nexport type TSchema = {\n action?: string;\n method?: string;\n /**\n * Give some configurations to the form\n */\n configs?: TConfigs;\n /**\n * Allows you to expose some messages to the outside world when something happen\n */\n messages?: TFormMessages;\n /**\n * Specify some static field defaults before rendereing the form.\n *\n * If you have initialValues in the frontend setted, those will ovewride this\n */\n formattedDataDefaults?: Record<string, unknown>;\n /**\n * Form level props mapping\n */\n propsMapping?: TPropsMapping;\n visibilityConditions?: TFormLevelSchemaHandler<TSchemaVisibilityConditions>;\n /**\n * Entry point for the form should be a step (even if you have only one)\n */\n components: TStep[];\n /**\n * You can have many fields in the form, but be interested only in some\n *\n * You can put here the fields you want to read onSubmit for example\n *\n * You will receive those in TFormValues.filteredFields\n */\n filteredFields?: string[];\n /**\n * Schema level iVars.\n *\n * These iVars will go to the global scope namespace\n */\n iVars?: TIVars;\n};\n\nexport type TSplitterFormatterValue = {\n value: string;\n position: number;\n}[];\n\nexport type TSchemaFormatters = {\n splitter?: TSplitterFormatterValue;\n capitalize?: boolean;\n upperCase?: boolean;\n gapsCreditCard?: string[];\n};\n\nexport type TConfigs = {\n observables?: { templates?: { exclude?: string[] } };\n};\n\nexport type TFlowType = {\n [x: string]: {\n events: (component?: TComponent) => EEVents[];\n handler: (args: TEventInformation) => void;\n }[];\n};\n"],"names":["TAvailableHooks"],"mappings":"aA6GA,IAAYA,yDAAAA,QAKXA,qBAAA,GALWA,EAAAA,QAAeA,kBAAfA,wBAKX,CAAA,IAJC,SAAA,WACAA,EAAA,WAAA,aACAA,EAAA,UAAA,YACAA,EAAA,WAAA"}