feature-form 0.0.60 → 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 (62) hide show
  1. package/MIGRATION.md +153 -0
  2. package/README.md +390 -68
  3. package/dist/cjs/create-form-field.js +1 -0
  4. package/dist/cjs/create-form.js +1 -1
  5. package/dist/cjs/features/dirty.js +1 -0
  6. package/dist/cjs/index.js +1 -1
  7. package/dist/cjs/lib/deep-copy.js +1 -0
  8. package/dist/cjs/lib/standard-schema.js +1 -0
  9. package/dist/cjs/lib/validation-status.js +1 -0
  10. package/dist/esm/create-form-field.js +1 -0
  11. package/dist/esm/create-form.js +1 -1
  12. package/dist/esm/features/dirty.js +1 -0
  13. package/dist/esm/index.js +1 -1
  14. package/dist/esm/lib/deep-copy.js +1 -0
  15. package/dist/esm/lib/standard-schema.js +1 -0
  16. package/dist/esm/lib/validation-status.js +1 -0
  17. package/dist/types/create-form-field.d.ts +18 -0
  18. package/dist/types/create-form.d.ts +48 -29
  19. package/dist/types/features/dirty.d.ts +33 -0
  20. package/dist/types/features/index.d.ts +1 -0
  21. package/dist/types/index.d.ts +2 -4
  22. package/dist/types/lib/deep-copy.d.ts +2 -0
  23. package/dist/types/lib/index.d.ts +3 -0
  24. package/dist/types/lib/standard-schema.d.ts +4 -0
  25. package/dist/types/lib/validation-status.d.ts +3 -0
  26. package/dist/types/types.d.ts +198 -0
  27. package/package.json +25 -10
  28. package/dist/cjs/form-field/create-form-field-validation-context.js +0 -1
  29. package/dist/cjs/form-field/create-form-field.js +0 -1
  30. package/dist/cjs/form-field/create-status.js +0 -1
  31. package/dist/cjs/form-field/is-form-field-status.js +0 -1
  32. package/dist/cjs/form-field/is-form-field.js +0 -1
  33. package/dist/cjs/helper/from-validator.js +0 -1
  34. package/dist/cjs/helper/has-form-changed.js +0 -1
  35. package/dist/cjs/helper/reset-form-submitted.js +0 -1
  36. package/dist/cjs/is-form-with-features.js +0 -1
  37. package/dist/cjs/types/form-field.js +0 -1
  38. package/dist/esm/form-field/create-form-field-validation-context.js +0 -1
  39. package/dist/esm/form-field/create-form-field.js +0 -1
  40. package/dist/esm/form-field/create-status.js +0 -1
  41. package/dist/esm/form-field/is-form-field-status.js +0 -1
  42. package/dist/esm/form-field/is-form-field.js +0 -1
  43. package/dist/esm/helper/from-validator.js +0 -1
  44. package/dist/esm/helper/has-form-changed.js +0 -1
  45. package/dist/esm/helper/reset-form-submitted.js +0 -1
  46. package/dist/esm/is-form-with-features.js +0 -1
  47. package/dist/esm/types/form-field.js +0 -1
  48. package/dist/types/form-field/create-form-field-validation-context.d.ts +0 -2
  49. package/dist/types/form-field/create-form-field.d.ts +0 -7
  50. package/dist/types/form-field/create-status.d.ts +0 -2
  51. package/dist/types/form-field/index.d.ts +0 -4
  52. package/dist/types/form-field/is-form-field-status.d.ts +0 -2
  53. package/dist/types/form-field/is-form-field.d.ts +0 -2
  54. package/dist/types/helper/from-validator.d.ts +0 -3
  55. package/dist/types/helper/has-form-changed.d.ts +0 -2
  56. package/dist/types/helper/index.d.ts +0 -3
  57. package/dist/types/helper/reset-form-submitted.d.ts +0 -2
  58. package/dist/types/is-form-with-features.d.ts +0 -3
  59. package/dist/types/types/features.d.ts +0 -0
  60. package/dist/types/types/form-field.d.ts +0 -71
  61. package/dist/types/types/form.d.ts +0 -49
  62. package/dist/types/types/index.d.ts +0 -2
