Skip to content
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

Closed
wants to merge 12 commits into from
135 changes: 34 additions & 101 deletions package-lock.json
Copy link
Contributor

@Jacobjeevan Jacobjeevan Dec 23, 2024

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 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLI

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -180,4 +180,4 @@
"node": ">=22.11.0"
},
"packageManager": "[email protected]"
}
}
35 changes: 25 additions & 10 deletions src/components/Form/Form.tsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets not modify the old custom Form component with new Form component from shadcn ui.

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,
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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:

  1. Using JSON.stringify for comparison could be performance-intensive for large forms
  2. Edge case: The form might still show as modified even when values are reset to initial state

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;
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not do this, comparing like this isn't always reliable.

Copy link
Member

@rithviknishad rithviknishad Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like already said in previous review, use shadcn's form

https://ui.shadcn.com/docs/components/form

setIsModified(hasChanges);
}, [state.form]);

const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
Expand Down Expand Up @@ -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?.();
};
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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"
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
Expand Down
Loading