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: cursor position when formatter appends characters on empty string #652

Merged
merged 6 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 3 additions & 3 deletions src/hooks/useCursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export default function useCursor(

let startPos = value.length;

if (value.endsWith(afterTxt)) {
startPos = value.length - selectionRef.current.afterTxt.length;
} else if (value.startsWith(beforeTxt)) {
if (value.startsWith(beforeTxt)) {
startPos = beforeTxt.length;
} else if (value.endsWith(afterTxt)) {
startPos = value.length - selectionRef.current.afterTxt.length;
} else {
const beforeLastChar = beforeTxt[start - 1];
const newIndex = value.indexOf(beforeLastChar, start - 1);
Expand Down
10 changes: 10 additions & 0 deletions tests/cursor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,14 @@ describe('InputNumber.Cursor', () => {
});
});
});

describe('append string', () => {
it('position caret before appended characters', () => {
const { container } = render(<InputNumber formatter={(value) => `${value}%`} parser={(value) => value.replace('%', '')} />);
const input = container.querySelector('input');
fireEvent.focus(input)
afc163 marked this conversation as resolved.
Show resolved Hide resolved
fireEvent.change(input,{ target: { value: '5' } });
expect(cursorInput(input)).toEqual(1);
});
});
});
Loading