@tagplus/components 1.2.3 → 1.2.6

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.3",
11
+ "version": "1.2.6",
12
12
  "main": "./dist/tp.common.js",
13
13
  "directories": {
14
14
  "lib": "src/lib"
@@ -2,7 +2,7 @@
2
2
  <el-input
3
3
  :id="`${_id}`"
4
4
  class="tp-money"
5
- :value="value | toCurrency"
5
+ :value="formattedValue"
6
6
  v-bind="$attrs"
7
7
  :maxlength="maxlength"
8
8
  @input="input"
@@ -30,12 +30,11 @@
30
30
 
31
31
  <script>
32
32
  import Locale from 'tp-ui/mixins/locale'
33
- import { toCurrency } from 'tp-ui/utils/filters'
34
- import { unformat } from 'tp-ui/utils/currency'
33
+ import { formatMoney } from 'tp-ui/utils/currency'
35
34
 
36
35
  export default {
37
36
  name: 'TpMoney',
38
- filters: { toCurrency },
37
+
39
38
  mixins: [Locale],
40
39
  props: {
41
40
  id: {
@@ -62,6 +61,12 @@ export default {
62
61
  }
63
62
  },
64
63
 
64
+ data () {
65
+ return {
66
+ formattedValue: this.value
67
+ }
68
+ },
69
+
65
70
  computed: {
66
71
  _id () {
67
72
  return this.id || this.$options.name
@@ -74,11 +79,12 @@ export default {
74
79
 
75
80
  methods: {
76
81
  input (evt) {
77
- this.$emit('input', unformat(evt))
82
+ this.formattedValue = formatMoney(evt, true)
83
+ this.$emit('input', formatMoney(evt, false))
78
84
  },
79
85
 
80
86
  handleChange (evt) {
81
- this.$emit('change', unformat(evt))
87
+ this.$emit('change', formatMoney(evt, false))
82
88
  }
83
89
  }
84
90
  }
@@ -2,7 +2,7 @@
2
2
  <el-input
3
3
  :id="`${_id}`"
4
4
  class="tp-percent"
5
- :value="value | toCurrency"
5
+ :value="formattedValue"
6
6
  v-bind="$attrs"
7
7
  :maxlength="maxlength"
8
8
  @input="input"
@@ -26,12 +26,10 @@
26
26
 
27
27
  <script>
28
28
  import Locale from 'tp-ui/mixins/locale'
29
- import { toCurrency } from 'tp-ui/utils/filters'
30
- import { unformat } from 'tp-ui/utils/currency'
29
+ import { formatMoney } from 'tp-ui/utils/currency'
31
30
 
32
31
  export default {
33
32
  name: 'TpPercent',
34
- filters: { toCurrency },
35
33
  mixins: [Locale],
36
34
  props: {
37
35
  id: {
@@ -65,6 +63,12 @@ export default {
65
63
  }
66
64
  },
67
65
 
66
+ data () {
67
+ return {
68
+ formattedValue: this.value
69
+ }
70
+ },
71
+
68
72
  computed: {
69
73
  _id () {
70
74
  return this.id || this.$options.name
@@ -73,11 +77,12 @@ export default {
73
77
 
74
78
  methods: {
75
79
  input (evt) {
76
- this.$emit('input', unformat(evt))
80
+ this.formattedValue = formatMoney(evt, true)
81
+ this.$emit('input', formatMoney(evt, false))
77
82
  },
78
83
 
79
84
  handleChange (evt) {
80
- this.$emit('change', unformat(evt))
85
+ this.$emit('change', formatMoney(evt, false))
81
86
  }
82
87
  }
83
88
  }
@@ -17,10 +17,21 @@ function format (input, opt = defaults) {
17
17
  }
18
18
 
19
19
  function unformat (input, precision = defaults.precision) {
20
- const negative = input.indexOf('-') >= 0 ? -1 : 1
20
+ const negative = ('' + input).indexOf('-') >= 0 ? -1 : 1
21
21
  const numbers = onlyNumbers(input)
22
22
  const currency = numbersToCurrency(numbers, precision)
23
- return parseFloat(currency) * negative
23
+ return currency * negative
24
+ }
25
+
26
+ function formatMoney (input, toString = false, precision = defaults.precision) {
27
+ const negative = ('' + input).indexOf('-') >= 0 ? -1 : 1
28
+ const numbers = onlyNumbers(input)
29
+ const currency = numbersToCurrency(numbers, precision)
30
+ if (toString) {
31
+ return negative === -1 ? '-' + currency : '' + currency
32
+ } else {
33
+ return currency * negative
34
+ }
24
35
  }
25
36
 
26
37
  function onlyNumbers (input) {
@@ -75,6 +86,7 @@ function event (name) {
75
86
  }
76
87
 
77
88
  export {
89
+ formatMoney,
78
90
  format,
79
91
  unformat,
80
92
  setCursor,
@@ -17,21 +17,26 @@ import { t } from 'tp-ui/locale'
17
17
  /**
18
18
  * Transforma valor em String para exibição conforme localidade
19
19
  * @param {Number} val
20
+ * @param {String} locale
20
21
  */
21
- export const toCurrency = (val) => {
22
+ export const toCurrency = (val, locale = false) => {
22
23
  if (val === '') {
23
24
  return val
24
25
  }
25
26
 
26
27
  try {
28
+ if (!locale) {
29
+ locale = t('locale.prefix')
30
+ }
31
+
27
32
  let with2Decimals = val.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]
28
- with2Decimals = Number(with2Decimals.toLocaleString(t('locale.prefix'), {
33
+ with2Decimals = Number(with2Decimals).toLocaleString(locale, {
29
34
  minimumFractionDigits: 2,
30
35
  maximumFractionDigits: 2
31
- }))
36
+ })
32
37
  return with2Decimals
33
38
  } catch (Exception) {
34
- return val.toLocaleString(t('locale.prefix'), {
39
+ return val.toLocaleString(locale, {
35
40
  minimumFractionDigits: 2,
36
41
  maximumFractionDigits: 2
37
42
  })