Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: show error text on settings number input's out of range (#9171)

01zulfi4e2fd9dde61b
diff --git a/apps/web/src/dialogs/settings/index.tsx b/apps/web/src/dialogs/settings/index.tsx
index 3000ce857..6d3bfa7fd 100644
--- a/apps/web/src/dialogs/settings/index.tsx
+++ b/apps/web/src/dialogs/settings/index.tsx
@@ -44,7 +44,7 @@ import {
} from "../../components/icons";
import NavigationItem from "../../components/navigation-menu/navigation-item";
import { FlexScrollContainer } from "../../components/scroll-container";
-import { useCallback, useEffect, useState } from "react";
+import { useCallback, useEffect, useRef, useState } from "react";
import {
DropdownSettingComponent,
SectionGroup,
@@ -580,34 +580,13 @@ function SettingItem(props: { item: Setting }) {
);
case "input":
return component.inputType === "number" ? (
- <Input
+ <NumberInput
key={component.defaultValue()}
- type={"number"}
min={component.min}
max={component.max}
step={component.step}
defaultValue={component.defaultValue()}
- sx={{ width: 80, mr: 1 }}
- onChange={debounce((e) => {
- let value = e.target.valueAsNumber;
- value =
- Number.isNaN(value) || value < component.min
- ? component.min
- : value > component.max
- ? component.max
- : value;
- component.onChange(value);
- }, 500)}
- onBlur={(e) => {
- let value = e.target.valueAsNumber;
- value =
- Number.isNaN(value) || value < component.min
- ? component.min
- : value > component.max
- ? component.max
- : value;
- component.onChange(value);
- }}
+ onChange={(value) => component.onChange(value)}
/>
) : (
<Input
@@ -677,3 +656,64 @@ export function SelectComponent(props: Omit<DropdownSettingComponent, "type">) {
</select>
);
}
+
+type NumberInputProps = {
+ min: number;
+ max: number;
+ step?: number;
+ defaultValue: number;
+ onChange: (value: number) => void;
+};
+
+function NumberInput({
+ min,
+ max,
+ step,
+ defaultValue,
+ onChange
+}: NumberInputProps) {
+ const [isInputValid, setIsInputValid] = useState(true);
+ const inputRef = useRef<HTMLInputElement>(null);
+
+ return (
+ <Flex sx={{ flexDirection: "column", alignItems: "flex-end" }}>
+ <Input
+ ref={inputRef}
+ type={"number"}
+ min={min}
+ max={max}
+ step={step}
+ defaultValue={defaultValue}
+ sx={{
+ width: 80,
+ mr: 1,
+ outline: isInputValid
+ ? undefined
+ : "2px solid var(--accent-error) !important"
+ }}
+ onChange={debounce((e) => {
+ const value = e.target.valueAsNumber;
+ const isValid = !Number.isNaN(value) && value >= min && value <= max;
+ setIsInputValid(isValid);
+ if (isValid) onChange(value);
+ }, 500)}
+ onBlur={(e) => {
+ const raw = e.target.valueAsNumber;
+ const clamped =
+ Number.isNaN(raw) || raw < min ? min : raw > max ? max : raw;
+ if (inputRef.current) inputRef.current.value = String(clamped);
+ setIsInputValid(true);
+ onChange(clamped);
+ }}
+ />
+ {!isInputValid && (
+ <Text
+ variant="subBody"
+ sx={{ fontSize: 11, color: "error", mt: 1, mr: 1 }}
+ >
+ {strings.valueMustBeBetween(min, max)}
+ </Text>
+ )}
+ </Flex>
+ );
+}
diff --git a/packages/intl/locale/en.po b/packages/intl/locale/en.po
index 08873c0b3..771fd8d5d 100644
--- a/packages/intl/locale/en.po
+++ b/packages/intl/locale/en.po
@@ -7052,6 +7052,10 @@ msgstr "Using official Notesnook instance"
msgid "v{version} available"
msgstr "v{version} available"
+#: src/strings.ts:2644
+msgid "Value must be between {min} and {max}"
+msgstr "Value must be between {min} and {max}"
+
#: src/strings.ts:1171
msgid "Vault"
msgstr "Vault"
diff --git a/packages/intl/locale/pseudo-LOCALE.po b/packages/intl/locale/pseudo-LOCALE.po
index b20e9c5bb..0b15cea02 100644
--- a/packages/intl/locale/pseudo-LOCALE.po
+++ b/packages/intl/locale/pseudo-LOCALE.po
@@ -7002,6 +7002,10 @@ msgstr ""
msgid "v{version} available"
msgstr ""
+#: src/strings.ts:2644
+msgid "Value must be between {min} and {max}"
+msgstr ""
+
#: src/strings.ts:1171
msgid "Vault"
msgstr ""
diff --git a/packages/intl/src/strings.ts b/packages/intl/src/strings.ts
index 3b6599563..e2b4f24e7 100644
--- a/packages/intl/src/strings.ts
+++ b/packages/intl/src/strings.ts
@@ -2640,5 +2640,7 @@ Use this if changes from other devices are not appearing on this device. This wi
back: () => t`Back`,
invalidRecoveryKey: () =>
t`Invalid recovery key. Make sure to input your account recovery key, not a 2FA recovery code.`,
- featureNotAvailable: () => t`This feature is not available on this plan.`
+ featureNotAvailable: () => t`This feature is not available on this plan.`,
+ valueMustBeBetween: (min: number, max: number) =>
+ t`Value must be between ${min} and ${max}`
};