UNPKG

tnt-form

Version:
119 lines (87 loc) 5.15 kB
"use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var Component = require('../component'), util = require('../util/util.js'); var TextField = function (_Component) { _inherits(TextField, _Component); function TextField(id) { var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; _classCallCheck(this, TextField); var _this = _possibleConstructorReturn(this, (TextField.__proto__ || Object.getPrototypeOf(TextField)).call(this, id, opts)); var _opts$v8nMaxLength = opts.v8nMaxLength; _this.v8nMaxLength = _opts$v8nMaxLength === undefined ? 255 : _opts$v8nMaxLength; var _opts$v8nEmail = opts.v8nEmail; _this.v8nEmail = _opts$v8nEmail === undefined ? false : _opts$v8nEmail; var _opts$multiline = opts.multiline; _this.multiline = _opts$multiline === undefined ? false : _opts$multiline; var _opts$defaultValue = opts.defaultValue; _this.defaultValue = _opts$defaultValue === undefined ? '' : _opts$defaultValue; var _opts$placeholder = opts.placeholder; _this.placeholder = _opts$placeholder === undefined ? '' : _opts$placeholder; return _this; } _createClass(TextField, [{ key: 'getValue', value: function getValue() { return this.input.value; } }, { key: 'validate', value: function validate() { _get(TextField.prototype.__proto__ || Object.getPrototypeOf(TextField.prototype), 'validate', this).call(this); if (this.v8nEmail && !util.RE_EMAIL.test(this.getValue())) { this.errors.push({ id: 'invalidEmail', default: 'Invalid Email' }); } if (this.getValue().length > this.v8nMaxLength) { this.errors.push({ id: 'maxLength', default: 'Input is too long ( max ' + this.v8nMaxLength + ' )' }); } this.postValidate(); } }, { key: 'setError', value: function setError(errorMessage) { _get(TextField.prototype.__proto__ || Object.getPrototypeOf(TextField.prototype), 'setError', this).call(this, errorMessage); if (this.form.inlineErrorMessages) { this.input.value = ''; this.input.setAttribute('placeholder', errorMessage); } } }, { key: 'build', value: function build() { var _this2 = this; _get(TextField.prototype.__proto__ || Object.getPrototypeOf(TextField.prototype), 'build', this).call(this); if (this.multiline) { this.input = document.createElement('textarea'); this.input.innerHTML = this.defaultValue; } else { this.input = document.createElement('input'); this.input.setAttribute('type', 'text'); this.input.setAttribute('value', this.defaultValue); } if (this.v8nRequired) { this.input.required = true; } this.getContainer().appendChild(this.input); if (this.placeholder) { this.input.setAttribute('placeholder', this.placeholder); } this.input.addEventListener('change', function (e) { _this2.trigger('change', { value: _this2.getValue() }); }); this.input.addEventListener('keyup', function (e) { _this2.trigger('change', { value: _this2.getValue() }); }); this.input.addEventListener('keydown', function (e) { _this2.trigger('change', { value: _this2.getValue() }); }); return this.getContainer(); } }]); return TextField; }(Component); module.exports = TextField;