Last active
May 30, 2023 20:02
-
-
Save insin/56a10daa808cdcdcbf83 to your computer and use it in GitHub Desktop.
react-calendarbits: Simple components for rendering calendars for prototyping date-based stuff (Live version: http://bl.ocks.org/insin/raw/56a10daa808cdcdcbf83/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void function() { 'use strict'; | |
var cx = (staticClasses, conditionalClasses) => { | |
var classNames = [] | |
if (typeof conditionalClasses == 'undefined') { | |
conditionalClasses = staticClasses | |
} | |
else { | |
classNames.push(staticClasses) | |
} | |
Object.keys(conditionalClasses).forEach(className => { | |
if (!!conditionalClasses[className]) { | |
classNames.push(className) | |
} | |
}) | |
return classNames.join(' ') | |
} | |
var DAYS = 'Su Mo Tu We Th Fr Sa Su'.split(' ') | |
var MONTH_NAMES = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ') | |
// ============================================================== Components === | |
var Month = React.createClass({ | |
getDefaultProps() { | |
return { | |
firstDayOfWeek: 0 | |
} | |
}, | |
render() { | |
var {year, month, firstDayOfWeek} = this.props | |
var date = moment([year, month, 1]).day(firstDayOfWeek) | |
var weeks = [] | |
do { | |
var week = [] | |
for (var i = 0; i < 7; i++) { | |
if (date.month() === month) { | |
week.push(date.clone()) | |
} | |
else { | |
week.push(null) | |
} | |
date.add(1, 'days') | |
} | |
weeks.push(week) | |
} | |
while (date.month() === month) | |
return <div className="Month"> | |
<table> | |
<caption>{MONTH_NAMES[month]} {year}</caption> | |
<thead> | |
<tr> | |
{DAYS.slice(firstDayOfWeek, firstDayOfWeek + 7).map(day => <th>{day}</th>)} | |
</tr> | |
</thead> | |
<tbody> | |
{weeks.map(week => <tr className="Week"> | |
{week.map(day => <Day date={day}/>)} | |
</tr>)} | |
</tbody> | |
</table> | |
</div> | |
} | |
}) | |
var Day = React.createClass({ | |
_onClick(e) { | |
// ... | |
}, | |
_onDoubleClick(e) { | |
// ... | |
}, | |
render() { | |
var date = this.props.date | |
var blank = date == null | |
return <td onClick={this._onClick} onDoubleClick={this._onDoubleClick} className={cx({ | |
'Blank': blank | |
, 'Day': !blank | |
// ... | |
})}> | |
{!blank && date.date()} | |
</td> | |
} | |
}) | |
var App = React.createClass({ | |
render() { | |
return <div className="App"> | |
<div className="Months"> | |
<Month month={11} year={2014}/> | |
<Month month={0} year={2015}/> | |
<Month month={1} year={2015}/> | |
</div> | |
</div> | |
} | |
}) | |
React.render(<App/>, document.getElementById('app')) | |
}() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta charset="utf-8"> | |
<title>react-calendarbits</title> | |
<script src="http://fb.me/react-with-addons-0.12.2.js"></script> | |
<script src="http://fb.me/JSXTransformer-0.12.2.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.js"></script> | |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto"> | |
<link rel="stylesheet" href="style.css"> | |
</head> | |
<body> | |
<div id="app"></div> | |
<script type="text/jsx;harmony=true" src="app.jsx"></script> | |
<a href="https://gist.github.com/insin/56a10daa808cdcdcbf83"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
font-family: Roboto, sans-serif; | |
} | |
.Month { | |
display: inline-block; | |
vertical-align: top; | |
margin-right: .5em; | |
margin-bottom: .5em; | |
} | |
.Month:last-child { | |
margin-right: 0; | |
} | |
.Month caption { | |
font-weight: bold; | |
background-color: #ddd; | |
text-align: center; | |
padding: .2em 0; | |
} | |
.Month table { | |
border-collapse: collapse; | |
} | |
.Month th { | |
text-align: center; | |
font-weight: normal; | |
text-transform: uppercase; | |
} | |
.Month td { | |
text-align: center; | |
padding: 6px; | |
} | |
.Day { | |
cursor: pointer; | |
-webkit-user-select: none; | |
-khtml-user-select: none; | |
-moz-user-select: -moz-none; | |
-o-user-select: none; | |
user-select: none; | |
} | |
.Day:hover { | |
outline: 1px dotted #222; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment