react16+antd4 RangePicker組件實現(xiàn)時間禁選示例
正文
開發(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}
/>效果如下
// 默認 顯示按月

// 選擇按日禁選 效果

// 按月禁選

// 按日禁選

本期的分享到了這里就結(jié)束啦,希望對你有所幫助,讓我們一起努力走向巔峰,更多關(guān)于react16 antd4 RangePicker的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Express+React+Antd實現(xiàn)上傳功能(前端和后端)
這篇文章主要介紹了Express+React+Antd實現(xiàn)上傳功能(前端和后端),本文通過示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2024-04-04
React Native預設(shè)占位placeholder的使用
本篇文章主要介紹了React Native預設(shè)占位placeholder的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
flouting?ui定位組件完美替代ant?deisgn使用詳解
這篇文章主要為大家介紹了flouting?ui定位組件完美替代ant?deisgn使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
react-native 圓弧拖動進度條實現(xiàn)的示例代碼
本篇文章主要介紹了react-native 圓弧拖動進度條實現(xiàn)的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
在React中實現(xiàn)分塊導出大量數(shù)據(jù)表格并壓縮成圖片的解決方案
在現(xiàn)代Web開發(fā)中,處理和展示大量數(shù)據(jù)是一個常見的挑戰(zhàn),特別是在使用React框架時,我們經(jīng)常需要將這些數(shù)據(jù)以表格的形式展示,并提供導出功能,本文將介紹如何在React中實現(xiàn)一個高效、分塊導出大量數(shù)據(jù)表格,并將其壓縮為圖片的解決方案,需要的朋友可以參考下2024-12-12
React?Native系列之Recyclerlistview使用詳解
這篇文章主要為大家介紹了React?Native系列之Recyclerlistview使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
React Native 混合開發(fā)多入口加載方式詳解
這篇文章主要介紹了React Native 混合開發(fā)多入口加載方式詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09

