first-npm-package-nicule
Version:
This isi first npm package
102 lines (87 loc) • 3.42 kB
text/typescript
import { Component, ContentChild, Inject, Input, OnChanges, TemplateRef, ViewChild } from '@angular/core';
import { HypermediaField, HypermediaFieldOption, HypermediaValue } from 'first-npm-package-nicule/core';
import { PARENT_FORM } from './parent-form';
import { AfterFieldDirective, BeforeFieldDirective, FieldDirective } from '../directives/form';
export class FormFieldComponent implements OnChanges {
named: string;
output: TemplateRef<any>;
beforeField: BeforeFieldDirective;
afterField: AfterFieldDirective;
fieldReplace: FieldDirective;
/* HypermediaField */
name: string;
type: string;
value?: HypermediaValue;
options?: Array<HypermediaFieldOption>;
required?: boolean;
maxlength?: number;
minlength?: number;
pattern?: string;
max?: number;
min?: number;
disabled?: boolean;
readonly?: boolean;
/* HypermediaField */
errors: any;
_fieldInputs: any;
get fieldInputs(): any {
return {
...this._fieldInputs,
disabled: this.form.disabled,
mode: this.form.mode,
errors: this.errors
};
}
// Temporary
set fieldInputs(newFieldInputs: any) {
this._fieldInputs = newFieldInputs;
}
baseField: HypermediaField;
constructor( private form) { }
ngOnChanges(): void {
// Copy all the inputs from this to the field
const {
named,
output,
fields,
form,
_fieldInputs,
fieldInputs,
disabled,
fieldReplace,
afterField,
beforeField,
baseField,
field,
formOwner, ...inputs } = this as any;
const imutableBaseField = this.named && this.form.getField(this.named);
if (!this.form || !this.form.action) {
return;
}
if (!imutableBaseField && this.named) {
console.warn(`Field named ${this.named} was not found within the form, make sure the field you try to override exists! Action name: ${this.form.action.name}`);
return;
}
this.baseField = {
...imutableBaseField,
...inputs,
isDisabled: this.disabled,
name: inputs.name || this.named
};
}
}