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(form): fix tab field content not updating #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion formule-demo/cypress/e2e/builder.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ describe("test basic functionality", () => {
cy.addFieldWithName("tabView", "mytab");
cy.getByDataCy("treeItem").contains("mytab").as("tabField");
cy.addFieldWithName("text", "myfield1", "@tabField");
cy.addFieldWithName("text", "myfield2", "@tabField");
cy.addFieldWithName("checkbox", "myfield2", "@tabField");

cy.getByDataCy("formPreview")
.find(".ant-menu-item")
Expand All @@ -639,13 +639,18 @@ describe("test basic functionality", () => {
cy.getByDataCy("formPreview")
.find(`input#root${SEP}mytab${SEP}myfield1`)
.should("exist");
// Verify that the content refreshes properly
cy.getByDataCy("formPreview")
.find(".ant-menu-item")
.contains("myfield2")
.click();
cy.getByDataCy("formPreview")
.find(`input#root${SEP}mytab${SEP}myfield2`)
.as("checkboxWidget")
.should("exist");
cy.get("@checkboxWidget").should("not.be.checked");
cy.get("@checkboxWidget").click();
cy.get("@checkboxWidget").should("be.checked");
});

it("tests code editor field", () => {
Expand Down
35 changes: 15 additions & 20 deletions src/forms/templates/Tabs/TabField.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect, useContext } from "react";
import { useState, useEffect, useContext, useMemo } from "react";
import PropTypes from "prop-types";
import {
Col,
Expand All @@ -18,6 +18,8 @@ const TabField = ({ uiSchema, properties, idSchema }) => {
const { useBreakpoint } = Grid;
const screens = useBreakpoint();
const customizationContext = useContext(CustomizationContext);
const [anchor, setAnchor] = useState("");
const [scroll, setScroll] = useState(false);

let options = uiSchema["ui:options"] || {};

Expand All @@ -38,9 +40,18 @@ const TabField = ({ uiSchema, properties, idSchema }) => {
? analysis_mode[0].content.props.formData == "true"
: false,
);
const [anchor, setAnchor] = useState("");
const [activeTabContent, setActiveTabContent] = useState([]);
const [scroll, setScroll] = useState(false);

const activeTabContent = useMemo(() => {
let activeTab = tabs.filter((prop) => prop.name == active);
if (options.tabs) {
return properties.filter(
(prop) =>
activeTab[0].content && activeTab[0].content.indexOf(prop.name) > -1,
);
} else {
return activeTab;
}
}, [active, options.tabs, properties, tabs]);

const updateActive = (newActiveTab) => {
setActive(newActiveTab);
Expand All @@ -61,22 +72,6 @@ const TabField = ({ uiSchema, properties, idSchema }) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
let activeTab = tabs.filter((prop) => prop.name == active);
if (options.tabs) {
setActiveTabContent(
properties.filter(
(prop) =>
activeTab[0].content &&
activeTab[0].content.indexOf(prop.name) > -1,
),
);
} else {
setActiveTabContent(activeTab);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [active]);

useEffect(() => {
if (anchor) {
const items = anchor.split(customizationContext.separator);
Expand Down
Loading