import ngModule from '../../module'; import Component from '../../lib/component'; /** * Base component for form inputs. * * @property {String} label Label to display along the component * @property {any} field The value with which the element is linked * @property {Boolean} disabled Put component in disabled mode */ export default class FormInput extends Component { constructor($element, $scope) { super($element, $scope); this.classList = this.element.classList; } $onInit() { // XXX: Compatibility with old inputs let attrs = this.$element[0].attributes; if (!this.name && attrs['ng-model']) { let split = attrs['ng-model'].nodeValue.split('.'); this.name = split[split.length - 1]; } if (!this.ngModel) return; this.ngModel.$render = () => { this.field = this.ngModel.$viewValue; }; } set field(value) { this._field = value; if (this.ngModel) this.ngModel.$setViewValue(value); } get field() { return this._field; } set name(value) { this.element.setAttribute('name', value); } get name() { return this.element.getAttribute('name'); } set disabled(value) { this._disabled = boolTag(value); this.input.disabled = this._disabled; this.classList.toggle('disabled', this._disabled); this.refreshTabIndex(); } get disabled() { return this._disabled; } set readonly(value) { this._readonly = boolTag(value); this.input.readOnly = this._readonly; this.classList.toggle('readonly', this._readonly); } get readonly() { return this._readonly; } set tabIndex(value) { this._tabIndex = value; this.refreshTabIndex(); } get tabIndex() { return this._tabIndex; } select() { this.input.select(); } focus() { this.input.focus(); } refreshTabIndex() { this.element.tabIndex = this.disabled ? -1 : this.tabIndex; } } ngModule.vnComponent('vnFormInput', { controller: FormInput, bindings: { label: '@?', field: '=?', name: '@?', disabled: '