create clendar component with constructor in pure js in javascript

Here is an example of creating a basic calendar component using constructor function in pure javascript:

index.tsx
function Calendar(container) {
  var date = new Date();
  var year = date.getFullYear();
  var month = date.getMonth();
  var months = [
    "January", "February", "March", "April", "May", "June", "July",
    "August", "September", "October", "November", "December"
  ];

  // create calendar
  var calendar = document.createElement("div");
  calendar.classList.add("calendar");

  // create header
  var header = document.createElement("div");
  header.classList.add("calendar-header");

  var prevBtn = document.createElement("button");
  prevBtn.innerHTML = "<";
  prevBtn.addEventListener("click", function() {
    month--;
    updateCalendar();
  });

  var nextBtn = document.createElement("button");
  nextBtn.innerHTML = ">";
  nextBtn.addEventListener("click", function() {
    month++;
    updateCalendar();
  });

  var title = document.createElement("span");
  title.classList.add("calendar-title");
  title.innerHTML = months[month] + " " + year;

  header.appendChild(prevBtn);
  header.appendChild(title);
  header.appendChild(nextBtn);

  calendar.appendChild(header);

  // create body
  var body = document.createElement("div");
  body.classList.add("calendar-body");

  function updateCalendar() {
    title.innerHTML = months[month] + " " + year;

    // clear previous cells
    while (body.hasChildNodes()) {
      body.removeChild(body.lastChild);
    }

    // get number of days in month
    var daysInMonth = new Date(year, month + 1, 0).getDate();

    // get starting day of the week
    var startDay = new Date(year, month, 1).getDay();

    // create cells
    for (var i = 0; i < startDay; i++) {
      var cell = document.createElement("span");
      cell.classList.add("calendar-cell");
      body.appendChild(cell);
    }

    for (var i = 1; i <= daysInMonth; i++) {
      var cell = document.createElement("span");
      cell.classList.add("calendar-cell");
      cell.innerHTML = i;
      body.appendChild(cell);
    }
  }

  updateCalendar();

  calendar.appendChild(body);

  // add calendar to container
  container.appendChild(calendar);
}

// usage
var container = document.getElementById("calendar-container");
var calendar = new Calendar(container);
2200 chars
86 lines

In the example above, the Calendar constructor function takes a DOM container element as an argument and generates a calendar component inside it. The generated calendar shows the current month and year by default, but the user can navigate through different months using the "prev" and "next" buttons.

The updateCalendar function is responsible for dynamically generating the calendar cells based on the current month and year. It calculates the number of days in the month and the starting day of the week, and creates cells accordingly.

Note that this is a basic example of a calendar component and can be customized further to fit your specific needs.

gistlibby LogSnag