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

refactor: clean up updateValueAndCaretPosition #813

Merged
merged 2 commits into from
Feb 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
36 changes: 11 additions & 25 deletions src/number_format_base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,46 +155,33 @@ export default function NumberFormatBase<BaseType = InputAttributes>(
| React.FocusEvent<HTMLInputElement>
| React.KeyboardEvent<HTMLInputElement>;
source: SourceType;
caretPos?: number;
setCaretPosition?: Boolean;
}) => {
const {
formattedValue: newFormattedValue = '',
input,
setCaretPosition = true,
source,
event,
numAsString,
} = params;
let { caretPos } = params;
let caretPos;

if (input) {
//calculate caret position if not defined
if (caretPos === undefined && setCaretPosition) {
const inputValue = params.inputValue || input.value;
const inputValue = params.inputValue || input.value;

const currentCaretPosition = geInputCaretPosition(input);

/**
* set the value imperatively, this is required for IE fix
* This is also required as if new caret position is beyond the previous value.
* Caret position will not be set correctly
*/
input.value = newFormattedValue;

//get the caret position
caretPos = getNewCaretPosition(inputValue, newFormattedValue, currentCaretPosition);
}
const currentCaretPosition = geInputCaretPosition(input);

/**
* set the value imperatively, as we set the caret position as well imperatively.
* This is to keep value and caret position in sync
* set the value imperatively, this is required for IE fix
* This is also required as if new caret position is beyond the previous value.
* Caret position will not be set correctly
*/
input.value = newFormattedValue;

//set caret position, and value imperatively when element is provided
if (setCaretPosition && caretPos !== undefined) {
//set caret position
//get the caret position
caretPos = getNewCaretPosition(inputValue, newFormattedValue, currentCaretPosition);

//set caret position imperatively
if (caretPos !== undefined) {
setPatchedCaretPosition(input, caretPos, newFormattedValue);
}
}
Expand Down Expand Up @@ -287,7 +274,6 @@ export default function NumberFormatBase<BaseType = InputAttributes>(
inputValue,
event,
source,
setCaretPosition: true,
input: event.target as HTMLInputElement,
});

Expand Down
Loading