react16+antd4 RangePicker組件實現(xiàn)時間禁選示例
更新時間:2023年05月31日 08:51:39 作者:灰太狼的情與殤
這篇文章主要為大家介紹了react16+antd4 RangePicker 時間禁選示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
正文
開發(fā)環(huán)境 react16+antd4
電腦系統(tǒng) windows11專業(yè)版
分享一下 react+antd RangePicker組件的時間禁選,廢話不多說
代碼
const [dateType, setdataType] = useState([
{
value: '按日',
key: 1
},
{
value: '按月',
key: 2
}
]);
const [dateTime, setDateTime] = useState([
moment().add(-1, 'years').add(0, "months").format("YYYY-MM"),
moment().format("YYYY-MM"),
]);
const [timeValue, setTimeValue] = useState([
moment(moment().add(-1, 'years').add(0, "months"), 'YYYY-MM'),
moment(moment(), 'YYYY-MM')
]);
const [timeFormat, setTimeFormat] = useState('YYYY-MM');
const [pickerValue, setPickerValue] = useState('month');const getDay = () => {
setDateTime([
moment().add(-1, "months").format("YYYY-MM-DD"),
moment().format("YYYY-MM-DD"),
]);
setTimeValue([
moment(moment().add(-1, "months"), 'YYYY-MM-DD'),
moment(moment(), 'YYYY-MM-DD')
]);
setTimeFormat('YYYY-MM-DD');
setPickerValue('date');
};
const getMonth = () => {
setDateTime([
moment().add(-1, 'years').add(0, "months").format("YYYY-MM"),
moment().format("YYYY-MM"),
]);
setTimeValue([
moment().add(-1, 'years').add(0, "months"),
moment(moment(), 'YYYY-MM')
]);
setTimeFormat('YYYY-MM');
setPickerValue('month');
}
// 時間類型 變化
const TimeTypeChange = (value, option) => {
setdateTypeValue(value);
switch (+value) {
case 1://按日
getDay();
break;
case 2://按月
getMonth();
break;
}
}
const timeDisabled = (current) => {
switch (+dateTypeValue) {
case 1:// 按日
return day_disabledDate(current);
break;
case 2:// 按月
return month_disabledDate(current);
break;
}
}
const month_disabledDate = (current) => {
if (!timeValue) {
return false;
}
const tooLate = timeValue[0] && current.diff(timeValue[0], 'months') > 11;
const tooEarly = timeValue[1] && timeValue[1].diff(current, 'months') > 11;
return !!tooEarly || !!tooLate;
};
const day_disabledDate = (current) => {
if (!timeValue) {
return false;
}
const tooLate = timeValue[0] && current.diff(timeValue[0], 'months') > 0;
const tooEarly = timeValue[1] && timeValue[1].diff(current, 'months') > 0;
const disable_day = current && current < moment().endOf('day');
return !!tooEarly || !!tooLate || !disable_day;
}
const onOpenChange = (open) => {
if (open) {
setTimeValue([null, null]);
}
};
const _onRangePickerChange = (date, dateString) => {
// setTimeValue(dateString);
setTimeValue(date);
setDateTime(dateString);
}
const onCalendarChange = (value) => {
setTimeValue(value);
}<Select placeholder='請選擇時間類型'
value={dateTypeValue == 1 ? '按日' : '按月'}
onChange={TimeTypeChange}
getPopupContainer={triggerNode => triggerNode.parentElement}>
{dateType && dateType.map((item) => {
return (
<Option key={item.key} value={item.key}>{item.value}</Option>
)
})}
</Select>
<RangePicker
style={{ width: 330, marginLeft: 5 }}
value={timeValue}
format={timeFormat}
allowClear={false}
disabledDate={timeDisabled}
picker={pickerValue}
onChange={_onRangePickerChange}
onOpenChange={onOpenChange}
onCalendarChange={onCalendarChange}
/>效果如下
// 默認 顯示按月

// 選擇按日禁選 效果

// 按月禁選

// 按日禁選

本期的分享到了這里就結束啦,希望對你有所幫助,讓我們一起努力走向巔峰,更多關于react16 antd4 RangePicker的資料請關注腳本之家其它相關文章!
相關文章
基于React.js實現(xiàn)原生js拖拽效果引發(fā)的思考
這篇文章主要為大家詳細介紹了基于React.js實現(xiàn)原生js拖拽效果,繼而引發(fā)的一系列思考,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03
詳解React如何使用??useReducer???高階鉤子來管理狀態(tài)
useReducer是React中的一個鉤子,用于替代?useState來管理復雜的狀態(tài)邏輯,本文將詳細介紹如何在React中使用?useReducer高階鉤子來管理狀態(tài),感興趣的可以了解下2025-02-02
React路由規(guī)則定義與聲明式導航及編程式導航分別介紹
這篇文章主要介紹了React路由規(guī)則的定義、聲明式導航、編程式導航,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-09-09

