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