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(date-picker): first keypress after focus should be treated as new input #383

Merged
merged 1 commit into from
Mar 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
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ export class GuxDatepicker {
@State()
active: boolean = false;

/**
* Tracks the amount of key presses when focusing on the input.
*/
@State()
pressedKeys: Array<string> = [];
321gillian marked this conversation as resolved.
Show resolved Hide resolved

@Watch('value')
watchValue() {
this.updateDate();
Expand Down Expand Up @@ -240,6 +246,7 @@ export class GuxDatepicker {
);
this.setIntervalRange(previousIntervalRange);
this.setCursorRange();
this.pressedKeys.length = 0;
break;
}
case 'ArrowRight': {
Expand All @@ -251,6 +258,7 @@ export class GuxDatepicker {
);
this.setIntervalRange(nextIntervalRange);
this.setCursorRange();
this.pressedKeys.length = 0;
break;
}
default:
Expand Down Expand Up @@ -312,6 +320,7 @@ export class GuxDatepicker {
@OnClickOutside({ triggerEvents: 'mousedown' })
onClickOutside() {
this.active = false;
this.pressedKeys.length = 0;
}

/********* Event Handlers **********/
Expand Down Expand Up @@ -362,6 +371,7 @@ export class GuxDatepicker {
}

updateIntervalValue(event: KeyboardEvent): void {
this.pressedKeys.push(event.key);
const inputNumber = parseInt(event.key, 10);

if (!isNaN(inputNumber)) {
Expand All @@ -377,7 +387,7 @@ export class GuxDatepicker {
) {
this.typeYearValue(currentSectionValue, event.key);
} else {
if (this.canSetDate(inputNumber)) {
if (this.canSetDate(inputNumber) && this.pressedKeys.length >= 2) {
this.updateSelection(
this.focusedField,
`${currentSectionValue[1]}${event.key}`
Expand Down Expand Up @@ -479,7 +489,7 @@ export class GuxDatepicker {
}
}

canSetDate(key: number) {
canSetDate(key: number): boolean {
const newValue = parseInt(
[
this.focusedField.value[this.intervalRange.selectionEnd - 1].toString(),
Expand Down
Loading