From 208248827b00b39b060f4c49f1f980ea0e0ccc8a Mon Sep 17 00:00:00 2001 From: Jan Vollmer Date: Fri, 12 Apr 2024 22:28:45 +0200 Subject: [PATCH] [@mantine/core] fix: NumberInput no jump to right when pressing backspace on pos 0 --- .../src/components/NumberInput/NumberInput.tsx | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/@mantine/core/src/components/NumberInput/NumberInput.tsx b/packages/@mantine/core/src/components/NumberInput/NumberInput.tsx index 15510f2961c..68f91dc19fa 100644 --- a/packages/@mantine/core/src/components/NumberInput/NumberInput.tsx +++ b/packages/@mantine/core/src/components/NumberInput/NumberInput.tsx @@ -256,7 +256,19 @@ export const NumberInput = factory((_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; + } + } } }; @@ -412,6 +424,9 @@ export const NumberInput = factory((_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}