Skip to content

Commit df5aa72

Browse files
committed
web: show error text on settings number input's out of range
Signed-off-by: 01zulfi <85733202+01zulfi@users.noreply.github.com>
1 parent 7e87cf7 commit df5aa72

File tree

4 files changed

+70
-14
lines changed

4 files changed

+70
-14
lines changed

apps/web/src/dialogs/settings/index.tsx

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -580,23 +580,12 @@ function SettingItem(props: { item: Setting }) {
580580
);
581581
case "input":
582582
return component.inputType === "number" ? (
583-
<Input
584-
type={"number"}
583+
<NumberInput
585584
min={component.min}
586585
max={component.max}
587586
step={component.step}
588587
defaultValue={component.defaultValue()}
589-
sx={{ width: 80, mr: 1 }}
590-
onChange={debounce((e) => {
591-
let value = e.target.valueAsNumber;
592-
value =
593-
Number.isNaN(value) || value < component.min
594-
? component.min
595-
: value > component.max
596-
? component.max
597-
: value;
598-
component.onChange(value);
599-
}, 500)}
588+
onChange={(value) => component.onChange(value)}
600589
/>
601590
) : (
602591
<Input
@@ -665,3 +654,60 @@ export function SelectComponent(props: Omit<DropdownSettingComponent, "type">) {
665654
</select>
666655
);
667656
}
657+
658+
type NumberInputProps = {
659+
min: number;
660+
max: number;
661+
step?: number;
662+
defaultValue: number;
663+
onChange: (value: number) => void;
664+
};
665+
666+
function NumberInput({
667+
min,
668+
max,
669+
step,
670+
defaultValue,
671+
onChange
672+
}: NumberInputProps) {
673+
const [isInputValid, setIsInputValid] = useState(true);
674+
675+
return (
676+
<Flex sx={{ flexDirection: "column", alignItems: "flex-end" }}>
677+
<Input
678+
type={"number"}
679+
min={min}
680+
max={max}
681+
step={step}
682+
defaultValue={defaultValue}
683+
sx={{
684+
width: 80,
685+
mr: 1,
686+
outline: isInputValid
687+
? undefined
688+
: "2px solid var(--accent-error) !important"
689+
}}
690+
onChange={debounce((e) => {
691+
let value = e.target.valueAsNumber;
692+
const isValid = !Number.isNaN(value) && value >= min && value <= max;
693+
setIsInputValid(isValid);
694+
value =
695+
Number.isNaN(value) || value < min
696+
? min
697+
: value > max
698+
? max
699+
: value;
700+
onChange(value);
701+
}, 500)}
702+
/>
703+
{!isInputValid && (
704+
<Text
705+
variant="subBody"
706+
sx={{ fontSize: 11, color: "error", mt: 1, mr: 1 }}
707+
>
708+
{strings.valueMustBeBetween(min, max)}
709+
</Text>
710+
)}
711+
</Flex>
712+
);
713+
}

packages/intl/locale/en.po

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6988,6 +6988,10 @@ msgstr "Using official Notesnook instance"
69886988
msgid "v{version} available"
69896989
msgstr "v{version} available"
69906990

6991+
#: src/strings.ts:2627
6992+
msgid "Value must be between {min} and {max}"
6993+
msgstr "Value must be between {min} and {max}"
6994+
69916995
#: src/strings.ts:1171
69926996
msgid "Vault"
69936997
msgstr "Vault"

packages/intl/locale/pseudo-LOCALE.po

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6938,6 +6938,10 @@ msgstr ""
69386938
msgid "v{version} available"
69396939
msgstr ""
69406940

6941+
#: src/strings.ts:2627
6942+
msgid "Value must be between {min} and {max}"
6943+
msgstr ""
6944+
69416945
#: src/strings.ts:1171
69426946
msgid "Vault"
69436947
msgstr ""

packages/intl/src/strings.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2622,5 +2622,7 @@ Use this if changes from other devices are not appearing on this device. This wi
26222622
t`Your free trial is on-going. Your subscription will start on ${trialExpiryDate}`,
26232623
weekFormat: () => t`Week format`,
26242624
weekFormatDesc: () =>
2625-
t`Choose what day to display as the first day of the week`
2625+
t`Choose what day to display as the first day of the week`,
2626+
valueMustBeBetween: (min: number, max: number) =>
2627+
t`Value must be between ${min} and ${max}`
26262628
};

0 commit comments

Comments
 (0)