@dzeio/form-manager
Version:
A powerfull Form Manager
65 lines (64 loc) • 2.56 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var DefaultInput_1 = __importDefault(require("./DefaultInput"));
var DatalistInput = (function (_super) {
__extends(DatalistInput, _super);
function DatalistInput(element, form) {
var _this = _super.call(this, element, form) || this;
_this.isStrict = _this.element.hasAttribute("data-strict");
var id = _this.element.getAttribute("list");
if (!id)
throw Error("Error: your input \"" + _this.getName() + "\" MUST have a list attribute");
_this.datalist = document.getElementById(id);
if (!_this.datalist)
throw Error("Error: Datalist not found for " + _this.getName() + " input");
return _this;
}
DatalistInput.prototype.setValue = function (value) {
if (value == "" || value === undefined) {
this.element.value = "";
return;
}
if (value.id != undefined) {
value = value.id;
}
var option = this.datalist.querySelector("[data-value=\"" + value + "\"]");
if (option != undefined) {
this.element.value = option.value;
return;
}
if (!this.isStrict) {
this.element.value = value;
return;
}
};
DatalistInput.prototype.getValue = function () {
var option = this.datalist.querySelector("[value=\"" + this.element.value + "\"]");
if (option)
return this.formatValue(option.dataset.value);
return this.isStrict ? undefined : this.formatValue(this.element.value);
};
DatalistInput.identity = {
input: DatalistInput,
attributes: "list",
tagName: "input"
};
return DatalistInput;
}(DefaultInput_1.default));
exports.default = DatalistInput;