From bf8d0812c3e16860ee946a551361c37bd4bec618 Mon Sep 17 00:00:00 2001 From: cbourget Date: Thu, 13 Jun 2019 13:05:22 -0400 Subject: [PATCH] feat(form): dynamic forms now support textareas --- .../form-field-textarea.component.html | 9 ++++ .../form-field-textarea.component.ts | 51 +++++++++++++++++++ .../lib/form/form-field/form-field.module.ts | 7 ++- packages/common/src/lib/form/form.module.ts | 4 +- 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 packages/common/src/lib/form/form-field/form-field-textarea.component.html create mode 100644 packages/common/src/lib/form/form-field/form-field-textarea.component.ts diff --git a/packages/common/src/lib/form/form-field/form-field-textarea.component.html b/packages/common/src/lib/form/form-field/form-field-textarea.component.html new file mode 100644 index 0000000000..fd272b15b0 --- /dev/null +++ b/packages/common/src/lib/form/form-field/form-field-textarea.component.html @@ -0,0 +1,9 @@ + + + {{getErrorMessage() | translate}} + diff --git a/packages/common/src/lib/form/form-field/form-field-textarea.component.ts b/packages/common/src/lib/form/form-field/form-field-textarea.component.ts new file mode 100644 index 0000000000..2e5dea4b60 --- /dev/null +++ b/packages/common/src/lib/form/form-field/form-field-textarea.component.ts @@ -0,0 +1,51 @@ +import { + Input, + Component, + ChangeDetectionStrategy, +} from '@angular/core'; +import { FormControl } from '@angular/forms'; + +import { formControlIsRequired, getControlErrorMessage } from '../shared/form.utils'; +import { FormFieldComponent } from '../shared/form-field-component'; + +/** + * This component renders a textarea field + */ +@FormFieldComponent('textarea') +@Component({ + selector: 'igo-form-field-textarea', + templateUrl: './form-field-textarea.component.html', + changeDetection: ChangeDetectionStrategy.OnPush +}) +export class FormFieldTextareaComponent { + + /** + * The field's form control + */ + @Input() formControl: FormControl; + + /** + * Field placeholder + */ + @Input() placeholder: string; + + /** + * Field placeholder + */ + @Input() errors: {[key: string]: string}; + + /** + * Whether the field is required + */ + get required(): boolean { + return formControlIsRequired(this.formControl); + } + + /** + * Get error message + */ + getErrorMessage(): string { + return getControlErrorMessage(this.formControl, this.errors); + } + +} diff --git a/packages/common/src/lib/form/form-field/form-field.module.ts b/packages/common/src/lib/form/form-field/form-field.module.ts index 68890a9295..873eae78ed 100644 --- a/packages/common/src/lib/form/form-field/form-field.module.ts +++ b/packages/common/src/lib/form/form-field/form-field.module.ts @@ -14,6 +14,7 @@ import { IgoDynamicOutletModule } from '../../dynamic-component/dynamic-outlet/d import { FormFieldComponent } from './form-field.component'; import { FormFieldSelectComponent } from './form-field-select.component'; import { FormFieldTextComponent } from './form-field-text.component'; +import { FormFieldTextareaComponent } from './form-field-textarea.component'; /** * @ignore @@ -33,12 +34,14 @@ import { FormFieldTextComponent } from './form-field-text.component'; exports: [ FormFieldComponent, FormFieldSelectComponent, - FormFieldTextComponent + FormFieldTextComponent, + FormFieldTextareaComponent ], declarations: [ FormFieldComponent, FormFieldSelectComponent, - FormFieldTextComponent + FormFieldTextComponent, + FormFieldTextareaComponent ] }) export class IgoFormFieldModule {} diff --git a/packages/common/src/lib/form/form.module.ts b/packages/common/src/lib/form/form.module.ts index 6f88e9bb25..321034cbe3 100644 --- a/packages/common/src/lib/form/form.module.ts +++ b/packages/common/src/lib/form/form.module.ts @@ -6,6 +6,7 @@ import { IgoFormGroupModule } from './form-group/form-group.module'; import { IgoFormFieldModule } from './form-field/form-field.module'; import { FormFieldSelectComponent } from './form-field/form-field-select.component'; import { FormFieldTextComponent } from './form-field/form-field-text.component'; +import { FormFieldTextareaComponent } from './form-field/form-field-textarea.component'; import { FormService } from './shared/form.service'; import { FormFieldService } from './shared/form-field.service'; @@ -30,7 +31,8 @@ import { FormFieldService } from './shared/form-field.service'; ], entryComponents: [ FormFieldSelectComponent, - FormFieldTextComponent + FormFieldTextComponent, + FormFieldTextareaComponent ] }) export class IgoFormModule {}