import { useState, useEffect } from "react"; import moment from 'moment'; import styles from "./modal.module.scss"; const DatePickerModal = ({ initialDate, changeDate, setChangeDate, show, setShow }) => { const [date, setDate] = useState({ year: moment(changeDate).get('year'), month: moment(changeDate).get('month') + 1 }) const [year, setYear] = useState({ start: moment(initialDate).get('year') - 3, end: moment(initialDate).get('year') + 3 }) const month = moment(initialDate).get('month') + 1 useEffect(() => { setDate({ year: moment(changeDate).get('year'), month: moment(changeDate).get('month') + 1 }) }, [changeDate]) function up(e) { const { id } = e.target if (id === "year") { if (date[id] < year.end) setDate({ ...date, [id]: date[id] + 1 }) } else { if (date[id] === 12) setDate({ ...date, [id]: 1 }) else setDate({ ...date, [id]: date[id] + 1 }) } } function down(e) { const { id } = e.target if (id === "year") { if (date[id] > year.start) setDate({ ...date, [id]: date[id] - 1 }) } else { if (date[id] === 1) setDate({ ...date, [id]: 12 }) else setDate({ ...date, [id]: date[id] - 1 }) } } function cancel() { setDate({ year: moment(changeDate).get('year'), month: moment(changeDate).get('month') + 1 }) setShow(false) } function handleClick() { if (date.year === year.start) { if (month > date.month) alert("선택하신 날짜는 유효하지 않습니다. 다시 선택해주세요.") else { let dateStr = date.year + "-" + date.month + "-01" setChangeDate(moment(dateStr).format("YYYY-MM-DD")) setShow(false) } } else if (date.year === year.end) { if (month < date.month) alert("선택하신 날짜는 유효하지 않습니다. 다시 선택해주세요.") else { let dateStr = date.year + "-" + date.month + "-01" setChangeDate(moment(dateStr).format("YYYY-MM-DD")) setShow(false) } } else { let dateStr = date.year + "-" + date.month + "-01" setChangeDate(moment(dateStr).format("YYYY-MM-DD")) setShow(false) } } return ( <> {show ?
: null}취소
완료