@@ -1 +0,0 @@
1
- "use strict";var l=Object.defineProperty,a=Object.getOwnPropertySymbols,i=Object.prototype.hasOwnProperty,n=Object.prototype.propertyIsEnumerable,o=(e,r,t)=>r in e?l(e,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[r]=t,u=(e,r)=>{for(var t in r||(r={}))i.call(r,t)&&o(e,t,r[t]);if(a)for(var t of a(r))n.call(r,t)&&o(e,t,r[t]);return e};function b(e,r){return u({validator:e},r)}exports.fromValidator=b;
@@ -1 +0,0 @@
1
- "use strict";function l(n){var i,r;for(const e in n.fields)if(((i=n.fields[e])==null?void 0:i.get())!==((r=n.fields[e])==null?void 0:r._intialValue))return!0;return!1}exports.hasFormChanged=l;
@@ -1 +0,0 @@
1
- "use strict";function s(e){e.isSubmitted.set(!1),e.isSubmitting.set(!1);for(const t of Object.values(e.fields))t.isSubmitted.set(!1),t.isSubmitting.set(!1)}exports.resetFormSubmitted=s;
@@ -1 +0,0 @@
1
- "use strict";function s(r,e){return typeof r=="object"&&r!=null&&"_features"in r&&Array.isArray(r._features)&&e.every(t=>r._features.includes(t))}exports.isFormWithFeatures=s;
@@ -1 +0,0 @@
1
- "use strict";var e=(n=>(n[n.OnBlur=1]="OnBlur",n[n.OnChange=2]="OnChange",n[n.OnSubmit=4]="OnSubmit",n[n.OnTouched=8]="OnTouched",n))(e||{}),d=(n=>(n[n.OnBlur=1]="OnBlur",n[n.OnChange=2]="OnChange",n[n.OnSubmit=4]="OnSubmit",n))(d||{});exports.FormFieldReValidateMode=d,exports.FormFieldValidateMode=e;
@@ -1 +0,0 @@
1
- function t(r){return{config:{collectErrorMode:r._config.collectErrorMode,name:r.key},value:r.get(),isValue(e){return!0},hasError(){var e;return((e=r.status._nextValue)==null?void 0:e.type)==="INVALID"},registerError(e){r.status.registerNextError({code:e.code,message:e.message,path:e.path})}}}export{t as createFormFieldValidationContext};
@@ -1 +0,0 @@
1
- import{bitwiseFlag as v,deepCopy as y,withNew as b}from"@blgc/utils";import{createState as a}from"feature-state";import{createValidator as M}from"validation-adapter";import{FormFieldReValidateMode as m,FormFieldValidateMode as o}from"../types/form-field.js";import{createFormFieldValidationContext as S}from"./create-form-field-validation-context.js";import{createStatus as O}from"./create-status.js";var V=(l,d,i)=>new Promise((n,r)=>{var h=t=>{try{s(i.next(t))}catch(e){r(e)}},u=t=>{try{s(i.throw(t))}catch(e){r(e)}},s=t=>t.done?n(t.value):Promise.resolve(t.value).then(h,u);s((i=i.apply(l,d)).next())});function F(l,d){const{key:i,validator:n=M([]),editable:r=!0,reValidateMode:h=v(m.OnBlur),validateMode:u=v(o.OnSubmit),collectErrorMode:s="firstError",notifyOnStatusChange:t=!0}=d,e=a(l),p={_config:{editable:r,validateMode:u,reValidateMode:h,collectErrorMode:s},_intialValue:y(e._v),_validator:n,key:i,isTouched:a(!1),isSubmitted:a(!1),isSubmitting:a(!1),isValidating:a(!1),status:O({type:"UNVALIDATED"}),validate(){return V(this,null,function*(){const f=S(this);return this.isValidating.set(!0),yield this._validator.validate(f),this.isValidating.set(!1),this.status._nextValue==null?this.status.set({type:"VALID"}):this.status.set(this.status._nextValue),this.status._nextValue=void 0,this.status.get().type==="VALID"})},isValid(){return this.status.get().type==="VALID"},blur(){(this.isSubmitted.get()&&this._config.reValidateMode.has(m.OnBlur)||!this.isSubmitted.get()&&(this._config.validateMode.has(o.OnBlur)||this._config.validateMode.has(o.OnTouched)&&!this.isTouched.get()))&&this.validate(),this.isTouched.set(!0)},reset(){this.set(this._intialValue,{listenerContext:{source:"form-field_reset"}}),this.isTouched.set(!1),this.isSubmitted.set(!1),this.isSubmitting.set(!1),this.status.set({type:"UNVALIDATED"})}},g=Object.assign(e,p);return g._features.push("form-field"),b(Object.assign(g,{_new(f){f&&this.status.listen(c=>{e._notify({listenerContext:{source:"form-field_status-change",status:c.value}})},{key:"form-field_status-change"}),this.listen(c=>V(this,[c],function*({source:_}){_==="form-field_reset"||_==="form-field_status-change"||(this.isSubmitted.get()&&this._config.reValidateMode.has(m.OnChange)||!this.isSubmitted.get()&&this._config.validateMode.has(o.OnChange)||this._config.validateMode.has(o.OnTouched)&&this.isTouched.get())&&(yield this.validate())}),{key:"form-field_validate"})}}),t)}export{F as createFormField};
@@ -1 +0,0 @@
1
- import{createState as u}from"feature-state";function o(s){const a=u(s),t=Object.assign(a,{_nextValue:void 0,registerNextError(e){var r;((r=this._nextValue)==null?void 0:r.type)==="INVALID"?this._nextValue.errors.push(e):this._nextValue={type:"INVALID",errors:[e]}}});return t._features.push("form-field-status"),t}export{o as createStatus};
@@ -1 +0,0 @@
1
- function r(e){return typeof e=="object"&&e!=null&&"_features"in e&&Array.isArray(e._features)&&e._features.includes("form-field-status")}export{r as isFormFieldStatus};
@@ -1 +0,0 @@
1
- function r(e){return typeof e=="object"&&e!=null&&"_features"in e&&Array.isArray(e._features)&&e._features.includes("form-field")}export{r as isFormField};
@@ -1 +0,0 @@
1
- var l=Object.defineProperty,a=Object.getOwnPropertySymbols,n=Object.prototype.hasOwnProperty,i=Object.prototype.propertyIsEnumerable,o=(e,r,t)=>r in e?l(e,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[r]=t,p=(e,r)=>{for(var t in r||(r={}))n.call(r,t)&&o(e,t,r[t]);if(a)for(var t of a(r))i.call(r,t)&&o(e,t,r[t]);return e};function u(e,r){return p({validator:e},r)}export{u as fromValidator};
@@ -1 +0,0 @@
1
- function e(n){var i,l;for(const r in n.fields)if(((i=n.fields[r])==null?void 0:i.get())!==((l=n.fields[r])==null?void 0:l._intialValue))return!0;return!1}export{e as hasFormChanged};
@@ -1 +0,0 @@
1
- function s(e){e.isSubmitted.set(!1),e.isSubmitting.set(!1);for(const t of Object.values(e.fields))t.isSubmitted.set(!1),t.isSubmitting.set(!1)}export{s as resetFormSubmitted};
@@ -1 +0,0 @@
1
- function u(r,e){return typeof r=="object"&&r!=null&&"_features"in r&&Array.isArray(r._features)&&e.every(t=>r._features.includes(t))}export{u as isFormWithFeatures};
@@ -1 +0,0 @@
1
- var O=(n=>(n[n.OnBlur=1]="OnBlur",n[n.OnChange=2]="OnChange",n[n.OnSubmit=4]="OnSubmit",n[n.OnTouched=8]="OnTouched",n))(O||{}),u=(n=>(n[n.OnBlur=1]="OnBlur",n[n.OnChange=2]="OnChange",n[n.OnSubmit=4]="OnSubmit",n))(u||{});export{u as FormFieldReValidateMode,O as FormFieldValidateMode};
@@ -1,2 +0,0 @@
1
- import { type TFormField, type TFormFieldValidationContext } from '../types';
2
- export declare function createFormFieldValidationContext<GValue>(formField: TFormField<GValue>): TFormFieldValidationContext<GValue>;
@@ -1,7 +0,0 @@
1
- import { type TFormField, type TFormFieldStateConfig, type TFormFieldValidator } from '../types';
2
- export declare function createFormField<GValue>(initialValue: GValue | undefined, config: TCreateFormFieldConfig<GValue>): TFormField<GValue>;
3
- export interface TCreateFormFieldConfig<GValue> extends Partial<TFormFieldStateConfig> {
4
- key: string;
5
- validator?: TFormFieldValidator<GValue>;
6
- notifyOnStatusChange?: boolean;
7
- }
@@ -1,2 +0,0 @@
1
- import { type TFormFieldStatus, type TFormFieldStatusValue } from '../types';
2
- export declare function createStatus(initialValue: TFormFieldStatusValue): TFormFieldStatus;
@@ -1,4 +0,0 @@
1
- export * from './create-form-field';
2
- export * from './create-status';
3
- export * from './is-form-field';
4
- export * from './is-form-field-status';
@@ -1,2 +0,0 @@
1
- import { TFormFieldStatus } from '../types';
2
- export declare function isFormFieldStatus(value: unknown): value is TFormFieldStatus;
@@ -1,2 +0,0 @@
1
- import { type TFormField } from '../types';
2
- export declare function isFormField<GValue = unknown>(value: unknown): value is TFormField<GValue>;
@@ -1,3 +0,0 @@
1
- import { type TCreateFormConfigFormField } from '../create-form';
2
- import { type TFormFieldValidator } from '../types';
3
- export declare function fromValidator<GValue>(validator: TFormFieldValidator<GValue>, config: Omit<TCreateFormConfigFormField<GValue>, 'validator'>): TCreateFormConfigFormField<GValue>;
@@ -1,2 +0,0 @@
1
- import { type TForm } from '../types';
2
- export declare function hasFormChanged(form: TForm<any, any>): boolean;
@@ -1,3 +0,0 @@
1
- export * from './from-validator';
2
- export * from './has-form-changed';
3
- export * from './reset-form-submitted';
@@ -1,2 +0,0 @@
1
- import { type TForm } from '../types';
2
- export declare function resetFormSubmitted(form: TForm<any, any>): void;
@@ -1,3 +0,0 @@
1
- import { TFeatureDefinition, TLooseFeatureNames } from '@blgc/types/features';
2
- import { TForm, TFormData } from './types';
3
- export declare function isFormWithFeatures<GFormData extends TFormData, GFeatures extends TFeatureDefinition[] = []>(value: unknown, features: TLooseFeatureNames<GFeatures>[]): value is TForm<GFormData, GFeatures>;
File without changes
@@ -1,71 +0,0 @@
1
- import { type BitwiseFlag } from '@blgc/utils';
2
- import { type TState } from 'feature-state';
3
- import { type TBaseValidationContext, type TCollectErrorMode, type TValidator } from 'validation-adapter';
4
- export type TFormField<GValue> = TState<GValue | undefined, [TFormFieldStateFeature<GValue>]>;
5
- export interface TFormFieldStateFeature<GValue> {
6
- key: 'form-field';
7
- api: {
8
- _config: TFormFieldStateConfig;
9
- _intialValue: GValue | undefined;
10
- _validator: TFormFieldValidator<GValue>;
11
- key: string;
12
- isTouched: TState<boolean, []>;
13
- isSubmitted: TState<boolean, []>;
14
- isSubmitting: TState<boolean, []>;
15
- isValidating: TState<boolean, []>;
16
- status: TFormFieldStatus;
17
- validate: () => Promise<boolean>;
18
- isValid: () => boolean;
19
- blur: () => void;
20
- reset: () => void;
21
- };
22
- }
23
- export interface TFormFieldStateConfig {
24
- editable: boolean;
25
- /**
26
- * Validation strategy before submitting.
27
- */
28
- validateMode: BitwiseFlag<FormFieldValidateMode>;
29
- /**
30
- * Validation strategy after submitting.
31
- */
32
- reValidateMode: BitwiseFlag<FormFieldReValidateMode>;
33
- collectErrorMode: TCollectErrorMode;
34
- }
35
- export declare enum FormFieldValidateMode {
36
- OnBlur = 1,// 1
37
- OnChange = 2,// 2
38
- OnSubmit = 4,// 4
39
- OnTouched = 8
40
- }
41
- export declare enum FormFieldReValidateMode {
42
- OnBlur = 1,// 1
43
- OnChange = 2,// 2
44
- OnSubmit = 4
45
- }
46
- export type TFormFieldStatus = TState<TFormFieldStatusValue, [TFormFielStatusStateFeature]>;
47
- export interface TFormFielStatusStateFeature {
48
- key: 'form-field-status';
49
- api: {
50
- _nextValue?: TFormFieldStatusValue;
51
- registerNextError: (error: TInvalidFormFieldError) => void;
52
- };
53
- }
54
- export type TFormFieldStatusValue = TInvalidFormFieldStatus | TValidFormFieldStatus | TUnvalidatedFormFieldStatus;
55
- export interface TInvalidFormFieldStatus {
56
- type: 'INVALID';
57
- errors: TInvalidFormFieldError[];
58
- }
59
- export interface TValidFormFieldStatus {
60
- type: 'VALID';
61
- }
62
- export interface TUnvalidatedFormFieldStatus {
63
- type: 'UNVALIDATED';
64
- }
65
- export interface TInvalidFormFieldError {
66
- code: string;
67
- message?: string;
68
- path?: string;
69
- }
70
- export type TFormFieldValidator<GValue> = TValidator<GValue, TFormFieldValidationContext<GValue>>;
71
- export type TFormFieldValidationContext<GValue> = TBaseValidationContext<GValue>;
@@ -1,49 +0,0 @@
1
- import { TFeatureDefinition, TWithFeatures } from '@blgc/types/features';
2
- import { type TState } from 'feature-state';
3
- import { type TFormField, type TInvalidFormFieldError } from './form-field';
4
- export type TForm<GFormData extends TFormData, GFeatures extends TFeatureDefinition[]> = TWithFeatures<{
5
- _config: TFormConfig;
6
- _validSubmitCallbacks: TValidSubmitCallback<GFormData>[];
7
- _invalidSubmitCallbacks: TInvalidSubmitCallback<GFormData>[];
8
- fields: TFormFields<GFormData>;
9
- isValid: TState<boolean, []>;
10
- isValidating: TState<boolean, []>;
11
- isSubmitted: TState<boolean, []>;
12
- isSubmitting: TState<boolean, []>;
13
- _revalidate: (cached?: boolean) => Promise<boolean>;
14
- submit: (options?: TSubmitOptions<GFormData, GFeatures>) => Promise<boolean>;
15
- validate: () => Promise<boolean>;
16
- getField: <GKey extends keyof TFormFields<GFormData>>(key: GKey) => TFormFields<GFormData>[GKey];
17
- getValidData: () => Readonly<GFormData> | null;
18
- getErrors: () => TInvalidFormFieldErrors<GFormData>;
19
- reset: () => void;
20
- }, GFeatures>;
21
- export type TFormFields<GFormData extends TFormData> = {
22
- [Key in keyof GFormData]: TFormField<GFormData[Key]>;
23
- };
24
- export type TFormData = Record<string, any>;
25
- export interface TSubmitOptions<GFormData extends TFormData, GFeatures extends TFeatureDefinition[]> {
26
- onValidSubmit?: TValidSubmitCallback<GFormData>;
27
- onInvalidSubmit?: TInvalidSubmitCallback<GFormData>;
28
- postSubmitCallback?: TPostSubmitCallback<GFormData, GFeatures>;
29
- context?: TSubmitContext;
30
- assignToInitial?: boolean;
31
- }
32
- export type TValidSubmitCallback<GFormData extends TFormData> = (formData: Readonly<GFormData>, context?: TSubmitContext) => TSubmitCallbackResponse;
33
- export type TInvalidSubmitCallback<GFormData extends TFormData> = (errors: TInvalidFormFieldErrors<GFormData>, context?: TSubmitContext) => TSubmitCallbackResponse;
34
- export type TSubmitCallbackResponse = Promise<void | TSubmitData> | void | TSubmitData;
35
- export type TSubmitData = Record<string, unknown>;
36
- export type TPostSubmitCallback<GFormData extends TFormData, GFeatures extends TFeatureDefinition[]> = (form: TForm<GFormData, GFeatures>, submitData: TSubmitData) => void;
37
- export interface TSubmitContext {
38
- [key: string]: unknown;
39
- event?: unknown;
40
- }
41
- export type TInvalidFormFieldErrors<GFormData extends TFormData> = {
42
- [Key in keyof GFormData]?: readonly TInvalidFormFieldError[];
43
- };
44
- export interface TFormConfig {
45
- /**
46
- * Indicates if the form is disabled.
47
- */
48
- disabled: boolean;
49
- }
@@ -1,2 +0,0 @@
1
- export * from './form';
2
- export * from './form-field';