feat: new type for control configuration ControlConfig
This commit is contained in:
parent
7db1be51e6
commit
8fe968fbc6
5 changed files with 30 additions and 35 deletions
|
@ -1,5 +1,9 @@
|
||||||
---
|
---
|
||||||
import { FormControl, FormGroup } from "astro-reactive-form/core";
|
import {
|
||||||
|
ControlConfig,
|
||||||
|
FormControl,
|
||||||
|
FormGroup,
|
||||||
|
} from "astro-reactive-form/core";
|
||||||
import Form from "astro-reactive-form";
|
import Form from "astro-reactive-form";
|
||||||
|
|
||||||
const form = new FormGroup([
|
const form = new FormGroup([
|
||||||
|
@ -17,15 +21,15 @@ const form = new FormGroup([
|
||||||
|
|
||||||
form.name = "Simple Form";
|
form.name = "Simple Form";
|
||||||
|
|
||||||
|
const config: ControlConfig = {
|
||||||
|
type: "checkbox",
|
||||||
|
name: "is-awesome",
|
||||||
|
label: "is Awesome?",
|
||||||
|
labelPosition: "right",
|
||||||
|
};
|
||||||
|
|
||||||
// insert a control
|
// insert a control
|
||||||
form.controls.push(
|
form.controls.push(new FormControl(config));
|
||||||
new FormControl({
|
|
||||||
type: "checkbox",
|
|
||||||
name: "is-awesome",
|
|
||||||
label: "is Awesome?",
|
|
||||||
labelPosition: "right",
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
// get the FormControl object
|
// get the FormControl object
|
||||||
const userNameControl = form.get("username");
|
const userNameControl = form.get("username");
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
import { FormControl, FormGroup } from '../core';
|
import { FormControl, FormGroup } from '../core';
|
||||||
import type { FormControlType } from '../core/form-control-types';
|
import type { ControlType } from '../core/form-control-types';
|
||||||
|
|
||||||
export const getFormGroup = (formName: string) => {
|
export const getFormGroup = (formName: string) => {
|
||||||
const fieldSetEl = (document.getElementById(formName) as HTMLFieldSetElement) || null;
|
const fieldSetEl = (document.getElementById(formName) as HTMLFieldSetElement) || null;
|
||||||
if (fieldSetEl === null) {
|
if (fieldSetEl === null) {
|
||||||
console.error(`Formgroup with name: '${formName}' doesn't exist!`);
|
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,14 +21,13 @@ export const getFormGroup = (formName: string) => {
|
||||||
const getFormControl = (name: string) => {
|
const getFormControl = (name: string) => {
|
||||||
const inputEl = document.getElementById(name) as HTMLInputElement | null;
|
const inputEl = document.getElementById(name) as HTMLInputElement | null;
|
||||||
if (inputEl === null) {
|
if (inputEl === null) {
|
||||||
console.error(`Input with name: ${name} doesn't exist!`);
|
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const formControl = new FormControl({
|
const formControl = new FormControl({
|
||||||
name: inputEl.name,
|
name: inputEl.name,
|
||||||
value: inputEl.value,
|
value: inputEl.value,
|
||||||
type: inputEl.type as FormControlType,
|
type: inputEl.type as ControlType,
|
||||||
label: inputEl.dataset.label as string,
|
label: inputEl.dataset.label as string,
|
||||||
labelPosition: inputEl.dataset.labelPosition as 'right' | 'left',
|
labelPosition: inputEl.dataset.labelPosition as 'right' | 'left',
|
||||||
});
|
});
|
||||||
|
@ -46,8 +44,6 @@ const getFormControl = (name: string) => {
|
||||||
|
|
||||||
const controlProxy = new Proxy(formControl, {
|
const controlProxy = new Proxy(formControl, {
|
||||||
set() {
|
set() {
|
||||||
//prevent any setter to be able to work in client;
|
|
||||||
console.error('Setting this property manually is prohibited!');
|
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
/**
|
/**
|
||||||
* FormControlType - determines the type of form control
|
* ControlType - determines the type of form control
|
||||||
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#input_types
|
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#input_types
|
||||||
*/
|
*/
|
||||||
export type FormControlType =
|
export type ControlType =
|
||||||
| 'text'
|
| 'text'
|
||||||
| 'checkbox'
|
| 'checkbox'
|
||||||
| 'radio'
|
| 'radio'
|
||||||
|
@ -25,31 +25,33 @@ export type FormControlType =
|
||||||
| 'url'
|
| 'url'
|
||||||
| 'week';
|
| 'week';
|
||||||
|
|
||||||
export interface FormControlBase {
|
export type ControlConfig = ControlBase | Checkbox | Radio | Submit | Button;
|
||||||
|
|
||||||
|
export interface ControlBase {
|
||||||
name: string;
|
name: string;
|
||||||
type?: FormControlType;
|
type?: ControlType;
|
||||||
value?: string | number | string[];
|
value?: string | number | string[];
|
||||||
label?: string;
|
label?: string;
|
||||||
labelPosition?: 'right' | 'left';
|
labelPosition?: 'right' | 'left';
|
||||||
placeholder?: string;
|
placeholder?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Checkbox extends FormControlBase {
|
export interface Checkbox extends ControlBase {
|
||||||
type: 'checkbox';
|
type: 'checkbox';
|
||||||
checked: boolean;
|
checked: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Radio extends FormControlBase {
|
export interface Radio extends ControlBase {
|
||||||
type: 'radio';
|
type: 'radio';
|
||||||
checked: boolean;
|
checked: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Submit extends FormControlBase {
|
export interface Submit extends ControlBase {
|
||||||
type: 'submit';
|
type: 'submit';
|
||||||
callBack?: () => void;
|
callBack?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Button extends FormControlBase {
|
export interface Button extends ControlBase {
|
||||||
type: 'button';
|
type: 'button';
|
||||||
callBack?: () => void;
|
callBack?: () => void;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,8 @@
|
||||||
import type {
|
import type { ControlConfig, ControlType } from './form-control-types';
|
||||||
FormControlType,
|
|
||||||
Button,
|
|
||||||
Checkbox,
|
|
||||||
FormControlBase,
|
|
||||||
Radio,
|
|
||||||
Submit,
|
|
||||||
} from './form-control-types';
|
|
||||||
|
|
||||||
export class FormControl {
|
export class FormControl {
|
||||||
private _name = '';
|
private _name = '';
|
||||||
private _type: FormControlType = 'text';
|
private _type: ControlType = 'text';
|
||||||
private _value?: string | number | null | string[];
|
private _value?: string | number | null | string[];
|
||||||
private _label?: string;
|
private _label?: string;
|
||||||
private _labelPosition?: 'right' | 'left' = 'left';
|
private _labelPosition?: 'right' | 'left' = 'left';
|
||||||
|
@ -17,7 +10,7 @@ export class FormControl {
|
||||||
private _isPristine = true;
|
private _isPristine = true;
|
||||||
private _placeholder?;
|
private _placeholder?;
|
||||||
|
|
||||||
constructor(config: FormControlBase | Checkbox | Radio | Submit | Button) {
|
constructor(config: ControlConfig) {
|
||||||
const { name, type, value, label, labelPosition, placeholder } = config;
|
const { name, type, value, label, labelPosition, placeholder } = config;
|
||||||
this._name = name;
|
this._name = name;
|
||||||
this._type = type || 'text';
|
this._type = type || 'text';
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import type { FormControlBase } from './form-control-types';
|
import type { ControlBase } from './form-control-types';
|
||||||
import { FormControl } from './form-control';
|
import { FormControl } from './form-control';
|
||||||
|
|
||||||
export class FormGroup {
|
export class FormGroup {
|
||||||
controls: FormControl[];
|
controls: FormControl[];
|
||||||
name?: string;
|
name?: string;
|
||||||
|
|
||||||
constructor(controls: FormControlBase[], name = '') {
|
constructor(controls: ControlBase[], name = '') {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.controls = controls
|
this.controls = controls
|
||||||
.filter((control) => control.type !== 'submit')
|
.filter((control) => control.type !== 'submit')
|
||||||
|
|
Loading…
Reference in a new issue