Understanding and Implementing Schemas in Python

Understanding and Implementing Schemas in Python Introduction In the world of programming, particularly in the context of data management and validation, schemas play a vital role. A schema is essentially a blueprint or a predefined structure that defines the expected format, data types, and constraints for a given data entity. In this blog, we will delve into the concept of schemas in Python, exploring what they are, why they are important, and how you can implement them in your projects. What is a Schema? A schema serves as a contract between different components of a system, ensuring that data is consistent, valid, and well-structured. It defines the rules for how data should be organized, what fields it should contain, and what types of values those fields can hold. In essence, a schema acts as a set of rules that data must adhere to in order to be considered valid. Why Are Schemas Important? Data Validation: Schemas provide a way to validate incoming data. When data is received o

Create a custom calendar in React Js | Interview Question

Create a custom calendar in React Js ( React Hooks ) | Interview Question

In this post, you will learn about "how to create a custom calendar with the help of React Js".

This is very simple calendar component in which you will be able to select a date from active month. Once you understand this component, you can add more functionalities to this as per your need.

I am assuming that you have basic understanding of React, html & css.

calendar
Calendar in React Js


Sample Code:

A) App.js

import React, { useStateuseEffect } from 'react';
import './style.css';

export default function App() {
  const [daySelsetDaySel] = useState(new Date());
  const [currentMonthsetCurrentMonth] = useState([]);
  const [monthsetMonth] = useState(new Date().getMonth());
  const [yearsetYear] = useState(new Date().getFullYear());

  const days = [
    { id: 0day: 'SUN' },
    { id: 1day: 'MON' },
    { id: 2day: 'TUE' },
    { id: 3day: 'WED' },
    { id: 4day: 'THR' },
    { id: 5day: 'FRI' },
    { id: 6day: 'SAT' },
  ];

  const buildCalenderForMonth = (yearmonth=> {
    let firstDate = new Date(yearmonth1);
    let firstDay = firstDate.getDay();

    let totalNoOfDays = new Date(yearmonth + 10).getDate();

    let lastDate = new Date(yearmonthtotalNoOfDays);
    let lastDay = lastDate.getDay();

    let totalNoOfDaysPreviousMonth = new Date(yearmonth0).getDate();

    let calenderMonthArr = [];

    // previous months date
    for (let i = 0i <= firstDay - 1i++) {
      let calMonth = {
        date: totalNoOfDaysPreviousMonth - (firstDay - 1) + i,
        isActive: false,
      };
      calenderMonthArr.push(calMonth);
    }
    // current months date
    for (let i = 1i <= totalNoOfDaysi++) {
      let calMonth = {
        date: i,
        isActive: true,
      };
      calenderMonthArr.push(calMonth);
    }
    // next months date
    let dy = 1;
    for (let i = lastDayi < days.length - 1i++) {
      let calMonth = {
        date: dy,
        isActive: false,
      };
      calenderMonthArr.push(calMonth);
      dy++;
    }

    setCurrentMonth(calenderMonthArr);
  };

  useEffect(() => {
    buildCalenderForMonth(yearmonth);
  }, []);

  const dateClick = (day=> {
    let firstDate = new Date(yearmonthday);
    setDaySel(firstDate);
  };

  const onPrevMonth = () => {
    let mnth = month - 1;
    buildCalenderForMonth(yearmonth - 1);
    setMonth(mnth);
  };

  const onNextMonth = () => {
    let mnth = month + 1;
    buildCalenderForMonth(yearmnth);
    setMonth(mnth);
  };

  return (
    <div>
      <input id="input" type="text" value={daySel} /> <br />
      <br />
      <div id="div1">
        <div className="days">
          {days &&
            days.length > 0 &&
            days.map((item=> {
              return <div key={item.id}>{item.day} </div>;
            })}
        </div>
        <div className="showCal">
          {currentMonth &&
            currentMonth.length > 0 &&
            currentMonth.map((item=> {
              return (
                <div
                  onClick={
                    item.isActive ? () => dateClick(item.date) : () => {}
                  }
                  className={`${item.isActive ? 'active' : 'inactive'}`}
                  key={item.id}
                >
                  {item.date}{' '}
                </div>
              );
            })}
        </div>
        <button id="prev" onClick={onPrevMonth}>
          Prev
        </button>
        <button id="next" onClick={onNextMonth}>
          Next
        </button>
      </div>
    </div>
  );
}


CSS:

B) style.css

.days div {
  displayinline;
  border1px solid black;
  floatleft;
  margin-left1px;
  margin-right1px;
  margin-top3px;
  width60px;
  text-aligncenter;
  background-colorcyan;
}

.showCal .active {
  displayinline;
  margin1px;
  width60px;
  floatleft;
  border1px solid black;
  background-colorbisque;
  text-aligncenter;
}

.inactive {
  margin2px;
  width60px;
  floatleft;
  background-colorgray;
  text-aligncenter;
}

#div1 {
  border1px solid black;
  width450px;
  height180px;
}

#input {
  width440px;
}
#prev {
  margin-top20px;
  margin-left1px;
}

#next {
  margin-top20px;
  margin-left1px;
}


Happy Learning!! Happy Coding!!

Comments

Popular posts from this blog

useNavigate and useLocation hooks react-router-dom-v6

How to implement error boundaries in React Js

Pass data from child component to its parent component in React Js

Create a Shopping Item App using React Js and Xstate

Localization in React Js

How to fetch data from an API using fetch() method in React Js

How to fetch data using Axios Http Get Request in React Js?

Routing in React using React-Router Version 6

Environment Setup and Installation for React Js Application