From 030752b4cc26317fdf45ce82f34720550c7dd4c9 Mon Sep 17 00:00:00 2001 From: Woramat Ngamkham Date: Thu, 20 Oct 2022 12:58:40 +0700 Subject: [PATCH] feat(form): Add radio (#111) --- apps/demo/src/pages/index.astro | 15 +++++++++++ packages/form/Form.astro | 33 ++++++++++++++++++++++-- packages/form/components/Field.astro | 33 ++++++++++++++---------- packages/form/core/form-control-types.ts | 11 ++++++-- packages/form/core/form-control.ts | 12 ++++++--- 5 files changed, 83 insertions(+), 21 deletions(-) diff --git a/apps/demo/src/pages/index.astro b/apps/demo/src/pages/index.astro index 93572ae..f1523d6 100644 --- a/apps/demo/src/pages/index.astro +++ b/apps/demo/src/pages/index.astro @@ -23,6 +23,21 @@ const form = new FormGroup([ type: "password", validators: [Validators.required, Validators.minLength(8)], }, + { + name: "rating", + label: "Rating", + type: "radio", + value: ["1", "2", "3", "4", "5"] + }, + { + name: "agreement", + label: "Agreement", + type: "radio", + value: [ + { label: "Agree", value: "yes" }, + { label: "Disagree", value: "no", labelPosition: "right" } + ] + } ]); form.name = "Simple Form"; diff --git a/packages/form/Form.astro b/packages/form/Form.astro index 408e9ae..9402cd5 100644 --- a/packages/form/Form.astro +++ b/packages/form/Form.astro @@ -1,5 +1,5 @@ --- -import { Submit, FormGroup, FormControl } from './core'; +import { Submit, FormGroup, FormControl, Radio, RadioOption } from './core'; import Field from './components/Field.astro'; import FieldSet from './components/FieldSet.astro'; @@ -21,8 +21,37 @@ const formName = Array.isArray(form) ? null : form?.name || null; Array.isArray(form) ? form?.map((group) =>
) : form?.controls.map((control) => ( + control.type === "radio" ? + ( + [ + (), + ...(control as Radio)?.value?.map((v: string | RadioOption) => ( + + )) + ] + ) : + ( - )) + ) + )) } { submitControl && ( diff --git a/packages/form/components/Field.astro b/packages/form/components/Field.astro index 207f625..6116642 100644 --- a/packages/form/components/Field.astro +++ b/packages/form/components/Field.astro @@ -4,9 +4,10 @@ import type { FormControl } from '../core/form-control'; export interface Props { control: FormControl; showValidationHints: boolean; + showOnlyLabel?: boolean; } -const { control, showValidationHints } = Astro.props; +const { control, showValidationHints, showOnlyLabel = false } = Astro.props; const { validators = [] } = control; @@ -31,28 +32,32 @@ const validatorAttirbutes: Record = validators?.reduce(
{ control.label && control.labelPosition === 'left' && ( -