今日本周本月本年react antd DatePicker封装

在这里插入图片描述
// 父组件

import SalesCard from './components/SalesCard';
import { getTimeDistance } from './utils/utils';
import styles from './style.less';

const Analysis = () => {
  const [rangePickerValue, setRangePickerValue] = useState(getTimeDistance('year'));
  console.log(rangePickerValue)  //  这个就是要获取的Pickervalue值
  
  // 封装一个方法 ,点击的时候更新type
  const selectDate = (type) => {
    setRangePickerValue(getTimeDistance(type));
  };

  // 接收子组件传过来的,当组将改变的时候获取值
  const handleRangePickerChange = (value) => {
    setRangePickerValue(value);
  };

  const isActive = (type) => {
    if (!rangePickerValue) {
      return '';
    }
    const value = getTimeDistance(type);

    if (!value) {
      return '';
    }

    if (!rangePickerValue[0] || !rangePickerValue[1]) {
      return '';
    }

    if (
      rangePickerValue[0].isSame(value[0], 'day') &&
      rangePickerValue[1].isSame(value[1], 'day')
    ) {
      return styles.currentDate;
    }

    return '';
  };
  return (
      <>
          <SalesCard
            rangePickerValue={rangePickerValue}
            isActive={isActive}
            handleRangePickerChange={handleRangePickerChange}
            selectDate={selectDate}
          />
      </>
  );
};

export default Analysis;

// 子组件

import { DatePicker } from 'antd';
import styles from '../style.less';
const { RangePicker } = DatePicker;

const SalesCard = ({
  rangePickerValue,
  isActive,
  handleRangePickerChange,  //这是子传父方法
  selectDate,
}) => {

  return (
    <div className={styles.salesCard}>
      <div className={styles.salesExtraWrap}>
        <div className={styles.salesExtra}>
          <a className={isActive('today')} onClick={() => selectDate('today')}>
            今日
          </a>
          <a className={isActive('week')} onClick={() => selectDate('week')}>
            本周
          </a>
          <a className={isActive('month')} onClick={() => selectDate('month')}>
            本月
          </a>
          <a className={isActive('year')} onClick={() => selectDate('year')}>
            本年
          </a>
        </div>
        <RangePicker
          value={rangePickerValue}
          onChange={handleRangePickerChange}
          style={{
            width: 256,
          }}
        />
      </div>
    </div>
  )
}

export default SalesCard;

until.js里面type封装

import moment from 'moment';
export function fixedZero(val) {
  return val * 1 < 10 ? `0${val}` : val;
}
export function getTimeDistance(type) {
  const now = new Date();
  const oneDay = 1000 * 60 * 60 * 24;

  if (type === 'today') {
    now.setHours(0);
    now.setMinutes(0);
    now.setSeconds(0);
    return [moment(now), moment(now.getTime() + (oneDay - 1000))];
  }

  if (type === 'week') {
    let day = now.getDay();
    now.setHours(0);
    now.setMinutes(0);
    now.setSeconds(0);

    if (day === 0) {
      day = 6;
    } else {
      day -= 1;
    }

    const beginTime = now.getTime() - day * oneDay;
    return [moment(beginTime), moment(beginTime + (7 * oneDay - 1000))];
  }

  const year = now.getFullYear();

  if (type === 'month') {
    const month = now.getMonth();
    const nextDate = moment(now).add(1, 'months');
    const nextYear = nextDate.year();
    const nextMonth = nextDate.month();
    return [
      moment(`${year}-${fixedZero(month + 1)}-01 00:00:00`),
      moment(moment(`${nextYear}-${fixedZero(nextMonth + 1)}-01 00:00:00`).valueOf() - 1000),
    ];
  }

  return [moment(`${year}-01-01 00:00:00`), moment(`${year}-12-31 23:59:59`)];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱技术的大仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值