@tagplus/components 4.7.16 → 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.7.16",
11
+ "version": "4.8.1",
12
12
  "main": "./dist/tp.common.js",
13
13
  "directories": {
14
14
  "lib": "src/lib"
@@ -279,7 +279,8 @@ export default {
279
279
 
280
280
  data () {
281
281
  return {
282
- tooltipVisible: false
282
+ tooltipVisible: false,
283
+ modifiedLabel: null
283
284
  }
284
285
  },
285
286
 
@@ -327,7 +328,10 @@ export default {
327
328
 
328
329
  return this.filterable && this.allowCreate && !hasExistingOption
329
330
  },
330
- rawSuggestionsList () {
331
+ // Monta a lista com ou sem "Cadastrar Novo Item"
332
+ suggestionsList () {
333
+ if (this.loading) return []
334
+
331
335
  // transformando em Array
332
336
  const list =
333
337
  typeof this.suggestions === 'object'
@@ -344,12 +348,6 @@ export default {
344
348
 
345
349
  return list
346
350
  },
347
- // Monta a lista com ou sem "Cadastrar Novo Item"
348
- suggestionsList () {
349
- if (this.loading) return []
350
-
351
- return this.rawSuggestionsList
352
- },
353
351
 
354
352
  emptyText () {
355
353
  if (this.loading) {
@@ -481,11 +479,6 @@ export default {
481
479
  }
482
480
  },
483
481
 
484
- /** Atualiza a label de algum dos elementos da listagem do altosuggest */
485
- atualizarLabelOpcao (index, val) {
486
- this.rawSuggestionsList[index].label = val
487
- },
488
-
489
482
  /**
490
483
  * Sobrescreve o getOption do select para mostrar o labelValue em vez do idValue
491
484
  */
@@ -527,16 +520,13 @@ export default {
527
520
 
528
521
  for (let i = 0; i <= this.cachedOptions.length - 1; i++) {
529
522
  const cachedOption = this.cachedOptions[i]
530
-
531
523
  let isEqual = false
532
-
533
524
  if (isObject) {
534
525
  isEqual = getValueByPath(cachedOption.value, this.valueKey) === getValueByPath(value, this.valueKey)
535
526
  } else {
536
527
  isEqual = cachedOption.value === value
537
-
538
- if (initialLabel && isEqual) {
539
- this.atualizarLabelOpcao(i, initialLabel)
528
+ if (initialLabel) {
529
+ this.modifiedLabel = initialLabel
540
530
  }
541
531
  }
542
532
 
@@ -551,7 +541,7 @@ export default {
551
541
  const label = !isObject && !isNull && !isUndefined ? value : ''
552
542
  const newOption = {
553
543
  value,
554
- currentLabel: initialLabel || label
544
+ currentLabel: this.modifiedLabel || label
555
545
  }
556
546
 
557
547
  if (this.multiple) {
@@ -560,6 +550,7 @@ export default {
560
550
 
561
551
  return newOption
562
552
  },
553
+
563
554
  checkDefaultFirstOption () {
564
555
  this.$nextTick(() => {
565
556
  this.hoverIndex = -1
@@ -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
  }