未加星标

VueJS : update input value without losing cursor position

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

In my recent project I came across a requirement which need to format the input value while typing. More preceisly I need to format the number into comma seperated format while the user types. In the first glance it seems to be easy, but when we tried one specific issue caught us.

The issue is when you format the value in input the cursor jumps to the end of input which gives bad experience for the user.


VueJS : update input value without losing cursor position

This blog post will explain how we solved it and gave our users better experience. Since the blog post is regarding cursor postion, we won’t go into the details on formatting the input value.

Capture current cursor position

To start first we will captiure the current position of cursor on every input change and keep this in state.

<template> <input :value="formatedValue" @input="handleInput" /> </template> <script> import formatNumber from 'accounting-js/lib/formatNumber'; import unformat from 'accounting-js/lib/unformat'; export default { name: "CommaFormattedNumber", props: { value: { type: String, default: "", required: true, } }, data() { return { formatedValue: this.processFormatting(this.value), position: 0, }; }, watch: { value() { this.formatedValue = this.processFormatting(this.value); } }, methods: { handleInput(e) { this.prevValue = e.target.value; let targetValue = unformat(e.target.value); this.position = e.target.selectionStart; this.formatedValue = formatNumber(targetValue) this.$emit("input", this.formatedValue); }, processFormatting(value) { // process formatting } } }; </script>

The CommaFormattedNumber component will accept the value as prop, format as comma seperated and render in input. On input change we will get the cursor position using e.target.selectionStart and seti it in the state.

Using custom directives

Now we have the current position of the cursor in the state, Next we need to set the cursor postion on input using selectionEnd after the VNode update. This can be achieved using custom directives in VueJS.

The VueJs directives have update hook function which we use for this. But there is a catch. we can’t access the this object inside the update. It will receive the element which is updated as the first argument. Since there is no this we can’t get the this.position in update. To by-pass this we decided to set the position as data attribute to input element.

<template> <input :value="formatedValue" @input="handleInput" :data-position="position" /> </template>

Now we have the position of cursor available inside the update method and can be accessed as e.dataset.position .

<script> import formatNumber from 'accounting-js/lib/formatNumber'; import unformat from 'accounting-js/lib/unformat'; export default { name: "CommaFormattedNumber", props: { value: { type: String, default: "", required: true, } }, data() { return { formatedValue: this.processFormatting(this.value), position: 0, }; }, directives: { formatWithComma: { update(e) { if (e.selectionEnd !== e.dataset.position) { e.selectionEnd = Number(e.dataset.position); } } } }, // other methods and watch }; </script>

This will give basic fix, but needed some corner case handling etc which I skipped here. The full code is available on codesandbox


VueJS : update input value without losing cursor position
VueJS : update input value without losing cursor position

This is now published as a node module vue-comma-formatted-number

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:VueJS : update input value without losing cursor position
本站链接:https://www.codesec.net/view/610662.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(63)