2021-06-24 22:15:18 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2021-06-14 18:53:20 +00:00
|
|
|
|
2021-10-20 15:42:40 +00:00
|
|
|
interface WeekdaySelectProps {
|
|
|
|
defaultValue: number[];
|
|
|
|
onSelect: (selected: number[]) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const WeekdaySelect = (props: WeekdaySelectProps) => {
|
2022-05-14 13:49:39 +00:00
|
|
|
const { onSelect } = props;
|
2021-10-20 15:42:40 +00:00
|
|
|
const [activeDays, setActiveDays] = useState<boolean[]>(
|
|
|
|
Array.from(Array(7).keys()).map((v, i) => (props.defaultValue || []).includes(i))
|
2021-06-24 22:15:18 +00:00
|
|
|
);
|
2021-06-14 18:53:20 +00:00
|
|
|
|
2021-06-24 22:15:18 +00:00
|
|
|
const days = ["S", "M", "T", "W", "T", "F", "S"];
|
2021-06-14 18:53:20 +00:00
|
|
|
|
2021-06-24 22:15:18 +00:00
|
|
|
useEffect(() => {
|
2022-05-14 13:49:39 +00:00
|
|
|
onSelect(activeDays.map((v, idx) => (v ? idx : -1)).filter((v) => v !== -1));
|
|
|
|
}, [onSelect, activeDays]);
|
2021-06-16 22:27:27 +00:00
|
|
|
|
2021-10-20 15:42:40 +00:00
|
|
|
const toggleDay = (idx: number) => {
|
2021-06-14 18:53:20 +00:00
|
|
|
activeDays[idx] = !activeDays[idx];
|
2021-10-20 15:42:40 +00:00
|
|
|
setActiveDays(([] as boolean[]).concat(activeDays));
|
2021-06-24 22:15:18 +00:00
|
|
|
};
|
2021-06-14 18:53:20 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="weekdaySelect">
|
|
|
|
<div className="inline-flex">
|
2021-06-24 22:15:18 +00:00
|
|
|
{days.map((day, idx) =>
|
|
|
|
activeDays[idx] ? (
|
|
|
|
<button
|
|
|
|
key={idx}
|
2021-10-20 15:42:40 +00:00
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
toggleDay(idx);
|
|
|
|
}}
|
2021-06-24 22:15:18 +00:00
|
|
|
className={`
|
2022-03-05 15:37:46 +00:00
|
|
|
bg-brand text-brandcontrast dark:bg-darkmodebrand dark:text-darkmodebrandcontrast
|
2022-02-09 22:32:31 +00:00
|
|
|
h-10 w-10 rounded px-3 py-1 focus:outline-none
|
2021-06-24 22:15:18 +00:00
|
|
|
${activeDays[idx + 1] ? "rounded-r-none" : ""}
|
|
|
|
${activeDays[idx - 1] ? "rounded-l-none" : ""}
|
|
|
|
${idx === 0 ? "rounded-l" : ""}
|
|
|
|
${idx === days.length - 1 ? "rounded-r" : ""}
|
2021-06-14 18:53:20 +00:00
|
|
|
`}>
|
2021-06-24 22:15:18 +00:00
|
|
|
{day}
|
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
key={idx}
|
2021-10-20 15:42:40 +00:00
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
toggleDay(idx);
|
|
|
|
}}
|
2021-06-24 22:15:18 +00:00
|
|
|
style={{ marginTop: "1px", marginBottom: "1px" }}
|
2022-02-09 00:05:13 +00:00
|
|
|
className={`h-10 w-10 rounded-none bg-gray-50 px-3 py-1 focus:outline-none ${
|
2021-06-24 22:15:18 +00:00
|
|
|
idx === 0 ? "rounded-l" : "border-l-0"
|
|
|
|
} ${idx === days.length - 1 ? "rounded-r" : ""}`}>
|
|
|
|
{day}
|
|
|
|
</button>
|
|
|
|
)
|
2021-06-14 18:53:20 +00:00
|
|
|
)}
|
|
|
|
</div>
|
2021-06-24 22:15:18 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|