-
Notifications
You must be signed in to change notification settings - Fork 510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed : Disable Submit Button #9526
Changes from 7 commits
f66ab92
3438758
2557354
7d7b8f4
b46c85d
6a8092f
96903ce
2c88392
9193e17
cf2bd9e
aeb32d5
8e179fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,7 @@ | |
"@radix-ui/react-dialog": "^1.1.4", | ||
"@radix-ui/react-dropdown-menu": "^2.1.2", | ||
"@radix-ui/react-icons": "^1.3.2", | ||
"@radix-ui/react-label": "^2.1.0", | ||
"@radix-ui/react-label": "^2.1.1", | ||
"@radix-ui/react-popover": "^1.1.2", | ||
"@radix-ui/react-scroll-area": "^1.2.0", | ||
"@radix-ui/react-slot": "^1.1.1", | ||
|
@@ -102,7 +102,7 @@ | |
"tailwind-merge": "^2.5.5", | ||
"tailwindcss-animate": "^1.0.7", | ||
"use-keyboard-shortcut": "^1.1.6", | ||
"xlsx": "^0.18.5" | ||
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz" | ||
}, | ||
"devDependencies": { | ||
"@julr/vite-plugin-validate-env": "^1.1.1", | ||
|
@@ -180,4 +180,4 @@ | |
"node": ">=22.11.0" | ||
}, | ||
"packageManager": "[email protected]" | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets not modify the old custom Shadcn's form is built to be used with shadcn's form fields, not our form field components. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { useEffect, useMemo, useRef, useState } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
import { Cancel, Submit } from "@/components/Common/ButtonV2"; | ||
import ButtonV2 from "@/components/Common/ButtonV2"; | ||
import { FieldValidator } from "@/components/Form/FieldValidators"; | ||
import { | ||
FormContextValue, | ||
|
@@ -43,10 +44,12 @@ const Form = <T extends FormDetails>({ | |
hideCancelButton = false, | ||
...props | ||
}: Props<T>) => { | ||
const { t } = useTranslation(); | ||
const initial = { form: props.defaults, errors: {} }; | ||
const [isLoading, setIsLoading] = useState(!!asyncGetDefaults); | ||
const [state, dispatch] = useAutoSaveReducer<T>(formReducer, initial); | ||
const formVals = useRef(props.defaults); | ||
const [isModified, setIsModified] = useState(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider optimizing form state comparison The current implementation has potential performance and edge case concerns:
Consider this optimization: - const hasChanges =
- JSON.stringify(state.form) !== JSON.stringify(formVals.current);
- setIsModified(hasChanges);
+ import { isEqual } from 'lodash';
+ const hasChanges = !isEqual(state.form, formVals.current);
+ setIsModified(hasChanges); Also applies to: 63-67 |
||
|
||
useEffect(() => { | ||
if (!asyncGetDefaults) return; | ||
|
@@ -55,7 +58,13 @@ const Form = <T extends FormDetails>({ | |
dispatch({ type: "set_form", form }); | ||
setIsLoading(false); | ||
}); | ||
}, [asyncGetDefaults]); | ||
}, [asyncGetDefaults, dispatch]); | ||
|
||
useEffect(() => { | ||
const hasChanges = | ||
JSON.stringify(state.form) !== JSON.stringify(formVals.current); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not do this, comparing like this isn't always reliable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. like already said in previous review, use shadcn's form |
||
setIsModified(hasChanges); | ||
}, [state.form]); | ||
|
||
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
@@ -88,6 +97,7 @@ const Form = <T extends FormDetails>({ | |
const handleCancel = () => { | ||
if (props.resetFormValsOnCancel) { | ||
dispatch({ type: "set_form", form: formVals.current }); | ||
setIsModified(false); | ||
} | ||
props.onCancel?.(); | ||
}; | ||
|
@@ -141,17 +151,22 @@ const Form = <T extends FormDetails>({ | |
</div> | ||
<div className="flex flex-col-reverse justify-end gap-3 sm:flex-row"> | ||
{!hideCancelButton && ( | ||
<Cancel | ||
<ButtonV2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use shadcn buttons instead (src/components/ui). |
||
variant="secondary" | ||
onClick={handleCancel} | ||
label={props.cancelLabel ?? "Cancel"} | ||
/> | ||
disabled={disabled} | ||
> | ||
{props.cancelLabel ?? t("common:cancel")} | ||
</ButtonV2> | ||
)} | ||
<Submit | ||
data-testid="submit-button" | ||
<ButtonV2 | ||
variant="primary" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure the data-testid is not being used in tests first, before removing them. |
||
type="submit" | ||
disabled={disabled} | ||
label={props.submitLabel ?? "Submit"} | ||
/> | ||
disabled={disabled || !isModified} | ||
data-testid="submit-button" | ||
> | ||
{props.submitLabel ?? t("common:submit")} | ||
</ButtonV2> | ||
</div> | ||
</Provider> | ||
</DraftSection> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't be in the commit.
How are you committing your merge changes? Cli or GUI 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CLI