需求:由于在项目开发中,当需要使用该组件时都需要对该组件进行大量的代码输出,为了方便代码统一管理,减少冗余代码,所以将此组件进行二次封装。

其他成员在使用中只需将自己的设置通过对应的参数传递到该组件,在执行选中操作后直接将返回值回传给调用的父组件即可。

技术涉及:react+ts+antd

代码如下:

parent.tsx

import React from 'react';
import MonthPicker from '../components/DatePicker/children';
import moment from 'moment'; class FormsView extends React.Component {
constructor(props: {}) {
super(props)
this.state = {
}
}
getSelectMonth(data: any) {
console.log('获取选中的月份', data);
}
disabledDate1(current:any) {
    // 设置不可选的日期
return current && current > moment().endOf('day');
}
render() {
return <div className="parent">
<MonthPicker data={new Date()}
getSelectMonth={this.getSelectMonth.bind(this)}
setDisableDate={this.disabledDate1.bind(this)}></MonthPicker>
</div>
}
} export default FormsView

封装的月份选择器  children.tsx

import React from 'react';
import { DatePicker } from 'antd';
import moment from 'moment'; interface Props {
data: Date // value
getSelectMonth?: any // 回调函数
setClassName?: string // 设置面板样式
setDateChange?: number // 月份显示形式 0默认(1月)1(一月)
setDisableDate?: any // 不可选择的月份
setMonthClassName?: string // 下拉面板样式
}
class Children extends React.Component<Props, any> {
constructor(props) {
super(props)
this.state = {
monthFormat: 'YYYY/MM',
dateText: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
popupStyle: {},
monthData: this.props.data || new Date(),
dateType: this.props.setDateChange || 0,
dateBoxClassName: this.props.setClassName,
dateMonthClassName: this.props.setMonthClassName || 'default-picker',
dateDisableDate: this.props.setDisableDate
}
}
handleChange(value: any, option: any) {
this.props.getSelectMonth(option);
} render() {
return <div className="date-picker">
<DatePicker
picker="month"
placeholder="请选择"
size="middle"
className={this.state.dateBoxClassName}
monthCellRender={(date, locale)=>{
return <div className={this.state.dateMonthClassName}>
{ !this.state.dateType ? (date.month()+1)+locale.month : this.state.dateText[date.month()] }
</div>
}}
popupStyle={this.state.popupStyle}
defaultValue={moment(this.state.monthData, this.state.monthFormat)}
format={this.state.monthFormat}
disabledDate={this.state.dateDisableDate}
onChange={this.handleChange.bind(this)} />
</div>
}
} export default Children
interface Props中是定义的接口属性,其中data是接收当前默认需要显示的值的,是必须的参数;其他带有问号的属性是可选择的,既非必传,根据需要设置。
因为下拉的面板展示的文本是数字+月,因此需要展示为中文月份的就需要用到转换属性。

小伙伴们可以直接复制使用,也可以在此基础上操作更多的自定义,反正都是以需求为主的啦。当然,有疑问的话也可以留言一起讨论哦!!!(*^__^*)

react+ts封装AntdUI的日期选择框之月份选择器DatePicker.month的更多相关文章

  1. js控制日期选择框datetime-local和select的展开

    注: js控制元素展开不受元素css属性的限制,例如opacity,z-index等 1. 使用js控制日期选择框的展开 ios: document.querySelector(".targ ...

  2. [RN] React Native 自定义 底部 弹出 选择框 实现

    React Native 自定义 底部选择框 实现 效果如图所示: 实现方法: 一.组件封装 CustomAlertDialog.js import React, {Component} from ' ...

  3. android 弹出日期选择框

    DatePickerDialog 在很多时候需要用户去设定时间,不可能让用户去在一个文本框中去输入时间,所以就需要有个日期弹出选择框,而这个框就是DatePickerDialog. 1.在API中的D ...

  4. 页面日期选择控件--jquery ui datepicker 插件

    日期选择插件Datepicker是一个配置灵活的插件,我们可以自定义其展示方式,包括日期格式.语言.限制选择日期范围.添加相关按钮以及其它导航等.官方地址:http://docs.jquery.com ...

  5. JQuery 日期选择框

    一    jeDate日期控件,关于官方的文档请查看: http://www.jayui.com/jedate/ 1 引入js文件 <script type="text/javascr ...

  6. Android 日期选择框 简洁常用

    效果 核心代码 >方法 /** * @description 选择日期弹出框 * @param listener 选择日期确定后执行的接口 * @param curDate 当前显示的日期 * ...

  7. iview日期选择框,获取的日期总是少一天

    使用iview的datepicker时间选择器发现获取的value值是比实际要少一天,严格来说应该是时间格式不一样,datepicker获取的时间是UTC时间 格式,也就是:yyyy-MM-ddTHH ...

  8. 使用elementUI的日期选择框,两选择框关联时间限值

    elementui 本身也提供了在一个输入框内关联选择时间的组件,非常好使,但无奈项目需要用两个输入框去关联的选择: <el-date-picker class="datepicker ...

  9. PyQt5显示日期选择框,获取日期保存文件

    一.UI显示选择日期,保存到文件 import sys,os from PyQt5 import QtCore from PyQt5.QtGui import * from PyQt5.QtWidge ...

随机推荐

  1. Kibana 地标图可视化

    ElasticSearch 可以使用 ingest-geoip 插件可以在 Kibana 上对 IP 进行地理位置分析, 这个插件需要 Maxmind 的 GeoLite2 City,GeoLite2 ...

  2. 2020 Web 全栈面经

    2020 Web 全栈面经 1.简历 2. 技术 3. 项目 4. 架构 5. 沟通,协作 6.成长 7. 面试技巧 准备 电话确认,面试流程,五险一金缴纳情况 有无笔试,几轮,面试时间,答复时间 细 ...

  3. npm-run-all

    npm-run-all npm scripts https://www.npmjs.com/package/npm-run-all A CLI tool to run multiple npm-scr ...

  4. free online linux terminal & github cli online

    free online linux terminal & github cli online gitpod https://www.gitpod.io/features/ https://bc ...

  5. auto scroll bottom in js

    auto scroll bottom in js autoScrollToBottom() { let box = document.querySelector(`[data-dom="ch ...

  6. 微信小程序 components

    微信小程序 components wx-xcx-components https://developers.weixin.qq.com/miniprogram/dev/component/ https ...

  7. 用WPF写了个处理视频,音频,图片的工具

    处理工具依赖ffmpeg,感兴趣可以看下Github上的源码 下载地址也在Github上

  8. Mybatis-05 注解开发

    Mybatis-05 注解开发 注解开发 注解的核心是反射机制 面向接口编程的根本原因:解耦,可拓展,提高复用,分层开发中.上层不用管具体的实现,大家都遵守共同的标准,使得开发变得容易,规范性好. 1 ...

  9. 10_MySQL数据表的基本查询

    为了更好的练习数据表的操作,我们需要有些数据来供我们练习,这里给大家分享一份数据,大家可以拿去自己练习使用. 文件地址:https://files.cnblogs.com/files/waterr/d ...

  10. MySQL数据库与NAVICAT安装与配置

    1.安装破解版的NAVICAT:https://www.cnblogs.com/yinfei/p/11427259.html 2.连接MYSQL 下载MYSQL并安装,配置环境变量 以管理员身份运行C ...