import React, { useEffect, useState } from "react"; interface WeekdaySelectProps { defaultValue: number[]; onSelect: (selected: number[]) => void; } export const WeekdaySelect = (props: WeekdaySelectProps) => { const [activeDays, setActiveDays] = useState( Array.from(Array(7).keys()).map((v, i) => (props.defaultValue || []).includes(i)) ); const days = ["S", "M", "T", "W", "T", "F", "S"]; useEffect(() => { props.onSelect(activeDays.map((v, idx) => (v ? idx : -1)).filter((v) => v !== -1)); }, [activeDays]); const toggleDay = (idx: number) => { activeDays[idx] = !activeDays[idx]; setActiveDays(([] as boolean[]).concat(activeDays)); }; return (
{days.map((day, idx) => activeDays[idx] ? ( ) : ( ) )}
); };