固定日期 & 自定义日期搜索

    • 默认隐藏自定义时间,点击自定义时间时,显示时间选择框
    • 点击其他的,就隐藏时间框

    image.png

    1. import React, { useState } from 'react';
    2. import PropTypes from 'prop-types';
    3. import { DatePicker } from 'antd';
    4. import styled from './index.module.less';
    5. import { format, disabledDate,, RANGE_DATE, MOMENT_VALUEOF } from '@utils'
    6. import { RadioButton } from '@components/Form';
    7. const { RangePicker } = DatePicker;
    8. const radioSource = [
    9. {label: '一周', value: 'week'},
    10. {label: '一月', value: 'month'},
    11. {label: '一年', value: 'year'},
    12. {label: '自定义', value: 0},
    13. ]
    14. SearchDate.propTypes = {
    15. onChange: PropTypes.func.isRequired,
    16. };
    17. function SearchDate({ onChange }) {
    18. const [visible, setVisible] = useState(false);
    19. function radioChange(value) {
    20. setVisible(value === 0);
    21. if(!value) return;
    22. rangeChange(RANGE_DATE(value))
    23. }
    24. function rangeChange(values) {
    25. // moment格式转时间戳
    26. const dates = MOMENT_VALUEOF(values)
    27. onChange(dates)
    28. }
    29. const rangeAttrs = {
    30. format,
    31. disabledDate,
    32. className: 'ml8',
    33. onChange: rangeChange,
    34. // size: 'small',
    35. }
    36. return (
    37. <div className={styled.flex}>
    38. <RadioButton
    39. dataSource={radioSource}
    40. onChange={radioChange}
    41. // size='small'
    42. />
    43. {visible && (<RangePicker {...rangeAttrs}/>)}
    44. </div>
    45. );
    46. }
    47. export default SearchDate;

    style.module.less

    1. .flex {
    2. display: flex;
    3. justify-content: flex-end;
    4. margin-bottom: 16px;
    5. }

    使用组件

    1. import SearchDate from './SearchDate'
    2. function App() {
    3. return(
    4. <SearchDate onChange={onSearch}/>
    5. )
    6. }