From 2fd25b7d93b7b7354ca8e4ce173490fda657a21f Mon Sep 17 00:00:00 2001 From: Fazza Razaq Amiarso <86255053+fazzaamiarso@users.noreply.github.com> Date: Fri, 7 Oct 2022 12:35:01 +0700 Subject: [PATCH] feat: initial form control state (#60) * Form control tries * feat: implement form-control state * fix: organize client methods * fix(client): return undefined instead of throwing error * fix: fix exports and lint * fix: Change implementation of setting isPristine with proxy --- demo/src/pages/index.astro | 8 -------- packages/astro-reactive-form/client/controls.ts | 13 +++++++++++-- packages/astro-reactive-form/core/form-control.ts | 4 ++++ packages/astro-reactive-form/index.ts | 2 -- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/demo/src/pages/index.astro b/demo/src/pages/index.astro index 783f577..4824091 100644 --- a/demo/src/pages/index.astro +++ b/demo/src/pages/index.astro @@ -27,14 +27,6 @@ form.controls.push( }) ); -//insert submit button -form.controls.push( - new FormControl({ - type: "submit", - name: "submit", - }) -); - // get the FormControl object const userNameControl = form.get("username"); diff --git a/packages/astro-reactive-form/client/controls.ts b/packages/astro-reactive-form/client/controls.ts index a73c4f5..227e034 100644 --- a/packages/astro-reactive-form/client/controls.ts +++ b/packages/astro-reactive-form/client/controls.ts @@ -41,7 +41,16 @@ const getFormControl = (name: string) => { value = formControl.value === 'checked' ? '' : 'checked'; } formControl.setValue(value); - formControl.setIsPristine(false); + formControl.isPristine = false; }); - return formControl; + + const controlProxy = new Proxy(formControl, { + set() { + //prevent any setter to be able to work in client; + console.error('Setting this property manually is prohibited!'); + return true; + }, + }); + + return controlProxy; }; diff --git a/packages/astro-reactive-form/core/form-control.ts b/packages/astro-reactive-form/core/form-control.ts index b427470..cd66881 100644 --- a/packages/astro-reactive-form/core/form-control.ts +++ b/packages/astro-reactive-form/core/form-control.ts @@ -55,6 +55,10 @@ export class FormControl { return this._isPristine; } + set isPristine(value: boolean) { + this._isPristine = value; + } + get isValid() { return this._isValid; } diff --git a/packages/astro-reactive-form/index.ts b/packages/astro-reactive-form/index.ts index 0c9b0b8..080add5 100644 --- a/packages/astro-reactive-form/index.ts +++ b/packages/astro-reactive-form/index.ts @@ -1,5 +1,3 @@ import Form from './Form.astro'; export default Form; -export * from './core/form-control-types'; -export * from './client'; export * from './Form.astro';