1 弹窗用法
如果是弹窗用法
import React, { useState, useEffect, useContext, useRef } from "react";
import { Button, Modal, DatePicker, Space, Row, Col,Divider} from 'antd';
import { arrTostr, filterOption } from '_s/libs/util.js'
const { RangePicker } = DatePicker;
interface Props {
isOpen: boolean,
onclose: () => void,
onhandleTime: (data: any) => void
}
const DateModal = (props: Props) => {
const { isOpen, onclose,onhandleTime } = props
const [time,setTime]=useState<any>(['',''])
const [dimension,setDimension]=useState<any>('')
const [pickerValue,setPickerValue]=useState<any>()
const [showTime,setShowTime]=useState<any>([])
const [selectedDate, setSelectedDate] = useState('');
const handleOk = () => {
onhandleTime({"time":time,"dimension":dimension,"showTime":showTime})
setTime(['',''])
setShowTime([])
setPickerValue(null)
setDimension('')
setSelectedDate('')
};
const handleCancel = () => {
setTime(['',''])
setShowTime([])
setDimension('')
setPickerValue(null)
onclose()
setSelectedDate('')
};
const handleRange = (dates: any) => {
const dataTimstamp = arrTostr(dates)
if(dates&&dates.length>0){
const start:any =dates[0].format('YYYY-MM-DD')
const end:any=dates[1].format('YYYY-MM-DD')
setShowTime([start,end])
}else {
setShowTime([])
}
console.log(dates,dataTimstamp)
setTime(dataTimstamp)
setPickerValue(dates)
setDimension('other')
}
const getToday=()=>{
const today = new Date()
const todayTimestamp = [Math.floor(today.getTime()/1000), Math.floor(today.getTime()/1000)]
console.log(today,todayTimestamp);
setShowTime([showTimeFn(today),showTimeFn(today)])
console.log('时间',showTime)
setTime(todayTimestamp)
}
const getYesterday = () => {
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
const start = new Date(yesterday.getFullYear(), yesterday.getMonth(), yesterday.getDate());
const end = new Date(yesterday.getFullYear(), yesterday.getMonth(), yesterday.getDate(), 23, 59, 59);
console.log(start,end)
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
const yesterdayTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(yesterdayTimestamp);
setTime(yesterdayTimestamp)
};
const getCurrentWeek = () => {
const today = new Date();
const currentDay = today.getDay();
const diff = today.getDate() - currentDay + (currentDay === 0 ? -6 : 1);
const start = new Date(today.setDate(diff));
const end = new Date();
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
const currentWeekTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(start, end);
console.log(currentWeekTimestamp);
setTime(currentWeekTimestamp)
};
const getLastWeek = () => {
const today = new Date();
const currentDay = today.getDay();
const diff = today.getDate() - currentDay - 6;
const start = new Date(today.setDate(diff));
const end = new Date(today.setDate(today.getDate() + 6));
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
const lastWeekTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(lastWeekTimestamp);
console.log(start, end);
setTime(lastWeekTimestamp)
};
const getCurrentMonth = () => {
const today = new Date();
const start = new Date(today.getFullYear(), today.getMonth(), 1);
const end = new Date();
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
console.log(start,end)
const currentMonthTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(currentMonthTimestamp);
setTime(currentMonthTimestamp)
};
const getLastMonth = () => {
const today = new Date();
const start = new Date(today.getFullYear(), today.getMonth() - 1, 1);
const end = new Date(today.getFullYear(), today.getMonth(), 0, 23, 59, 59);
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
console.log(start,end)
const lastMonthTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(lastMonthTimestamp);
setTime(lastMonthTimestamp)
};
const getCurrentQuarter = () => {
const today = new Date();
const quarter = Math.floor(today.getMonth() / 3);
const start = new Date(today.getFullYear(), quarter * 3, 1);
const end = new Date();
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
console.log(start,end)
const currentQuarterTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(currentQuarterTimestamp);
setTime(currentQuarterTimestamp)
};
const getLastQuarter = () => {
const today = new Date();
const quarter = Math.floor(today.getMonth() / 3) - 1;
const start = new Date(today.getFullYear(), quarter * 3, 1);
const end = new Date(today.getFullYear(), quarter * 3 + 2, new Date(today.getFullYear(), quarter * 3 + 1, 0).getDate(), 23, 59, 59);
console.log(start,end)
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
const lastQuarterTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(lastQuarterTimestamp);
setTime(lastQuarterTimestamp)
};
const getCurrentYear = () => {
const today = new Date();
const start = new Date(today.getFullYear(), 0, 1);
const end = new Date();
console.log(start,end)
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
const currentYearTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(currentYearTimestamp);
setTime(currentYearTimestamp)
};
const getLastYear = () => {
const today = new Date();
const start = new Date(today.getFullYear() - 1, 0, 1);
const end = new Date(today.getFullYear() - 1, 11, 31, 23, 59, 59);
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
console.log(start,end)
const lastYearTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(lastYearTimestamp);
setTime(lastYearTimestamp)
};
const showTimeFn=(time:any)=>{
var date = new Date(time)
var y = date.getFullYear()
var m = date.getMonth() + 1
m = m < 10 ? '0' + m : m
var d = date.getDate()
d = d < 10 ? '0' + d : d
const showTime= y + '-' + m + '-' + d
console.log('showTime',showTime)
return showTime
}
const clickDate=(dates:any,dimension:string)=>{
console.log(dates,dimension,selectedDate)
setDimension(dimension)
setSelectedDate(dates)
switch(dates){
case 'today':
getToday()
break;
case 'yesterday':
getYesterday()
break;
case 'thisWeek':
getCurrentWeek()
break;
case 'lastWeek':
getLastWeek()
break;
case 'thisMonth':
getCurrentMonth()
break;
case 'lastMonth':
getLastMonth()
break;
case 'thisQuarter':
getCurrentQuarter()
break;
case 'lastQuarter':
getLastQuarter()
break;
case 'thisYear':
getCurrentYear()
break;
case 'lastYear':
getLastYear()
break;
default:
break;
}
}
return (
<div className="modalClass" >
<Modal title="日期选择" className="timeModal" width={360} open={isOpen} footer={null} onCancel={handleCancel}>
<div>
<div style={{width:'100%'}}>
<Space direction="vertical" size={12} style={{width:'100%'}}>
<div className="rowClass">
<div className="titleLeft">天</div>
<div className={`cursorClass ${selectedDate =='today' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('today','day')}>今天</div>
<div className={`cursorClass ${selectedDate =='yesterday' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('yesterday','day')}>昨天</div>
</div>
<div className="rowClass">
<div className="titleLeft">周</div>
<div className={`cursorClass ${selectedDate =='thisWeek' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('thisWeek','week')}>本周</div>
<div className={`cursorClass ${selectedDate =='lastWeek' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('lastWeek','week')}>上周</div>
</div>
<div className="rowClass">
<div className="titleLeft">月</div>
<div className={`cursorClass ${selectedDate =='thisMonth' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('thisMonth','month')}>本月</div>
<div className={`cursorClass ${selectedDate =='lastMonth' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('lastMonth','month')}>上月</div>
</div>
<div className="rowClass">
<div className="titleLeft">季度</div>
<div className={`cursorClass ${selectedDate =='thisQuarter' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('thisQuarter','quarter')}>本季度</div>
<div className={`cursorClass ${selectedDate =='lastQuarter' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('lastQuarter','quarter')}>上一季度</div>
</div>
<div className="rowClass">
<div className="titleLeft">年度</div>
<div className={`cursorClass ${selectedDate =='thisYear' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('thisYear','year')}>本年度</div>
<div className={`cursorClass ${selectedDate =='lastYear' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('lastYear','year')}>上一年度</div>
</div>
</Space>
</div>
<div className="timeClass">
<div className="title">
自定义
</div>
<div>
<RangePicker value={pickerValue} allowClear style={{ width: '100%' }} onChange={handleRange} />
</div>
</div>
<div style={{textAlign:'right',marginTop:'20px'}}> <Button type="primary" onClick={handleOk}>确认</Button> </div>
</div>
</Modal>
</div>
);
};
export default DateModal;
2 popver
### 父组件
<div className="desktop_event_time">
<Popover placement="bottomRight" content={<DatePop onhandleTime={handleTime} onclose={hideOpen} />} title="选择时间" trigger="click" open={open}
onOpenChange={handleOpenChange}>
{
flagTime ? (<div>
<span style={{ color: '#cccccc' }}>选择日期</span>
<span style={{ marginLeft: '70px' }}>
<InsertRowAboveOutlined />
</span>
</div>) : (
<Space>
<span className="showTime" >{showTime[0]} 至 {showTime[1]}</span>
<div style={{ paddingTop: '5px', display: 'inline-block', }}>
<span><TimesIcondate /></span>
</div>
</Space>
)
}
</Popover>
</div>
子组件
import React, { useState, useEffect, useContext, useRef } from "react";
import { Button, Modal, DatePicker, Space, Row, Col,Divider} from 'antd';
import { arrTostr, filterOption } from '_s/libs/util.js'
const { RangePicker } = DatePicker;
interface Props {
isOpen: boolean,
onclose: () => void,
onhandleTime: (data: any) => void
}
const DateModal = (props: Props) => {
const { isOpen, onclose,onhandleTime } = props
const [time,setTime]=useState<any>(['',''])
const [dimension,setDimension]=useState<any>('')
const [pickerValue,setPickerValue]=useState<any>()
const [showTime,setShowTime]=useState<any>([])
const [selectedDate, setSelectedDate] = useState('');
const handleOk = () => {
onhandleTime({"time":time,"dimension":dimension,"showTime":showTime})
onclose()
setTime(['',''])
setShowTime([])
setPickerValue(null)
setDimension('')
setSelectedDate('')
};
const handleCancel = () => {
setTime(['',''])
setShowTime([])
setDimension('')
setPickerValue(null)
onclose()
setSelectedDate('')
};
const handleRange = (dates: any) => {
const dataTimstamp = arrTostr(dates)
if(dates&&dates.length>0){
const start:any =dates[0].format('YYYY-MM-DD')
const end:any=dates[1].format('YYYY-MM-DD')
setShowTime([start,end])
}else {
setShowTime([])
}
console.log(dates,dataTimstamp)
setTime(dataTimstamp)
setPickerValue(dates)
setDimension('other')
}
const getToday=()=>{
const today = new Date()
const todayTimestamp = [Math.floor(today.getTime()/1000), Math.floor(today.getTime()/1000)]
console.log(today,todayTimestamp);
setShowTime([showTimeFn(today),showTimeFn(today)])
console.log('时间',showTime)
setTime(todayTimestamp)
}
const getYesterday = () => {
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
const start = new Date(yesterday.getFullYear(), yesterday.getMonth(), yesterday.getDate());
const end = new Date(yesterday.getFullYear(), yesterday.getMonth(), yesterday.getDate(), 23, 59, 59);
console.log(start,end)
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
const yesterdayTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(yesterdayTimestamp);
setTime(yesterdayTimestamp)
};
const getCurrentWeek = () => {
const today = new Date();
const currentDay = today.getDay();
const diff = today.getDate() - currentDay + (currentDay === 0 ? -6 : 1);
const start = new Date(today.setDate(diff));
const end = new Date();
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
const currentWeekTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(start, end);
console.log(currentWeekTimestamp);
setTime(currentWeekTimestamp)
};
const getLastWeek = () => {
const today = new Date();
const currentDay = today.getDay();
const diff = today.getDate() - currentDay - 6;
const start = new Date(today.setDate(diff));
const end = new Date(today.setDate(today.getDate() + 6));
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
const lastWeekTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(lastWeekTimestamp);
console.log(start, end);
setTime(lastWeekTimestamp)
};
const getCurrentMonth = () => {
const today = new Date();
const start = new Date(today.getFullYear(), today.getMonth(), 1);
const end = new Date();
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
console.log(start,end)
const currentMonthTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(currentMonthTimestamp);
setTime(currentMonthTimestamp)
};
const getLastMonth = () => {
const today = new Date();
const start = new Date(today.getFullYear(), today.getMonth() - 1, 1);
const end = new Date(today.getFullYear(), today.getMonth(), 0, 23, 59, 59);
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
console.log(start,end)
const lastMonthTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(lastMonthTimestamp);
setTime(lastMonthTimestamp)
};
const getCurrentQuarter = () => {
const today = new Date();
const quarter = Math.floor(today.getMonth() / 3);
const start = new Date(today.getFullYear(), quarter * 3, 1);
const end = new Date();
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
console.log(start,end)
const currentQuarterTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(currentQuarterTimestamp);
setTime(currentQuarterTimestamp)
};
const getLastQuarter = () => {
const today = new Date();
const quarter = Math.floor(today.getMonth() / 3) - 1;
const start = new Date(today.getFullYear(), quarter * 3, 1);
const end = new Date(today.getFullYear(), quarter * 3 + 2, new Date(today.getFullYear(), quarter * 3 + 1, 0).getDate(), 23, 59, 59);
console.log(start,end)
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
const lastQuarterTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(lastQuarterTimestamp);
setTime(lastQuarterTimestamp)
};
const getCurrentYear = () => {
const today = new Date();
const start = new Date(today.getFullYear(), 0, 1);
const end = new Date();
console.log(start,end)
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
const currentYearTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(currentYearTimestamp);
setTime(currentYearTimestamp)
};
const getLastYear = () => {
const today = new Date();
const start = new Date(today.getFullYear() - 1, 0, 1);
const end = new Date(today.getFullYear() - 1, 11, 31, 23, 59, 59);
setShowTime([showTimeFn(start),showTimeFn(end)])
console.log('时间',showTime)
console.log(start,end)
const lastYearTimestamp = [Math.floor(start.getTime() / 1000), Math.floor(end.getTime() / 1000)];
console.log(lastYearTimestamp);
setTime(lastYearTimestamp)
};
const showTimeFn=(time:any)=>{
var date = new Date(time)
var y = date.getFullYear()
var m = date.getMonth() + 1
m = m < 10 ? '0' + m : m
var d = date.getDate()
d = d < 10 ? '0' + d : d
const showTime= y + '-' + m + '-' + d
console.log('showTime',showTime)
return showTime
}
const clickDate=(dates:any,dimension:string)=>{
console.log(dates,dimension,selectedDate)
setDimension(dimension)
setSelectedDate(dates)
switch(dates){
case 'today':
getToday()
break;
case 'yesterday':
getYesterday()
break;
case 'thisWeek':
getCurrentWeek()
break;
case 'lastWeek':
getLastWeek()
break;
case 'thisMonth':
getCurrentMonth()
break;
case 'lastMonth':
getLastMonth()
break;
case 'thisQuarter':
getCurrentQuarter()
break;
case 'lastQuarter':
getLastQuarter()
break;
case 'thisYear':
getCurrentYear()
break;
case 'lastYear':
getLastYear()
break;
default:
break;
}
}
return (
<div className="modalClass">
<div className="modalshortcut">
<div className="modalshortcutItem">
<div className="titleLeft">天</div>
<div className={`cursorClass ${selectedDate =='today' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('today','day')}>今天</div>
<div className={`cursorClass ${selectedDate =='yesterday' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('yesterday','day')}>昨天</div>
</div>
<div className="modalshortcutItem">
<div className="titleLeft">周</div>
<div className={`cursorClass ${selectedDate =='thisWeek' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('thisWeek','week')}>本周</div>
<div className={`cursorClass ${selectedDate =='lastWeek' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('lastWeek','week')}>上周</div>
</div>
<div className="modalshortcutItem">
<div className="titleLeft">月</div>
<div className={`cursorClass ${selectedDate =='thisMonth' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('thisMonth','month')}>本月</div>
<div className={`cursorClass ${selectedDate =='lastMonth' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('lastMonth','month')}>上月</div>
</div>
<div className="modalshortcutItem">
<div className="titleLeft">季度</div>
<div className={`cursorClass ${selectedDate =='thisQuarter' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('thisQuarter','quarter')}>本季度</div>
<div className={`cursorClass ${selectedDate =='lastQuarter' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('lastQuarter','quarter')}>上一季度</div>
</div>
<div className="modalshortcutItem">
<div className="titleLeft">年度</div>
<div className={`cursorClass ${selectedDate =='thisYear' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('thisYear','year')}>本年度</div>
<div className={`cursorClass ${selectedDate =='lastYear' ? 'cursorClassSelect' : ''}`} onClick={()=>clickDate('lastYear','year')}>上一年度</div>
</div>
</div>
<div className="timeClass">
<div className="title">
自定义
</div>
<div>
<RangePicker value={pickerValue} allowClear style={{ width: '100%' }} onChange={handleRange} />
</div>
</div>
<div style={{textAlign:'right',marginTop:'20px'}}> <Button type="primary" onClick={handleOk}>确认</Button> </div>
</div>
);
};
export default DateModal;