import React, { Dispatch, HTMLAttributes, SetStateAction } from 'react'; import styled from 'styled-components'; export interface Options { label: React.ReactNode; value: string; } export interface CheckboxProps extends HTMLAttributes { children?: React.ReactNode; options: Options[]; checked: string[]; setChecked: Dispatch>; } const CheckboxStyled = styled.div` display: flex; gap: 15px; flex-wrap: wrap; .wrap { display: flex; align-items: center; gap: 5px; > .checkBox { cursor: pointer; } > .label { font-size: 14px; cursor: pointer; user-select: none; } } `; const Checkbox: React.FC = (props) => { const { children, options, checked, setChecked, ...rest } = props; const haveChange = (e: React.MouseEvent) => { const el = e.currentTarget as HTMLDivElement; const input = el.children[0] as HTMLInputElement; if (checked.indexOf(input.value) >= 0) { setChecked((state) => { const arr = [...state]; arr.splice(arr.indexOf(input.value), 1); return arr; }); } else { setChecked((state) => { return [...state, input.value]; }); } }; const render = () => { return options.map((item) => { return (
) => { haveChange(e); }} > = 0} onChange={() => {}} />
{item.label}
); }); }; return {render()}; }; Checkbox.defaultProps = { children: '' }; export default Checkbox;