├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico └── index.html └── src ├── App.jsx ├── components └── Calendar │ ├── calendar.js │ ├── index.css │ └── index.jsx ├── index.css └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `npm start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `npm test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `npm run build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `npm run eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `npm run build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-calendar", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "classnames": "^2.2.6", 7 | "react": "^16.8.0", 8 | "react-dom": "^16.8.0", 9 | "react-scripts": "2.1.3" 10 | }, 11 | "scripts": { 12 | "start": "react-scripts start", 13 | "build": "react-scripts build", 14 | "test": "react-scripts test", 15 | "eject": "react-scripts eject" 16 | }, 17 | "eslintConfig": { 18 | "extends": "react-app" 19 | }, 20 | "browserslist": [ 21 | ">0.2%", 22 | "not dead", 23 | "not ie <= 11", 24 | "not op_mini all" 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codedojo/react-calendar/b6919663f5a5d9b37865eab216b5409fbed7353a/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 10 | 11 | React Calendar 12 | 13 | 14 |
15 | 16 | 17 | -------------------------------------------------------------------------------- /src/App.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | import Calendar from './components/Calendar'; 4 | 5 | export default class App extends React.Component { 6 | state = { 7 | date: null 8 | }; 9 | 10 | handleDateChange = date => this.setState({ date }); 11 | 12 | render() { 13 | const { date } = this.state; 14 | 15 | return ( 16 |
17 | {date &&

Выбранная дата: {date.toLocaleDateString()}

} 18 | 19 | 22 |
23 | ); 24 | } 25 | } -------------------------------------------------------------------------------- /src/components/Calendar/calendar.js: -------------------------------------------------------------------------------- 1 | const DAYS_IN_WEEK = 7; 2 | 3 | const DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 4 | 5 | const WEEK_DAYS_FROM_MONDAY = [6, 0, 1, 2, 3, 4, 5]; 6 | 7 | const Month = { 8 | January: 0, 9 | February: 1, 10 | March: 2, 11 | April: 3, 12 | May: 4, 13 | June: 5, 14 | July: 6, 15 | August: 7, 16 | September: 8, 17 | October: 9, 18 | Novermber: 10, 19 | December: 11 20 | }; 21 | 22 | export function areEqual(a, b) { 23 | if (!a || !b) return false; 24 | 25 | return ( 26 | a.getFullYear() === b.getFullYear() && 27 | a.getMonth() === b.getMonth() && 28 | a.getDate() === b.getDate() 29 | ); 30 | } 31 | 32 | export function isLeapYear(year) { 33 | return !((year % 4) || (!(year % 100) && (year % 400))); 34 | } 35 | 36 | export function getDaysInMonth(date) { 37 | const month = date.getMonth(); 38 | const year = date.getFullYear(); 39 | const daysInMonth = DAYS_IN_MONTH[month]; 40 | 41 | if (isLeapYear(year) && month === Month.February) { 42 | return daysInMonth + 1; 43 | } else { 44 | return daysInMonth; 45 | } 46 | } 47 | 48 | export function getDayOfWeek(date) { 49 | const dayOfWeek = date.getDay(); 50 | 51 | return WEEK_DAYS_FROM_MONDAY[dayOfWeek]; 52 | } 53 | 54 | export function getMonthData(year, month) { 55 | const result = []; 56 | const date = new Date(year, month); 57 | const daysInMonth = getDaysInMonth(date); 58 | const monthStartsOn = getDayOfWeek(date); 59 | let day = 1; 60 | 61 | for (let i = 0; i < (daysInMonth + monthStartsOn) / DAYS_IN_WEEK; i++) { 62 | result[i] = []; 63 | 64 | for (let j = 0; j < DAYS_IN_WEEK; j++) { 65 | if ((i === 0 && j < monthStartsOn) || day > daysInMonth) { 66 | result[i][j] = undefined; 67 | } else { 68 | result[i][j] = new Date(year, month, day++); 69 | } 70 | } 71 | } 72 | 73 | return result; 74 | } -------------------------------------------------------------------------------- /src/components/Calendar/index.css: -------------------------------------------------------------------------------- 1 | .calendar header { 2 | display: flex; 3 | justify-content: center; 4 | margin-bottom: 8px; 5 | } 6 | 7 | .calendar header button, 8 | .calendar header select { 9 | margin: 0 4px; 10 | } 11 | 12 | .calendar table { 13 | table-layout: fixed; 14 | border-collapse: separate; 15 | border-spacing: 0; 16 | margin: auto; 17 | } 18 | 19 | .calendar th { 20 | vertical-align: middle; 21 | text-align: center; 22 | height: 36px; 23 | } 24 | 25 | .calendar .day { 26 | padding: 4px 8px; 27 | vertical-align: middle; 28 | text-align: center; 29 | border: 2px solid transparent; 30 | } 31 | 32 | .calendar .day:hover { 33 | background-color: #eaeaea; 34 | cursor: pointer; 35 | } 36 | 37 | .calendar .day.selected { 38 | border: 2px solid dodgerblue; 39 | } 40 | 41 | .calendar .day.today { 42 | background-color: dodgerblue; 43 | color: white; 44 | } 45 | 46 | .calendar .day.today:hover { 47 | background-color: #0081ff; 48 | } 49 | 50 | .calendar .day.today.selected { 51 | border: 2px solid lightgray; 52 | } -------------------------------------------------------------------------------- /src/components/Calendar/index.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import classnames from 'classnames'; 3 | 4 | import * as calendar from './calendar'; 5 | 6 | import './index.css'; 7 | 8 | export default class Calendar extends React.Component { 9 | static defaultProps = { 10 | date: new Date(), 11 | years: [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020], 12 | monthNames: ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'], 13 | weekDayNames: ['Пн', 'Вт', 'Ср', 'Чт' , 'Пт', 'Сб', 'Вс'], 14 | onChange: Function.prototype 15 | }; 16 | 17 | state = { 18 | date: this.props.date, 19 | currentDate: new Date(), 20 | selectedDate: null 21 | }; 22 | 23 | get year() { 24 | return this.state.date.getFullYear(); 25 | } 26 | 27 | get month() { 28 | return this.state.date.getMonth(); 29 | } 30 | 31 | get day() { 32 | return this.state.date.getDate(); 33 | } 34 | 35 | handlePrevMonthButtonClick = () => { 36 | const date = new Date(this.year, this.month - 1); 37 | 38 | this.setState({ date }); 39 | }; 40 | 41 | handleNextMonthButtonClick = () => { 42 | const date = new Date(this.year, this.month + 1); 43 | 44 | this.setState({ date }); 45 | }; 46 | 47 | handleSelectChange = () => { 48 | const year = this.yearSelect.value; 49 | const month = this.monthSelect.value; 50 | 51 | const date = new Date(year, month); 52 | 53 | this.setState({ date }); 54 | }; 55 | 56 | handleDayClick = date => { 57 | this.setState({ selectedDate: date }); 58 | 59 | this.props.onChange(date); 60 | }; 61 | 62 | render() { 63 | const { years, monthNames, weekDayNames } = this.props; 64 | const { currentDate, selectedDate } = this.state; 65 | 66 | const monthData = calendar.getMonthData(this.year, this.month); 67 | 68 | return ( 69 |
70 |
71 | 72 | 73 | 82 | 83 | 92 | 93 | 94 |
95 | 96 | 97 | 98 | 99 | {weekDayNames.map(name => 100 | 101 | )} 102 | 103 | 104 | 105 | 106 | {monthData.map((week, index) => 107 | 108 | {week.map((date, index) => date ? 109 | 117 | : 118 | 121 | )} 122 | 123 |
{name}
this.handleDayClick(date)} 116 | >{date.getDate()} 119 | )} 120 |
124 |
125 | ); 126 | } 127 | } -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; 5 | -webkit-font-smoothing: antialiased; 6 | -moz-osx-font-smoothing: grayscale; 7 | width: 100vw; 8 | height: 100vh; 9 | display: flex; 10 | flex-direction: column; 11 | justify-content: center; 12 | align-items: center; 13 | } 14 | 15 | button, select { 16 | height: 2em; 17 | font-size: 1rem; 18 | background-color: #fff; 19 | color: #363636; 20 | border: 1px solid #dbdbdb; 21 | border-radius: 4px; 22 | cursor: pointer; 23 | } 24 | 25 | button:focus, select:focus { 26 | outline: none; 27 | } 28 | 29 | button { 30 | display: inline-flex; 31 | justify-content: center; 32 | align-items: center; 33 | padding: .25em .75em; 34 | text-align: center; 35 | } 36 | 37 | select { 38 | padding: .25em .5em; 39 | } -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | 4 | import App from './App'; 5 | 6 | import './index.css'; 7 | 8 | ReactDOM.render(, document.getElementById('root')); --------------------------------------------------------------------------------