From dadcdca489e42d0e4c78a6083e29a07cc5b65821 Mon Sep 17 00:00:00 2001 From: Jelle van der Waa Date: Thu, 25 Jan 2024 10:42:05 +0100 Subject: [PATCH] Add validation for the rename input This is mostly the same as the create directory validation. --- src/fileActions.jsx | 51 ++++++++++++++++++++++++++---------------- test/check-application | 15 +++++++++++++ 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/fileActions.jsx b/src/fileActions.jsx index 83edc2c0..30464af8 100644 --- a/src/fileActions.jsx +++ b/src/fileActions.jsx @@ -252,18 +252,8 @@ export const CreateDirectoryModal = ({ selected, currentPath }) => { { - setErrorMessage(undefined); - setName(val); - - if (val === "") { - setNameError(_("Directory name cannot be empty.")); - } else if (val.length >= 256) { - setNameError(_("Directory name too long.")); - } else { - setNameError(null); - } - }} + value={name} + onChange={(_, val) => setDirectoryName(val, setName, setNameError, setErrorMessage)} id="create-directory-input" autoFocus // eslint-disable-line jsx-a11y/no-autofocus /> @@ -277,6 +267,7 @@ export const CreateDirectoryModal = ({ selected, currentPath }) => { export const RenameItemModal = ({ path, selected, setHistory, setHistoryIndex }) => { const Dialogs = useDialogs(); const [name, setName] = useState(selected.name); + const [nameError, setNameError] = useState(null); const [errorMessage, setErrorMessage] = useState(undefined); let title; @@ -301,11 +292,18 @@ export const RenameItemModal = ({ path, selected, setHistory, setHistoryIndex }) variant={ModalVariant.medium} isOpen onClose={Dialogs.close} - footer={errorMessage === undefined && - <> - - - } + footer={ + <> + + + + } > {errorMessage !== undefined && @@ -315,11 +313,13 @@ export const RenameItemModal = ({ path, selected, setHistory, setHistoryIndex }) isInline />}
- + setName(val)} + value={name} + onChange={(_, val) => setDirectoryName(val, setName, setNameError, setErrorMessage)} id="rename-item-input" /> +
@@ -568,3 +568,16 @@ export const EditPermissionsModal = ({ selected, path }) => { ); }; + +const setDirectoryName = (val, setName, setNameError, setErrorMessage) => { + setErrorMessage(undefined); + setName(val); + + if (val === "") { + setNameError(_("Directory name cannot be empty.")); + } else if (val.length >= 256) { + setNameError(_("Directory name too long.")); + } else { + setNameError(null); + } +}; diff --git a/test/check-application b/test/check-application index c958ff05..85355973 100755 --- a/test/check-application +++ b/test/check-application @@ -483,6 +483,21 @@ class TestNavigator(testlib.MachineCase): self.enter_navigator() + # validation + b.click("#dropdown-menu") + b.click("#rename-item") + b.set_input_text("#rename-item-input", "test") + b.set_input_text("#rename-item-input", "") + b.wait_visible("button.pf-m-primary:disabled") + b.wait_in_text("#rename-item-input-helper", "Directory name cannot be empty.") + + b.set_input_text("#rename-item-input", "a" * 256) + b.wait_visible("button.pf-m-primary:disabled") + b.wait_in_text("#rename-item-input-helper", "Directory name too long.") + b.set_input_text("#rename-item-input", "test") + b.wait_visible("button.pf-m-primary:not(:disabled)") + b.click(".pf-v5-c-modal-box__footer button.pf-m-link") # cancel + # Rename file m.execute("touch /home/admin/newfile") self.rename_item(b, "newfile", "newfile1")