import React from "react"; import { useState, useEffect } from "react"; import { View, Text, Picker } from "@tarojs/components"; import dayjs from "dayjs"; const TimeSelector = (props: any) => { let { data, hook, isOpened, onItemClick, chooseDate, changeActiveIndex, activeIndex, } = props; console.log("sjfksjkdfjk", activeIndex, data, hook, isOpened, onItemClick); if (!onItemClick) { onItemClick = () => {}; } const { mode = "week", nowChooseDate } = hook || {}; const [nowMonth, setNowMonth] = useState( `${dayjs().year()}年 ${dayjs().month() + 1}月` ); const [isOpen, setIsOpen] = useState(isOpened); useEffect(() => { setIsOpen(isOpened); }, [mode]); const dateChange = (value: any) => { let date = dayjs(value).toDate(); chooseDate(date); // 设置月份选择值为选择月份-页面显示值 setNowMonth(`${dayjs(date).year()}年 ${dayjs(date).month() + 1}月`); // 设置每月天所在位置-activeIndex,不是当月默认1号,当月默认当天 if ( dayjs(date).year() == dayjs().year() && dayjs(date).month() == dayjs().month() ) { changeActiveIndex(dayjs().date()); } else { const firstDayWeek = new Date( `${dayjs(date).year()}/${dayjs(date).month() + 1}/1` ).getDay(); // 如果第一天为0,即周天 const temp = firstDayWeek != 0 ? firstDayWeek : 7; console.log("temp :>> ", temp); changeActiveIndex(temp - 1); } }; const changeOpen = () => { setIsOpen(!isOpen); }; // 获取今天在所有dayNode中的下标 const goToday = () => { // 设置月份选择默认值-picker chooseDate(new Date()); // 设置月份选择值为今天所在月份-页面显示值 setNowMonth( `${dayjs(new Date()).year()}年 ${dayjs(new Date()).month() + 1}月` ); // 设置今天所在位置-activeIndex changeActiveIndex(dayjs().date()); }; const chooseDay = (item: any) => { console.log("988==>", item); changeActiveIndex(item.day); }; return ( {mode === "week" && ( { dateChange(e.target.value); }} > {nowMonth} )} {mode === "month" && {nowMonth}} {mode === "week" && ( {isOpen ? "收起" : "展开"} 回到今天 )} {data && data.slice(0, isOpen ? data.length : 7).map((item) => { return ( chooseDay(item) : () => onItemClick(item) } > {item.day && ( {item.day} {item.num != 0 && activeIndex !== item.day && ( )} )} {`${ item.num != 0 && item.day ? `${item.num}场` : "" }`} ); })} ); }; TimeSelector.config = { backgroundTextStyle: "light", enablePullDownRefresh: false, backgroundColor: "#3387ff", onReachBottomDistance: 50, navigationBarTitleText: "投资管理平台", }; export default TimeSelector;