Building an Antd-Style DatePicker Component (Simplified)

发表于 2022-03-11 21:30 541 字 3 min read

cos avatar

cos

FE / ACG / 手工 / 深色模式强迫症 / INFP / 兴趣广泛养两只猫的老宅女 / remote

这篇文章介绍了作者基于 React 封装的 Ant Design DatePicker 日期选择框组件,实现了日、月、年三个面板的交互功能,支持日期选择、面板切换、箭头导航及输入实时更新。通过 ref 判断点击事件位置实现外部关闭功能,并提供了默认日期设置和日期变化回调等对外 API,整体结构清晰,虽有优化空间但展现了自定义组件开发的乐趣与学习过程。

This article has been machine-translated from Chinese. The translation may contain inaccuracies or awkward phrasing. If in doubt, please refer to the original Chinese version.

Takeaways

  • I won’t say what inspired me
  • This was my first time building a small component with React. The encapsulation may not be great, there are still quite a few bugs, and looking back at my own code makes me want to complain.
  • But it was really fun! For example, the feature of closing the date picker when clicking outside it kept me stuck for a long time, but it felt so satisfying when I finally got it working!
  • Used React’s ref to determine whether a click event occurred inside or outside the component.
  • Building your own components is really cool! Although sometimes it feels like reinventing the wheel
  • While studying Antd’s component implementation, I also noticed many subtle details in their approach. I’m in awe. There will be future optimizations, adding more APIs, features, and custom sizing options.

Component Overview

DatePicker Component

DatePicker component development Code and live preview: DatePicker (by cos)

  1. Day, month, and year selection panel implementation Interactions: a. Day panel: Click a date to select it. Click left/right arrows in the header to navigate to the previous/next year or month. b. Month panel: Enter by clicking the month in the day panel header. Click a month to select it and return to the day panel. Click left/right arrows to navigate to the previous/next year. c. Year panel: Enter by clicking the month in the day panel header. Shows decades. Click a year to select it and return to the day panel. Click left/right arrows to navigate to the previous/next decade. d. Real-time date changes based on input field content.
  2. External API implementation a. Default date setting via defaultDate — set defaultDate to a moment object to configure the default date. b. Date retrieval via onDateChange — use the date change callback function to get the current date, returns a moment object. Example:
class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      nowDate: 'No Value!',
    };
  }
  handleDateChange(date) {
    this.setState({ nowDate: date.format('YYYY-MM-DD') });
  }
  render() {
    return (
      <div className="App">
        <div>nowDate: {this.state.nowDate}</div>
        <DatePicker defaultDate={moment()} onDateChange={(date) => this.handleDateChange(date)} />
      </div>
    );
  }
}
  1. Panel display and transition animations Through the picker-panel class with initial opacity set to 0, transform-origin set to top-left, initial size at scale(0), and display controlled via the custom attribute data-active set to true.
.picker-panel {
  z-index: 10;
  position: absolute;
  background-color: white;
  top: 30px;
  left: 0;
  width: 300px;
  height: 300px;
  border-radius: 3px;
  border: 1px solid $gray-4;
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.4);

  display: flex;
  flex-direction: column;

  opacity: 0;
  transform: scale(0);
  transform-origin: top left;
  transition: all 300ms ease-in-out;
  &[data-active='true'] {
    transform: scale(1);
    opacity: 1;
  }
}
  1. Interface Screenshots
  • Initial value

Initial value

  • Selecting another date

    Selecting another date

  • Next year

    Next year

  • Previous month

    Previous month

  • Entering the year panel

    Entering the year panel

  • Switching year panels

    Switching year panels

  • Month panel

Month panel

喜欢的话,留下你的评论吧~

© 2020 - 2026 cos @cosine
Powered by theme astro-koharu · Inspired by Shoka