From 19ae25d4a72637c29a7144c9824fce88f6e94c96 Mon Sep 17 00:00:00 2001 From: Guy Schlider Date: Fri, 15 Sep 2017 14:49:18 +0300 Subject: [PATCH 1/2] Support min/max limits --- README.md | 9 +++++++-- src/component.vue | 16 +++++++++++++++- src/docs/docs.vue | 17 ++++++++++++++++- src/options.js | 4 +++- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6d89762..c070d7d 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ - Component or Directive flavors - Accept copy/paste - Editable +- Min/Max Range. For other types of mask, use [vue-the-mask](https://vuejs-tips.github.io/vue-the-mask) @@ -49,7 +50,9 @@ Vue.use(money, {precision: 4}) prefix: 'R$ ', suffix: ' #', precision: 2, - masked: false + masked: false, + min: 0, + max: 10000 } } } @@ -79,7 +82,7 @@ Must use `vmodel.lazy` to bind works properly. prefix: 'R$ ', suffix: ' #', precision: 2, - masked: false /* doesn't work with directive */ + masked: false /* doesn't work with directive */, } } }, @@ -99,6 +102,8 @@ Must use `vmodel.lazy` to bind works properly. | prefix | false | String | "" | Currency symbol followed by a Space, like "R$ " | | suffix | false | String | "" | Percentage for example: " %" | | masked | false | Boolean | false | If the component output should include the mask or not | +| min | false | Number | null | Limit min value, when null no limit | +| max | false | Number | null | Limit max value, when null no limit | ### References diff --git a/src/component.vue b/src/component.vue index cfb5cbc..b48a77b 100644 --- a/src/component.vue +++ b/src/component.vue @@ -2,7 +2,7 @@ @@ -42,6 +42,14 @@ export default { suffix: { type: String, default: () => defaults.suffix + }, + min: { + type: Number, + default: () => defaults.min + }, + max: { + type: Number, + default: () => defaults.max } }, @@ -67,6 +75,12 @@ export default { methods: { change (evt) { + var unMaskedValue = unformat(evt.target.value, this.precision) + if (this.min !== null && unMaskedValue < this.min) { + evt.target.value = format(this.min, this.$props) + } else if (this.max !== null && unMaskedValue > this.max) { + evt.target.value = format(this.max, this.$props) + } this.$emit('input', this.masked ? evt.target.value : unformat(evt.target.value, this.precision)) } } diff --git a/src/docs/docs.vue b/src/docs/docs.vue index 2f0df75..4bae657 100644 --- a/src/docs/docs.vue +++ b/src/docs/docs.vue @@ -75,6 +75,21 @@
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+