Skip to content

Commit 9bc7098

Browse files
authored
fix(core): keep the caret in place when backspace has nothing to delete (#113)
1 parent 4c35096 commit 9bc7098

4 files changed

Lines changed: 34 additions & 6 deletions

File tree

packages/core/src/modules/input-controller/international-input-controller/__tests__/delete-operations.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ describe('InternationalInputController.deleteBackward', () => {
5252
expect(state.selectionEnd).toBe(1);
5353
});
5454

55+
it('stores the no-op caret in currentState when backspacing right after the fixed "+"', () => {
56+
const controller = createInternationalInputController({
57+
defaultRegion: 'US',
58+
display: { callingCodeInInput: true, plusPrefix: 'fixed' },
59+
});
60+
61+
controller.deleteBackward('+1 ', 1, 1);
62+
63+
// Adapters render from currentState: the stored selection must match the no-op caret.
64+
const current = controller.currentState;
65+
expect(current.value).toBe('+1 ');
66+
expect(current.selectionStart).toBe(1);
67+
expect(current.selectionEnd).toBe(1);
68+
});
69+
5570
it('deletes the last digit when callingCodeInInput is false (national-only display)', () => {
5671
const controller = createInternationalInputController({
5772
defaultRegion: 'US',

packages/core/src/modules/input-controller/international-input-controller/international-input-controller.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,8 @@ class InternationalInputController implements InputController {
220220
const position: number = findPreviousDigitPosition(value, selectionStart);
221221

222222
if (position === -1) {
223-
return toInputState(
224-
this.#resolveState(value, { insertText: '', selectionStart, selectionEnd }, 'backward', this.#plusErased),
225-
);
223+
this.#history.updateCurrentSelection(selectionStart, selectionEnd);
224+
return toInputStateWithSelection(this.#history.current, selectionStart, selectionEnd);
226225
}
227226

228227
effectiveStart = position;

packages/core/src/modules/input-controller/national-input-controller/__tests__/delete-operations.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ describe('NationalInputController.deleteBackward', () => {
1717
expect(state.selectionEnd).toBe(0);
1818
});
1919

20+
it('stores the no-op caret in currentState when no digit precedes the caret', () => {
21+
const controller = createNationalInputController({ defaultRegion: 'US' });
22+
const seeded = controller.setValue('4155550132');
23+
expect(seeded.value).toBe('(415) 555-0132');
24+
25+
// Position 1 sits after "(" with no digit before it: the delete is a no-op.
26+
controller.deleteBackward(seeded.value, 1, 1);
27+
28+
// Adapters render from currentState: the stored selection must match the no-op caret.
29+
const current = controller.currentState;
30+
expect(current.value).toBe('(415) 555-0132');
31+
expect(current.selectionStart).toBe(1);
32+
expect(current.selectionEnd).toBe(1);
33+
});
34+
2035
it('deletes the digit before the caret through a formatting char (caret just after "011 ")', () => {
2136
const controller = createNationalInputController({ defaultRegion: 'AR' });
2237
controller.setValue(AR_RAW);

packages/core/src/modules/input-controller/national-input-controller/national-input-controller.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,8 @@ class NationalInputController implements InputController {
184184
const position: number = findPreviousDigitPosition(value, selectionStart);
185185

186186
if (position === -1) {
187-
return toInputState(
188-
this.#resolveFromEdit(value, { insertText: '', selectionStart, selectionEnd }, 'backward', true),
189-
);
187+
this.#history.updateCurrentSelection(selectionStart, selectionEnd);
188+
return toInputStateWithSelection(this.#history.current, selectionStart, selectionEnd);
190189
}
191190

192191
effectiveStart = position;

0 commit comments

Comments
 (0)