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高阶组件学习笔记
高阶函数的基本概念: 函数可以作为参数被传递,函数可以作为函数值输出. 高阶组件基本概念: 高阶组件就说接受一个组件作为参数,并返回一个新组件的函数. 为什么需要高阶组件 多个组件都需要某个相同的功能 ...
随机推荐
- 微信小程序 --- Cannot read property 'setData' of null 解决
在外部定义一个为 this 的 that
- fopen和fopen_s用法的比较
open和fopen_s用法的比较 fopen 和 fopen_s fopen用法: fp = fopen(filename,"w"). fop ...
- ChartControl控件0和null的效果
DevExpress的ChartControl虽然还不能完全代替Office图表(例如它暂时不支持添加数据表),但它算同类产品中相当优秀的了,下面是对0值和空值的处理. DataTable zeroD ...
- 使用pyenv安装多个Python版本
pyenv是一个便于使用多版本Python环境的工具 pyenv使用shell脚本编写的,只需要下载就可以使用了,不需要root用户,这个我比较喜欢. 具体介绍看网址:https://github.c ...
- 【jquery采坑】Ajax配合form的submit提交(微擎表单提交,ajax验证,submit提交)
1.采坑:实现form的submit提交,在提交之前,进行ajax的不同校验,然后onsubmit=return check(),进行提交 1/1 目的:可以实现以 from的submit提交,然后还 ...
- SQL Server 性能优化实战系列(二)
SQL Server datetime数据类型设计.优化误区 一.场景 在SQL Server 2005中,有一个表TestDatetime,其中Dates这个字段的数据类型是datetime,如果你 ...
- Linux: Linux C 获取当前系统时间的时间戳(精确到秒、毫秒、微秒) gettimeofday
说明 获取当前的时间的秒数和微秒数本方法需要用到 gettimeofday() 函数,该函数需要引入的头文件是 <sys/time.h> . 函数说明 int gettimeofday ...
- Ubuntu18.04提示wifi无法连接
[时间:2018-07] [状态:Open] [关键词:Ubuntu 18.04,wifi,apt-get,mount] 0 现象描述及背景 最近需要一个Ubuntu的系统作为开发环境,随机下载了v1 ...
- [Memcached]分布式缓存系统Memcached在Asp.net下的应用
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...
- U-BOOT2016.05 配置内存大小
bootargs 里面有一个 mem 的参数,将这个配置 512M 便是将内存配置为 512M