Skip to content

Commit

Permalink
chore: using memo to optimize performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhood Shapouran committed Apr 26, 2023
1 parent 21b738f commit f03750d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-ui-datepicker",
"version": "1.0.1",
"version": "1.0.2",
"description": "Customizable datetime picker for React Native",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
13 changes: 9 additions & 4 deletions src/components/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ import DaySelector from './DaySelector';
import TimeSelector from './TimeSelector';

const Calendar = () => {
const { calendarView, mode } = useCalendarContext();
const { utils, currentDate, selectedDate, calendarView, mode } =
useCalendarContext();
const days = utils.getMonthDays(currentDate);
const currentMonth = utils.getDateMonth(currentDate);
const currentYear = utils.getDateYear(currentDate);
const selectedYear = utils.getDateYear(selectedDate);

return (
<View style={styles.container}>
{mode !== 'time' ? <Header /> : null}
<View style={styles.calendarContainer}>
{calendarView === CalendarViews.year ? (
<YearSelector />
<YearSelector currentYear={currentYear} selectedYear={selectedYear} />
) : calendarView === CalendarViews.month ? (
<MonthSelector />
<MonthSelector month={currentMonth} />
) : calendarView === CalendarViews.day ? (
<DaySelector />
<DaySelector days={days} />
) : (
<TimeSelector />
)}
Expand Down
8 changes: 4 additions & 4 deletions src/components/DaySelector.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import React, { memo } from 'react';
import { Text, View, TouchableOpacity, StyleSheet } from 'react-native';
import { useCalendarContext } from '../CalendarContext';
import type { IDayObject } from '../utils';

const DaySelector = () => {
const DaySelector = ({ days }: { days: IDayObject[] }) => {
const { utils, currentDate, selectedDate, onSelectDate, theme } =
useCalendarContext();
const days = utils.getMonthDays(currentDate);

const handleSelectDate = (date: string) => {
const newDate = utils
Expand Down Expand Up @@ -145,4 +145,4 @@ const styles = StyleSheet.create({
},
});

export default DaySelector;
export default memo(DaySelector);
13 changes: 6 additions & 7 deletions src/components/MonthSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import React from 'react';
import React, { memo } from 'react';
import { Text, View, TouchableOpacity, StyleSheet } from 'react-native';
import { useCalendarContext } from '../CalendarContext';

const MonthSelector = () => {
const { utils, currentDate, onSelectMonth, theme } = useCalendarContext();
const currentMonth = utils.getDateMonth(currentDate);
const MonthSelector = ({ month }: { month: number }) => {
const { utils, onSelectMonth, theme } = useCalendarContext();

return (
<View style={styles.container}>
<View style={styles.monthsContainer}>
{utils.getMonths().map((item, index) => {
const activeItemStyle =
index === currentMonth
index === month
? {
borderColor: theme?.selectedItemColor || '#0047FF',
backgroundColor: theme?.selectedItemColor || '#0047FF',
}
: null;

const textStyle =
index === currentMonth
index === month
? { color: '#fff', ...theme?.selectedTextStyle }
: theme?.calendarTextStyle;

Expand Down Expand Up @@ -73,4 +72,4 @@ const styles = StyleSheet.create({
},
});

export default MonthSelector;
export default memo(MonthSelector);
17 changes: 10 additions & 7 deletions src/components/YearSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import React from 'react';
import React, { memo } from 'react';
import { Text, View, TouchableOpacity, StyleSheet } from 'react-native';
import { useCalendarContext } from '../CalendarContext';

const YearSelector = () => {
const { utils, currentDate, selectedDate, onSelectYear, theme } =
useCalendarContext();
type Props = {
currentYear: number;
selectedYear: number;
};

const YearSelector = ({ currentYear, selectedYear }: Props) => {
const { onSelectYear, theme } = useCalendarContext();
const rowArray = [1, 2, 3];
const colArray = [1, 2, 3, 4];
const currentYear = utils.getDateYear(currentDate);
const selectedYear = utils.getDateYear(selectedDate);

let year = 12 * Math.ceil(currentYear / 12) - 12;
if (year < 0) year = 0;

Expand Down Expand Up @@ -82,4 +85,4 @@ const styles = StyleSheet.create({
},
});

export default YearSelector;
export default memo(YearSelector);
8 changes: 7 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ConfigTypes = {
timeFormat: string;
};

interface IDayObject {
export interface IDayObject {
text: string;
day: number;
date: string;
Expand Down Expand Up @@ -102,6 +102,12 @@ export default class utils {

getDate = (date: DateType) => dayjs(date, this.config.calendarFormat);

/**
* Calculate month days array based on current date
*
* @param {DateType} datetime The current date that selected
* @returns {IDayObject[]} days array based on current date
*/
getMonthDays = (datetime: DateType): IDayObject[] => {
let date = this.getDate(datetime);
const currentMonthDays = date.daysInMonth();
Expand Down

0 comments on commit f03750d

Please sign in to comment.