react 编写日历组件
简单的日历组件
import React, { Component } from "react";
import * as _ from "lodash";
const l = console.log;
const weeks = ["日", "一", "二", "三", "四", "五", "六"];
class Test extends Component {
state = {};
componentWillMount() {
this.initState();
}
initState = ({ y, m } = {}) => {
let date = new Date();
let year = y || date.getFullYear(); // 本年
let month = m || date.getMonth() + 1; // 本月
l(`${year}年${month}月.`);
let date2 = new Date(year, month, 0);
let days = date2.getDate(); // 本月有多少天
l(`本月有${days}天.`);
date2.setDate(1);
let day = date2.getDay(); // 本月第一天是星期几
l(`本月第一天是星期${day}.`);
let list = [];
for (let i = 0; i < days + day; i++) {
if (i < day) {
list.push(0);
} else {
list.push(i - day + 1);
}
}
let hlist = _.chunk(list, 7); // 转化为二维数组
let len = hlist.length;
let to = 7 - hlist[len - 1].length;
// 循环尾部补空格
for (let i = 0; i < to; i++) {
hlist[len - 1].push(0);
}
this.setState({
date,
year,
month,
days,
day,
hlist,
});
};
// 上月
handlePrevMonth = () => {
let prevMonth = this.state.month + -1;
let prevYear = this.state.year;
if (prevMonth < 1) {
prevMonth = 12;
prevYear -= 1;
}
this.initState({
y: prevYear,
m: prevMonth,
});
};
// 下月
handleNextMonth = () => {
let nextMonth = this.state.month + 1;
let nextYear = this.state.year;
if (nextMonth > 12) {
nextMonth = 1;
nextYear += 1;
}
this.initState({
y: nextYear,
m: nextMonth,
});
};
render() {
const { year, month } = this.state;
return (
<>
<h2>
{year}年,{month}月
</h2>
<div>
<button onClick={this.handlePrevMonth}>上月</button>
<button onClick={this.handleNextMonth}>下月</button>
</div>
<table>
<tbody>
<tr>
{weeks.map(el => (
<th key={el}>{el}</th>
))}
</tr>
{this.state.hlist.map((el, i) => {
return (
<tr key={i}>
{el.map((n, ii) => (
<td key={ii}>{n}</td>
))}
</tr>
);
})}
</tbody>
</table>
</>
);
}
}
export default Test;
2
import React, { Component } from "react";
import * as _ from "lodash";
class DateItem {
/**
*
* @param dayNum 日数, 如果和 new Date().getDate() 相等则是今天
* @param isSignIn=false 是否签到
* @param isShowSignIn=false 是否显示是否签到,大于今日和这个月的日期应该都不显示
*/
constructor({ dayNum, isSignIn = false, isShowSignIn = false }) {
Object.assign(this, {
dayNum,
isSignIn,
isShowSignIn,
});
}
}
const l = console.log;
const weeks = ["日", "一", "二", "三", "四", "五", "六"];
class Test extends Component {
state = {};
componentWillMount() {
this.initState();
}
initState = ({ y, m } = {}) => {
const date = new Date();
const year = y || date.getFullYear(); // 本年
const month = m || date.getMonth() + 1; // 本月
l(`${year}年${month}月.`);
let date2 = new Date(year, month, 0);
let days = date2.getDate(); // 本月有多少天
l(`本月有${days}天.`);
date2.setDate(1);
let day = date2.getDay(); // 本月第一天是星期几
l(`本月第一天是星期${day}.`);
let list = [];
const nowadays = date.getDate(); // 本日
const thisMonth = date.getMonth() + 1; // 本月
let isShowSignIn = false;
const date2GtDate = date2 > date;
const isThisMonth = month === thisMonth; // 选择的日期的月份是否是本月
for (let i = 0; i < days + day; i++) {
const dayNum = i - day + 1;
if (date2GtDate) {
isShowSignIn = false;
} else {
if (isThisMonth && i >= day + nowadays) {
isShowSignIn = false;
} else {
isShowSignIn = true;
}
}
if (i < day) {
list.push(new DateItem({ dayNum: 0, isShowSignIn }));
} else {
list.push(new DateItem({ dayNum, isShowSignIn }));
}
}
let hlist = this.getHlist(list, isShowSignIn);
this.setState({
date,
year,
month,
days,
day,
list,
hlist,
nowadays,
thisMonth,
});
};
// 把一维日期切成二维日期
getHlist = (list, isShowSignIn) => {
let hlist = _.chunk(list, 7); // 转化为二维数组
let len = hlist.length;
let to = 7 - hlist[len - 1].length;
// 循环尾部补空格
for (let i = 0; i < to; i++) {
hlist[len - 1].push(new DateItem({ dayNum: 0, isShowSignIn }));
}
return hlist;
};
// 上月
handlePrevMonth = () => {
let prevMonth = this.state.month + -1;
let prevYear = this.state.year;
if (prevMonth < 1) {
prevMonth = 12;
prevYear -= 1;
}
this.initState({
y: prevYear,
m: prevMonth,
});
};
// 下月
handleNextMonth = () => {
let nextMonth = this.state.month + 1;
let nextYear = this.state.year;
if (nextMonth > 12) {
nextMonth = 1;
nextYear += 1;
}
this.initState({
y: nextYear,
m: nextMonth,
});
};
// 点击每个日期
handleDateItemClick = (dateItem, i, j) => () => {
const { year, month, date, nowadays } = this.state;
const { isShowSignIn, isSignIn, dayNum } = dateItem;
if (dayNum === 0) return;
const selectDate = new Date(`${year}-${month}-${dayNum}`);
if (nowadays === dayNum) {
l("签到");
} else if (selectDate < date) {
l("补签");
}
if (!isShowSignIn || isSignIn)
// 不能签到的日期和已签到的日期直接返回
return;
this.setState(state => {
const hlist = state.hlist.slice();
hlist[i][j].isSignIn = true;
return {
hlist,
};
});
};
render() {
const { year, month, nowadays, thisMonth } = this.state;
return (
<>
<h2>
{year}年,{month}月
</h2>
<div>
<button onClick={this.handlePrevMonth}>上月</button>
<button onClick={this.handleNextMonth}>下月</button>
<button
onClick={() => {
this.initState();
}}
>
今天
</button>
</div>
<table>
<tbody>
<tr>
{weeks.map(el => (
<th key={el}>{el}</th>
))}
</tr>
{this.state.hlist.map((el, i) => {
return (
<tr key={i}>
{el.map((dateItem, j) => {
const dayNum = dateItem.dayNum;
const isSignIn = dateItem.isSignIn;
const isShowSignIn = dateItem.isShowSignIn;
return (
<td
key={j}
style={{
color:
dayNum === nowadays && month === thisMonth && "red",
textAlign: "center",
padding: 8,
border: "1px solid",
borderColor: dateItem.isSignIn
? "red"
: "transparent",
opacity: dayNum === 0 ? 0 : 1,
}}
onClick={this.handleDateItemClick(dateItem, i, j)}
>
<div>{dayNum}</div>
{!!isShowSignIn && (
<div
style={{
whiteSpace: "nowrap",
fontSize: "12px",
}}
>
{!!isSignIn ? `已签到` : `未签到`}
</div>
)}
</td>
);
})}
</tr>
);
})}
</tbody>
</table>
</>
);
}
}
export default Test;
react 编写日历组件的更多相关文章
- React编写input组件传参共用onChange
之前写页面上的input比较少,所以没有单提出来一个组件,今天研究了下input组件,但共用一个onChange的问题卡了一会儿,查了下发现几个比较好的方法,分享下: 方法一 Input组件 let ...
- 前端笔记之React(四)生命周期&Virtual DOM和Diff算法&日历组件开发
一.React生命周期 一个组件从出生到消亡,在各个阶段React提供给我们调用的接口,就是生命周期. 生命周期这个东西,必须有项目,才知道他们干嘛的. 1.1 Mouting阶段[装载过程] 这个阶 ...
- react 编写组件 五
看以下示例了解如何定义一个组件 // 定义一个组件LikeButton var LikeButton = React.createClass({ // 给state定义初始值 getInitialSt ...
- 日历组件 原生js
自己基于原生js编写的日历组件 git地址: https://github.com/lihefen/calendar.git demo : https://lihefen.github.io/cale ...
- React jQuery公用组件开发模式及实现
目前较为流行的react确实有很多优点,例如虚拟dom,单向数据流状态机的思想.还有可复用组件化的思想等等.加上搭配jsx语法和es6,适应之后开发确实快捷很多,值得大家去一试.其实组件化的思想一直在 ...
- react native 常用组件汇总
react-native-uploader //文件上传https://github.com/aroth/react-native-uploader jpush-react-native //官方版本 ...
- 用 React 编写移动应用 React Native
转载:用 React 编写移动应用 React Native ReactNative 可以基于目前大热的开源JavaScript库React.js来开发iOS和Android原生App.而且React ...
- 使用JavaScript和React编写原生移动应用
使用JavaScript和React编写原生移动应用 React Native使你只使用JavaScript也能编写原生移动应用. 它在设计原理上和React一致,通过声明式的组件机制来搭建丰富多彩的 ...
- React高阶组件学习笔记
高阶函数的基本概念: 函数可以作为参数被传递,函数可以作为函数值输出. 高阶组件基本概念: 高阶组件就说接受一个组件作为参数,并返回一个新组件的函数. 为什么需要高阶组件 多个组件都需要某个相同的功能 ...
随机推荐
- .NET开源Protobuf-net组件葵花手册
一.前言 我们都知道 protobuf是由Google开发的一款与平台无关,语言无关,可扩展的序列化结构数据格式,可用做数据存储格式, 通信协议 ! 在前面<.NET开源Protobuf-net ...
- 使用 vscode + chrome debuger断点调试 Vue 程序
总体参考:https://cn.vuejs.org/v2/cookbook/debugging-in-vscode.html 注意点: 1.修改 source-map 2.设置 webRoot 的路径 ...
- 说说GIL
上一篇:线程深入篇引入 Code:https://github.com/lotapp/BaseCode/tree/master/python/5.concurrent/Thread/3.GIL 说说G ...
- OHDSI——数据标准化
Home › Data Standardization Data Standardization Data standardization is the critical process of bri ...
- Javascript正则表达入参是null
今天群友问了一个问题,如下的执行结果是什么? var reg = /^[a-z0-9\u4e00-\u9fa5]{0,15}$/; console.log(reg.test(null)); // tr ...
- [Canvas]空战游戏 已经可以玩了 1.13Playable
空战游戏做到这里,己方运动,己方发射子弹,敌方运动,敌方发射子弹,子弹与飞机碰撞,飞机与飞机碰撞都已经具备了,换言之已经可以玩了. 还需要一个奖励升级系统,在上面显示击落敌机数量等,还有己方不幸被击落 ...
- InfluxDB源码阅读之httpd服务
操作系统 : CentOS7.3.1611_x64 go语言版本:1.8.3 linux/amd64 InfluxDB版本:1.1.0 服务模块介绍 源码路径: github.com/influxda ...
- UESTC 1034 AC Milan VS Juventus 分情况讨论
AC Milan VS Juventus Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Oth ...
- 【Shiro】小读Shiro Filter
类继承结构图 看不明白此图不要紧,后面慢慢提到此图的类: AbstractFilter,抽象过滤器 它实现Filter.继承ServletContextSupport. 它主要实现了init(Filt ...
- underscore.js学习笔记
一.理清概念 1.Underscore封装了常用的JavaScript对象操作方法,用于提高开发效率,Underscore还可以被使用在Node.js运行环境.从API中,你已经可以看出,Unders ...