Skip to content

Instantly share code, notes, and snippets.

@jdanyow
Created April 8, 2016 20:02
Show Gist options
  • Save jdanyow/3101e8f73cf4da32445505d0e4258f01 to your computer and use it in GitHub Desktop.
Save jdanyow/3101e8f73cf4da32445505d0e4258f01 to your computer and use it in GitHub Desktop.
<template>
<require from="./numeric-input"></require>
<numeric-input value.bind="value"></numeric-input>
${value}
</template>
export class App {
value = '';
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/jspm_packages/system.js"></script>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/config.js"></script>
<script>
System.import('aurelia-bootstrapper');
</script>
</body>
</html>
<template>
<input type="text" value.bind="value" placeholder.bind="placeholder">
</template>
import {
inject,
bindable,
bindingMode
} from 'aurelia-framework';
// http://stackoverflow.com/a/995193/725866
function isNavigationOrSelectionKey(e) {
// Allow: backspace, delete, tab, escape, enter and .
if ([46, 8, 9, 27, 13, 110, 190].indexOf(e.keyCode) !== -1 ||
// Allow: Ctrl+A/X/C/V, Command+A/X/C/V
([65, 67, 86, 88].indexOf(e.keyCode) !== -1 && (e.ctrlKey === true || e.metaKey === true)) ||
// Allow: home, end, left, right, down, up
(e.keyCode >= 35 && e.keyCode <= 40)) {
// let it happen, don't do anything
return true;
}
return false;
}
// http://stackoverflow.com/a/995193/725866
function keydown (e) {
if (isNavigationOrSelectionKey(e)) {
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
}
@inject(Element)
export class NumericInput {
@bindable({ defaultBindingMode: bindingMode.twoWay }) value;
@bindable placeholder = '';
constructor(element) {
this.element = element;
}
attached() {
this.element.addEventListener('keydown', keydown);
}
detached() {
this.element.removeEventListener('keydown', keydown);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment