Skip to content

Commit 4806607

Browse files
author
Dipak Sarkar
committed
format at once after found decimal key
1 parent 233b47a commit 4806607

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/core.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ export function getInputElement(el) {
4747
* @param {Number} position
4848
*/
4949
export function updateCursor(el, position) {
50-
const config = el[CONFIG_KEY] && el[CONFIG_KEY].config
51-
position = Math.max(position, config.suffix.length)
52-
position = el.value.length - position
53-
position = Math.max(position, config.prefix.length + 1)
5450
const setSelectionRange = () => { el.setSelectionRange(position, position) }
5551
if (el === document.activeElement) {
5652
setSelectionRange()
@@ -111,10 +107,14 @@ export function inputHandler(event) {
111107
// we can stop propagation of this native event
112108
event.stopPropagation()
113109

114-
const positionFromEnd = target.value.length - target.selectionEnd
115-
const { oldValue } = target[CONFIG_KEY]
110+
let positionFromEnd = target.value.length - target.selectionEnd
111+
const { oldValue, config } = target[CONFIG_KEY]
116112

117113
updateValue(target, null, { emit: false }, event)
114+
// updated cursor position
115+
positionFromEnd = Math.max(positionFromEnd, config.suffix.length)
116+
positionFromEnd = target.value.length - positionFromEnd
117+
positionFromEnd = Math.max(positionFromEnd, config.prefix.length + 1)
118118
updateCursor(target, positionFromEnd)
119119

120120
if (oldValue !== target.value) {

src/directive.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default {
1414
inserted: (el) => {
1515
el = core.getInputElement(el)
1616
const option = el[CONFIG_KEY]
17+
const { config } = option
1718
// prefer adding event listener to parent element to avoid Firefox bug which does not
1819
// execute `useCapture: true` event handlers before non-capturing event handlers
1920
const handlerOwner = el.parentElement || el
@@ -25,6 +26,16 @@ export default {
2526

2627
el.onblur = (e) => core.blurHandler(e)
2728

29+
// check decimal key and insert to current element
30+
el.onkeydown = (e) => {
31+
if (e.key === '.') {
32+
e.preventDefault()
33+
el.setRangeText(config.decimal)
34+
el.dispatchEvent(new Event('input'))
35+
core.updateCursor(el, el.value.length - config.precision - config.suffix.length)
36+
}
37+
}
38+
2839
option.cleanup = () => handlerOwner.removeEventListener('input', oninput, true)
2940
},
3041

0 commit comments

Comments
 (0)