Skip to content

Commit

Permalink
Merge pull request #37 from farhoudshapouran/refactor/utils
Browse files Browse the repository at this point in the history
Refactor/utils
  • Loading branch information
farhoudshapouran authored Nov 10, 2023
2 parents 1359007 + b0c562c commit ef74482
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 63 deletions.
4 changes: 4 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default function App() {
},
]}
onPress={() => setTheme(item)}
accessibilityRole="button"
/>
))}
</View>
Expand All @@ -71,6 +72,7 @@ export default function App() {
},
]}
onPress={() => setLocale(item)}
accessibilityRole="button"
>
<Text
style={[
Expand Down Expand Up @@ -117,6 +119,7 @@ export default function App() {
onPress={() => {
setValue(dayjs());
}}
accessibilityRole="button"
>
<View
style={[
Expand Down Expand Up @@ -145,6 +148,7 @@ export default function App() {
'https://github.com/farhoudshapouran/react-native-ui-datepicker'
)
}
accessibilityRole="button"
>
<Image
source={require('../assets/github-logo.png')}
Expand Down
6 changes: 4 additions & 2 deletions src/components/DaySelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getWeekdaysMin,
getToday,
getFormatedDate,
dateFormat,
} from '../utils';

const DaySelector = () => {
Expand Down Expand Up @@ -71,15 +72,15 @@ const DaySelector = () => {
: null;

const activeItemStyle =
day && day.date === getFormatedDate(selectedDate, 'YYYY/MM/DD')
day && day.date === getFormatedDate(selectedDate, dateFormat)
? {
borderColor: theme?.selectedItemColor || '#0047FF',
backgroundColor: theme?.selectedItemColor || '#0047FF',
}
: null;

const textStyle =
day && day.date === getFormatedDate(selectedDate, 'YYYY/MM/DD')
day && day.date === getFormatedDate(selectedDate, dateFormat)
? { color: '#fff', ...theme?.selectedTextStyle }
: day && day.date === getToday()
? {
Expand All @@ -103,6 +104,7 @@ const DaySelector = () => {
day.disabled && styles.disabledDay,
]}
testID={day.date}
accessibilityRole="button"
>
<View style={styles.dayTextContainer}>
<Text style={textStyle}>{day.text}</Text>
Expand Down
7 changes: 6 additions & 1 deletion src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const Header = ({ buttonPrevIcon, buttonNextIcon }: HeaderProps) => {
: calendarView === CalendarViews.year && onChangeYear(-12)
}
testID="btn-prev"
accessibilityRole="button"
>
<View
style={[styles.iconContainer, styles.prev, theme?.headerButtonStyle]}
Expand Down Expand Up @@ -61,6 +62,7 @@ const Header = ({ buttonPrevIcon, buttonNextIcon }: HeaderProps) => {
: calendarView === CalendarViews.year && onChangeYear(12)
}
testID="btn-next"
accessibilityRole="button"
>
<View
style={[styles.iconContainer, styles.next, theme?.headerButtonStyle]}
Expand Down Expand Up @@ -91,6 +93,7 @@ const Header = ({ buttonPrevIcon, buttonNextIcon }: HeaderProps) => {
)
}
testID="btn-month"
accessibilityRole="button"
>
<View style={[styles.textContainer, theme?.headerTextContainerStyle]}>
<Text style={[styles.text, theme?.headerTextStyle]}>
Expand All @@ -108,6 +111,7 @@ const Header = ({ buttonPrevIcon, buttonNextIcon }: HeaderProps) => {
)
}
testID="btn-year"
accessibilityRole="button"
>
<View style={[styles.textContainer, theme?.headerTextContainerStyle]}>
<Text style={[styles.text, theme?.headerTextStyle]}>
Expand All @@ -127,6 +131,7 @@ const Header = ({ buttonPrevIcon, buttonNextIcon }: HeaderProps) => {
: CalendarViews.time
)
}
accessibilityRole="button"
>
<View style={[styles.textContainer, theme?.headerTextContainerStyle]}>
<Text style={[styles.text, theme?.headerTextStyle]}>
Expand All @@ -141,7 +146,7 @@ const Header = ({ buttonPrevIcon, buttonNextIcon }: HeaderProps) => {
return (
<View
style={[styles.headerContainer, theme?.headerContainerStyle]}
testID="header"
accessibilityRole="header"
>
{theme?.headerButtonsPosition === 'left' ? (
<View style={styles.container}>
Expand Down
1 change: 1 addition & 0 deletions src/components/MonthSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const MonthSelector = () => {
key={index}
style={styles.monthCell}
onPress={() => onSelectMonth(index)}
accessibilityRole="button"
>
<View
style={[
Expand Down
1 change: 1 addition & 0 deletions src/components/YearSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const YearSelector = () => {
key={cellYear}
onPress={() => onSelectYear(cellYear)}
style={styles.yearCell}
accessibilityRole="button"
>
<View
style={[styles.year, theme?.yearContainerStyle, activeItemStyle]}
Expand Down
112 changes: 52 additions & 60 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export interface IDayObject {
isCurrentMonth: boolean;
}

const calendarFormat = 'YYYY/MM/DD HH:mm';
const dateFormat = 'YYYY/MM/DD';
export const calendarFormat = 'YYYY-MM-DD HH:mm';
export const dateFormat = 'YYYY-MM-DD';

export const getMonths = () => dayjs.months();

Expand Down Expand Up @@ -68,78 +68,70 @@ export const getMonthDays = (
maximumDate: DateType
): IDayObject[] => {
const date = getDate(datetime);
const currentMonthDays = date.daysInMonth();
const daysInMonth = date.daysInMonth();
const prevMonthDays = date.add(-1, 'month').daysInMonth();
const firstDay = date.date(1);
const dayOfMonth = firstDay.day() % 7;

const prevDaysArray = Array.from(
{ length: dayOfMonth },
(_, i) => i + (prevMonthDays - dayOfMonth + 1)
);
const monthDaysOffset = dayOfMonth + currentMonthDays;
const prevDays = displayFullDays
? Array.from({ length: dayOfMonth }, (_, i) => {
const day = i + (prevMonthDays - dayOfMonth + 1);
const thisDay = date.add(-1, 'month').date(day);
return generateDayObject(day, thisDay, minimumDate, maximumDate, false);
})
: Array(dayOfMonth).fill(null);

const monthDaysOffset = dayOfMonth + daysInMonth;
const nextMonthDays = displayFullDays
? monthDaysOffset > 35
? 42 - monthDaysOffset
: 35 - monthDaysOffset
: 0;

const prevDays =
displayFullDays && prevDaysArray && prevDaysArray.length > 0
? prevDaysArray?.map((day) => {
const thisDay = date.add(-1, 'month').date(day);
let disabled = false;
if (minimumDate) {
disabled = thisDay < getDate(minimumDate);
}
if (maximumDate && !disabled) {
disabled = thisDay > getDate(maximumDate);
}
return {
text: day.toString(),
day,
date: getFormatedDate(thisDay, 'YYYY/MM/DD'),
disabled,
isCurrentMonth: false,
};
})
: new Array(dayOfMonth);

const currentDays = Array.from({ length: currentMonthDays }, (_, i) => {
const thisDay = date.date(i + 1);
let disabled = false;
if (minimumDate) {
disabled = thisDay < getDate(minimumDate);
}
if (maximumDate && !disabled) {
disabled = thisDay > getDate(maximumDate);
}
return {
text: (i + 1).toString(),
day: i + 1,
date: getFormatedDate(thisDay, 'YYYY/MM/DD'),
disabled,
isCurrentMonth: true,
};
const currentDays = Array.from({ length: daysInMonth }, (_, i) => {
const day = i + 1;
const thisDay = date.date(day);
return generateDayObject(day, thisDay, minimumDate, maximumDate, true);
});

const nextDays = Array.from({ length: nextMonthDays }, (_, i) => {
const thisDay = date.add(1, 'month').date(i + 1);
let disabled = false;
if (minimumDate) {
disabled = thisDay < getDate(minimumDate);
}
if (maximumDate && !disabled) {
disabled = thisDay > getDate(maximumDate);
}
return {
text: (i + 1).toString(),
day: i + 1,
date: getFormatedDate(thisDay, 'YYYY/MM/DD'),
disabled,
isCurrentMonth: false,
};
const day = i + 1;
const thisDay = date.add(1, 'month').date(day);
return generateDayObject(day, thisDay, minimumDate, maximumDate, false);
});

return [...prevDays, ...currentDays, ...nextDays];
};

/**
* Generate day object for displaying inside day cell
*
* @param {number} day - number of day
* @param {dayjs.Dayjs} date - calculated date based on day, month, and year
* @param {DateType} minDate - min selectable date
* @param {DateType} maxDate - max selectable date
* @param {boolean} isCurrentMonth - define the day is in the current month
* @returns {IDayObject} days object based on current date
*/
const generateDayObject = (
day: number,
date: dayjs.Dayjs,
minDate: DateType,
maxDate: DateType,
isCurrentMonth: boolean
) => {
let disabled = false;
if (minDate) {
disabled = date < getDate(minDate);
}
if (maxDate && !disabled) {
disabled = date > getDate(maxDate);
}
return {
text: day.toString(),
day: day,
date: getFormatedDate(date, dateFormat),
disabled,
isCurrentMonth,
};
};

0 comments on commit ef74482

Please sign in to comment.