I would like to ask or gather some important suggestions for this situation.
I wan’t to create my own Datetimepicker react component. I was searching some codes for easily constructing a calendar. Now I found some Regular Class which makes the calendar and I want to re-use it for creating my Datetimepicker component.
Now I would like to ask/open for suggestion for better or good practice that would be logical for my component.
In the code below, I have a functional component with a stateless class Day
inside of it and I tried to instantiate it for future use. For me it works fine. Since I don’t want to look my code very messy or make it fewer lines, Is there anyway to separate this Day
stateless class to import it in my Functional component? or any suggestions for this?. Or Can anyone explain to me if what am I doing is logically wrong or not, since I was using a Functional based component and putting a class inside of it. I would like to hear some good advice for this to implement in much better way.
import React, {useState} from "react";
import styles from "./Datetimepicker.module.scss";
import classNames from "classnames";
import CalendarSVG from "../../../styles/icons/Calendar/Calendar"
const Datetimepicker = (props) => {
const {
style,
name,
color,
size,
inputRef,
errorMsg,
helperMsg,
placeholder,
disabled,
...others
} = props;
const [addVisibility, setAddVisibility] = useState(false);
const showCalendar = () => {
setAddVisibility((prevState) => (!prevState));
}
const getWeekNumber = (date) => {
const firstDayOfTheYear = new Date(date.getFullYear(), 0, 1);
const pastDaysOfYear = (date - firstDayOfTheYear) / 86400000;
return Math.ceil((pastDaysOfYear + firstDayOfTheYear.getDay() + 1) / 7)
}
class Day {
constructor(date = null, lang = 'default') {
date = date ?? new Date();
this.Date = date;
this.date = date.getDate();
this.day = date.toLocaleString(lang, { weekday: 'long'});
this.dayNumber = date.getDay() + 1;
this.dayShort = date.toLocaleString(lang, { weekday: 'short'});
this.year = date.getFullYear();
this.yearShort = date.toLocaleString(lang, { year: '2-digit'});
this.month = date.toLocaleString(lang, { month: 'long'});
this.monthShort = date.toLocaleString(lang, { month: 'short'});
this.monthNumber = date.getMonth() + 1;
this.timestamp = date.getTime();
this.week = getWeekNumber(date);
}
get isToday() {
return this.isEqualTo(new Date());
}
isEqualTo(date) {
date = date instanceof Day ? date.Date : date;
return date.getDate() === this.date &&
date.getMonth() === this.monthNumber - 1 &&
date.getFullYear() === this.year;
}
format(formatStr) {
return formatStr
.replace(/bYYYYb/, this.year)
.replace(/bYYYb/, this.yearShort)
.replace(/bWWb/, this.week.toString().padStart(2, '0'))
.replace(/bWb/, this.week)
.replace(/bDDDDb/, this.day)
.replace(/bDDDb/, this.dayShort)
.replace(/bDDb/, this.date.toString().padStart(2, '0'))
.replace(/bDb/, this.date)
.replace(/bMMMMb/, this.month)
.replace(/bMMMb/, this.monthShort)
.replace(/bMMb/, this.monthNumber.toString().padStart(2, '0'))
.replace(/bMb/, this.monthNumber)
}
}
const day = new Day();
console.log('--day', day);
return (
<div className={styles.DatetimepickerWrapper}>
<input
className={classNames(
styles.InputText,
errorMsg && styles.InputError,
style ?? ""
)}
type="text"
name={name}
placeholder={placeholder ?? "mm/dd/yyyy"}
{...inputRef}
{...others}
disabled={disabled}
/>
<div className={addVisibility ? `${styles.CalendarVisible} ${styles.CalendarDropDown}` : `${styles.CalendarHidden}`}>
<div className={styles.CalendarContainer}>
<div className={styles.CalendarHeaderYear}>
<p>2022</p>
</div>
<div className={styles.CalendarHeaderMonth}>
<button type="button" className="prev-month" aria-label="previous month"></button>
<h4 tabIndex="0" aria-label="current month">
January
</h4>
<button type="button" className="prev-month" aria-label="next month"></button>
</div>
<div className={styles.CalendarDaysContainer}>
<p>Test</p>
<p>Test</p>
<p>Test</p>
<p>Test</p>
</div>
</div>
</div>
<CalendarSVG width={23} fill="#294197" className={styles.CalendarDateTimePickerIcon} onClick={() => showCalendar()}/>
{errorMsg && <span className={styles.ErrorMessage}>{errorMsg}</span>}
{!errorMsg && helperMsg && (
<span className={styles.HelperMessage}>{helperMsg}</span>
)}
</div>
);
};
export default Datetimepicker;