Skip to content

Commit

Permalink
[@mantine/core] fix: NumberInput no jump to right when pressing backs…
Browse files Browse the repository at this point in the history
…pace on pos 0
  • Loading branch information
jvllmr committed Apr 12, 2024
1 parent afc9929 commit 2082488
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,19 @@ export const NumberInput = factory<NumberInputFactory>((_props, ref) => {

const adjustCursor = (position?: number) => {
if (inputRef.current && position) {
inputRef.current.setSelectionRange(position, position);
try {
inputRef.current.setSelectionRange(position, position);
} catch (error) {
// catch only InvalidStateError DOMException
// using setSelectionChange cannot be used on type='number' inputs,
// but we run into the following issue when not using it:
// https://github.com/mantinedev/mantine/issues/6056
// weirdly enough, the cursor change still takes change even if an error is thrown!?
// this is true for chromium-based browsers and firefox
if (!(error instanceof DOMException) || error.name !== 'InvalidStateError') {
throw error;
}
}
}
};

Expand Down Expand Up @@ -412,6 +424,9 @@ export const NumberInput = factory<NumberInputFactory>((_props, ref) => {
allowNegative={allowNegative}
className={cx(classes.root, className)}
size={size}
// type='number' to avoid cursor issues
// @ts-ignore
type="number"
{...others}
readOnly={readOnly}
disabled={disabled}
Expand Down

0 comments on commit 2082488

Please sign in to comment.