Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add allowBlank option #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,15 @@ Must use `vmodel.lazy` to bind works properly.

## Properties

| property | Required | Type | Default | Description |
|-----------|----------|---------|---------|---------------------------------------------------------|
| precision | **true** | Number | 2 | How many decimal places |
| decimal | false | String | "." | Decimal separator |
| thousands | false | String | "," | Thousands separator |
| 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 |
| property | Required | Type | Default | Description |
|------------|----------|---------|---------|---------------------------------------------------------|
| precision | **true** | Number | 2 | How many decimal places |
| decimal | false | String | "." | Decimal separator |
| thousands | false | String | "," | Thousands separator |
| 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 |
| allowBlank | false | Boolean | false | If the field can start blank and be cleared out by user |

### References

Expand Down
6 changes: 5 additions & 1 deletion src/component.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<input type="tel"
:value="formattedValue"
@change="change"
v-money="{precision, decimal, thousands, prefix, suffix}"
v-money="{precision, decimal, thousands, prefix, suffix, allowBlank}"
class="v-money" />
</template>

Expand Down Expand Up @@ -42,6 +42,10 @@ export default {
suffix: {
type: String,
default: () => defaults.suffix
},
allowBlank: {
type: Boolean,
default: () => defaults.allowBlank
}
},

Expand Down
11 changes: 10 additions & 1 deletion src/directive.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {format, setCursor, event} from './utils'
import {format, unformat, setCursor, event} from './utils'
import assign from './assign'
import defaults from './options'

Expand All @@ -16,6 +16,15 @@ export default function (el, binding) {
}
}

el.onkeydown = function (e) {
var backspacePressed = e.which == 8 || e.which == 46
var atEndPosition = (el.value.length - el.selectionEnd) === 0
if (opt.allowBlank && backspacePressed && atEndPosition && (unformat(el.value, 0) === 0)) {
el.value = ''
el.dispatchEvent(event('change')) // v-model.lazy
}
}

el.oninput = function () {
var positionFromEnd = el.value.length - el.selectionEnd
el.value = format(el.value, opt)
Expand Down
3 changes: 2 additions & 1 deletion src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export default {
suffix: '',
thousands: ',',
decimal: '.',
precision: 2
precision: 2,
allowBlank: false
}
4 changes: 4 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import defaults from './options'

function format (input, opt = defaults) {
if (opt.allowBlank && input == '') {
return ''
}

if (typeof input === 'number') {
input = input.toFixed(fixed(opt.precision))
}
Expand Down