salix/front/core/components/form-input/index.js

59 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-10-09 22:47:29 +00:00
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 {
$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');
}
}
ngModule.vnComponent('vnFormInput', {
controller: FormInput,
bindings: {
label: '@?',
field: '=?',
name: '@?',
disabled: '<?',
readonly: '<?'
},
require: {
ngModel: '?ngModel'
}
});