@tagplus/components 4.8.0 → 4.8.1

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": "4.8.0",
11
+ "version": "4.8.1",
12
12
  "main": "./dist/tp.common.js",
13
13
  "directories": {
14
14
  "lib": "src/lib"
@@ -2,12 +2,13 @@
2
2
  <el-input
3
3
  :id="`${_id}`"
4
4
  class="tp-money"
5
- :value="formattedValue"
5
+ :value="isTyping ? rawValue : formattedValue"
6
6
  v-bind="$attrs"
7
7
  :maxlength="maxlength"
8
- @input="input"
8
+ @input="handleInput"
9
9
  @change="handleChange"
10
- @focus="$event.target.select()"
10
+ @focus="handleFocus"
11
+ @blur="handleBlur"
11
12
  >
12
13
  <template
13
14
  v-if="!removePrepend"
@@ -36,6 +37,7 @@ export default {
36
37
  name: 'TpMoney',
37
38
 
38
39
  mixins: [Locale, floatFormatterMixin],
40
+
39
41
  props: {
40
42
  id: {
41
43
  type: String,
@@ -1,12 +1,14 @@
1
1
  import { formatMoney } from 'tp-ui/utils/currency'
2
2
 
3
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
4
+ * Mixin para formatar valores float.
5
5
  */
6
6
  export default {
7
7
  data () {
8
8
  return {
9
- formattedValue: this.value
9
+ formattedValue: this.value,
10
+ isTyping: false,
11
+ rawValue: ''
10
12
  }
11
13
  },
12
14
 
@@ -15,7 +17,6 @@ export default {
15
17
  type: Boolean,
16
18
  default: false
17
19
  },
18
-
19
20
  customPrecision: {
20
21
  default: false
21
22
  }
@@ -38,16 +39,80 @@ export default {
38
39
  },
39
40
 
40
41
  methods: {
41
- input (evt) {
42
- this.formattedValue = formatMoney(evt, true, this.locale, this.customPrecision)
43
- const moneyFormatado = formatMoney(evt, false, this.locale, this.customPrecision)
42
+ /**
43
+ * Converte o valor de entrada para float.
44
+ * @param {string} value - O valor de entrada.
45
+ * @returns {number} O valor float convertido.
46
+ */
47
+ parseValue (value) {
48
+ if (!value) return 0
49
+ const parts = value.split(',')
50
+ if (parts.length > 1) {
51
+ const precision = this.customPrecision !== false ? this.customPrecision : 2
52
+ parts[1] = parts[1].slice(0, precision)
53
+ }
54
+ return parseFloat(parts.join('.')) || 0
55
+ },
56
+
57
+ /**
58
+ * Manipula o valor de entrada sanitizando-o, limitando a precisão decimal e emitindo o número analisado.
59
+ *
60
+ * Esta função executa os seguintes passos:
61
+ * 1. Remove todos os caracteres do valor de entrada que não são dígitos ou a primeira vírgula.
62
+ * 2. Garante que há no máximo uma vírgula no valor sanitizado.
63
+ * 3. Divide o valor sanitizado em partes inteiras e fracionárias com base na vírgula.
64
+ * 4. Limita a parte fracionária a uma precisão especificada (o padrão é 2 se `customPrecision` não estiver definido).
65
+ * 5. Junta as partes inteiras e fracionárias novamente e atualiza `rawValue`.
66
+ * 6. Converte o `rawValue` para um número float e o emite usando o evento `input`.
67
+ *
68
+ * @param {string} value - O valor de entrada a ser manipulado.
69
+ * @emits input - Emite o valor numérico analisado.
70
+ */
71
+ handleInput (value) {
72
+ // remove todos os caracteres que não são números ou a primeira vírgula
73
+ const sanitized = value.replace(/[^\d,]/g, '')
74
+ const commaCount = (sanitized.match(/,/g) || []).length
75
+
76
+ if (commaCount > 1) return
77
+
78
+ const parts = sanitized.split(',')
79
+ if (parts.length > 1) {
80
+ const precision = this.customPrecision !== false ? this.customPrecision : 2
81
+ parts[1] = parts[1].slice(0, precision)
82
+ this.rawValue = parts.join(',')
83
+ } else {
84
+ this.rawValue = sanitized
85
+ }
86
+
87
+ const numberValue = this.parseValue(this.rawValue)
88
+ this.$emit('input', numberValue)
89
+ },
44
90
 
45
- this.$emit('input', moneyFormatado)
91
+ /**
92
+ * Ao focar o input, o valor existente é selecionado e
93
+ * caso o usuário comece a digitar, o valor é substituído.
94
+ * @param {Event} event - O evento de foco.
95
+ */
96
+ handleFocus (event) {
97
+ this.isTyping = true
98
+ this.rawValue = this.formattedValue
99
+ event.target.select()
100
+ },
101
+
102
+ /**
103
+ * Função responsável por formatar o valor de acordo com a locale
104
+ * e emitir o valor convertido, após o usuário sair do input.
105
+ */
106
+ handleBlur () {
107
+ this.isTyping = false
108
+ const value = this.parseValue(this.rawValue)
109
+ this.formattedValue = formatMoney(value, true, this.locale, this.customPrecision)
110
+ this.$emit('input', value)
46
111
  },
47
112
 
48
113
  handleChange (evt) {
49
- this.$emit('change', formatMoney(evt, false, this.locale, this.customPrecision))
114
+ const value = this.parseValue(evt)
115
+ this.$emit('change', value)
50
116
  }
51
117
  }
52
-
53
118
  }