欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

react16+antd4 RangePicker組件實(shí)現(xiàn)時間禁選示例

 更新時間:2023年05月31日 08:51:39   作者:灰太狼的情與殤  
這篇文章主要為大家介紹了react16+antd4 RangePicker 時間禁選示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jì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ā)的思考

    基于React.js實(shí)現(xiàn)原生js拖拽效果引發(fā)的思考

    這篇文章主要為大家詳細(xì)介紹了基于React.js實(shí)現(xiàn)原生js拖拽效果,繼而引發(fā)的一系列思考,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • React組件之多選Checkbox實(shí)例

    React組件之多選Checkbox實(shí)例

    這篇文章主要介紹了React組件之多選Checkbox實(shí)例,具有很好的參考價值,希望對大家有所幫助,
    2023-10-10
  • 詳解React如何使用??useReducer???高階鉤子來管理狀態(tài)

    詳解React如何使用??useReducer???高階鉤子來管理狀態(tài)

    useReducer是React中的一個鉤子,用于替代?useState來管理復(fù)雜的狀態(tài)邏輯,本文將詳細(xì)介紹如何在React中使用?useReducer高階鉤子來管理狀態(tài),感興趣的可以了解下
    2025-02-02
  • react合成事件與原生事件的相關(guān)理解

    react合成事件與原生事件的相關(guān)理解

    本文主要介紹了react合成事件與原生事件的相關(guān)概念,幫助大家區(qū)分這兩種事件,學(xué)習(xí)react的同學(xué)不妨了解下
    2021-05-05
  • 取消正在運(yùn)行的Promise技巧詳解

    取消正在運(yùn)行的Promise技巧詳解

    這篇文章主要為大家介紹了取消正在運(yùn)行的Promise技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • react中引入less并支持antd主題換膚方式

    react中引入less并支持antd主題換膚方式

    這篇文章主要介紹了react中引入less并支持antd主題換膚方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • React useCallback鉤子的作用方法demo

    React useCallback鉤子的作用方法demo

    這篇文章主要為大家介紹了React useCallback鉤子的作用方法demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • react滾動加載useInfiniteScroll?詳解

    react滾動加載useInfiniteScroll?詳解

    使用useInfiniteScroll?hook可以處理檢測用戶何時滾動到頁面底部的邏輯,并觸發(fā)回調(diào)函數(shù)以加載更多數(shù)據(jù),它還提供了一種簡單的方法來管理加載和錯誤消息的狀態(tài),今天通過實(shí)例代碼介紹下react滾動加載useInfiniteScroll?相關(guān)知識,感興趣的朋友跟隨小編一起看看吧
    2023-09-09
  • React 添加引用路徑時如何使用@符號作為src文件

    React 添加引用路徑時如何使用@符號作為src文件

    這篇文章主要介紹了React 添加引用路徑時如何使用@符號作為src文件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-06-06
  • React路由規(guī)則定義與聲明式導(dǎo)航及編程式導(dǎo)航分別介紹

    React路由規(guī)則定義與聲明式導(dǎo)航及編程式導(dǎo)航分別介紹

    這篇文章主要介紹了React路由規(guī)則的定義、聲明式導(dǎo)航、編程式導(dǎo)航,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-09-09

最新評論