Skip to content

Commit

Permalink
Merge pull request #45 from farhoudshapouran/support-starting-day-of-…
Browse files Browse the repository at this point in the history
…week

Support starting day of week
  • Loading branch information
farhoudshapouran authored Dec 11, 2023
2 parents 701851b + ba44f76 commit f3617e6
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ For more, take a look at the `/example` directory.
| locale | `string` | `'en'` | Defines the DatePicker locale |
| minimumDate | `DateType` | `null` | Defines DatePicker minimum selectable date |
| maximumDate | `DateType` | `null` | Defines DatePicker maximum selectable date |
| firstDayOfWeek | `number` | `0` | Defines the starting day of week, number 0-6, 0 - Sunday, 6 - Saturday |
| displayFullDays | `boolean` | `false` | Defines show previous and next month's days in the current calendar view |
| calendarTextStyle | `TextStyle` | `null` | Defines all text styles inside the calendar (Days, Months, Years, Hours, and Minutes) |
| selectedTextStyle | `TextStyle` | `null` | Defines selected (Day, Month, Year) text styles |
Expand Down
1 change: 1 addition & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export default function App() {
value={value}
//minimumDate={dayjs().startOf('day')}
//maximumDate={dayjs().add(3, 'day').endOf('day')}
//firstDayOfWeek={1}
displayFullDays={true}
locale={locale}
onValueChange={(date) => setValue(date)}
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.10",
"version": "1.0.11",
"description": "Customizable datetime picker for React Native",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
1 change: 1 addition & 0 deletions src/CalendarContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface CalendarContextType extends CalendarState {
displayFullDays: boolean;
minimumDate: DateType;
maximumDate: DateType;
firstDayOfWeek: number;
theme?: CalendarTheme;
setCalendarView: (value: CalendarViews) => void;
onSelectDate: (date: DateType) => void;
Expand Down
4 changes: 4 additions & 0 deletions src/DateTimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface PropTypes extends CalendarTheme, HeaderProps {
locale?: string | ILocale;
minimumDate?: DateType;
maximumDate?: DateType;
firstDayOfWeek?: number;
onValueChange?: (value: DateType) => void;
displayFullDays?: boolean;
}
Expand All @@ -36,6 +37,7 @@ const DateTimePicker = ({
locale = 'en',
minimumDate = null,
maximumDate = null,
firstDayOfWeek = 0,
onValueChange = () => {},
displayFullDays = false,
headerButtonsPosition = 'around',
Expand Down Expand Up @@ -200,6 +202,8 @@ const DateTimePicker = ({
displayFullDays,
minimumDate,
maximumDate,
firstDayOfWeek:
firstDayOfWeek >= 0 && firstDayOfWeek <= 6 ? firstDayOfWeek : 0,
theme,
}}
>
Expand Down
6 changes: 4 additions & 2 deletions src/components/DaySelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const DaySelector = () => {
displayFullDays,
minimumDate,
maximumDate,
firstDayOfWeek,
theme,
} = useCalendarContext();
const { year, month, hour, minute } = getParsedDate(currentDate);
Expand All @@ -30,7 +31,8 @@ const DaySelector = () => {
currentDate,
displayFullDays,
minimumDate,
maximumDate
maximumDate,
firstDayOfWeek
).map((day) => {
return day
? {
Expand Down Expand Up @@ -60,7 +62,7 @@ const DaySelector = () => {
style={[styles.weekDaysContainer, theme?.weekDaysContainerStyle]}
testID="week-days"
>
{getWeekdaysMin()?.map((item, index) => (
{getWeekdaysMin(firstDayOfWeek)?.map((item, index) => (
<View key={index} style={styles.weekDayCell}>
<Text style={theme?.weekDaysTextStyle}>{item}</Text>
</View>
Expand Down
17 changes: 14 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ export const getWeekdays = () => dayjs.weekdays();

export const getWeekdaysShort = () => dayjs.weekdaysShort();

export const getWeekdaysMin = () => dayjs.weekdaysMin();
export const getWeekdaysMin = (firstDayOfWeek: number) => {
let days = dayjs.weekdaysMin();
if (firstDayOfWeek > 0) {
days = [
...days.slice(firstDayOfWeek, days.length),
...days.slice(0, firstDayOfWeek),
] as dayjs.WeekdayNames;
}
return days;
};

export const getFormated = (date: DateType) =>
dayjs(date).format(CALENDAR_FORMAT);
Expand Down Expand Up @@ -71,19 +80,21 @@ export const getParsedDate = (date: DateType) => {
* @param displayFullDays
* @param minimumDate - min selectable date
* @param maximumDate - max selectable date
* @param firstDayOfWeek - first day of week, number 0-6, 0 – Sunday, 6 – Saturday
*
* @returns days array based on current date
*/
export const getMonthDays = (
datetime: DateType = dayjs(),
displayFullDays: boolean,
minimumDate: DateType,
maximumDate: DateType
maximumDate: DateType,
firstDayOfWeek: number
): IDayObject[] => {
const date = getDate(datetime);
const daysInMonth = date.daysInMonth();
const prevMonthDays = date.add(-1, 'month').daysInMonth();
const firstDay = date.date(1);
const firstDay = date.date(1 - firstDayOfWeek);
const dayOfMonth = firstDay.day() % 7;

const prevDays = displayFullDays
Expand Down

0 comments on commit f3617e6

Please sign in to comment.