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

fix(web): disable the save button when creating a new item with invalid metadata #1327

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions web/e2e/project/item/metadata/text.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ test("Text metadata editing has succeeded", async ({ page }) => {
await expect(page.getByRole("main")).toContainText("new text1 description");
await expect(page.getByRole("textbox").nth(0)).toHaveValue("text2");
await expect(page.getByRole("textbox").nth(1)).toHaveValue("text1");
await page.getByLabel("new text1(unique)").click();
await page.getByLabel("new text1(unique)").fill("text22");
await expect(page.getByRole("button", { name: "Save" })).toBeDisabled();
await page.getByLabel("new text1(unique)").fill("text2");

await page.getByRole("button", { name: "Save" }).click();
await closeNotification(page);
Expand Down
19 changes: 18 additions & 1 deletion web/src/components/molecules/Content/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,19 @@ const ContentForm: React.FC<Props> = ({
}
}, [itemId, item, metaForm, onMetaItemUpdate, model?.metadataSchema?.fields]);

const handleMetaValuesChange = useCallback(async () => {
if (itemId) return;
try {
await metaForm.validateFields();
} catch (e) {
if ((e as ValidateErrorEntity).errorFields.length > 0) {
setIsDisabled(true);
return;
}
}
setIsDisabled(false);
}, [itemId, metaForm]);

const items: MenuProps["items"] = useMemo(() => {
const menuItems = [
{
Expand Down Expand Up @@ -656,7 +669,11 @@ const ContentForm: React.FC<Props> = ({
</FormItemsWrapper>
</StyledForm>
<SideBarWrapper>
<Form form={metaForm} layout="vertical" initialValues={initialMetaFormValues}>
<Form
form={metaForm}
layout="vertical"
initialValues={initialMetaFormValues}
onValuesChange={handleMetaValuesChange}>
<ContentSidebarWrapper item={item} onNavigateToRequest={onNavigateToRequest} />
{model?.metadataSchema?.fields?.map(field => {
const FieldComponent = FIELD_TYPE_COMPONENT_MAP[field.type];
Expand Down