@codeleap/mobile 5.2.6 → 5.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,21 +1,85 @@
1
- import React, { useCallback } from 'react';
1
+ import React, { useCallback, useMemo } from 'react';
2
2
  import { TypeGuards } from '@codeleap/types';
3
3
  import { Calendar as RNCalendar } from 'react-native-calendars';
4
4
  import { MobileStyleRegistry } from '../../Registry';
5
5
  import { useStylesFor } from '../../hooks';
6
6
  import dayjs from 'dayjs';
7
+ import utc from 'dayjs/plugin/utc';
8
+ dayjs.extend(utc);
7
9
  export * from './styles';
8
10
  export * from './types';
9
11
  export const Calendar = (props) => {
10
12
  const { style, value, onValueChange, parseToDate, ...calendarProps } = props;
11
13
  const styles = useStylesFor(Calendar.styleRegistryName, style);
12
14
  const isDateValue = TypeGuards.isInstance(value, Date);
13
- const stringValue = isDateValue ? dayjs(value).format('YYYY-MM-DD') : value ?? '';
15
+ const isDateRange = Array.isArray(value);
16
+ const stringValue = useMemo(() => {
17
+ if (isDateRange) {
18
+ return value.map(date => isDateValue ? dayjs.utc(date).format('YYYY-MM-DD') : date ?? '');
19
+ }
20
+ return isDateValue ? dayjs.utc(value).format('YYYY-MM-DD') : value ?? '';
21
+ }, [value]);
22
+ const markedDates = useMemo(() => {
23
+ if (isDateRange && Array.isArray(stringValue)) {
24
+ if (stringValue.length === 0)
25
+ return {};
26
+ if (stringValue.length === 1) {
27
+ const [start] = [...stringValue];
28
+ const dateStr = dayjs(start).format('YYYY-MM-DD');
29
+ return { [dateStr]: { selected: true } };
30
+ }
31
+ const [start, end] = [...stringValue];
32
+ const startDate = dayjs(start);
33
+ const endDate = dayjs(end);
34
+ const markedDates = {};
35
+ let currentDate = startDate;
36
+ while (currentDate.isBefore(endDate) || currentDate.isSame(endDate)) {
37
+ const dateStr = currentDate.format('YYYY-MM-DD');
38
+ markedDates[dateStr] = {
39
+ selected: true,
40
+ ...(currentDate.isSame(startDate) && { startingDay: true }),
41
+ ...(currentDate.isSame(endDate) && { endingDay: true })
42
+ };
43
+ currentDate = currentDate.add(1, 'day');
44
+ }
45
+ return markedDates;
46
+ }
47
+ return { [stringValue]: { selected: true } };
48
+ }, [stringValue]);
14
49
  const onChange = useCallback((date) => {
15
- const newValue = isDateValue || parseToDate ? dayjs(date.dateString).toDate() : date.dateString;
16
- onValueChange?.(newValue);
17
- }, [onValueChange]);
18
- return (<RNCalendar onDayPress={onChange} current={stringValue} monthFormat={'MMMM yyyy'} {...calendarProps} markedDates={{ [stringValue]: { selected: true }, ...calendarProps?.markedDates }} style={styles?.wrapper} headerStyle={styles?.header} theme={{
50
+ if (isDateRange) {
51
+ const newValue = Array.isArray(value) ? [...value] : [];
52
+ let newDates = [];
53
+ if (newValue.length === 0 || newValue.length === 2) {
54
+ newDates = [date.dateString];
55
+ }
56
+ else if (newValue.length === 1) {
57
+ const firstDate = newValue[0];
58
+ const secondDate = date.dateString;
59
+ if (dayjs(firstDate).isAfter(dayjs(secondDate))) {
60
+ newDates = [secondDate, firstDate];
61
+ }
62
+ else {
63
+ newDates = [firstDate, secondDate];
64
+ }
65
+ }
66
+ const parsedDates = isDateValue || parseToDate ?
67
+ newDates.map(d => dayjs(d).toDate()) :
68
+ newDates;
69
+ onValueChange?.(parsedDates);
70
+ }
71
+ else {
72
+ const newValue = isDateValue || parseToDate ? dayjs(date.dateString).toDate() : date.dateString;
73
+ onValueChange?.(newValue);
74
+ }
75
+ }, [onValueChange, value]);
76
+ const currentValue = useMemo(() => {
77
+ return isDateRange ? (Array.isArray(stringValue) ? stringValue[0] : '') : stringValue;
78
+ }, [stringValue]);
79
+ return (<RNCalendar onDayPress={onChange} current={currentValue} monthFormat={'MMMM yyyy'} {...calendarProps} markedDates={{
80
+ ...markedDates,
81
+ ...calendarProps?.markedDates
82
+ }} style={styles?.wrapper} headerStyle={styles?.header} theme={{
19
83
  ...styles?.calendar,
20
84
  ...calendarProps?.theme,
21
85
  }}/>);
@@ -1 +1 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AQ,CAAC,CAAA"}
@@ -1,9 +1,10 @@
1
1
  import { StyledProp } from '@codeleap/styles';
2
2
  import { CalendarProps as RNCalendarProps } from 'react-native-calendars';
3
3
  import { CalendarComposition } from './styles';
4
+ export type CalendarValue = (string | string[]) | (Date | Date[]);
4
5
  export type CalendarProps = Omit<RNCalendarProps, 'style'> & {
5
- onValueChange: (date: Date | string) => void;
6
- value: Date | string;
6
+ onValueChange: (date: CalendarValue) => void;
7
+ value: CalendarValue;
7
8
  style?: StyledProp<CalendarComposition>;
8
9
  parseToDate?: boolean;
9
10
  };
@@ -1,4 +1,4 @@
1
- import React, { useState } from 'react';
1
+ import React, { useMemo, useState } from 'react';
2
2
  import { useCompositionStyles } from '@codeleap/styles';
3
3
  import { MobileStyleRegistry } from '../../Registry';
4
4
  import { useStylesFor } from '../../hooks';
@@ -6,14 +6,16 @@ import { View } from '../View';
6
6
  import { Calendar } from '../Calendar';
7
7
  import { TextInput } from '../TextInput';
8
8
  import dayjs from 'dayjs';
9
+ import utc from 'dayjs/plugin/utc';
9
10
  import Animated, { FadeOut, FadeIn } from 'react-native-reanimated';
10
11
  import { useInputBase } from '../InputBase';
11
12
  import { fields } from '@codeleap/form';
12
13
  import { useInputOverlay } from '../InputBase/useInputOverlay';
14
+ dayjs.extend(utc);
13
15
  export * from './styles';
14
16
  export * from './types';
15
17
  export const CalendarInput = (props) => {
16
- const { style, value, onValueChange, disabled, gap, calendarPosition, rightIcon, leftIcon, autoClosePeersCalendars, field, format, overlay, ...textInputProps } = {
18
+ const { style, value, onValueChange, disabled, gap, calendarPosition, rightIcon, leftIcon, autoClosePeersCalendars, field, format, overlay, calendarProps, ...textInputProps } = {
17
19
  ...CalendarInput.defaultProps,
18
20
  ...props
19
21
  };
@@ -22,6 +24,17 @@ export const CalendarInput = (props) => {
22
24
  const { inputValue, onInputValueChange, } = useInputBase(field, fields.date, { value, onValueChange });
23
25
  const [inputHeight, setInputHeight] = useState(0);
24
26
  const [isOpen, toggle] = useInputOverlay(autoClosePeersCalendars);
27
+ const formattedValue = useMemo(() => {
28
+ const isRange = Array.isArray(inputValue);
29
+ if (isRange ? inputValue?.some(v => !v) : !inputValue)
30
+ return '';
31
+ if (isRange) {
32
+ return inputValue
33
+ .map(date => dayjs.utc(date).format(format))
34
+ .join(' - ');
35
+ }
36
+ return dayjs.utc(inputValue).format(format);
37
+ }, [inputValue, format]);
25
38
  return (<View style={[styles.wrapper, { position: 'relative' }]}>
26
39
  <TextInput placeholder='Select Date' disabled={disabled} {...textInputProps} style={compositionStyles.input} leftIcon={!leftIcon ? null : {
27
40
  ...leftIcon,
@@ -29,7 +42,7 @@ export const CalendarInput = (props) => {
29
42
  }} rightIcon={!rightIcon ? null : {
30
43
  ...rightIcon,
31
44
  onPress: toggle,
32
- }} value={!inputValue ? '' : dayjs(inputValue).format(format)} onValueChange={() => inputValue} onPress={disabled ? null : toggle} innerWrapperProps={{
45
+ }} value={formattedValue} onValueChange={() => inputValue} onPress={disabled ? null : toggle} innerWrapperProps={{
33
46
  rippleDisabled: true,
34
47
  }} focused={isOpen} onLayout={(e) => setInputHeight(e.nativeEvent.layout.height)}/>
35
48
 
@@ -41,7 +54,7 @@ export const CalendarInput = (props) => {
41
54
  } : {
42
55
  marginTop: gap,
43
56
  }}>
44
- <Calendar style={compositionStyles.calendar} value={inputValue} onValueChange={onInputValueChange} parseToDate/>
57
+ <Calendar style={compositionStyles.calendar} value={inputValue} onValueChange={onInputValueChange} parseToDate {...calendarProps}/>
45
58
  </Animated.View>)}
46
59
  </View>);
47
60
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/components/CalendarInput/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AACvC,OAAO,EAAyC,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AAC9F,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAA;AACpD,OAAO,EAA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CAAC,CAClB,aAAa,CAAC,CAAC,kBAAkB,CAAC,CAClC,WAAW,CACX,IAAI,aAAa,CAAC,EAEtB;QAAA,EAAE,QAAQ,CAAC,IAAI,CAAC,CACjB,CACH;IAAA,EAAE,IAAI,CAAC,CACR,CAAA;AACH,CAAC,CAAA;AAED,aAAa,CAAC,iBAAiB,GAAG,eAAe,CAAA;AACjD,aAAa,CAAC,QAAQ,GAAG,CAAC,SAAS,EAAE,UAAU,EAAE,OAAO,CAAC,CAAA;AACzD,aAAa,CAAC,WAAW,GAAG,SAAS,CAAA;AAErC,aAAa,CAAC,gBAAgB,GAAG,CAAsB,MAAS,EAAE,EAAE;IAClE,OAAO,aAAyF,CAAA;AAClG,CAAC,CAAA;AAED,aAAa,CAAC,YAAY,GAAG;IAC3B,GAAG,EAAE,CAAC;IACN,gBAAgB,EAAE,MAAM;IACxB,uBAAuB,EAAE,KAAK;IAC9B,MAAM,EAAE,YAAY;IACpB,OAAO,EAAE,IAAI;CACiB,CAAA;AAEhC,mBAAmB,CAAC,iBAAiB,CAAC,aAAa,CAAC,CAAA"}
@@ -12,6 +12,7 @@ export type CalendarInputProps = Omit<TextInputProps, 'value' | 'onValueChange'
12
12
  field?: DateField<any>;
13
13
  format?: string;
14
14
  overlay?: boolean;
15
+ calendarProps?: Partial<CalendarProps>;
15
16
  /**
16
17
  * Defines the position where the calendar will be anchored relative to the input.
17
18
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeleap/mobile",
3
- "version": "5.2.6",
3
+ "version": "5.3.0",
4
4
  "main": "src/index.ts",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -9,28 +9,28 @@
9
9
  "directory": "packages/mobile"
10
10
  },
11
11
  "devDependencies": {
12
- "@codeleap/types": "5.2.6",
13
- "@codeleap/utils": "5.2.6",
14
- "@codeleap/hooks": "5.2.6",
15
- "@codeleap/form": "5.2.6",
16
- "@codeleap/query": "5.2.6",
17
- "@codeleap/logger": "5.2.6",
18
- "@codeleap/config": "5.2.6",
19
- "@codeleap/modals": "5.2.6"
12
+ "@codeleap/types": "5.3.0",
13
+ "@codeleap/utils": "5.3.0",
14
+ "@codeleap/hooks": "5.3.0",
15
+ "@codeleap/form": "5.3.0",
16
+ "@codeleap/query": "5.3.0",
17
+ "@codeleap/logger": "5.3.0",
18
+ "@codeleap/config": "5.3.0",
19
+ "@codeleap/modals": "5.3.0"
20
20
  },
21
21
  "scripts": {
22
22
  "build": "tsc --build",
23
23
  "lint": "eslint -c .eslintrc.js --fix \"./src/**/*.{ts,tsx,js,jsx}\""
24
24
  },
25
25
  "peerDependencies": {
26
- "@codeleap/types": "5.2.6",
27
- "@codeleap/utils": "5.2.6",
28
- "@codeleap/hooks": "5.2.6",
29
- "@codeleap/form": "5.2.6",
30
- "@codeleap/query": "5.2.6",
31
- "@codeleap/logger": "5.2.6",
32
- "@codeleap/styles": "5.2.6",
33
- "@codeleap/modals": "5.2.6",
26
+ "@codeleap/types": "5.3.0",
27
+ "@codeleap/utils": "5.3.0",
28
+ "@codeleap/hooks": "5.3.0",
29
+ "@codeleap/form": "5.3.0",
30
+ "@codeleap/query": "5.3.0",
31
+ "@codeleap/logger": "5.3.0",
32
+ "@codeleap/styles": "5.3.0",
33
+ "@codeleap/modals": "5.3.0",
34
34
  "@d11/react-native-fast-image": "8.9.2",
35
35
  "@react-native-firebase/messaging": "21.12.0",
36
36
  "@react-navigation/bottom-tabs": "7.3.10",
package/package.json.bak CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeleap/mobile",
3
- "version": "5.2.6",
3
+ "version": "5.3.0",
4
4
  "main": "src/index.ts",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -1,4 +1,4 @@
1
- import React, { useCallback } from 'react'
1
+ import React, { useCallback, useMemo } from 'react'
2
2
  import { TypeGuards } from '@codeleap/types'
3
3
  import { Calendar as RNCalendar, DateData } from 'react-native-calendars'
4
4
  import { AnyRecord, IJSX, StyledComponentProps } from '@codeleap/styles'
@@ -6,6 +6,9 @@ import { MobileStyleRegistry } from '../../Registry'
6
6
  import { useStylesFor } from '../../hooks'
7
7
  import { CalendarProps } from './types'
8
8
  import dayjs from 'dayjs'
9
+ import utc from 'dayjs/plugin/utc'
10
+
11
+ dayjs.extend(utc)
9
12
 
10
13
  export * from './styles'
11
14
  export * from './types'
@@ -22,20 +25,98 @@ export const Calendar = (props: CalendarProps) => {
22
25
  const styles = useStylesFor(Calendar.styleRegistryName, style)
23
26
 
24
27
  const isDateValue = TypeGuards.isInstance(value, Date)
25
- const stringValue = isDateValue ? dayjs(value).format('YYYY-MM-DD') : value ?? ''
28
+ const isDateRange = Array.isArray(value)
29
+
30
+ const stringValue = useMemo(() => {
31
+ if (isDateRange) {
32
+ return value.map(date =>
33
+ isDateValue ? dayjs.utc(date).format('YYYY-MM-DD') : date ?? ''
34
+ )
35
+ }
36
+
37
+ return isDateValue ? dayjs.utc(value).format('YYYY-MM-DD') : value ?? ''
38
+ }, [value])
39
+
40
+ const markedDates = useMemo(() => {
41
+ if (isDateRange && Array.isArray(stringValue)) {
42
+ if (stringValue.length === 0) return {}
43
+ if (stringValue.length === 1) {
44
+ const [start] = [...stringValue]
45
+
46
+ const dateStr = dayjs(start).format('YYYY-MM-DD')
47
+
48
+ return { [dateStr]: { selected: true } }
49
+ }
50
+
51
+ const [start, end] = [...stringValue]
52
+ const startDate = dayjs(start)
53
+ const endDate = dayjs(end)
54
+
55
+ const markedDates: Record<string, any> = {}
56
+
57
+ let currentDate = startDate
58
+
59
+ while (currentDate.isBefore(endDate) || currentDate.isSame(endDate)) {
60
+ const dateStr = currentDate.format('YYYY-MM-DD')
61
+
62
+ markedDates[dateStr] = {
63
+ selected: true,
64
+ ...(currentDate.isSame(startDate) && { startingDay: true }),
65
+ ...(currentDate.isSame(endDate) && { endingDay: true })
66
+ }
67
+
68
+ currentDate = currentDate.add(1, 'day')
69
+ }
70
+
71
+ return markedDates
72
+ }
73
+
74
+ return { [stringValue as string]: { selected: true } }
75
+ }, [stringValue])
26
76
 
27
77
  const onChange = useCallback((date: DateData) => {
28
- const newValue = isDateValue || parseToDate ? dayjs(date.dateString).toDate() : date.dateString
29
- onValueChange?.(newValue)
30
- }, [onValueChange])
78
+ if (isDateRange) {
79
+ const newValue = Array.isArray(value) ? [...value] : []
80
+ let newDates: any = []
81
+
82
+ if (newValue.length === 0 || newValue.length === 2) {
83
+ newDates = [date.dateString]
84
+ } else if (newValue.length === 1) {
85
+ const firstDate = newValue[0]
86
+ const secondDate = date.dateString
87
+
88
+ if (dayjs(firstDate).isAfter(dayjs(secondDate))) {
89
+ newDates = [secondDate, firstDate]
90
+ } else {
91
+ newDates = [firstDate, secondDate]
92
+ }
93
+ }
94
+
95
+ const parsedDates = isDateValue || parseToDate ?
96
+ newDates.map(d => dayjs(d).toDate()) :
97
+ newDates
98
+
99
+ onValueChange?.(parsedDates)
100
+ } else {
101
+ const newValue = isDateValue || parseToDate ? dayjs(date.dateString).toDate() : date.dateString
102
+ onValueChange?.(newValue)
103
+ }
104
+ }, [onValueChange, value])
31
105
 
106
+ const currentValue = useMemo(() => {
107
+ return isDateRange ? (Array.isArray(stringValue) ? stringValue[0] : '') : stringValue
108
+ }, [stringValue])
109
+
32
110
  return (
33
111
  <RNCalendar
34
112
  onDayPress={onChange}
35
- current={stringValue}
113
+ current={currentValue}
36
114
  monthFormat={'MMMM yyyy'}
37
115
  {...calendarProps}
38
- markedDates={{ [stringValue]: { selected: true }, ...calendarProps?.markedDates }}
116
+ markedDates={{
117
+ ...markedDates,
118
+ ...calendarProps?.markedDates
119
+ }}
39
120
  style={styles?.wrapper}
40
121
  headerStyle={styles?.header}
41
122
  theme={{
@@ -58,4 +139,4 @@ Calendar.defaultProps = {
58
139
  parseToDate: false,
59
140
  } as Partial<CalendarProps>
60
141
 
61
- MobileStyleRegistry.registerComponent(Calendar)
142
+ MobileStyleRegistry.registerComponent(Calendar)
@@ -2,11 +2,13 @@ import { StyledProp } from '@codeleap/styles'
2
2
  import { CalendarProps as RNCalendarProps } from 'react-native-calendars'
3
3
  import { CalendarComposition } from './styles'
4
4
 
5
+ export type CalendarValue = (string | string[]) | (Date | Date[])
6
+
5
7
  export type CalendarProps =
6
8
  Omit<RNCalendarProps, 'style'> &
7
9
  {
8
- onValueChange: (date: Date | string) => void
9
- value: Date | string
10
+ onValueChange: (date: CalendarValue) => void
11
+ value: CalendarValue
10
12
  style?: StyledProp<CalendarComposition>
11
13
  parseToDate?: boolean
12
14
  }
@@ -1,17 +1,20 @@
1
- import React, { useState } from 'react'
1
+ import React, { useMemo, useState } from 'react'
2
2
  import { AnyRecord, IJSX, StyledComponentProps, useCompositionStyles } from '@codeleap/styles'
3
3
  import { MobileStyleRegistry } from '../../Registry'
4
4
  import { useStylesFor } from '../../hooks'
5
5
  import { CalendarInputProps } from './types'
6
6
  import { View } from '../View'
7
- import { Calendar } from '../Calendar'
7
+ import { Calendar, CalendarProps } from '../Calendar'
8
8
  import { TextInput } from '../TextInput'
9
9
  import dayjs from 'dayjs'
10
+ import utc from 'dayjs/plugin/utc'
10
11
  import Animated, { FadeOut, FadeIn } from 'react-native-reanimated'
11
12
  import { useInputBase } from '../InputBase'
12
13
  import { fields } from '@codeleap/form'
13
14
  import { useInputOverlay } from '../InputBase/useInputOverlay'
14
15
 
16
+ dayjs.extend(utc)
17
+
15
18
  export * from './styles'
16
19
  export * from './types'
17
20
 
@@ -29,6 +32,7 @@ export const CalendarInput = (props: CalendarInputProps) => {
29
32
  field,
30
33
  format,
31
34
  overlay,
35
+ calendarProps,
32
36
  ...textInputProps
33
37
  } = {
34
38
  ...CalendarInput.defaultProps,
@@ -42,12 +46,26 @@ export const CalendarInput = (props: CalendarInputProps) => {
42
46
  const {
43
47
  inputValue,
44
48
  onInputValueChange,
45
- } = useInputBase<Date | string>(field, fields.date as any, { value, onValueChange })
49
+ } = useInputBase<CalendarProps['value']>(field, fields.date as any, { value, onValueChange })
46
50
 
47
51
  const [inputHeight, setInputHeight] = useState(0)
48
52
 
49
53
  const [isOpen, toggle] = useInputOverlay(autoClosePeersCalendars)
50
54
 
55
+ const formattedValue = useMemo(() => {
56
+ const isRange = Array.isArray(inputValue)
57
+
58
+ if (isRange ? inputValue?.some(v => !v) : !inputValue) return ''
59
+
60
+ if (isRange) {
61
+ return inputValue
62
+ .map(date => dayjs.utc(date).format(format))
63
+ .join(' - ')
64
+ }
65
+
66
+ return dayjs.utc(inputValue).format(format)
67
+ }, [inputValue, format])
68
+
51
69
  return (
52
70
  <View style={[styles.wrapper, { position: 'relative' }]}>
53
71
  <TextInput
@@ -63,7 +81,7 @@ export const CalendarInput = (props: CalendarInputProps) => {
63
81
  ...rightIcon,
64
82
  onPress: toggle,
65
83
  }}
66
- value={!inputValue ? '' : dayjs(inputValue).format(format)}
84
+ value={formattedValue}
67
85
  onValueChange={() => inputValue}
68
86
  onPress={disabled ? null : toggle}
69
87
  innerWrapperProps={{
@@ -91,6 +109,7 @@ export const CalendarInput = (props: CalendarInputProps) => {
91
109
  value={inputValue}
92
110
  onValueChange={onInputValueChange}
93
111
  parseToDate
112
+ {...calendarProps}
94
113
  />
95
114
  </Animated.View>
96
115
  )}
@@ -15,6 +15,7 @@ export type CalendarInputProps =
15
15
  field?: DateField<any>
16
16
  format?: string
17
17
  overlay?: boolean
18
+ calendarProps?: Partial<CalendarProps>
18
19
 
19
20
  /**
20
21
  * Defines the position where the calendar will be anchored relative to the input.