-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpaper-input-money.html
102 lines (83 loc) · 3.75 KB
/
paper-input-money.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<link rel="import" href="../../bower_components/polymer/polymer-element.html">
<link rel="import" href="../../bower_components/paper-input/paper-input.html" />
<!--
`<paper-input-money>` is a single-line text field with Material Design styling. It extends the Polymer 2 element `<paper-input>`.
<paper-input-money label="Input label"
always-float-label
required
currency-value="{{currValue}}"></paper-input>
The prefix and pattern are predetermined (only USD for right now). To get the numeric value of the input, bind to the custom `currencyValue` property. (The `value` field from the PaperInput superclass will always be of type String.)
The input mask is applied during the "blur" event on the input.
-->
<dom-module id="paper-input-money">
<template>
<style>
:host {
display: block;
text-align: right;
}
</style>
</template>
<script>
let memoizedTemplate;
class PaperInputMoney extends customElements.get('paper-input') {
static get template() {
if (!memoizedTemplate) {
memoizedTemplate = Polymer.DomModule.import(this.is, 'template');
let superTemplateContents = document.importNode(customElements.get('paper-input').template.content, true);
let prefixSlot = superTemplateContents.querySelector('[slot="prefix"]');
prefixSlot.textContent = '$';
memoizedTemplate.content.appendChild(superTemplateContents);
}
return memoizedTemplate;
}
static get is() { return 'paper-input-money'; }
static get properties() {
return {
'currencyValue': {
type: Number,
notify: true,
observer: 'onCurrencyValueChanged'
},
'parsedValue': {
type: Number,
computed: 'computeParsedValue(value)',
notify: true
},
'allowedPattern': {
type: String,
value: () => '[0-9\.\,\=\-]'
},
'value': {
type: String,
value: () => '0.00'
}
}
}
_focusBlurHandler(evt) {
super._focusBlurHandler(evt);
if (this.focused && !this._shiftTabPressed && this._focusableElement && this._focusableElement.select) {
this._focusableElement.select();
}
if (!this.focused)
this.handleOnFocusOut(evt);
}
onCurrencyValueChanged(newValue) {
if (newValue != this.parsedValue) {
this.set('value', newValue.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 }));
}
}
computeParsedValue(value) {
return parseFloat(value.replace(/,/g, ''));
}
handleOnFocusOut(e) {
if (typeof this.value === 'undefined' || this.value == null) {
this.set('value', '0.00');
}
var parsedValue = parseFloat(this.value.replace(/,/g, ''));
this.setProperties({ 'value': parsedValue.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 }), 'currencyValue': parsedValue });
}
}
customElements.define(PaperInputMoney.is, PaperInputMoney);
</script>
</dom-module>