-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #359 from ai-cfia/328-ingredients-form
issue #328: ingredients form
- Loading branch information
Showing
7 changed files
with
151 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { FormComponentProps, LabelData, UNITS } from "@/types/types"; | ||
import { Box } from "@mui/material"; | ||
import { useEffect } from "react"; | ||
import { FormProvider, useForm, useWatch } from "react-hook-form"; | ||
import VerifiedBilingualTable from "./VerifiedBilingualTable"; | ||
|
||
function IngredientsForm({ labelData, setLabelData }: FormComponentProps) { | ||
const methods = useForm<LabelData>({ | ||
defaultValues: labelData, | ||
}); | ||
|
||
const { control } = methods; | ||
|
||
const watchedIngredients = useWatch({ | ||
control, | ||
name: "ingredients", | ||
}); | ||
|
||
useEffect(() => { | ||
if (watchedIngredients) { | ||
setLabelData((prevLabelData) => ({ | ||
...prevLabelData, | ||
ingredients: watchedIngredients, | ||
})); | ||
} | ||
}, [watchedIngredients, setLabelData]); | ||
|
||
return ( | ||
<FormProvider {...methods}> | ||
<Box className="p-4" data-testid="ingredients-form"> | ||
<VerifiedBilingualTable | ||
path={"ingredients"} | ||
unitOptions={UNITS.ingredients} | ||
valueColumn | ||
/> | ||
</Box> | ||
</FormProvider> | ||
); | ||
} | ||
|
||
export default IngredientsForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { DEFAULT_LABEL_DATA, LabelData } from "@/types/types"; | ||
import { render, screen } from "@testing-library/react"; | ||
import { useEffect, useState } from "react"; | ||
import { FormProvider, useForm } from "react-hook-form"; | ||
import IngredientsForm from "../IngredientsForm"; | ||
|
||
const Wrapper = ({ | ||
initialData, | ||
onStateChange, | ||
}: { | ||
initialData: LabelData; | ||
onStateChange?: (data: LabelData) => void; | ||
}) => { | ||
const [labelData, setLabelData] = useState(initialData); | ||
const methods = useForm({ | ||
defaultValues: labelData, | ||
}); | ||
|
||
useEffect(() => { | ||
if (onStateChange) { | ||
onStateChange(labelData); | ||
} | ||
}, [labelData, onStateChange]); | ||
|
||
return ( | ||
<FormProvider {...methods}> | ||
<IngredientsForm labelData={labelData} setLabelData={setLabelData} /> | ||
</FormProvider> | ||
); | ||
}; | ||
|
||
describe("IngredientsForm Rendering", () => { | ||
it("should render the title input fields and nutrients table", () => { | ||
render(<Wrapper initialData={DEFAULT_LABEL_DATA} />); | ||
|
||
expect(screen.getByTestId("ingredients-form")).toBeInTheDocument(); | ||
expect( | ||
screen.getByTestId("table-container-ingredients"), | ||
).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters