Skip to content

Commit 188056a

Browse files
authored
cleanups (#1982)
* Move 'MarkedDates' to types and use it from there * clean unused * Add 'getLocal' to dateutils and use it in services * Moving to utils directory: hooks, momentResolver, Profiler, testUtils. velocityTracker * Get markings enum from Marking component * import specific lodash methods * CalendarPlaygroundScreen - create sub switches for dep props * reverting file move
1 parent a92f3a2 commit 188056a

File tree

7 files changed

+47
-41
lines changed

7 files changed

+47
-41
lines changed

example/src/mocks/agendaItems.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import isEmpty from 'lodash/isEmpty';
2+
import {MarkedDates} from '../../../src/types';
23

34
const today = new Date().toISOString().split('T')[0];
45
const fastDate = getPastDate(3);
@@ -116,12 +117,8 @@ export const agendaItems = [
116117
}
117118
];
118119

119-
type MarkedDate = {
120-
[key: string]: object;
121-
}
122-
123120
export function getMarkedDates() {
124-
const marked: MarkedDate = {};
121+
const marked: MarkedDates = {};
125122

126123
agendaItems.forEach(item => {
127124
// NOTE: only mark dates with data

example/src/screens/calendarPlaygroundScreen.tsx

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
import times from 'lodash/times';
2-
import React, {useState, Fragment, useCallback, useMemo, useRef} from 'react';
2+
import React, {useState, useCallback, useMemo, useRef} from 'react';
33
import {StyleSheet, View, ScrollView, Text, TouchableOpacity, Switch, Alert} from 'react-native';
44
import {Calendar, CalendarUtils} from 'react-native-calendars';
5+
56
import testIDs from '../testIDs';
7+
import Marking from '../../../src/calendar/day/marking';
68

79
const INITIAL_DATE = '2022-07-06';
810
const GREEN = '#13ba7d';
911
const PINK = '#a68a9f';
1012
const RED = '#ba1313';
11-
enum Markings {
12-
DOT = 'dot',
13-
MULTI_DOT = 'multi-dot',
14-
PERIOD = 'period',
15-
MULTI_PERIOD = 'multi-period',
16-
CUSTOM = 'custom'
17-
}
1813

1914
const NewCalendarScreen = () => {
2015
const [selected, setSelected] = useState(INITIAL_DATE);
2116
const [currentMonth, setCurrentMonth] = useState(INITIAL_DATE);
22-
const [markingType, setMarkingType] = useState(Markings.DOT);
17+
const [markingType, setMarkingType] = useState(Marking.markings.DOT);
2318
const [selectedButtonIndex, setSelectedButtonIndex] = useState(0);
2419

2520
/** props */
@@ -289,15 +284,15 @@ const NewCalendarScreen = () => {
289284

290285
const markingForType = useCallback(() => {
291286
switch (markingType) {
292-
case Markings.DOT:
287+
case Marking.markings.DOT:
293288
return dotMarks;
294-
case Markings.MULTI_DOT:
289+
case Marking.markings.MULTI_DOT:
295290
return multiDotMarks;
296-
case Markings.PERIOD:
291+
case Marking.markings.PERIOD:
297292
return periodWithDotsMarks; //periodMarks;
298-
case Markings.MULTI_PERIOD:
293+
case Marking.markings.MULTI_PERIOD:
299294
return multiPeriodMarks;
300-
case Markings.CUSTOM:
295+
case Marking.markings.CUSTOM:
301296
return customMarks;
302297
}
303298
}, [markingType, selected]);
@@ -450,32 +445,36 @@ const NewCalendarScreen = () => {
450445
return (
451446
<View>
452447
{renderSwitch('Min and Max Dates', minAndMax, toggleMinAndMax)}
453-
{renderSwitch('Allow Selection Out Of Range', allowSelectionOutOfRange, toggleAllowSelectionOutOfRange)}
448+
<View style={styles.subSwitchContainer}>
449+
{minAndMax && renderSwitch('Allow Selection Out Of Range', allowSelectionOutOfRange, toggleAllowSelectionOutOfRange)}
450+
</View>
454451
{renderSwitch('Enable Swipe Months', enableSwipeMonths, toggleEnableSwipeMonths)}
455452
{renderSwitch('Disable Month Change', disableMonthChange, toggleDisableMonthChange)}
456453
{renderSwitch('Show Week Numbers', showWeekNumbers, toggleShowWeekNumbers)}
457454
{renderSwitch('Show Six Weeks', showSixWeeks, toggleShowSixWeeks)}
458455
{renderSwitch('Hide Extra Days', hideExtraDays, toggleHideExtraDays)}
459456
{renderSwitch('Hide Day Names', hideDayNames, toggleHideDayNames)}
460-
{renderSwitch('Hide Arrows', hideArrows, toggleHideArrows)}
461457
{renderSwitch('Disabled By Default', disabledByDefault, toggleDisabledByDefault)}
462458
{renderSwitch('Disable All Touch Events For Disabled Days', disableAllTouchEventsForDisabledDays, toggleDisableAllTouchEventsForDisabledDays)}
463459
{renderSwitch('Disable All Touch Events For Inactive Days', disableAllTouchEventsForInactiveDays, toggleDisableAllTouchEventsForInactiveDays)}
464460
{renderSwitch('Display Loading Indicator', displayLoadingIndicator, toggleDisplayLoadingIndicator)}
465461
{renderSwitch('Disabled Days Indexes', disabledDaysIndexes, toggleDisabledDaysIndexes)}
462+
{renderSwitch('Hide Arrows', hideArrows, toggleHideArrows)}
463+
<View style={styles.subSwitchContainer}>
464+
{!hideArrows && renderSwitch('Disable Arrow Left', disableArrowLeft, toggleDisableArrowLeft)}
465+
{!hideArrows && renderSwitch('Disable Arrow Right', disableArrowRight, toggleDisableArrowRight)}
466+
{!hideArrows && renderSwitch('Render Arrow', renderArrow, toggleRenderArrow)}
467+
</View>
466468
{renderSwitch('Day Component', dayComponent, toggleDayComponent)}
467469
{renderSwitch('Custom Header', customHeader, toggleCustomHeader)}
468470
{renderSwitch('Custom Header Title', customHeaderTitle, toggleCustomHeaderTitle)}
469-
{renderSwitch('Render Arrow', renderArrow, toggleRenderArrow)}
470-
{renderSwitch('Disable Arrow Left', disableArrowLeft, toggleDisableArrowLeft)}
471-
{renderSwitch('Disable Arrow Right', disableArrowRight, toggleDisableArrowRight)}
472471
</View>
473472
);
474473
};
475474

476475
/** Buttons */
477476
const getValue = (index = 0) => {
478-
return Object.values(Markings)[index];
477+
return Object.values(Marking.markings)[index];
479478
};
480479

481480
const setType = (index = 0) => {
@@ -528,12 +527,12 @@ const NewCalendarScreen = () => {
528527
};
529528

530529
return (
531-
<Fragment>
530+
<>
532531
{renderCalendar()}
533532
<ScrollView showsVerticalScrollIndicator={false} testID={testIDs.calendars.CONTAINER}>
534533
{renderOptions()}
535534
</ScrollView>
536-
</Fragment>
535+
</>
537536
);
538537
};
539538

@@ -578,6 +577,9 @@ const styles = StyleSheet.create({
578577
switchText: {
579578
marginHorizontal: 10
580579
},
580+
subSwitchContainer: {
581+
marginLeft: 20
582+
},
581583
buttonsContainer: {
582584
margin: 10
583585
},

src/calendar/index.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,12 @@ import {getState} from '../day-state-manager';
1313
import {extractHeaderProps, extractDayProps} from '../componentUpdater';
1414
// @ts-expect-error
1515
import {WEEK_NUMBER} from '../testIDs';
16-
import {DateData, Theme} from '../types';
16+
import {DateData, Theme, MarkedDates} from '../types';
1717
import {useDidUpdate} from '../hooks';
1818
import styleConstructor from './style';
1919
import CalendarHeader, {CalendarHeaderProps} from './header';
2020
import Day, {DayProps} from './day/index';
2121
import BasicDay from './day/basic';
22-
import {MarkingProps} from './day/marking';
23-
24-
type MarkedDatesType = {
25-
[key: string]: MarkingProps;
26-
};
2722

2823
export interface CalendarProps extends CalendarHeaderProps, DayProps {
2924
/** Specify theme properties to override specific styles for calendar parts */
@@ -45,7 +40,7 @@ export interface CalendarProps extends CalendarHeaderProps, DayProps {
4540
/** Maximum date that can be selected, dates after maxDate will be grayed out */
4641
maxDate?: string;
4742
/** Collection of dates that have to be marked */
48-
markedDates?: MarkedDatesType;
43+
markedDates?: MarkedDates;
4944
/** Do not show days of other months in month page */
5045
hideExtraDays?: boolean;
5146
/** Always show six weeks on each month (only when hideExtraDays = false) */

src/dateutils.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const XDate = require('xdate');
22
const {toMarkingFormat} = require('./interface');
3-
const {getDefaultLocale} = require('./services');
43

54
const latinNumbersPattern = /[0-9]/g;
65

@@ -64,7 +63,7 @@ export function isLTE(a: XDate, b: XDate) {
6463
}
6564

6665
export function formatNumbers(date: any) {
67-
const numbers = getDefaultLocale().numbers;
66+
const numbers = getLocale().numbers;
6867
return numbers ? date.toString().replace(latinNumbersPattern, (char: any) => numbers[+char]) : date;
6968
}
7069

@@ -91,7 +90,7 @@ export function month(date: XDate) { // exported for tests only
9190
}
9291

9392
export function weekDayNames(firstDayOfWeek = 0) {
94-
let weekDaysNames = getDefaultLocale().dayNamesShort;
93+
let weekDaysNames = getLocale().dayNamesShort;
9594
const dayShift = firstDayOfWeek % 7;
9695
if (dayShift) {
9796
weekDaysNames = weekDaysNames.slice(dayShift).concat(weekDaysNames.slice(0, dayShift));
@@ -191,3 +190,7 @@ export function generateDay(originDate: string | XDate, daysOffset = 0) {
191190
const baseDate = originDate instanceof XDate ? originDate : new XDate(originDate);
192191
return toMarkingFormat(baseDate.clone().addDays(daysOffset));
193192
}
193+
194+
export function getLocale() {
195+
return XDate.locales[XDate.defaultLocale];
196+
}

src/services/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import {isUndefined, isDate, isString, isNumber} from 'lodash';
1+
import isUndefined from 'lodash/isUndefined';
2+
import isDate from 'lodash/isDate';
3+
import isString from 'lodash/isString';
4+
import isNumber from 'lodash/isNumber';
25
import XDate from 'xdate';
6+
7+
const {getLocale} = require('../dateutils');
38
const {padNumber, toMarkingFormat} = require('../interface');
49

510
export function getCalendarDateString(date?: Date | string | number) {
@@ -17,7 +22,7 @@ export function getCalendarDateString(date?: Date | string | number) {
1722
}
1823

1924
export function getDefaultLocale(): any {
20-
return XDate.locales[XDate.defaultLocale];
25+
return getLocale();
2126
}
2227

2328
export default {

src/timeline/__tests__/Packer.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import _ from 'lodash';
1+
import sortBy from 'lodash/sortBy';
22
import * as uut from '../Packer';
33

44
describe('Timeline Packer utils', () => {
@@ -97,7 +97,7 @@ describe('Timeline Packer utils', () => {
9797
dayStart: 0,
9898
overlapEventsSpacing: 4
9999
});
100-
packedEvents = _.sortBy(packedEvents, 'index');
100+
packedEvents = sortBy(packedEvents, 'index');
101101
expect(packedEvents[0].width).toBe(96);
102102
expect(packedEvents[1].width).toBe(96);
103103
expect(packedEvents[2].width).toBe(100);

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import {ViewStyle, TextStyle} from 'react-native';
2+
import {MarkingProps} from './calendar/day/marking';
23

34
export type MarkingTypes = 'dot' | 'multi-dot' | 'period' | 'multi-period' | 'custom';
5+
export type MarkedDates = {
6+
[key: string]: MarkingProps;
7+
};
48
export type DayState = 'selected' | 'disabled' | 'inactive' | 'today' | '';
59
export type Direction = 'left' | 'right';
610
export type DateData = {

0 commit comments

Comments
 (0)