浏览代码

Merging latest updates from AMP

Richard Knight 4 年之前
父节点
当前提交
eb0cbeaf3e

+ 7 - 1
src/app/dynaform/components/_abstract/native-input.component.ts

@@ -42,7 +42,13 @@ export abstract class NativeInputComponent implements OnInit, OnDestroy {
 		// this.control is not an instance of FormControl when it's a Checkbutton in a Checkbutton group (but is when it's a solo Checkbutton) - BUT WHY? WHY!!!
 		// Hence the 'if' statement
 		if (this.control instanceof FormControl) {
-			this.valueSBX = this.control.valueChanges.subscribe(this.markForCheck.bind(this));
+			this.valueSBX = this.control.valueChanges.subscribe(val => {
+				if (val === null) {
+					this.control.setValue(this._meta.default); // Reset to default value (as opposed to null) after an Angular FormGroup.reset()
+					return;
+				}
+				this.markForCheck();
+			});
 			this.statusSBX = this.control.statusChanges.subscribe(status => {
 				if (status === 'PENDING') {
 					this.pendingValidation = true;

+ 2 - 0
src/app/dynaform/components/clarity/datepicker/datepicker.component.html

@@ -1,8 +1,10 @@
 <clr-date-container [ngClass]="extraClass">
+	<label [ngClass]="{ 'label-error': control.touched && control.invalid }">{{ label }}</label>
 	<input clrDate
 		[formControl]="control"
 		(focus)="gainFocus()"
 		(blur)="loseFocus()"
 		(keyup)="handleKeyup($event.target.value)"
 	>
+	<clr-control-error>{{ getFirstFailureMsg() }}</clr-control-error>
 </clr-date-container>

+ 2 - 1
src/app/dynaform/components/clarity/datepicker/datepicker.component.ts

@@ -10,8 +10,9 @@ import { FriendlyValidationErrorsService } from './../../../services/friendly-va
 })
 export class ClrDatepickerComponent extends NativeInputComponent implements OnInit {
 
-	exposeMetaInTemplate: string[] = ['extraClass', 'placeholder'];
+	exposeMetaInTemplate: string[] = ['label', 'extraClass', 'placeholder'];
 
+	label: string;
 	extraClass: string;
 	placeholder: string;
 

+ 5 - 5
src/app/dynaform/directives/dynafield.directive.ts

@@ -136,7 +136,7 @@ export class DynafieldDirective extends NgControl implements OnInit, OnChanges,
 					this.validators = [...(this.validators || []), ...(ngValidators as Array<Validator|ValidatorFn>)];
 				}
 				*/
-				this._control = this.formGroupDirective.addControl(this);
+				this._control = this.formGroupDirective.addControl(this as NgControl);
 			}
 		} catch (e) {
 			console.error('ERROR INSTANTIATING DYNAFORM CHILD COMPONENT', type);
@@ -158,7 +158,7 @@ export class DynafieldDirective extends NgControl implements OnInit, OnChanges,
 
 	ngOnDestroy(): void {
 		if (this.formGroupDirective) {
-			this.formGroupDirective.removeControl(this);
+			this.formGroupDirective.removeControl(this as NgControl);
 		}
 		if (this.component) {
 			this.component.destroy();
@@ -173,7 +173,7 @@ export class DynafieldDirective extends NgControl implements OnInit, OnChanges,
 	}
 
 	get path(): string[] {
-		return [...this.cc.path, this.name];
+		return [...this.cc.path, this.name] as string[];
 	}
 
 	get formGroupDirective(): Form | null {
@@ -197,12 +197,12 @@ export class DynafieldDirective extends NgControl implements OnInit, OnChanges,
 		}
 	}
 
-	setCssId(cssId): void {
+	setCssId(cssId: string): void {
 		const el = this.component.location.nativeElement;
 		cssId ? el.setAttribute('id', cssId) : el.removeAttribute('id');
 	}
 
-	setCssClasses(type, cssClass): void {
+	setCssClasses(type: string, cssClass: string|string[]): void {
 		const classList = this.component.location.nativeElement.classList as DOMTokenList;
 		const componentTypeClass = type.toLowerCase().replace('component', '');
 		classList.add(componentTypeClass);

+ 9 - 3
src/app/dynaform/models/field.model.ts

@@ -97,6 +97,9 @@ abstract class SimpleField {
 			// e.g. supervisorCardNumber --> Supervisor Card Number
 			this.label = unCamelCase(this.name);
 		}
+		if (typeof this.value === 'undefined') {
+			this.value = this.default;
+		}
 		// TODO: Consider binding context to standard validators as well as async validators
 		// BUT check this doesn't stop Angular's built-in validators from working (do they use 'this'?)
 		if (typeof this.asyncValidators === 'function' || Array.isArray(this.asyncValidators)) {
@@ -197,14 +200,17 @@ class CheckboxField extends SimpleField {
 	rowLabel: null;
 	constructor(meta: ISimpleFieldMetaData) {
 		super(meta);
+		if (meta.default) {
+			this.default = meta.default; // Necessary to ovveride defaut of true, since this cannot be done by super(meta)
+		}
 		if (meta.value) {
 			this.checkedValue = meta.value;
 		}
 		if (meta.checkedValue) {
-			this.checkedValue = meta.checkedValue;
+			this.checkedValue = meta.checkedValue; // Ditto
 		}
-		if (meta.default) {
-			this.default = meta.default;
+		if (typeof meta.value === 'undefined') {
+			this.value = this.default; // Get default from this class, not superclass
 		}
 	}
 }

+ 1 - 1
src/app/dynaform/services/_formdata-utils.ts

@@ -69,7 +69,7 @@ const combineMetaForField = (metaF, containerSeed, extraMetaF) => ({ ...metaF, .
 
 const combineExtraMeta = (metaG, extraMeta, createFromExtra = false, containerSeed = {}) => {
 	const combinedMeta = {};
-	Object.entries(extraMeta).forEach(([key, val]) => {
+	Object.entries(extraMeta).forEach(([key, val]: [string, StringMap<any>]) => {
 		if (typeof metaG[key] === 'object' || createFromExtra) { // If the key exists (in the model) OR we're creating from metadata
 			const isCon = isContainer(val);
 			const isRepeating = isRepeatingContainer(val);

+ 1 - 1
src/app/dynaform/utils.ts

@@ -3,7 +3,7 @@
 import { FormControl, FormGroup, ValidationErrors } from '@angular/forms';
 import { ValueTransformer } from './interfaces';
 import { Observable, of, merge } from 'rxjs';
-import { filter, map, mapTo, switchMap, } from 'rxjs/internal/operators';
+import { filter, map, mapTo, switchMap } from 'rxjs/internal/operators';
 
 
 // Dropdown Modified Input - Starts With / Contains / Matches