From f766045f6ddcfb8ec29643ab75ae158576074844 Mon Sep 17 00:00:00 2001 From: Florian Demmer Date: Wed, 8 Jan 2025 16:14:31 +0000 Subject: [PATCH] presets and sets: trim names --- src/components/presets/item.tsx | 11 ++++++----- src/components/presets/save.tsx | 5 +++-- src/components/sets/item.tsx | 11 ++++++----- src/components/sets/save.tsx | 5 +++-- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/components/presets/item.tsx b/src/components/presets/item.tsx index 622e7d2d..775186a4 100644 --- a/src/components/presets/item.tsx +++ b/src/components/presets/item.tsx @@ -53,14 +53,15 @@ export const PresetItem: FunctionComponent = memo(function Wrap const onRenamePreset = useCallback((e: FormEvent) => { e.preventDefault(); inputRef.current?.focus(); - if (preset.name === name) { + const trimmedName = name.trim(); + if (preset.name === trimmedName) { setIsEditing(false); - } else if (!name?.length) { + } else if (!trimmedName?.length) { setError("Please provide a valid preset name"); - } else if (!validateUniqueName(name)) { - setError(`A preset with the name "${name}" already exists`); + } else if (!validateUniqueName(trimmedName)) { + setError(`A preset with the name "${trimmedName}" already exists`); } else { - onRename(preset, name); + onRename(preset, trimmedName); } }, [name, onRename, preset, setError, inputRef, setIsEditing, validateUniqueName]); diff --git a/src/components/presets/save.tsx b/src/components/presets/save.tsx index 0f04e128..a24bace5 100644 --- a/src/components/presets/save.tsx +++ b/src/components/presets/save.tsx @@ -20,11 +20,12 @@ export const SavePresetForm: FunctionComponent = memo(funct const onSavePreset = (e: FormEvent): void => { e.preventDefault(); - if (!name?.length) { + const trimmedName = name.trim(); + if (!trimmedName?.length) { setError("Please provide a valid preset name"); } else { setError(undefined); - onSave(name); + onSave(trimmedName); setName(""); } }; diff --git a/src/components/sets/item.tsx b/src/components/sets/item.tsx index 4cff2954..bd662f38 100644 --- a/src/components/sets/item.tsx +++ b/src/components/sets/item.tsx @@ -39,14 +39,15 @@ export const GraphSetItem: FunctionComponent = memo(function const onRenameSet = useCallback((e: FormEvent) => { e.preventDefault(); inputRef.current?.focus(); - if (set.name === name) { + const trimmedName = name.trim(); + if (set.name === trimmedName) { setIsEditing(false); - } else if (!name?.length) { + } else if (!trimmedName?.length) { setError("Please provide a valid set name"); - } else if (!validateUniqueName(name)) { - setError((`A set with the name "${name} already exists"`)); + } else if (!validateUniqueName(trimmedName)) { + setError((`A set with the name "${trimmedName} already exists"`)); } else { - onRename(set, name); + onRename(set, trimmedName); } }, [name, onRename, set, setError, inputRef, setIsEditing, validateUniqueName]); diff --git a/src/components/sets/save.tsx b/src/components/sets/save.tsx index 3e271954..8cf270c7 100644 --- a/src/components/sets/save.tsx +++ b/src/components/sets/save.tsx @@ -19,11 +19,12 @@ export const SaveGraphSetForm: FunctionComponent = memo(f const onSaveSet = (e: FormEvent): void => { e.preventDefault(); - if (!name?.length) { + const trimmedName = name.trim(); + if (!trimmedName?.length) { setError("Please provide a valid set name"); } else { setError(undefined); - onSave(name); + onSave(trimmedName); setName(""); } };