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高阶组件学习笔记
高阶函数的基本概念: 函数可以作为参数被传递,函数可以作为函数值输出. 高阶组件基本概念: 高阶组件就说接受一个组件作为参数,并返回一个新组件的函数. 为什么需要高阶组件 多个组件都需要某个相同的功能 ...
随机推荐
- gdb调试动态链接so
http://blog.csdn.net/weed_hz/article/details/12710429 gdb) file <你的exe>(gdb) load <你的so> ...
- 前端工程化系列[01]-Bower包管理工具的使用
本文主要介绍前端开发中常用的包管理工具Bower,具体包括Bower的基本情况.安装.使用和常见命令等内容,最后还介绍了依赖树管理的常见方式以及Bower采用的策略并进行了比较. 1.1 关于Bowe ...
- APB协议
https://wenku.baidu.com/view/2663f629ef06eff9aef8941ea76e58fafab04592.html https://www.cnblogs.com/l ...
- 第一章 java nio三大组件与使用姿势
本案例来源于<netty权威指南> 一.三大组件 Selector:多路复用器.轮询注册在其上的Channel,当发现某个或者多个Channel处于“就绪状态”后(accept接收连接事件 ...
- 不要使用Integer做HashMap的key,尤其在json序列化的时候
使用redisson cache来实现一个缓存功能,缓存省市县的名称,key是区域编码,integer,value是name.结果取的时候,怎么都取不出. Map<Integer, String ...
- FFM及DeepFFM模型在推荐系统的探索及实践
12月20日至23日,全球人工智能与机器学习技术大会 AiCon 2018 在北京国际会议中心盛大举行,新浪微博AI Lab 的资深算法专家 张俊林@张俊林say 主持了大会的 搜索推荐与算法专题,并 ...
- 转:C# Delegate委托 1
Delegate中文翻译为“委托”.MSDN中对Delegate的解释如下: C#中的委托类似于C或C++中的函数指针.使用委托使程序员可以将方法引用封装在委托对象内.然后可以将该委托对象传递给可调用 ...
- 人人网框架导入uidGenerator的ID生成方式
人人网框架导入uidGenerator的ID生成方式 2019-03-11 LIUREN SpringBoot2.0 uidGenerator SpringBoot2.0 uidGener ...
- C#版Websocket实例
C#版Websocket实例 Demo地址:www.awbeci.xyz websocket有java.nodejs.python,Php等等版本,我使用的是C#版本,服务器端是Fleck,git ...
- jmeter 执行python脚本
jmeter 可以通过Jython 执:行python代码 1.下载Jython jar包:http://www.jython.org/downloads.html 2.把下载的Jython 的jar ...