ng-semantic
Version:
Angular2 building blocks based on Semantic UI
114 lines (106 loc) • 3.5 kB
text/typescript
import {
Component, Input, ChangeDetectionStrategy, Output, ViewContainerRef,
EventEmitter, OnInit
} from "@angular/core";
import { FormControl } from "@angular/forms";
/**
* Implementation of Input element
*
* @link http://semantic-ui.com/elements/input.html
*
* @example
* <sm-input icon="dollar" type="number" [(model)]="model" class="right fluid" placeholder="Enter a sum..."></sm-input>
*/
export class SemanticInputComponent implements OnInit {
label: string;
class: string;
icon: string;
type: string = "text";
placeholder: string;
model: {};
control: FormControl = new FormControl();
modelChange: EventEmitter<string|number> = new EventEmitter<string|number>();
private isInsideForm: boolean = false;
constructor(public viewRef: ViewContainerRef) {}
ngOnInit() {
// if input field is inside form
if (this.inForm(this.viewRef.element.nativeElement, "form")) {
this.isInsideForm = true;
}
}
inForm(el: Node, classname: string): boolean {
if (el.parentNode) {
if (el.parentNode.nodeName.toLowerCase() === classname.toLowerCase()) {
return !!el.parentNode;
} else {
return this.inForm(el.parentNode, classname);
}
} else {
return false;
}
}
}
/**
* Implementation of Checkbox element
*
* @link http://semantic-ui.com/modules/checkbox.html
*/
export class SemanticCheckboxComponent {
control: FormControl;
label: string;
disabled: boolean;
value: string|number;
name: string;
set type(data: string) {
if (data && data !== "checkbox") {
this.classType = data;
if (data === "radio") {
this.inputType = data;
}
}
}
private inputType: string = "checkbox";
private classType = "checkbox";
}
/**
* Implementation of Textarea element
*
* @link http://semantic-ui.com/collections/form.html#text-area
*/
export class SemanticTextareaComponent {
control: FormControl;
label: string;
rows: string;
}