feat: support for labelPosition, radio/checkboxed checked value

This commit is contained in:
Ayo 2022-09-26 11:11:27 +02:00
parent 0b286eba11
commit 5d83965c0c
3 changed files with 23 additions and 5 deletions

View file

@ -1,12 +1,24 @@
---
import {FormGroup} from './form-group';
export interface Props {
formGroup: FormGroup;
}
const form: FormGroup = Astro.props.formGroup;
---
<form>
{form?.controls?.map(control => (
<field>
<label for={control.name}>{control.label}</label>
<input name={control.name} id={control.name} type={control.type} value={control.value} />
</field>
<div>
{(control.labelPosition === 'left')
? <label for={control.name}>{control.label}</label>
: null}
<input name={control.name} id={control.name} type={control.type} value={control.value} checked={control.value==='checked'} />
{(control.labelPosition === 'right')
? <label for={control.name}>{control.label}</label>
: null}
</div>
))}
</form>

View file

@ -3,4 +3,5 @@ export class FormControl {
type?: 'text' | 'checkbox' | 'radio' = 'text'; // add more
value?: string | number | null | string[];
label?: string;
labelPosition?: 'right' | 'left' = 'left';
}

View file

@ -4,6 +4,11 @@ export class FormGroup {
controls: FormControl[];
constructor(controls: FormControl[]) {
this.controls = controls;
this.controls = controls.map(control => (
{
...control,
labelPosition: control.labelPosition || 'left'
}
));
}
}