@tagplus/components 1.0.6 → 1.0.7

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.0.6",
11
+ "version": "1.0.7",
12
12
  "main": "./dist/tp.common.js",
13
13
  "directories": {
14
14
  "lib": "src/lib"
@@ -10,15 +10,44 @@ export default {
10
10
  usarVirgula: {
11
11
  type: Boolean,
12
12
  default: true
13
+ },
14
+ // Se false permite campo vazio
15
+ stringDefaultsZero: {
16
+ type: Boolean,
17
+ default: true
18
+ }
19
+ },
20
+
21
+ data () {
22
+ return {
23
+ currentValue: ''
13
24
  }
14
25
  },
15
26
 
16
27
  computed: {
28
+ minDisabled () {
29
+ return this.value < this.min
30
+ },
31
+
32
+ maxDisabled () {
33
+ return this.value > this.max
34
+ },
35
+
17
36
  displayValue () {
18
37
  if (this.userInput !== null) {
38
+ // corrige o bug para deixar limpar o campo
39
+ if (!this.stringDefaultsZero && this.userInput === '') {
40
+ this.currentValue = ''
41
+ }
19
42
  return this.userInput
20
43
  }
21
- let currentValue = this.currentValue
44
+
45
+ let currentValue
46
+ if (this.value === '' && !this.stringDefaultsZero) {
47
+ currentValue = ''
48
+ } else {
49
+ currentValue = this.currentValue
50
+ }
22
51
 
23
52
  if (typeof currentValue === 'number') {
24
53
  if (this.stepStrictly) {
@@ -43,16 +72,65 @@ export default {
43
72
  }
44
73
  },
45
74
 
75
+ watch: {
76
+ value: {
77
+ immediate: true,
78
+ handler (value) {
79
+ let newVal
80
+ if (value === undefined) {
81
+ newVal = value
82
+ } else if (!this.stringDefaultsZero && value === '') {
83
+ newVal = ''
84
+ } else {
85
+ newVal = Number(value)
86
+ }
87
+
88
+ if (newVal !== undefined) {
89
+ if (isNaN(newVal)) {
90
+ return
91
+ }
92
+
93
+ if (this.stepStrictly) {
94
+ const stepPrecision = this.getPrecision(this.step)
95
+ const precisionFactor = Math.pow(10, stepPrecision)
96
+ newVal = Math.round(newVal / this.step) * precisionFactor * this.step / precisionFactor
97
+ }
98
+
99
+ if (this.precision !== undefined) {
100
+ newVal = this.toPrecision(newVal, this.precision)
101
+ }
102
+ }
103
+ if (newVal >= this.max) newVal = this.max
104
+ if (newVal <= this.min) newVal = this.min
105
+ this.currentValue = newVal
106
+ this.userInput = null
107
+ this.$emit('input', newVal)
108
+ }
109
+ }
110
+ },
111
+
46
112
  methods: {
47
113
  handleInput (value) {
48
114
  this.userInput = value
49
- this.$emit('change', Number(value.replace(',', '.')))
115
+ let newVal
116
+ if (value === '' && this.stringDefaultsZero) {
117
+ newVal = Number(value.replace(',', '.'))
118
+ } else {
119
+ newVal = value
120
+ }
121
+
122
+ this.$emit('change', newVal)
50
123
  },
51
124
 
52
125
  handleInputChange (value) {
53
126
  let newVal
54
127
  if (value === '') {
55
- newVal = 0
128
+ // Por padrão converte '' para 0
129
+ if (this.stringDefaultsZero) {
130
+ newVal = 0
131
+ } else {
132
+ newVal = ''
133
+ }
56
134
  } else {
57
135
  newVal = Number(value.replace(',', '.'))
58
136
  }
@@ -61,6 +139,20 @@ export default {
61
139
  this.setCurrentValue(newVal)
62
140
  }
63
141
  this.userInput = null
142
+ },
143
+
144
+ setCurrentValue (newVal) {
145
+ const oldVal = this.currentValue
146
+ if (typeof newVal === 'number' && this.precision !== undefined) {
147
+ newVal = this.toPrecision(newVal, this.precision)
148
+ }
149
+ if (newVal >= this.max) newVal = this.max
150
+ if (newVal <= this.min) newVal = this.min
151
+ if (oldVal === newVal && newVal !== '') return
152
+ this.userInput = null
153
+ this.$emit('input', newVal)
154
+ this.$emit('change', newVal, oldVal)
155
+ this.currentValue = newVal
64
156
  }
65
157
  }
66
158
  }