Calendar.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React, {Component} from 'react';
  2. import moment from "moment";
  3. export default class Calendar extends Component {
  4. state={
  5. current:moment(),
  6. };
  7. componentDidMount() {
  8. moment.locale('ru', {
  9. week : {
  10. dow:1
  11. }
  12. });
  13. }
  14. render() {
  15. const {doctor,setAppointmentShedule} = this.props
  16. const {current} = this.state
  17. const daysArray = []
  18. for (let x=1; x <= current.daysInMonth();x++){
  19. daysArray.push(current.date(x).format('YYYY-MM-DD'))
  20. }
  21. const prevMonth = moment(current).subtract(1,'months')
  22. const startDay = current.startOf('month').day() === 0 ? 7 : current.startOf('month').day()
  23. for (let x=1; x < startDay ;x++){
  24. daysArray.unshift(prevMonth.endOf('month').subtract(x-1,'days').format('YYYY-MM-DD'))
  25. }
  26. return (
  27. <div style={{display:'flex',flexDirection:'column',margin:"0px 20px"}}>
  28. <div style={{display:'flex',margin:'20px'}}>
  29. <button onClick={() => this.setState({current:current.subtract(1,"month")})}>{'<'}</button>
  30. <h4 style={{margin:'0 20px'}}>{current.format('MMMM-YYYY')}</h4>
  31. <button onClick={() => this.setState({current:current.add(1,"month")})}>{'>'}</button>
  32. </div>
  33. <div style={{display:'flex',}}>
  34. {moment.weekdaysShort(true).map(el => (
  35. <p key={el} style={{margin:'0 6px'}}>{el}</p>
  36. ))}
  37. </div>
  38. <div style={{display:'flex',flexWrap:'wrap',maxWidth:'350px',margin:'5px'}}>
  39. {daysArray.map(el => (
  40. <button
  41. key={el}
  42. id={el}
  43. disabled={
  44. moment(el).format('YYYY-MM-DD') < moment().format('YYYY-MM-DD')
  45. || (moment(el).day()===6)
  46. || (moment(el).day()===0)
  47. || moment(el).month() !== current.month()
  48. || !doctor.shedule.find(item => item.data === el)
  49. }
  50. style={{
  51. height:'50px',
  52. width:'50px',
  53. backgroundColor:moment(el).month() === current.month() ? doctor.shedule.find(item => item.data === el) ? "lightgreen" : "coral" : "lightgrey",
  54. border:moment().format('YYYY-MM-DD') === moment(el).format('YYYY-MM-DD') ? "3px solid black" : null
  55. }}
  56. onClick={(e) => setAppointmentShedule(e.target.id)}
  57. >
  58. {moment(el).format('DD')}
  59. </button>
  60. ))}
  61. </div>
  62. </div>
  63. );
  64. }
  65. }