Skip to content

Instantly share code, notes, and snippets.

@jasoncarreira
Last active April 4, 2018 04:19
Show Gist options
  • Save jasoncarreira/7437bb73153301648947a1f3037a8ea8 to your computer and use it in GitHub Desktop.
Save jasoncarreira/7437bb73153301648947a1f3037a8ea8 to your computer and use it in GitHub Desktop.
Percent / Dollar input
<template>
<require from="./au-input"></require>
<au-input value.bind="value" label.bind="label" percent.bind="percent" salary.bind="salary"></au-input>
<button click.delegate="onClick()">Change</button>
</template>
export class App {
percent= false;
salary = 50000;
label = "Dollar value";
value = 1000;
onClick() {
console.log('APP: clicked: percent = ' + this.percent);
this.percent = !this.percent;
this.label = this.percent ? "Percentage value" : "Dollar value";
console.log('APP: clicked: percent = ' + this.percent);
}
valueChange(newValue, oldValue) {
console.log('APP: value changed from ' + oldValue + ' ' + newValue);
}
}
<template>
<h1>${label} - ${value}</h1>
<div class="input-group">
<span if.bind="prefix" class="input-group-addon">${prefix}</span>
<input type="${type}" class="form-control" aria-label="${label}" value.bind="value">
<span if.bind="suffix" class="input-group-addon">${suffix}</span>
</div>
</template>
import {observable,bindable,bindingMode, computedFrom} from 'aurelia-framework';
export class AuInput {
suppressValueChanged: boolean;
@observable
dollarValue: number;
dollarValueChanged(newValue,oldValue){
console.log('AuInput: dollarValueChanged: newValue = ' + newValue + ' oldValue = ' + oldValue + ' state = ' + JSON.stringify(this));
if(this.suppressValueChanged){
return;
}
this.suppressValueChanged = true;
this.value = this.dollarValue;
this.suppressValueChanged = false;
}
@observable
percentValue: number;
percentValueChanged(newValue,oldValue){
console.log('AuInput: percentValueChanged: newValue = ' + newValue + ' oldValue = ' + oldValue + ' state = ' + JSON.stringify(this));
if(this.suppressValueChanged){
return;
}
this.suppressValueChanged = true;
this.value = this.percentValue;
this.suppressValueChanged = false;
}
@bindable({defaultBindingMode: bindingMode.twoWay})
value: number;
valueChanged(newValue, oldValue) {
if(this.suppressValueChanged || !this.salary){
return;
}
this.suppressValueChanged = true;
console.log('AuInput: valueChanged: newValue = ' + newValue + ' oldValue = ' + oldValue + ' state = ' + JSON.stringify(this));
if (this.percent) {
this.percentValue = this.value;
this.dollarValue = this.percentValue * this.salary / 100;
} else {
console.log('here');
this.dollarValue = this.value;
this.percentValue = this.dollarValue / this.salary * 100;
}
this.suppressValueChanged = false;
}
@bindable
salary: number;
salaryChanged(){
if(!this.value){
return;
}
this.valueChanged();
}
@bindable label : string;
prefix : string;
suffix : string;
@bindable
percent: boolean;
percentChanged(newValue, oldValue) {
console.log('AuInput: percentChanged: newValue = ' + newValue + ' oldValue = ' + oldValue + ' state = ' + JSON.stringify(this));
if (this.percent) {
this.suffix = "%";
this.prefix = null;
this.percentValueChanged();
} else {
this.suffix = null;
this.prefix = "$";
this.dollarValueChanged();
}
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</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>
import {Aurelia} from "aurelia-framework";
import environment from "./environment";
import {UIConfig} from "aurelia-ui-framework";
export function configure(aurelia: Aurelia) {
aurelia.use
.standardConfiguration();
aurelia.start().then(() => aurelia.setRoot());
}
/* todo: add styles */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment