React datepicker

Developer
Size
6,518 Kb
Views
28,336

How do I make an react datepicker?

What is a react datepicker? How do you make a react datepicker? This script and codes were developed by Tim Cheng on 17 November 2022, Thursday.

React datepicker Previews

React datepicker - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>React datepicker</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="app"></div> <script src='https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

React datepicker - Script Codes CSS Codes

html,
body,
#app { height: 100%; width: 100%; background: #1d2227; color: #edeff6;
}
#app { width: 600px; margin: 0 auto;
}
.tb-main { display: table; table-layout: fixed;
}
.tb-tr, .tb-th { display: table-row;
}
.tb-th { color: #757b8b;
}
.tb-cell { display: table-cell; text-align: center; vertical-align: center; font-size: 14px; width: 2.5em; padding: 4px 0;
}
.dp-main { display: inline-block; background: #333;
}
.dp-header { position: relative; text-align: center; background-color: #222; height: 1.8em; line-height: 1.8em; font-size: 14px;
}
.dp-sel { display: inline-block; position: relative; padding: 0 1em;
}
.dp-sel-icon { position: absolute; top: 0; cursor: pointer; color: #757b8b; height: 1.8em; line-height: 1.8em; padding: 0 4px; -webkit-transition: .3s color; transition: .3s color;
}
.dp-sel-icon:hover { color: #edeff6;
}
.dp-sel-icon.left { left: 28px;
}
.dp-sel-icon.left:after { content: '\2039'; position: relative; top: -1px;
}
.dp-sel-icon.double-left { left: 8px;
}
.dp-sel-icon.double-left:after { content: '\AB'; position: relative; top: -1px;
}
.dp-sel-icon.right { right: 28px;
}
.dp-sel-icon.right:after { content: '\203A'; position: relative; top: -1px;
}
.dp-sel-icon.double-right { right: 8px;
}
.dp-sel-icon.double-right:after { content: '\BB'; position: relative; top: -1px;
}
.dp-day-item { cursor: pointer;
}
.dp-day-item.active, .dp-day-item:hover { background-color: red;
}

React datepicker - Script Codes JS Codes

