I have a form with a social object, I want to add an icon to the social notification that has a boolean value and also want to toggle the icon and update the form.
export default function UserProfile(props) {
const form = useForm({
initialValues: {
name: props.userProfile.name,
socials: {
email: {
notifications: true,
},
facebook: {
notifications: false,
},
twitter: {
notifications: false,
},
},
},
});
return (
<>
<form
method="post"
className="pt-5"
key={props.userProfile.id}
onSubmit={form.onSubmit(handleSubmit)}
>
<TextInput
label="Name"
placeholder={props.userProfile.name}
{...form.getInputProps("name")}
/>
<Tabledata
socials={{ ...form.getInputProps("socials") }}
session={session}
/>
<button type="submit">Save</button>
</form>
</>
)
}
The table data component is :
function Tabledata({ socials }) {
// console.log(socials.value["email"]["notifications"]);
function ToggleButton() {
const [isChecked, setIsChecked] = useState(true);
return isChecked? <CheckCircleIcon
className="h-6 w-6 text-green-500"
onClick={() => setIsChecked(!isChecked)}
/>
: <XCircleIcon
className="h-6 w-6 text-red-500"
onClick={() => setIsChecked(!isChecked)}
/>
}
return (
<>
<Table>
<thead>
<tr>
<th>Social channel</th>
<th>Notifications</th>
</tr>
</thead>
<tbody>
{Object.keys(socials.value).map((data, index) => (
<tr key={index}>
<td>{data}</td>
<td>
{socials.value[data]["notifications"]? (
<ToggleButton />
) : (
"Null"
)}
</td>
</tr>
))}
</tbody>
</Table>
</>
);
}
But I'm unable to toggle the value of the notification. How can I approach that? I was trying the above method and also I tried the below method, but not getting the desired result. I'm not sure how to do that.
function Tabledata({ socials }) {
return (
<>
<Table>
<thead>
<tr>
<th>Social channel</th>
<th>Notifications</th>
</tr>
</thead>
<tbody>
{Object.keys(socials.value).map((data, index) => (
<tr key={index}>
<td>{data}</td>
<td>
{socials.value[data]["notifications"]? (
<CheckCircleIcon
className="h-6 w-6 text-green-500"
onClick={() =>
console.log(`change the notification to false`)
}
/>
) : (
<XCircleIcon
className="h-6 w-6 text-red-500"
onClick={() =>
console.log(`change the notification to true`)
}
/>
)}
</td>
</tr>
))}
</tbody>
</Table>
</>
);
}
Please leave you suggestions. I tried different StackOverflow suggestions but could not able to solve my problem.