@tagplus/components 1.2.7 → 1.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "email": "bruno@tagplus.com.br"
9
9
  }
10
10
  ],
11
- "version": "1.2.7",
11
+ "version": "1.2.8",
12
12
  "main": "./dist/tp.common.js",
13
13
  "directories": {
14
14
  "lib": "src/lib"
@@ -30,12 +30,12 @@
30
30
 
31
31
  <script>
32
32
  import Locale from 'tp-ui/mixins/locale'
33
- import { formatMoney } from 'tp-ui/utils/currency'
33
+ import floatFormatterMixin from 'tp-ui/mixins/floatFormatter'
34
34
 
35
35
  export default {
36
36
  name: 'TpMoney',
37
37
 
38
- mixins: [Locale],
38
+ mixins: [Locale, floatFormatterMixin],
39
39
  props: {
40
40
  id: {
41
41
  type: String,
@@ -69,26 +69,6 @@ export default {
69
69
  prepend () {
70
70
  return this.t('tp.money.currency')
71
71
  }
72
- },
73
-
74
- watch: {
75
- value: {
76
- immediate: true,
77
- handler (newValue, oldValue) {
78
- this.formattedValue = newValue
79
- }
80
- }
81
- },
82
-
83
- methods: {
84
- input (evt) {
85
- this.formattedValue = formatMoney(evt, true)
86
- this.$emit('input', formatMoney(evt, false))
87
- },
88
-
89
- handleChange (evt) {
90
- this.$emit('change', formatMoney(evt, false))
91
- }
92
72
  }
93
73
  }
94
74
  </script>
@@ -26,11 +26,12 @@
26
26
 
27
27
  <script>
28
28
  import Locale from 'tp-ui/mixins/locale'
29
- import { formatMoney } from 'tp-ui/utils/currency'
29
+ import floatFormatterMixin from 'tp-ui/mixins/floatFormatter'
30
30
 
31
31
  export default {
32
32
  name: 'TpPercent',
33
- mixins: [Locale],
33
+
34
+ mixins: [Locale, floatFormatterMixin],
34
35
  props: {
35
36
  id: {
36
37
  type: String,
@@ -67,26 +68,6 @@ export default {
67
68
  _id () {
68
69
  return this.id || this.$options.name
69
70
  }
70
- },
71
-
72
- watch: {
73
- value: {
74
- immediate: true,
75
- handler (newValue, oldValue) {
76
- this.formattedValue = newValue
77
- }
78
- }
79
- },
80
-
81
- methods: {
82
- input (evt) {
83
- this.formattedValue = formatMoney(evt, true)
84
- this.$emit('input', formatMoney(evt, false))
85
- },
86
-
87
- handleChange (evt) {
88
- this.$emit('change', formatMoney(evt, false))
89
- }
90
71
  }
91
72
  }
92
73
  </script>
@@ -0,0 +1,34 @@
1
+ import { formatMoney } from 'tp-ui/utils/currency'
2
+
3
+ /**
4
+ * Eventos para componentes tp-money e tp-percent mostratem o número formatado com a precisão correta e devolver float sem arredondar
5
+ */
6
+ export default {
7
+ data () {
8
+ return {
9
+ formattedValue: this.value
10
+ }
11
+ },
12
+
13
+ watch: {
14
+ value: {
15
+ immediate: true,
16
+ handler (newValue, oldValue) {
17
+ this.formattedValue = formatMoney(newValue, true)
18
+ }
19
+ }
20
+ },
21
+
22
+ methods: {
23
+ input (evt) {
24
+ this.formattedValue = formatMoney(evt, true)
25
+ const moneyFormatado = formatMoney(evt, false)
26
+ this.$emit('input', moneyFormatado)
27
+ },
28
+
29
+ handleChange (evt) {
30
+ this.$emit('change', formatMoney(evt, false))
31
+ }
32
+ }
33
+
34
+ }
@@ -25,7 +25,10 @@ function unformat (input, precision = defaults.precision) {
25
25
 
26
26
  function formatMoney (input, toString = false, precision = defaults.precision) {
27
27
  const negative = ('' + input).indexOf('-') >= 0 ? -1 : 1
28
- const numbers = onlyNumbers(input)
28
+ if (toString) {
29
+ input = completeZeros(('' + input), precision)
30
+ }
31
+ const numbers = onlyNumbers(input, precision)
29
32
  const currency = numbersToCurrency(numbers, precision)
30
33
  if (toString) {
31
34
  return negative === -1 ? '-' + currency : '' + currency
@@ -34,6 +37,19 @@ function formatMoney (input, toString = false, precision = defaults.precision) {
34
37
  }
35
38
  }
36
39
 
40
+ function completeZeros (input, precision = defaults.precision) {
41
+ const i = input.indexOf('.')
42
+ const qntDecimais = i ? input.length - i - 1 : 0
43
+
44
+ // correção para decimal quebrado
45
+ if (i === -1) {
46
+ input = input + '.' + '0'.repeat(precision)
47
+ } else if (i && qntDecimais < precision) {
48
+ input = input + '0'.repeat(precision - qntDecimais)
49
+ }
50
+ return input
51
+ }
52
+
37
53
  function onlyNumbers (input) {
38
54
  return toStr(input).replace(/\D+/g, '') || '0'
39
55
  }
@@ -90,5 +106,6 @@ export {
90
106
  format,
91
107
  unformat,
92
108
  setCursor,
109
+ completeZeros,
93
110
  event
94
111
  }