'use strict';
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _React = React;
var PropTypes = _React.PropTypes;
var getDaysOfMonth = function getDaysOfMonth(year, month) { return new Date(year, (month + 1) % 12, 0).getDate();
};
var DatePicker = function (_React$Component) { _inherits(DatePicker, _React$Component); function DatePicker(props) { _classCallCheck(this, DatePicker); // Day of week var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); _this.dowDisp = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; _this.monthDisp = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; var calDate = props.date || new Date(); var calYear = calDate.getFullYear(); var calMonth = calDate.getMonth(); _this.state = { calYear: calYear, calMonth: calMonth // Jan = 0 }; _this.nextMonth = _this.nextMonth.bind(_this); _this.nextYear = _this.nextYear.bind(_this); _this.prevMonth = _this.prevMonth.bind(_this); _this.prevYear = _this.prevYear.bind(_this); return _this; } DatePicker.prototype.nextMonth = function nextMonth() { var state = this.state; var calMonth = state.calMonth; var calYear = state.calYear; var month = (calMonth + 1) % 12; var year = calYear; if (month < calMonth) year++; this.setState({ calMonth: month, calYear: year }); }; DatePicker.prototype.prevMonth = function prevMonth() { var state = this.state; var calMonth = state.calMonth; var calYear = state.calYear; var month = calMonth - 1; var year = calYear; if (month < 0) { // Going into prev year year = year - 1; month = 11; } this.setState({ calMonth: month, calYear: year }); }; DatePicker.prototype.nextYear = function nextYear() { var state = this.state; this.setState({ calYear: state.calYear + 1 }); }; DatePicker.prototype.prevYear = function prevYear() { var state = this.state; this.setState({ calYear: state.calYear - 1 }); }; DatePicker.prototype.renderCalendar = function renderCalendar() { // date => // Get year and month from date || today // get total number of days eg. 30 => // place days to dow display: while (i < totalDays) { if (dow == getDay(year, month, i) placeDay) } // Highlight the selected day var props = this.props; var state = this.state; var dowDisp = this.dowDisp; var calYear = state.calYear; var calMonth = state.calMonth; var selectedDate = props.date || null; var curMonthDays = getDaysOfMonth(calYear, calMonth); var firstDOW = new Date(calYear, calMonth, 1).getDay(); var calStartDOW = 0; // Starts on sunday var DAYS_OF_WEEK = 7; // Number of days to show from prev month var prevMonthDays = (DAYS_OF_WEEK + (firstDOW - calStartDOW)) % DAYS_OF_WEEK; // Number of days to show from next month var nextMonthDays = DAYS_OF_WEEK - (prevMonthDays + curMonthDays) % DAYS_OF_WEEK; console.log('prevmonthdays', prevMonthDays, 'nextMoDays', nextMonthDays, 'curmodays', curMonthDays); var dayArr = []; var MAX_DAYS = 42; // Hack: force calendar to always show 7*6 = 42 slots, so that the height will stay the same for (var i = 1 - prevMonthDays; dayArr.length < MAX_DAYS; i++) { dayArr.push(i); } console.log('dayarr len', dayArr.length); var dowArr = []; for (var i = 0; i < DAYS_OF_WEEK; i++) { var dowIndex = (calStartDOW + i) % DAYS_OF_WEEK; dowArr.push(dowDisp[dowIndex]); } var renderDays = function renderDays() { var rows = []; var cells = []; for (var i = 0; i < dayArr.length; i++) { var disp = dayArr[i]; if (disp < 1 || disp > curMonthDays) { disp = React.createElement( 'span', null, ' ' ); } else { disp = React.createElement( 'div', { className: 'dp-day-item' }, disp ); } cells.push(React.createElement( 'div', { className: 'tb-cell', key: i }, disp )); if ((i + 1) % DAYS_OF_WEEK === 0) { rows.push(React.createElement( 'div', { className: 'tb-tr', key: i }, cells )); cells = []; } } return rows; }; return React.createElement( 'div', { className: 'dp-calendar tb-main' }, React.createElement( 'div', { className: 'tb-th' }, dowArr.map(function (dow, i) { return React.createElement( 'div', { className: 'tb-cell', key: i }, dow ); }) ), renderDays() ); }; DatePicker.prototype.renderHeader = function renderHeader() { var state = this.state; var monthDisp = this.monthDisp; return React.createElement( 'div', { className: 'dp-header' }, React.createElement('div', { className: 'dp-sel-icon double-left', onClick: this.prevYear }), React.createElement('div', { className: 'dp-sel-icon left', onClick: this.prevMonth }), state.calYear + ' ' + monthDisp[state.calMonth], React.createElement('div', { className: 'dp-sel-icon right', onClick: this.nextMonth }), React.createElement('div', { className: 'dp-sel-icon double-right', onClick: this.nextYear }) ); }; DatePicker.prototype.render = function render() { var props = this.props; var state = this.state; var monthDisp = this.monthDisp; var date = props.date; return React.createElement( 'div', { className: 'dp-main' }, this.renderHeader(), this.renderCalendar() ); }; return DatePicker;
}(React.Component);
DatePicker.propTypes = { date: PropTypes.object, maxDate: PropTypes.object, minDate: PropTypes.object
};
var AppMain = function (_React$Component2) { _inherits(AppMain, _React$Component2); function AppMain(props) { _classCallCheck(this, AppMain); return _possibleConstructorReturn(this, _React$Component2.call(this, props)); } AppMain.prototype.render = function render() { return React.createElement( 'div', null, React.createElement(DatePicker, { date: new Date(), maxDate: new Date() }) ); }; return AppMain;
}(React.Component);
React.render(React.createElement(AppMain, null), document.getElementById('app'));
React datepicker - Script Codes
React datepicker - Script Codes
Home Page Home
Developer Tim Cheng
Username tmchng
Uploaded November 17, 2022
Rating 3
Size 6,518 Kb
Views 28,336
Do you need developer help for React datepicker?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Tim Cheng (tmchng) Script Codes
Create amazing art & images with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!