How to make the checkbox such that if the header checkbox is checked then all other checkboxes will be checked and unchecked if all other checkboxes will be unchecked? Also if other checkboxes are selected then the header checkbox should be undeterminate using react with typescript?
I have the following code in App.tsx
import React, {useState} from "react";
import { useQuery } from "react-query";
import { GraphQLClient, gql } from "graphql-request";
import "./index.css";
import Button from "./Components/Button";
const API_URL = `https://graphqlzero.almansi.me/api`;
const graphQLClient = new GraphQLClient(API_URL, {
headers: {
Authorization: `Bearer ${process.env.API_KEY}`,
},
});
export function useGetPosts() {
return useQuery("get-posts", async () => {
const {
users: { data },
} = await graphQLClient.request(gql`
{
users {
data {
id
name
username
email
address {
street
}
phone
website
}
}
}
`);
return data;
});
}
const App = () => {
const [checked, setChecked] = useState(false)
const handleClick = () => setChecked(!checked)
const response = useGetPosts();
const { data } = response;
console.log({ response });
// address: {street: 'Kulas Light'}
// email: "[email protected]"
// id: "1"
// name: "Leanne Graham"
// phone: "1-770-736-8031 x56442"
// username: "Bret"
// website: "hildegard.org"
return (
<div>
<section>
<div className="header">Users
<form>
<input
type="text"
placeholder="Search Here" />
</form>
<Button name = "New User" />
</div>
<header>
<div className="col" >
<input
onClick={handleClick} checked={checked}
type="checkbox"
className = "column"
/>
</div>
<div className="col">Name</div>
<div className="col">Username</div>
<div className="col">Email</div>
<div className="col">Phone</div>
<div className="col">Website</div>
<div className="col">Address</div>
</header>
{data?.map((item: any) => (
<>
<div className="row">
<div className="col">
<input
onClick={handleClick} checked={checked}
type="checkbox" />
</div>
<div className="col">{item?.name}</div>
<div className="col">{item?.username}</div>
<div className="col">{item?.email}</div>
<div className="col">{item?.phone}</div>
<div className="col">{item?.website}</div>
<div className="col">{item?.address?.street}</div>
</div>
</>
))}
</section>
</div>
);
};
export default App;