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

Fix #6837 - Updated onInputKeyDown() on InputNumber so that it works as expected #6845

Merged
merged 1 commit into from
Nov 25, 2024
Merged
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
11 changes: 9 additions & 2 deletions packages/primevue/src/inputnumber/InputNumber.vue
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ export default {

let selectionStart = event.target.selectionStart;
let selectionEnd = event.target.selectionEnd;
let selectionRange = selectionEnd - selectionStart;
let inputValue = event.target.value;
let newValueStr = null;
const code = event.code || event.key;
Expand All @@ -401,14 +402,20 @@ export default {
break;

case 'ArrowLeft':
if (!this.isNumeralChar(inputValue.charAt(selectionStart - 1))) {
if (selectionRange > 1) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, this switch case block was behaving as follows: if the character to the left of the current cursor position is not a numeral (or a valid part of the number format, such as a decimal point, grouping character, or minus sign), is to prevent the default action (cursor movement), This ensures that the user cannot accidentally move the cursor to the prefix for example.

However, this logic did not account for cases where there is a text selection, often occurring when users navigate between fields using the TAB key. In such cases, selectionStart would be 0, which coincides with the prefix (if provided). This resulted in the ArrowLeft and ArrowRight keys not working as expected, leaving users unable to properly adjust the cursor position.

const cursorPosition = this.isNumeralChar(inputValue.charAt(selectionStart)) ? selectionStart + 1 : selectionStart + 2;
this.$refs.input.$el.setSelectionRange(cursorPosition, cursorPosition);
} else if (!this.isNumeralChar(inputValue.charAt(selectionStart - 1))) {
event.preventDefault();
}

break;

case 'ArrowRight':
if (!this.isNumeralChar(inputValue.charAt(selectionStart))) {
if (selectionRange > 1) {
const cursorPosition = selectionEnd - 1;
this.$refs.input.$el.setSelectionRange(cursorPosition, cursorPosition);
} else if (!this.isNumeralChar(inputValue.charAt(selectionStart))) {
event.preventDefault();
}

Expand Down
Loading