Skip to content

Commit

Permalink
#8 - Supporting min/max range
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpmartins committed Apr 30, 2021
1 parent dd1828e commit d7bc225
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Feel free to open an issue or post a pull request!
- Component or Directive flavors
- Accept copy/paste
- Editable
- Min / Max Limits

## Usage

Expand Down Expand Up @@ -50,6 +51,8 @@ npm i v-money3 --save
precision: 2,
masked: false,
disableNegative: false,
min: Number.MIN_SAFE_INTEGER,
max: Number.MAX_SAFE_INTEGER,
}
}
}
Expand Down Expand Up @@ -81,6 +84,8 @@ Must use `vmodel.lazy` to bind works properly.
precision: 2,
masked: false /* doesn't work with directive */,
disableNegative: false,
min: Number.MIN_SAFE_INTEGER,
max: Number.MAX_SAFE_INTEGER,
}
}
},
Expand All @@ -100,7 +105,9 @@ 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 |
| disable-negative | false | Boolean | false | Component does not allow negative values |
| disable-negative | false | Boolean | false | Component does not allow negative values |
| min | false | Number | Number.MIN_SAFE_INTEGER | The min value allowed |
| max | false | Number | Number.MAX_SAFE_INTEGER | The max value allowed |

### References

Expand Down
12 changes: 10 additions & 2 deletions src/component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
:id="id"
:value="data.formattedValue"
@change="change"
v-money3="{precision, decimal, thousands, prefix, suffix, disableNegative}"
v-money3="{precision, decimal, thousands, prefix, suffix, disableNegative, min, max}"
class="v-money3" />
</template>

Expand Down Expand Up @@ -53,7 +53,15 @@ export default defineComponent({
disableNegative: {
type: Boolean,
default: false
}
},
max: {
type: Number,
default: () => defaults.max
},
min: {
type: Number,
default: () => defaults.min
},
},
directives: { money3 },
Expand Down
2 changes: 2 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export default {
decimal: '.',
precision: 2,
disableNegative: false,
min: Number.MIN_SAFE_INTEGER,
max: Number.MAX_SAFE_INTEGER,
}
10 changes: 9 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ function format(input, opt = defaults) {
const negative = (!opt.disableNegative) ? (input.indexOf('-') >= 0 ? '-' : '') : ''
const filtered = input.replace(opt.prefix, '').replace(opt.suffix, '')
const numbers = onlyNumbers(filtered)
const currency = numbersToCurrency(numbers, opt.precision)

let currency = Number(numbersToCurrency(numbers, opt.precision));
if (currency > opt.max) {
currency = opt.max
} else if (input < opt.min) {
currency = opt.min
}
currency = currency.toFixed(fixed(opt.precision))

const parts = toStr(currency).split('.')
let integer = parts[0]
const decimal = parts[1]
Expand Down

0 comments on commit d7bc225

Please sign in to comment.