@tagplus/components 1.2.14 → 1.2.17

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.14",
11
+ "version": "1.2.17",
12
12
  "main": "./dist/tp.common.js",
13
13
  "directories": {
14
14
  "lib": "src/lib"
@@ -450,7 +450,7 @@ export default {
450
450
  .join(', ')
451
451
  },
452
452
 
453
- suggestionsList(_newValue, _oldValue){
453
+ suggestionsList (_newValue, _oldValue) {
454
454
  this.setSelectedLabel()
455
455
  }
456
456
  },
@@ -503,23 +503,23 @@ export default {
503
503
  },
504
504
 
505
505
  setSelectedLabel () {
506
- if (Array.isArray(this.value)) {
507
- this.value.forEach((item, key) => {
508
- this.suggestionsList.forEach(suggestion => {
509
- if (suggestion[this.valueKey] === item) {
510
- this.selectedLabelsArray.push(suggestion)
511
- }
506
+ if (Array.isArray(this.value)) {
507
+ this.value.forEach((item, key) => {
508
+ this.suggestionsList.forEach(suggestion => {
509
+ if (suggestion[this.valueKey] === item) {
510
+ this.selectedLabelsArray.push(suggestion)
511
+ }
512
+ })
512
513
  })
513
- })
514
- } else if (process.env.DEBUG === 'true') {
515
- console.log(`'${this.$options.name}' recebeu valor em formato inválido.`)
516
- }
514
+ } else if (process.env.DEBUG === 'true') {
515
+ console.log(`'${this.$options.name}' recebeu valor em formato inválido.`)
516
+ }
517
517
 
518
- this.selectedLabels = this.selectedLabelsArray
519
- .map(item => {
520
- return item[this.labelKey]
521
- })
522
- .join(', ')
518
+ this.selectedLabels = this.selectedLabelsArray
519
+ .map(item => {
520
+ return item[this.labelKey]
521
+ })
522
+ .join(', ')
523
523
  },
524
524
 
525
525
  // eslint-disable-next-line no-unused-vars
@@ -10,24 +10,30 @@ export default {
10
10
  }
11
11
  },
12
12
 
13
+ computed: {
14
+ locale () {
15
+ return this.t('locale')
16
+ }
17
+ },
18
+
13
19
  watch: {
14
20
  value: {
15
21
  immediate: true,
16
22
  handler (newValue, oldValue) {
17
- this.formattedValue = formatMoney(newValue, true)
23
+ this.formattedValue = formatMoney(newValue, true, this.locale)
18
24
  }
19
25
  }
20
26
  },
21
27
 
22
28
  methods: {
23
29
  input (evt) {
24
- this.formattedValue = formatMoney(evt, true)
25
- const moneyFormatado = formatMoney(evt, false)
30
+ this.formattedValue = formatMoney(evt, true, this.locale)
31
+ const moneyFormatado = formatMoney(evt, false, this.locale)
26
32
  this.$emit('input', moneyFormatado)
27
33
  },
28
34
 
29
35
  handleChange (evt) {
30
- this.$emit('change', formatMoney(evt, false))
36
+ this.$emit('change', formatMoney(evt, false, this.locale))
31
37
  }
32
38
  }
33
39
 
@@ -1,4 +1,4 @@
1
- import { messages, t } from 'tp-ui/locale'
1
+ import { messages } from 'tp-ui/locale'
2
2
 
3
3
  const defaults = messages().locale.number
4
4
 
@@ -24,28 +24,55 @@ function unformat (input, precision = defaults.precision) {
24
24
  return currency * negative
25
25
  }
26
26
 
27
- function formatMoney (input, toString = false, precision = defaults.precision) {
28
- const negative = ('' + input).indexOf('-') >= 0 ? -1 : 1
27
+ /**
28
+ * Formata dinheiro para float sem arredondar ou para string garantindo decimais completos (.00) e respeitando caracter de decimal
29
+ * @author gteixeira
30
+ * @param {Float} input Valor
31
+ * @param {Boolean} toString Se o retorno será float ou string
32
+ * @param {Object} locale Configuração de localidade para saber número de casas decimais (precision) e caracter de decimal (decimal)
33
+ * @returns
34
+ */
35
+ function formatMoney (input, toString = false, locale = defaults) {
36
+ debugger
37
+ let inputString = ('' + input)
38
+ // Impede de digitar . ou , porque o formatador já adiciona
39
+ if ((inputString.slice(-1).match(/[.,]/))) {
40
+ inputString = inputString.substring(0, inputString.length - 1)
41
+ }
42
+
43
+ const negative = inputString.indexOf('-') >= 0 ? -1 : 1
44
+
29
45
  if (toString) {
30
- input = completeZeros(('' + input), precision)
46
+ input = completeZeros(inputString, locale)
31
47
  }
48
+
49
+ const precision = locale.number.precision
32
50
  const numbers = onlyNumbers(input, precision)
33
51
  const currency = numbersToCurrency(numbers, precision)
34
52
  if (toString) {
35
- const num = negative === -1 ? '-' + currency : '' + currency
36
- return num.replaceAll(',', t('locale.number.decimal')).replaceAll('.', t('locale.number.decimal'))
53
+ let num = negative === -1 ? '-' + currency : '' + currency
54
+ if (locale?.number?.decimal) {
55
+ const decimalChar = locale?.number?.decimal
56
+ num = num.replaceAll(',', decimalChar).replaceAll('.', decimalChar)
57
+ }
58
+
59
+ return num
37
60
  } else {
38
61
  return currency * negative
39
62
  }
40
63
  }
41
64
 
42
- function completeZeros (input, precision = defaults.precision) {
43
- const i = input.indexOf('.')
65
+ function completeZeros (input, locale) {
66
+ const precision = locale?.number?.precision || defaults.precision
67
+ const decimal = locale?.number?.decimal || defaults.decimal
68
+ let i = input.lastIndexOf('.')
69
+ const j = input.lastIndexOf(',')
70
+ i = i > j ? i : j
44
71
  const qntDecimais = i ? input.length - i - 1 : 0
45
72
 
46
73
  // correção para decimal quebrado
47
74
  if (i === -1) {
48
- input = input + '.' + '0'.repeat(precision)
75
+ input = input + decimal + '0'.repeat(precision)
49
76
  } else if (i && qntDecimais < precision) {
50
77
  input = input + '0'.repeat(precision - qntDecimais)
51
78
  }