Skip to content

Commit

Permalink
Merge branch 'master' into clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yadav authored Feb 25, 2024
2 parents f49c5a4 + fe3db3c commit 5882307
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/number_format_base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default function NumberFormatBase<BaseType = InputAttributes>(
setCaretPosition(el, caretPos);

timeout.current.setCaretTimeout = setTimeout(() => {
if (el.value === currentValue && el.selectionStart !== el.selectionEnd) {
if (el.value === currentValue && el.selectionStart !== caretPos) {
setCaretPosition(el, caretPos);
}
}, 0);
Expand Down Expand Up @@ -369,6 +369,7 @@ export default function NumberFormatBase<BaseType = InputAttributes>(
if (e.persist) e.persist();

const el = e.target;
const currentTarget = e.currentTarget;
focusedElm.current = el;

timeout.current.focusTimeout = setTimeout(() => {
Expand All @@ -384,7 +385,7 @@ export default function NumberFormatBase<BaseType = InputAttributes>(
setPatchedCaretPosition(el, caretPosition, value);
}

onFocus(e);
onFocus({ ...e, currentTarget });
}, 0);
};

Expand Down
16 changes: 16 additions & 0 deletions test/library/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,22 @@ describe('NumberFormat as input', () => {
}, 0);
});

it('should contain currentTarget on focus event', async () => {
let currentTarget;
const { input } = await render(
<NumericFormat
value="1234"
onFocus={(e) => {
currentTarget = e.currentTarget;
}}
/>,
);
input.focus();

await wait(0);
expect(currentTarget).not.toBeNull();
});

it('should not reset the selection when manually focused on mount', async () => {
function Test() {
const localInputRef = useRef();
Expand Down
21 changes: 21 additions & 0 deletions test/library/keypress_and_caret.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import NumericFormat from '../../src/numeric_format';
import PatternFormat from '../../src/pattern_format';
import NumberFormatBase from '../../src/number_format_base';
import { cleanup, fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import {
simulateFocusEvent,
Expand Down Expand Up @@ -182,6 +183,26 @@ describe('Test keypress and caret position changes', () => {
expect(input.selectionStart).toEqual(2);
});

it('should not reset caret position if caret is updated by browser after we set caret position #811', async () => {
// https://codesandbox.io/p/sandbox/recursing-poitras-rxtjkj?file=%2Fsrc%2Findex.test.js%3A15%2C5-15%2C44
const { input } = await render(
<NumericFormat
allowLeadingZeros={false}
allowNegative={false}
decimalSeparator="."
displayType="input"
placeholder="people"
suffix=" people"
valueIsNumericString={false}
/>,
);


await userEvent.type(input, '91');

expect(input.value).toEqual('91 people');
});

describe('Test character insertion', () => {
it('should add any number properly when input is empty without format prop passed', async () => {
const { input } = await render(<NumericFormat thousandSeparator={true} prefix={'$'} />);
Expand Down

0 comments on commit 5882307

Please sign in to comment.