You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In my code below , I am trying to update the values of the subjectName using a Select component.
From the debugging , the watch function and the field.value at the render function of the Controller gives the updated selected option.
But upon submission , the subjectName value is set to undefined.
alternatively , i used an input field (which is commented out above the Controller component )to check if the names are correctly set. And so when i update using input field, values received upon submission are updated /expected values.
I am using the reset function to load values when the component mounts.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In my code below , I am trying to update the values of the subjectName using a Select component.
From the debugging , the watch function and the field.value at the render function of the Controller gives the updated selected option.
But upon submission , the subjectName value is set to undefined.
alternatively , i used an input field (which is commented out above the Controller component )to check if the names are correctly set. And so when i update using input field, values received upon submission are updated /expected values.
I am using the reset function to load values when the component mounts.
the code I am using is;
`const initialAssignments = [
{
id: 1,
name: "10AM - 11AM",
rooms: [
{
roomId: 1,
teacherId: 1,
teacherName: "Mr. Smith",
subjectName: "english.L1.S1",
},
{
roomId: 2,
teacherId: 2,
teacherName: "Ms. Johnson",
subjectName: "english.L1.S2",
},
{
roomId: 3,
teacherId: 3,
teacherName: "Mr. Lee",
subjectName: "french.L1.S3",
},
{
roomId: 4,
teacherId: 4,
teacherName: "Mr. doe",
subjectName: "french.L2.S1",
},
{
roomId: 5,
teacherId: 4,
teacherName: "Mr. will",
subjectName: "french.L2.S1",
},
{
roomId: 6,
teacherId: 4,
teacherName: "Mr. sam",
subjectName: "french.L2.S1",
},
],
},
...
]
const NUM_ROOMS = 6
export default function TimeTable() {
// const [assignments, setAssignments] = useState(initialAssignments)
const [isEditMode, setEditMode] = useState(false)
const {
register,
handleSubmit,
control,
formState,
reset,
} = useForm({
resolver: zodResolver(timetableSchema),
// defaultValues: [...initialAssignments],
})
console.log(formState.errors)
useEffect(() => {
reset([...initialAssignments])
}, [reset])
const {
data: currentTermClassesData,
// isLoading: _currentTermClassesLoading,
} = useQuery({
queryKey: [api.admin.classes.findCurrentTermAllClass.querykey],
queryFn: api.admin.classes.findCurrentTermAllClass.query,
})
const transformedData = useMemo(() => {
const data: { value: string; label: string }[] = []
}, [currentTermClassesData])
const onSubmit = (data: TimetableSchema) => {
console.log("submite", data)
setEditMode(false)
}
// console.log(transformedData)
console.log(getValues(), "GETVALUE")
return (
Time Table
Edit and Manage your timeTable
<button
type="button"
onClick={() => setEditMode(!isEditMode)}
className="block rounded-md bg-indigo-600 px-3 py-2 text-center text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
>
{isEditMode ? "Cancel" : "Edit"}
submit
{Array.from({ length: NUM_ROOMS }, (_, index) => (
))}
{initialAssignments.map((timeslot, index) => {
return (
{timeslot.rooms.map((room, roomIndex) => {
return (
<td
key={room.roomId}
className={
px-3 py-3.5 text-sm text-gray-500 border-4 shadow-sm w-32 h-20 ${ !room.teacherName && !room.subjectName && !isEditMode ? "bg-red-300" : "" } ${ room.teacherName && room.subjectName && !isEditMode ? "bg-green-300" : "" }
}>
{isEditMode ? (
<input
type="text"
defaultValue={room.teacherName}
{...register(
${index}.rooms.${roomIndex}.teacherName
as const)}
className={
mb-2 block w-full rounded-md shadow-sm
}/>
)
}
`
Time/ Rooms
Room {index + 1}
{isEditMode ? (
<input
type="text"
defaultValue={timeslot.name}
{...register(
${index}.name
as const)}className="rounded-md border-gray-300 shadow-s"
/>
{formState.errors?.[index]?.rooms?.root
?.message && (
<>
Unique Teacher Name required
</>
)}
) : (
{timeslot.name}
)}
Beta Was this translation helpful? Give feedback.
All reactions