Skip to content

Commit

Permalink
Allow null value & v-model.number config + handle input of type number
Browse files Browse the repository at this point in the history
- Dev should be allowed to set input to null and expect v-model to
  retain the null value. Requesting a reconsideration against returning
a string as in commit 95b7f83

- Handle v-model.number modifier correctly. Whether the input is string,
  number or null, the resulting emit() should return the same type or
null. Requesting this as an alternative to resolve vuetifyjs#2137.

- Handle input of type number. Likewise, the resulting emit() should
  return the same type. Requesting this as an alternative to resolve vuetifyjs#2136.
  • Loading branch information
azaars committed Oct 11, 2017
1 parent 96a095a commit 8137c0f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/components/VTextField/VTextField.js
Expand Up @@ -90,7 +90,8 @@ export default {
// Inner unmaskText() strips off non-alphanum
// maskText() masks and removes dirty alphanum
// Outer unmaskText() provides a clean lazyValue
this.lazyValue = this.unmaskText(this.maskText(this.unmaskText(val)))
const value = this.unmaskText(this.maskText(this.unmaskText(val)))
this.lazyValue = typeof val === 'number' ? +value : value
this.mask ? this.setSelectionRange()
: this.$emit('input', this.returnMaskedValue
? this.$refs.input.value : this.lazyValue)
Expand Down Expand Up @@ -118,8 +119,9 @@ export default {
value (val) {
// Value was changed externally, update lazy
const masked = this.maskText(this.unmaskText(val))
const value = this.unmaskText(masked)

this.lazyValue = this.unmaskText(masked)
this.lazyValue = typeof val === 'number' ? +value : value
!this.validateOnBlur && this.validate()
this.shouldAutoGrow && this.calculateInputHeight()

Expand Down
20 changes: 12 additions & 8 deletions src/mixins/maskable.js
Expand Up @@ -67,10 +67,12 @@ export default {
}

selection = 0
for (const char of newText) {
isMaskDelimiter(char) || position--
selection++
if (position <= 0) break
if (newText) {
for (const char of newText) {
isMaskDelimiter(char) || position--
selection++
if (position <= 0) break
}
}

this.$nextTick(() => {
Expand All @@ -94,10 +96,12 @@ export default {
let selection = 0

this.$refs.input.value = newValue
for (const char of newValue) {
if (this.lazySelection <= 0) break
isMaskDelimiter(char) || this.lazySelection--
selection++
if (newValue) {
for (const char of newValue) {
if (this.lazySelection <= 0) break
isMaskDelimiter(char) || this.lazySelection--
selection++
}
}

this.setCaretPosition(selection)
Expand Down
9 changes: 5 additions & 4 deletions src/util/mask.js
Expand Up @@ -90,14 +90,15 @@ const maskValidates = (mask, char) => {
* @return {String}
*/
export const maskText = (text, masked, dontFillMaskBlanks) => {
if (text == null) return ''
text = String(text)
if (!masked.length || !text.length) return text
if (!masked.length || !text) return text
if (!Array.isArray(masked)) masked = masked.split('')

let textIndex = 0
let maskIndex = 0
let newText = ''

text = String(text)

while (maskIndex < masked.length) {
const mask = masked[maskIndex]

Expand Down Expand Up @@ -134,5 +135,5 @@ export const maskText = (text, masked, dontFillMaskBlanks) => {
* @return {String}
*/
export const unmaskText = (text) => {
return text ? text.replace(new RegExp(defaultDelimiters, 'g'), '') : text
return text ? String(text).replace(new RegExp(defaultDelimiters, 'g'), '') : text
}

0 comments on commit 8137c0f

Please sign in to comment.