mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-08-19 13:01:44 +10:00
fix: address review feedback for margin input clamping
Move clamp helpers above PageSectionForm for consistent scope/indentation, and only persist finite numeric values so empty or invalid input cannot enter resume metadata. Co-authored-by: Amruth Pillai <im.amruth@gmail.com>
This commit is contained in:
co-authored by
Amruth Pillai
parent
3e2c991344
commit
f55e584e34
@@ -29,6 +29,11 @@ const formSchema = pageSchema;
|
||||
|
||||
type FormValues = z.infer<typeof formSchema>;
|
||||
|
||||
const CLAMP_MIN = 0;
|
||||
const CLAMP_MAX = 100;
|
||||
|
||||
const clamp = (value: number, min: number, max: number) => Math.min(Math.max(value, min), max);
|
||||
|
||||
function PageSectionForm() {
|
||||
const resume = useResume();
|
||||
const page = resume?.data.metadata.page;
|
||||
@@ -53,16 +58,11 @@ function PageSectionForm() {
|
||||
persist({ ...form.state.values, [name]: value });
|
||||
};
|
||||
|
||||
const CLAMP_MIN = 0;
|
||||
const CLAMP_MAX = 100;
|
||||
|
||||
const clamp = (value: number, min: number, max: number) => Math.min(Math.max(value, min), max);
|
||||
|
||||
const pageNumberFields = [
|
||||
{ name: "marginX" as const, label: <Trans>Margin (Horizontal)</Trans>, min: CLAMP_MIN, max: CLAMP_MAX },
|
||||
{ name: "marginY" as const, label: <Trans>Margin (Vertical)</Trans>, min: CLAMP_MIN, max: CLAMP_MAX },
|
||||
{ name: "gapX" as const, label: <Trans>Spacing (Horizontal)</Trans>, min: 0, max: undefined },
|
||||
{ name: "gapY" as const, label: <Trans>Spacing (Vertical)</Trans>, min: 0, max: undefined },
|
||||
const pageNumberFields = [
|
||||
{ name: "marginX" as const, label: <Trans>Margin (Horizontal)</Trans>, min: CLAMP_MIN, max: CLAMP_MAX },
|
||||
{ name: "marginY" as const, label: <Trans>Margin (Vertical)</Trans>, min: CLAMP_MIN, max: CLAMP_MAX },
|
||||
{ name: "gapX" as const, label: <Trans>Spacing (Horizontal)</Trans>, min: 0, max: undefined },
|
||||
{ name: "gapY" as const, label: <Trans>Spacing (Vertical)</Trans>, min: 0, max: undefined },
|
||||
];
|
||||
|
||||
const pageSwitchFields = [
|
||||
@@ -156,8 +156,13 @@ const pageNumberFields = [
|
||||
onBlur={field.handleBlur}
|
||||
onChange={(e) => {
|
||||
const v = e.target.value;
|
||||
const raw = v === "" ? ("" as unknown as number) : Number(v);
|
||||
const num = max !== undefined && typeof raw === "number" ? clamp(raw, min ?? 0, max) : raw;
|
||||
// Ignore transient empty/invalid input so resume metadata stays numeric
|
||||
if (v === "") return;
|
||||
|
||||
const raw = Number(v);
|
||||
if (!Number.isFinite(raw)) return;
|
||||
|
||||
const num = max !== undefined ? clamp(raw, min ?? 0, max) : Math.max(raw, min ?? 0);
|
||||
field.handleChange(num);
|
||||
handleAutoSave(name, num);
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user