maille
Version:
Component library for MithrilJS
60 lines (59 loc) • 2.08 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mithril_1 = __importDefault(require("mithril")); // +standalone
const types_1 = require("../../types");
class TextInput {
constructor(vnode) {
if (vnode) {
if (vnode.attrs.value) {
this.value = vnode.attrs.value;
}
}
}
view(vnode) {
const classes = ["maille", "maille-text-input"];
const inputType = vnode.attrs.type || "text";
const onChange = vnode.attrs.onChange || types_1.NoOpFn;
const onEnterPress = vnode.attrs.onEnterPress || types_1.NoOpFn;
const size = vnode.attrs.size || types_1.Size.Medium;
// If outlined, add the outline class
if (vnode.attrs.rounded) {
classes.push("rounded");
}
if (vnode.attrs.disabled) {
classes.push("disabled");
}
classes.push(`size-${size}`);
const className = classes.join(" ");
return mithril_1.default("input", {
id: vnode.attrs.id,
className,
type: inputType,
name: vnode.attrs.name,
disabled: vnode.attrs.disabled,
placeholder: vnode.attrs.placeholder,
onkeyup: (e) => {
// Don't redraw, too many will cause dropped events
e.redraw = false;
// If it's disabled don't do anything
if (vnode.attrs.disabled) {
return;
}
if (e.keyCode === types_1.KeyboardKeyCode.Enter) {
onEnterPress(this.value, e);
return;
}
if (!e.target) {
return;
}
this.value = e.target.value;
onChange(this.value, e);
},
value: this.value,
});
}
}
exports.default = TextInput;