const [checkItems, setCheckItems] = useState<any[]>([]);
const handleSingleCheck = (checked: any, id: any) => {
if (checked) {
setCheckItems((prev: any) => [...prev, id]);
} else {
setCheckItems(checkItems.filter((el: any) => el !== id));
}
};
const handleAllCheck = (checked: any) => {
if (checked) {
const idArray: any = [];
memberList.forEach((el: any) => idArray.push(el.memberId));
setCheckItems(idArray);
} else {
setCheckItems([]);
}
};
const dataList2 = (data: any) => {
return (
<S.DataList>
data.map((el: any, index: any) => {
return (
<S.Container key={index}>
<div>{el.memberId}</div>
<div>{el.name}</div>
<div>{el.memberStatus}</div>
<div>
<S.CheckBox
type='checkbox'
name={`select-${el.memberId}`}
onChange={(e) =>
handleSingleCheck(e.target.checked, el.memberId)}
checked={checkItems.includes(el.memberId) ? true : false}
/>
</div>
</S.Container>
);
})
)
</S.DataList>
);
};
const TitleList = () => {
return (
<S.AdminTitle>
<div>No</div>
<div>회원명</div>
<div>상태</div>
<div>
<S.CheckBox
type='checkbox'
name='select-all'
onChange={(e) => handleAllCheck(e.target.checked)}
checked={checkItems.length === memberList.length ? true : false}
/>
</div>
</S.AdminTitle>
);
};
😎 선택된 리스트를 axios로 전송
const deleteMember = async () => {
checkItems.map((el: any) => {
return axios({
method: 'get',
url: `${process.env.REACT_APP_API_URL}/admin/withdrawal/${el}`,
});
});
await axios({
method: 'get',
url: `${process.env.REACT_APP_API_URL}/admin/member/list?
size=${size}&page=${page}&${sort}&name=${memberName}
&status=${filter}`,
}).then((res) => {
setMemberList(res.data.content);
setTotalPages(res.data.totalElements);
setCheckItems([]);
});
}
};
styled-component
import styled from 'styled-components';
export const DataList = styled.div`
margin-bottom: 0.24rem;
`;
export const AdminTitle = styled.div`
width: 14.4rem;
margin-top: 0.24rem;
display: flex;
background-color: ${({ theme }) => theme.palette.whitegreen};
padding: 0.24rem 0.9rem 0.24rem 0.54rem;
align-items: center;
border-top: 0.03rem solid ${({ theme }) => theme.palette.txtgray};
font-size: 0.24rem;
div {
&:nth-child(1) {
flex-grow: 1.85;
}
&:nth-child(2) {
flex-grow: 2;
}
&:nth-child(3) {
flex-grow: 2;
}
&:nth-child(4) {
}
}
`;
export const Container = styled.div`
cursor: pointer;
width: 14.4rem;
display: flex;
align-items: center;
padding: 0.24rem 0.9rem 0.24rem 0.58rem;
border-bottom: 0.01rem solid ${({ theme }) => theme.palette.lightgray};
font-size: 0.24rem;
div {
&:nth-child(1) {
// flex-grow: 2;
width: 2rem;
}
&:nth-child(2) {
width: 4.4rem;
text-align: center;
}
&:nth-child(3) {
width: 4.8rem;
text-align: center;
}
&:nth-child(4) {
width: 2rem;
text-align: right;
}
}
`;
export const CheckBox = styled.input.attrs(() => ({
type: 'checkbox',
}))`
accent-color: ${({ theme }) => theme.palette.green};
width: 0.32rem;
height: 0.32rem;
`;