Skip to content

Commit

Permalink
Merge pull request #33 from farhoudshapouran/change-utils-usage
Browse files Browse the repository at this point in the history
Change utils usage
  • Loading branch information
farhoudshapouran authored Nov 1, 2023
2 parents caefa22 + 077ac26 commit 9185a4f
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 132 deletions.
1 change: 1 addition & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default function App() {
fontWeight: 'bold',
color: theme?.activeTextColor,
}}
// eslint-disable-next-line react-native/no-inline-styles
todayContainerStyle={{
borderWidth: 1,
}}
Expand Down
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.7",
"version": "1.0.8",
"description": "Customizable datetime picker for React Native",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
17 changes: 8 additions & 9 deletions src/CalendarContext.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { createContext, useContext } from 'react';
import calendarUtils from './utils';
import { CalendarViews } from './enums';
import type { DateType, CalendarTheme, CalendarModes } from './types';

export interface CalendarContextType {
utils: calendarUtils;
calendarView: CalendarViews;
selectedDate: DateType;
currentDate: DateType;
mode: CalendarModes;
locale: string | ILocale;
displayFullDays: boolean;
minimumDate: DateType;
maximumDate: DateType;
theme?: CalendarTheme;
setCalendarView: (value: CalendarViews) => void;
onSelectDate: (date: DateType) => void;
Expand All @@ -19,17 +21,14 @@ export interface CalendarContextType {
}

const CalendarContext = createContext<CalendarContextType>({
utils: new calendarUtils({
mode: 'datetime',
locale: 'en',
minimumDate: null,
maximumDate: null,
displayFullDays: false,
}),
calendarView: CalendarViews.day,
selectedDate: Date.now(),
currentDate: Date.now(),
mode: 'datetime',
locale: 'en',
minimumDate: null,
maximumDate: null,
displayFullDays: false,
setCalendarView: () => {},
onSelectDate: () => {},
onSelectMonth: () => {},
Expand Down
42 changes: 30 additions & 12 deletions src/DateTimePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useReducer } from 'react';
import calendarUtils from './utils';
import utils from './utils';
import CalendarContext from './CalendarContext';
import { CalendarViews, CalendarActionKind } from './enums';
import type {
Expand All @@ -11,6 +11,12 @@ import type {
HeaderProps,
} from './types';
import Calendar from './components/Calendar';
import dayjs from 'dayjs';
import localeData from 'dayjs/plugin/localeData';
import relativeTime from 'dayjs/plugin/relativeTime';

dayjs.extend(localeData);
dayjs.extend(relativeTime);

interface PropTypes extends CalendarTheme, HeaderProps {
value: DateType;
Expand Down Expand Up @@ -52,13 +58,8 @@ const DateTimePicker = ({
buttonPrevIcon,
buttonNextIcon,
}: PropTypes) => {
const utils = new calendarUtils({
mode,
locale,
minimumDate,
maximumDate,
displayFullDays,
});
dayjs.locale(locale);

const theme = {
headerButtonsPosition,
headerContainerStyle,
Expand Down Expand Up @@ -103,9 +104,8 @@ const DateTimePicker = ({
},
{
calendarView: mode === 'time' ? CalendarViews.time : CalendarViews.day,
selectedDate: value ? utils.getFormatedValue(value) : utils.getNow(),
currentDate: value ? utils.getFormatedValue(value) : utils.getNow(),
mode: mode,
selectedDate: value ? utils.getFormated(value) : utils.getNow(),
currentDate: value ? utils.getFormated(value) : utils.getNow(),
}
);

Expand All @@ -120,6 +120,13 @@ const DateTimePicker = ({
});
}, [value]);

useEffect(() => {
dispatch({
type: CalendarActionKind.SET_CALENDAR_VIEW,
payload: mode === 'time' ? CalendarViews.time : CalendarViews.day,
});
}, [mode]);

const actions = {
setCalendarView: (view: CalendarViews) =>
dispatch({ type: CalendarActionKind.SET_CALENDAR_VIEW, payload: view }),
Expand Down Expand Up @@ -173,7 +180,18 @@ const DateTimePicker = ({
};

return (
<CalendarContext.Provider value={{ ...state, ...actions, utils, theme }}>
<CalendarContext.Provider
value={{
...state,
...actions,
locale,
mode,
displayFullDays,
minimumDate,
maximumDate,
theme,
}}
>
<Calendar
buttonPrevIcon={buttonPrevIcon}
buttonNextIcon={buttonNextIcon}
Expand Down
21 changes: 17 additions & 4 deletions src/components/DaySelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@ import React, { memo, useMemo } from 'react';
import { Text, View, TouchableOpacity, StyleSheet } from 'react-native';
import { useCalendarContext } from '../CalendarContext';
import { CALENDAR_HEIGHT } from '../enums';
import utils from '../utils';

const DaySelector = () => {
const { utils, currentDate, selectedDate, onSelectDate, theme } =
useCalendarContext();
const {
currentDate,
selectedDate,
onSelectDate,
displayFullDays,
minimumDate,
maximumDate,
theme,
} = useCalendarContext();
const month = utils.getDateMonth(currentDate);
const year = utils.getDateYear(currentDate);
const days = useMemo(
() => {
return utils.getMonthDays(currentDate);
return utils.getMonthDays(
currentDate,
displayFullDays,
minimumDate,
maximumDate
);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[month, year, utils.displayFullDays, utils.minimumDate, utils.maximumDate]
[month, year, displayFullDays, minimumDate, maximumDate]
);

const handleSelectDate = (date: string) => {
Expand Down
3 changes: 2 additions & 1 deletion src/components/MonthSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { memo } from 'react';
import { Text, View, TouchableOpacity, StyleSheet } from 'react-native';
import { useCalendarContext } from '../CalendarContext';
import utils from '../utils';

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

return (
Expand Down
5 changes: 3 additions & 2 deletions src/components/TimeSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { Text, View, StyleSheet } from 'react-native';
import { useCalendarContext } from '../CalendarContext';
import Wheel from './TimePicker/Wheel';
import { CALENDAR_HEIGHT } from '../enums';
import utils from '../utils';

function createNumberList(num: number) {
return new Array(num).fill(0).map((_, index) => index);
}

const TimeSelector = () => {
const { utils, selectedDate, currentDate, onSelectDate, theme } =
const { selectedDate, currentDate, onSelectDate, theme } =
useCalendarContext();
const [time, setTime] = useState({
minute: utils.getDateMinute(selectedDate),
Expand All @@ -21,7 +22,7 @@ const TimeSelector = () => {
minute: utils.getDateMinute(selectedDate),
hour: utils.getDateHour(selectedDate),
});
}, [selectedDate, utils]);
}, [selectedDate]);

return (
<View style={styles.container}>
Expand Down
3 changes: 2 additions & 1 deletion src/components/YearSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { memo } from 'react';
import { Text, View, TouchableOpacity, StyleSheet } from 'react-native';
import { useCalendarContext } from '../CalendarContext';
import utils from '../utils';

const YearSelector = () => {
const { utils, currentDate, selectedDate, onSelectYear, theme } =
const { currentDate, selectedDate, onSelectYear, theme } =
useCalendarContext();
const currentYear = utils.getDateYear(currentDate);
const selectedYear = utils.getDateYear(selectedDate);
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export type CalendarState = {
calendarView: CalendarViews;
selectedDate: DateType;
currentDate: DateType;
mode: CalendarModes;
};

export type CalendarAction = {
Expand Down
Loading

0 comments on commit 9185a4f

Please sign in to comment.