Prevent invalid values for "auto scroll speed"

The min/max limit was only acknowledged when the value was changed via MUIs step logic.
In case the number got inputted via keystrokes the limits did not get applied
This commit is contained in:
schroda
2024-12-29 22:58:42 +01:00
parent 06d0f7147a
commit 530ae1578f
2 changed files with 5 additions and 2 deletions

View File

@@ -37,3 +37,5 @@ export const getValueFromObject = <T>(obj: Record<string, any>, key: string): T
return keys.reduce((acc, curr) => acc?.[curr], obj) as T;
};
export const coerceIn = (value: number, min: number, max: number): number => Math.min(Math.max(value, min), max);

View File

@@ -17,6 +17,7 @@ import { useRef } from 'react';
import { useReaderAutoScrollContext } from '@/modules/reader/contexts/ReaderAutoScrollContext.tsx';
import { IReaderSettings } from '@/modules/reader/types/Reader.types.ts';
import { AUTO_SCROLL_SPEED } from '@/modules/reader/constants/ReaderSettings.constants.tsx';
import { coerceIn } from '@/lib/HelperFunctions.ts';
export const ReaderNavBarDesktopAutoScroll = ({
autoScroll,
@@ -46,7 +47,7 @@ export const ReaderNavBarDesktopAutoScroll = ({
type="number"
size="small"
onBlur={(e) => {
const value = +e.target.value;
const value = coerceIn(+e.target.value, AUTO_SCROLL_SPEED.min, AUTO_SCROLL_SPEED.max);
if (value !== autoScroll.value) {
clearTimeout(updateTimeout.current);
@@ -54,7 +55,7 @@ export const ReaderNavBarDesktopAutoScroll = ({
}
}}
onChange={(e) => {
const value = +e.target.value;
const value = coerceIn(+e.target.value, AUTO_SCROLL_SPEED.min, AUTO_SCROLL_SPEED.max);
setAutoScroll({ ...autoScroll, value }, false);
clearTimeout(updateTimeout.current);