1
1
import _ from 'lodash' ;
2
- import React , { useState , useCallback } from 'react' ;
3
- import { Alert } from 'react-native' ;
4
- import { Text , View , SectionsWheelPicker , SegmentedControl , Button , Incubator } from 'react-native-ui-lib' ;
2
+ import React , { useState , useCallback , useMemo } from 'react' ;
3
+ import { Alert , StyleSheet } from 'react-native' ;
4
+ import {
5
+ Text ,
6
+ View ,
7
+ SectionsWheelPicker ,
8
+ SegmentedControl ,
9
+ Button ,
10
+ Incubator ,
11
+ Constants ,
12
+ Switch ,
13
+ Colors
14
+ } from 'react-native-ui-lib' ;
5
15
6
16
const { WheelPicker} = Incubator ;
17
+
18
+ const DAYS = _ . times ( 10 , i => i ) ;
19
+ const HOURS = _ . times ( 24 , i => i ) ;
20
+ const MINUTES = _ . times ( 60 , i => i ) ;
21
+
7
22
const SectionsWheelPickerScreen = ( ) => {
8
23
const [ numOfSections , setNumOfSections ] = useState ( 1 ) ;
9
-
24
+ const [ disableRTL , setDisableRTL ] = useState ( false ) ;
10
25
const [ selectedDays , setSelectedDays ] = useState ( 0 ) ;
11
26
const [ selectedHours , setSelectedHours ] = useState ( 0 ) ;
12
27
const [ selectedMinutes , setSelectedMinutes ] = useState ( 0 ) ;
13
28
14
- const days = _ . times ( 10 , i => i ) ;
15
- const hours = _ . times ( 24 , i => i ) ;
16
- const minutes = _ . times ( 60 , i => i ) ;
29
+ const shouldDisableRTL = useMemo ( ( ) => {
30
+ return Constants . isRTL && disableRTL ;
31
+ } , [ disableRTL ] ) ;
17
32
18
33
const getItems = useCallback ( values => {
19
34
return _ . map ( values , item => ( { label : '' + item , value : item } ) ) ;
20
35
} , [ ] ) ;
21
36
22
- const onDaysChange = ( item : number | string ) => {
37
+ const onDaysChange = useCallback ( ( item : number | string ) => {
23
38
setSelectedDays ( item as number ) ;
24
- } ;
39
+ } , [ ] ) ;
25
40
26
- const onHoursChange = ( item : number | string ) => {
41
+ const onHoursChange = useCallback ( ( item : number | string ) => {
27
42
setSelectedHours ( item as number ) ;
28
- } ;
43
+ } , [ ] ) ;
29
44
30
- const onMinutesChange = ( item : number | string ) => {
45
+ const onMinutesChange = useCallback ( ( item : number | string ) => {
31
46
setSelectedMinutes ( item as number ) ;
32
- } ;
47
+ } , [ ] ) ;
33
48
34
- const onSavePress = ( ) => {
49
+ const onSavePress = useCallback ( ( ) => {
35
50
const days = selectedDays === 1 ? 'day' : 'days' ;
36
51
const hours = selectedHours === 1 ? 'hour' : 'hours' ;
37
52
const minutes = selectedMinutes === 1 ? 'minute' : 'minutes' ;
@@ -52,67 +67,132 @@ const SectionsWheelPickerScreen = () => {
52
67
: numOfSections === 2
53
68
? Alert . alert ( 'Your chosen duration is:\n' + selectedDays + ' ' + days + ' and ' + selectedHours + ' ' + hours )
54
69
: Alert . alert ( 'Your chosen duration is:\n' + selectedDays + ' ' + days ) ;
55
- } ;
70
+ } , [ numOfSections , selectedDays , selectedHours , selectedMinutes ] ) ;
56
71
57
- const onResetPress = ( ) => {
72
+ const onResetPress = useCallback ( ( ) => {
58
73
setSelectedDays ( 0 ) ;
59
74
setSelectedHours ( 0 ) ;
60
75
setSelectedMinutes ( 0 ) ;
61
- } ;
62
-
63
- const sections : Incubator . WheelPickerProps [ ] = [
64
- {
65
- items : getItems ( days ) ,
66
- onChange : onDaysChange ,
67
- initialValue : selectedDays ,
68
- label : 'Days' ,
69
- align : numOfSections === 1 ? WheelPicker . alignments . CENTER : WheelPicker . alignments . RIGHT ,
70
- style : { flex : 1 }
71
- } ,
72
- {
73
- items : getItems ( hours ) ,
74
- onChange : onHoursChange ,
75
- initialValue : selectedHours ,
76
- label : 'Hrs' ,
77
- align : numOfSections === 2 ? WheelPicker . alignments . LEFT : WheelPicker . alignments . CENTER ,
78
- style : numOfSections === 2 ? { flex : 1 } : undefined
79
- } ,
80
- {
81
- items : getItems ( minutes ) ,
82
- onChange : onMinutesChange ,
83
- initialValue : selectedMinutes ,
84
- label : 'Mins' ,
85
- align : WheelPicker . alignments . LEFT ,
86
- style : { flex : 1 }
87
- }
88
- ] ;
89
-
90
- const sectionsToPresent = _ . slice ( sections , 0 , numOfSections ) ;
91
-
92
- const onChangeIndex = ( index : number ) => {
76
+ } , [ ] ) ;
77
+
78
+ const sections : Incubator . WheelPickerProps [ ] = useMemo ( ( ) => {
79
+ return [
80
+ {
81
+ items : getItems ( DAYS ) ,
82
+ onChange : onDaysChange ,
83
+ initialValue : selectedDays ,
84
+ label : Constants . isRTL ? 'ימים' : 'Days' ,
85
+ align :
86
+ numOfSections === 1
87
+ ? WheelPicker . alignments . CENTER
88
+ : shouldDisableRTL
89
+ ? WheelPicker . alignments . LEFT
90
+ : WheelPicker . alignments . RIGHT ,
91
+ style : {
92
+ flex : 1 ,
93
+ flexDirection : numOfSections !== 1 && Constants . isRTL && ! disableRTL ? 'row-reverse' : undefined
94
+ }
95
+ } ,
96
+ {
97
+ items : getItems ( HOURS ) ,
98
+ onChange : onHoursChange ,
99
+ initialValue : selectedHours ,
100
+ label : Constants . isRTL ? 'שעות' : 'Hrs' ,
101
+ align :
102
+ numOfSections === 2
103
+ ? shouldDisableRTL
104
+ ? WheelPicker . alignments . RIGHT
105
+ : WheelPicker . alignments . LEFT
106
+ : WheelPicker . alignments . CENTER ,
107
+ style : numOfSections === 2 ? { flex : 1 , flexDirection : shouldDisableRTL ? 'row-reverse' : 'row' } : undefined
108
+ } ,
109
+ {
110
+ items : getItems ( MINUTES ) ,
111
+ onChange : onMinutesChange ,
112
+ initialValue : selectedMinutes ,
113
+ label : Constants . isRTL ? 'דקות' : 'Mins' ,
114
+ align : shouldDisableRTL ? WheelPicker . alignments . RIGHT : WheelPicker . alignments . LEFT ,
115
+ style : { flex : 1 , flexDirection : shouldDisableRTL ? 'row-reverse' : 'row' }
116
+ }
117
+ ] ;
118
+ } , [
119
+ getItems ,
120
+ disableRTL ,
121
+ selectedDays ,
122
+ selectedHours ,
123
+ selectedMinutes ,
124
+ onDaysChange ,
125
+ onHoursChange ,
126
+ onMinutesChange ,
127
+ numOfSections ,
128
+ shouldDisableRTL
129
+ ] ) ;
130
+
131
+ const sectionsToPresent = useMemo ( ( ) => _ . slice ( sections , 0 , numOfSections ) , [ numOfSections , sections ] ) ;
132
+
133
+ const timeSections = useMemo ( ( ) => {
134
+ return [
135
+ {
136
+ items : getItems ( _ . times ( 24 , i => i + 1 ) )
137
+ } ,
138
+ {
139
+ items : getItems ( _ . times ( 12 , i => {
140
+ if ( i < 2 ) {
141
+ return `0${ i * 5 } ` ;
142
+ }
143
+ return i * 5 ;
144
+ } ) )
145
+ }
146
+ ] ;
147
+ } , [ getItems ] ) ;
148
+
149
+ const onChangeIndex = useCallback ( ( index : number ) => {
93
150
return setNumOfSections ( index + 1 ) ;
94
- } ;
151
+ } , [ ] ) ;
152
+
153
+ const updateDisableRTLValue = useCallback ( ( value : boolean ) => {
154
+ setDisableRTL ( value ) ;
155
+ } , [ ] ) ;
95
156
96
157
return (
97
158
< View >
98
159
< Text text40 marginL-10 marginT-20 >
99
160
Sections Wheel Picker
100
161
</ Text >
101
- < View centerH marginT-40 >
162
+ < View row center style = { styles . bottomDivider } >
163
+ < Text margin-s5 > Disable RTL</ Text >
164
+ < Switch value = { shouldDisableRTL } onValueChange = { updateDisableRTLValue } />
165
+ </ View >
166
+ < View centerH marginT-20 >
167
+ < Text text60 marginB-20 >
168
+ Pick a duration
169
+ </ Text >
102
170
< SegmentedControl
103
171
segments = { [ { label : '1 section' } , { label : '2 sections' } , { label : '3 sections' } ] }
104
172
onChangeIndex = { onChangeIndex }
105
173
throttleTime = { 400 }
106
174
/>
107
- < Text text50 marginV-20 >
108
- Pick a duration
175
+ </ View >
176
+ < SectionsWheelPicker numberOfVisibleRows = { 4 } disableRTL = { disableRTL } sections = { sectionsToPresent } />
177
+ < View paddingB-20 center spread row style = { styles . bottomDivider } >
178
+ < Button marginR-40 link label = { 'Save' } onPress = { onSavePress } />
179
+ < Button label = { 'Reset' } link onPress = { onResetPress } />
180
+ </ View >
181
+ < View >
182
+ < Text center text60 marginV-20 >
183
+ Pick a time
109
184
</ Text >
185
+ < SectionsWheelPicker disableRTL = { disableRTL } sections = { timeSections } />
110
186
</ View >
111
- < SectionsWheelPicker sections = { sectionsToPresent } />
112
- < Button marginH-150 marginT-40 label = { 'Save' } onPress = { onSavePress } />
113
- < Button marginH-150 marginT-15 label = { 'Reset' } onPress = { onResetPress } />
114
187
</ View >
115
188
) ;
116
189
} ;
117
190
118
191
export default SectionsWheelPickerScreen ;
192
+
193
+ const styles = StyleSheet . create ( {
194
+ bottomDivider : {
195
+ borderBottomColor : Colors . $outlineDefault ,
196
+ borderBottomWidth : 4
197
+ }
198
+ } ) ;
0 commit comments