React & Calendar
React & Calendar
日历

https://github.com/YutHelloWorld/calendar/blob/master/src/Calendar.js
// 国际化 i18n & L10n ??? local.config.json
renderTable() {
const weekdays = this.props.locale === 'en' ? ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
: ['日', '一', '二', '三', '四', '五', '六']
return (
<table className="calendar-table">
<tbody onClick={this.handleSelectDate}>
<tr>
{
weekdays.map((w, i) => <th key={w} >{w}</th>)
}
</tr>
{this.state.list.map(arr => {
return (
<tr key={arr[0]}>
{arr.map(value =>
(<td key={value}>
<span
data-date={value}
className={this.getClassName(value)}
>
{value.slice(8)}
</span>
</td>)
)}
</tr>
)
})}
</tbody>
</table>
)
}
https://github.com/YutHelloWorld/calendar/blob/master/src/utils/index.js
/**
* 获取日历展示日期列表
*
* @export
* @param {Number} y
* @param {Number} m
* @returns
*/
export function getDateList(y, m) {
const year = y
const month = m - 1
let list = []
const now = new Date(year, month)
const monthEnd = new Date(year, month + 1, 0) // 当月最后一天
const lastMonthEnd = new Date(year, month, 0) // 上月最后一天
const firstDay = now.getDay() // 当月第一天
const mEDate = monthEnd.getDate()
const lMEDate = lastMonthEnd.getDate()
// 计算上月出现在 中的日期
for (let i = 0; i < firstDay; i++) {
const tempM = month > 0 ? month : 12
const tempY = month > 0 ? year : year - 1
const strMonth = tempM < 10 ? `0${tempM}` : tempM
list.unshift(`${tempY}-${strMonth}-${lMEDate - i}`)
}
// 当月
for (let i = 1; i < mEDate + 1; i++) {
const strI = i < 10 ? '0' + i : i
const tempM = month + 1
const strMonth = tempM < 10 ? `0${tempM}` : tempM
list.push(`${year}-${strMonth}-${strI}`)
}
const tempLen = 42 - list.length
// 下月
for (let i = 1; i < tempLen + 1; i++) {
const strI = i < 10 ? '0' + i : i
const tempM = month + 2 < 13 ? month + 2 : 1
const tempY = month + 2 < 13 ? year : year + 1
const strMonth = tempM < 10 ? `0${tempM}` : `${tempM}`
list.push(`${tempY}-${strMonth}-${strI}`)
}
return list
}
const weeks = ["日", "一", "二", "三", "四", "五", "六"];
https://cloud.tencent.com/developer/article/1497665
https://segmentfault.com/a/1190000016296697
import * as React from 'react'
const WEEK_NAMES = ['日', '一', '二', '三', '四', '五', '六']
const LINES = [1,2,3,4,5,6]
const MONTH_DAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
export default class Calendar extends React.Component {
state = {
month: 0,
year: 0,
currentDate: new Date()
}
componentWillMount() {
this.setCurrentYearMonth(this.state.currentDate)
}
setCurrentYearMonth(date) {
var month = Calendar.getMonth(date)
var year = Calendar.getFullYear(date)
this.setState({
month,
year
})
}
static getMonth(date: Date): number{
return date.getMonth()
}
static getFullYear(date: Date): number{
return date.getFullYear()
}
static getCurrentMonthDays(month: number): number {
return MONTH_DAYS[month]
}
static getWeeksByFirstDay(year: number, month: number): number {
var date = Calendar.getDateByYearMonth(year, month)
return date.getDay()
}
static getDayText(line: number, weekIndex: number, weekDay: number, monthDays: number): any {
var number = line * 7 + weekIndex - weekDay + 1
if ( number <= 0 || number > monthDays ) {
return <span> </span>
}
return number
}
static formatNumber(num: number): string {
var _num = num + 1
return _num < 10 ? `0${_num}` : `${_num}`
}
static getDateByYearMonth(year: number, month: number, day: number=1): Date {
var date = new Date()
date.setFullYear(year)
date.setMonth(month, day)
return date
}
checkToday(line: number, weekIndex: number, weekDay: number, monthDays: number): Boolean {
var { year, month } = this.state
var day = Calendar.getDayText(line, weekIndex, weekDay, monthDays)
var date = new Date()
var todayYear = date.getFullYear()
var todayMonth = date.getMonth()
var todayDay = date.getDate()
return year === todayYear && month === todayMonth && day === todayDay
}
monthChange(monthChanged: number) {
var { month, year } = this.state
var monthAfter = month + monthChanged
var date = Calendar.getDateByYearMonth(year, monthAfter)
this.setCurrentYearMonth(date)
}
render() {
var { year, month } = this.state
console.log(this.state)
var monthDays = Calendar.getCurrentMonthDays(month)
var weekDay = Calendar.getWeeksByFirstDay(year, month)
return (<div>
{this.state.month}
<table cellPadding={0} cellSpacing={0} className="table">
<caption>
<div className="caption-header">
<span className="arrow" onClick={this.monthChange.bind(this, -1)}><</span>
<span>{year} - {Calendar.formatNumber(month)}</span>
<span className="arrow" onClick={this.monthChange.bind(this, 1)}>></span>
</div>
</caption>
<thead>
<tr>
{
WEEK_NAMES.map((week, key) => {
return <td key={key}>{week}</td>
})
}
</tr>
</thead>
<tbody>
{
LINES.map((l, key) => {
return <tr key={key}>
{
WEEK_NAMES.map((week, index) => {
return <td key={index} style={{color: this.checkToday(key, index, weekDay, monthDays) ? 'red' : '#000'}}>
{Calendar.getDayText(key, index, weekDay, monthDays)}
</td>
})
}
</tr>
})
}
</tbody>
</table>
</div>)
}
}
https://github.com/LingyuCoder/react-lunar-calendar/blob/master/index.jsx
https://rsuitejs.com/components/calendar
https://github.com/rsuite/rsuite
https://www.cnblogs.com/ajanuw/p/10100320.html
https://github.com/hanse/react-calendar
https://github.com/BelkaLab/react-yearly-calendar
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
React & Calendar的更多相关文章
- React 学习笔记(学习地址汇总)
好的博文地址:http://www.ruanyifeng.com/blog/2015/03/react.html 官网学习地址:http://facebook.github.io/react/docs ...
- react路由深度解析
先看一段代码能否秒懂很重要 这是app.js 全局js的入口 import React from 'react' import { render } from 'react-dom' import ...
- react native 中时间选择插件
npm install react-native-datepicker --save import DatePicker from 'react-native-datepicker'; <Vie ...
- React Router 4.x 开发,这些雷区我们都帮你踩过了
前言 在前端框架层出不穷的今天,React 以其虚拟 DOM .组件化开发思想等特性迅速占据了主流位置,成为前端开发工程师热衷的 Javascript 库.作为 React 体系中的重要组成部分:Re ...
- react学习一篇就够了
webstrom自动格式化代码 命令 js框架 MVC 安装 npm install create-react-app -g 生成项目(项目名npm发包包命名规范 /^[a-z0-9_-]$/) cr ...
- React Router API文档
React Router API文档 一.<BrowserRouter> 使用HTML5历史记录API(pushState,replaceState和popstate事件)的<Rou ...
- Expo大作战(四十)--expo sdk api之 Calendar,Constants
简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...
- 时间选择控件YearPicker(基于React,antd)
不知道为什么蚂蚁金服团队没有在ant design的DatePicker中单独给出选择年份的组件,这给我们这种懒人造成了很大的痛苦,自己手造轮子是很麻烦的.毕竟只是一个伸手党,emmmmm..... ...
- React半科普文
React半科普文 什么是React getting started 文件分离 Server端编译 定义一个组件 使用property 组件嵌套 组件更新 Virtual DOM react nati ...
随机推荐
- Routine Subroutine Coroutine 子程序 协程 子例程
https://en.wikipedia.org/wiki/Subroutine In computer programming, a subroutine is a sequence of prog ...
- Apache HTTP Server 映射URL到文件系统(翻译)
div.example { background-color: rgba(229, 236, 243, 1); color: rgba(0, 0, 0, 1); padding: 0.5em; mar ...
- ECMAScript6常用新特性总结
一.let声明变量 1.基本用法: ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. 如下代码: { let a = 10; var ...
- Node.js 安全指南
当项目周期快结束时,开发人员会越来越关注应用的"安全性"问题.一个安全的应用程序并不是一种奢侈,而是必要的.你应该在开发的每个阶段都考虑应用程序的安全性,例如系统架构.设计.编码, ...
- 最简单直接地理解Java软件设计原则之依赖倒置原则
理论性知识 定义 依赖倒置原则,Dependence Inversion Principle (DIP) 高层模块不应该依赖低层模块.二者都应该依赖其抽象. 抽象不应该依赖细节,细节应该依赖抽象. 针 ...
- JavaWeb——JSP内置对象application,JSP属性范围
application application语法 application对象 JSP属性范围 范围的分类 page request session application pagecontext延伸 ...
- Java——序列化与反序列化
序列化 序列化 序列化是什么 如何实现对象序列化 实战练习 Serializable 反序列化 总结 使用Reader读取文件内容1 使用Reader读取文件内容2 序列化是什么? 将对象的状态存储到 ...
- SSM、SSH框架搭建,面试点总结
文章目录 1.SSM如何搭建:三个框架的搭建: 2.SSM系统架构 3.SSM整合步骤 4.Spring,Spring MVC,MyBatis,Hibernate个人总结 5.面试资源 关于SSM.S ...
- mvn 多模块
mvn archetype:generate -DgroupId=com.xxx.cloud -DartifactId=myapp -Dversion=1.0.0-SNAPSHOT -Dpackage ...
- 7. Linux命令行的通配符、转义字符
1.命令行的通配符 举例:1)列出所有在/dev 目录中以sda 开头的文件 [root@Centos test]# ll /dev/sda* brw-rw----. 1 root disk 8, 0 ...