astro-reactive-form/packages/astro-reactive-form/client/controls.ts
Fazza Razaq Amiarso 2fd25b7d93
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
2022-10-07 07:35:01 +02:00

56 lines
1.6 KiB
TypeScript

import { FormControl, FormGroup } from '../core';
import type { FormControlType } from '../core/form-control-types';
export const getFormGroup = (formName: string) => {
const fieldSetEl = (document.getElementById(formName) as HTMLFieldSetElement) || null;
if (fieldSetEl === null) {
console.error(`Formgroup with name: '${formName}' doesn't exist!`);
return undefined;
}
const formGroup = new FormGroup([], formName);
fieldSetEl.querySelectorAll('input').forEach((field) => {
const formControl = getFormControl(field.name);
if (!formControl) return;
formGroup.controls.push(formControl);
});
return formGroup;
};
const getFormControl = (name: string) => {
const inputEl = document.getElementById(name) as HTMLInputElement | null;
if (inputEl === null) {
console.error(`Input with name: ${name} doesn't exist!`);
return undefined;
}
const formControl = new FormControl({
name: inputEl.name,
value: inputEl.value,
type: inputEl.type as FormControlType,
label: inputEl.dataset.label as string,
labelPosition: inputEl.dataset.labelPosition as 'right' | 'left',
});
inputEl.addEventListener('change', (e) => {
if (!(e.target instanceof HTMLInputElement)) return;
let value = e.target.value;
if (formControl.type === 'checkbox') {
value = formControl.value === 'checked' ? '' : 'checked';
}
formControl.setValue(value);
formControl.isPristine = false;
});
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;
};