react+ts封装AntdUI的日期选择框之月份选择器DatePicker.month
需求:由于在项目开发中,当需要使用该组件时都需要对该组件进行大量的代码输出,为了方便代码统一管理,减少冗余代码,所以将此组件进行二次封装。
其他成员在使用中只需将自己的设置通过对应的参数传递到该组件,在执行选中操作后直接将返回值回传给调用的父组件即可。
技术涉及: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的更多相关文章
- js控制日期选择框datetime-local和select的展开
注: js控制元素展开不受元素css属性的限制,例如opacity,z-index等 1. 使用js控制日期选择框的展开 ios: document.querySelector(".targ ...
- [RN] React Native 自定义 底部 弹出 选择框 实现
React Native 自定义 底部选择框 实现 效果如图所示: 实现方法: 一.组件封装 CustomAlertDialog.js import React, {Component} from ' ...
- android 弹出日期选择框
DatePickerDialog 在很多时候需要用户去设定时间,不可能让用户去在一个文本框中去输入时间,所以就需要有个日期弹出选择框,而这个框就是DatePickerDialog. 1.在API中的D ...
- 页面日期选择控件--jquery ui datepicker 插件
日期选择插件Datepicker是一个配置灵活的插件,我们可以自定义其展示方式,包括日期格式.语言.限制选择日期范围.添加相关按钮以及其它导航等.官方地址:http://docs.jquery.com ...
- JQuery 日期选择框
一 jeDate日期控件,关于官方的文档请查看: http://www.jayui.com/jedate/ 1 引入js文件 <script type="text/javascr ...
- Android 日期选择框 简洁常用
效果 核心代码 >方法 /** * @description 选择日期弹出框 * @param listener 选择日期确定后执行的接口 * @param curDate 当前显示的日期 * ...
- iview日期选择框,获取的日期总是少一天
使用iview的datepicker时间选择器发现获取的value值是比实际要少一天,严格来说应该是时间格式不一样,datepicker获取的时间是UTC时间 格式,也就是:yyyy-MM-ddTHH ...
- 使用elementUI的日期选择框,两选择框关联时间限值
elementui 本身也提供了在一个输入框内关联选择时间的组件,非常好使,但无奈项目需要用两个输入框去关联的选择: <el-date-picker class="datepicker ...
- PyQt5显示日期选择框,获取日期保存文件
一.UI显示选择日期,保存到文件 import sys,os from PyQt5 import QtCore from PyQt5.QtGui import * from PyQt5.QtWidge ...
随机推荐
- Python:装饰器是如何调用的
应用举例:(1)装饰器 # 装饰器的调用: # 一旦用上装饰器会: # 第一步:调outer函数 # 第二步:被装饰的函数play_game会被当作参数fn给outer # 第三步:最后调用play_ ...
- MySQL 主从复制(上)
目录 主从复制前提 主从复制原理 涉及的文件 & 线程 Master 主库 Slave 从库 主从复制的搭建 主库配置 配置 /etc/my.cnf 建立专用复制用户 查看 Binlog 信息 ...
- spring再学习之AOP实操
一.spring导包 2.目标对象 public class UserServiceImpl implements UserService { @Override public void save() ...
- free video tutorial of Deep Learning
free video tutorial of Deep Learning AI 深度学习/ 机器学习/人工智能 Deep Learning With Deep Learning – a form of ...
- LeetCode 刷题 App / LeetCode 题解 App
LeetCode 刷题 APP / LeetCode 题解 App 全端支持 http://leetcode-app.xgqfrms.xyz/ http://leetcode-desktop.xgqf ...
- holy shit StackOverflow
holy shit StackOverflow refs https://stackoverflow.com/users/5934465/xgqfrms?tab=questions xgqfrms 2 ...
- copyright@xgqfrms
copyright@xgqfrms copyright & seo ## refs *** <div> <a href="https://info.flagcoun ...
- Array in Depth
Array in Depth Array.concat() & Array.push() https://developer.mozilla.org/en-US/docs/Web/JavaSc ...
- Dart 中断Future
更多 中断future 方法1) import 'package:async/async.dart'; void main() { var get = CancelableOperation.from ...
- Baccarat凭什么能成为DeFi后时代火爆新趋势?
在各币种经历涨涨跌跌以后,DeFi后时代已然来临.那么,当前DeFi市场中哪个项目更被市场生态建设者看好呢?毫无疑问,Baccarat会成为最被看好的DeFi项目. Baccarat采用了独特的共识算 ...