1
+ import _ from 'lodash' ;
1
2
import React , { useCallback , useState } from 'react' ;
2
3
import { View , Text , Incubator , Colors , Typography , Button , Dialog } from 'react-native-ui-lib' ;
3
- import _ from 'lodash' ;
4
-
5
- type WheelPickerValue = Incubator . WheelPickerProps [ 'initialValue' ] ;
6
4
7
5
const monthItems = _ . map ( [
8
6
'January' ,
@@ -18,18 +16,25 @@ const monthItems = _.map([
18
16
'November' ,
19
17
'December'
20
18
] ,
21
- item => ( { label : item , value : item } ) ) ;
19
+ item => ( { label : item , value : item , align : Incubator . WheelPickerAlign . RIGHT } ) ) ;
22
20
23
- const yearItems = _ . times ( 2030 , i => i )
21
+ const yearItems = _ . times ( 2050 , i => i )
24
22
. reverse ( )
25
23
. map ( item => ( { label : `${ item } ` , value : item } ) ) ;
26
24
const dayItems = _ . times ( 31 , i => i + 1 ) . map ( day => ( { label : `${ day } ` , value : day } ) ) ;
27
25
28
- const useData = ( initialMonth ?: string , initialYear ?: string , initialDays ?: number ) => {
29
- const [ selectedMonth , setMonth ] = useState < WheelPickerValue > ( initialMonth ) ;
30
- const [ , setYear ] = useState < WheelPickerValue > ( initialYear ) ;
31
- const [ selectedDays , setDays ] = useState < WheelPickerValue > ( initialDays ) ;
26
+ export default ( ) => {
32
27
const [ showDialog , setShowDialog ] = useState ( false ) ;
28
+ const [ yearsValue , setYearsValue ] = useState ( 2022 ) ;
29
+
30
+ const updateYearsInitialValue = useCallback ( ( increaseYears : boolean ) => {
31
+ increaseYears ? setYearsValue ( Math . min ( yearsValue + 1 , 2049 ) ) : setYearsValue ( Math . max ( yearsValue - 1 , 0 ) ) ;
32
+ } ,
33
+ [ yearsValue ] ) ;
34
+
35
+ const onChange = useCallback ( ( value ) => {
36
+ setYearsValue ( value ) ;
37
+ } , [ ] ) ;
33
38
34
39
const onPickDaysPress = useCallback ( ( ) => {
35
40
setShowDialog ( true ) ;
@@ -39,75 +44,52 @@ const useData = (initialMonth?: string, initialYear?: string, initialDays?: numb
39
44
setShowDialog ( false ) ;
40
45
} , [ ] ) ;
41
46
42
- const onMonthChange = useCallback ( ( item : WheelPickerValue , _ : number ) => {
43
- setMonth ( item ) ;
44
- } , [ ] ) ;
45
-
46
- const onYearChange = useCallback ( ( item : WheelPickerValue , _ : number ) => {
47
- setYear ( item ) ;
48
- } , [ ] ) ;
49
-
50
- const onDaysChange = useCallback ( ( item : WheelPickerValue , _ : number ) => {
51
- setDays ( item ) ;
52
- } , [ ] ) ;
53
-
54
- return {
55
- onMonthChange,
56
- onYearChange,
57
- onDaysChange,
58
- selectedMonth,
59
- selectedDays,
60
- onPickDaysPress,
61
- onDialogDismissed,
62
- showDialog
63
- } ;
64
- } ;
65
-
66
- export default ( ) => {
67
- const {
68
- selectedMonth,
69
- onMonthChange,
70
- onYearChange,
71
- selectedDays,
72
- onDaysChange,
73
- onPickDaysPress,
74
- onDialogDismissed,
75
- showDialog
76
- } = useData ( 'February' , undefined , 5 ) ;
77
-
78
47
return (
79
48
< View flex padding-page >
80
49
< Text h1 > Wheel Picker</ Text >
81
50
82
- < View marginT-s5 centerH >
83
- < Text h3 > Months</ Text >
84
- < Incubator . WheelPicker
85
- onChange = { onMonthChange }
86
- activeTextColor = { Colors . primary }
87
- inactiveTextColor = { Colors . grey20 }
88
- items = { monthItems }
89
- textStyle = { Typography . text60R }
90
- selectedValue = { selectedMonth }
91
- />
51
+ < View row center marginT-30 >
52
+ < View center >
53
+ < Text h3 > Months</ Text >
54
+ < Incubator . WheelPicker
55
+ initialValue = { 'February' }
56
+ activeTextColor = { Colors . primary }
57
+ inactiveTextColor = { Colors . grey20 }
58
+ items = { monthItems }
59
+ textStyle = { Typography . text60R }
60
+ numberOfVisibleRows = { 3 }
61
+ />
62
+ </ View >
92
63
93
- < Text h3 > Years</ Text >
94
- < Text bodySmall grey30 >
95
- (Uncontrolled, initialValue passed)
96
- </ Text >
97
- < View width = { '100%' } marginT-s3 >
64
+ < View center >
65
+ < Text h3 > Years</ Text >
98
66
< Incubator . WheelPicker
99
- onChange = { onYearChange }
100
- numberOfVisibleRows = { 3 }
101
- initialValue = { 2021 }
67
+ numberOfVisibleRows = { 3 }
68
+ initialValue = { yearsValue }
102
69
items = { yearItems }
70
+ onChange = { onChange }
103
71
/>
104
72
</ View >
105
73
</ View >
106
74
107
- < View marginB-s10 >
108
- < Button marginT-40 label = { 'Pick Days' } marginH-100 onPress = { onPickDaysPress } />
75
+ < View center marginT-30 >
76
+ < Text body >
77
+ Move the wheel programmatically
78
+ </ Text >
79
+ < Text bodySmall grey30 >
80
+ (by updating the initialValue prop)
81
+ </ Text >
82
+ < View marginT-10 row >
83
+ < Button size = "medium" label = { 'Previous' } marginR-20 onPress = { ( ) => updateYearsInitialValue ( false ) } />
84
+ < Button size = "medium" label = { 'Next' } onPress = { ( ) => updateYearsInitialValue ( true ) } />
85
+ </ View >
86
+ </ View >
87
+
88
+ < View center marginT-40 >
89
+ < Text h3 marginB-20 > Days</ Text >
90
+ < Button size = "small" label = { 'Pick Days' } onPress = { onPickDaysPress } />
109
91
< Dialog width = { '90%' } height = { 260 } bottom visible = { showDialog } onDismiss = { onDialogDismissed } >
110
- < Incubator . WheelPicker onChange = { onDaysChange } selectedValue = { selectedDays } label = { 'Days' } items = { dayItems } />
92
+ < Incubator . WheelPicker initialValue = { 5 } label = { 'Days' } items = { dayItems } />
111
93
</ Dialog >
112
94
</ View >
113
95
</ View >
0 commit comments