1+ import _ from 'lodash' ;
12import React , { useCallback , useState } from 'react' ;
23import { View , Text , Incubator , Colors , Typography , Button , Dialog } from 'react-native-ui-lib' ;
3- import _ from 'lodash' ;
4-
5- type WheelPickerValue = Incubator . WheelPickerProps [ 'initialValue' ] ;
64
75const monthItems = _ . map ( [
86 'January' ,
@@ -18,18 +16,25 @@ const monthItems = _.map([
1816 'November' ,
1917 'December'
2018] ,
21- item => ( { label : item , value : item } ) ) ;
19+ item => ( { label : item , value : item , align : Incubator . WheelPickerAlign . RIGHT } ) ) ;
2220
23- const yearItems = _ . times ( 2030 , i => i )
21+ const yearItems = _ . times ( 2050 , i => i )
2422 . reverse ( )
2523 . map ( item => ( { label : `${ item } ` , value : item } ) ) ;
2624const dayItems = _ . times ( 31 , i => i + 1 ) . map ( day => ( { label : `${ day } ` , value : day } ) ) ;
2725
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 ( ) => {
3227 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+ } , [ ] ) ;
3338
3439 const onPickDaysPress = useCallback ( ( ) => {
3540 setShowDialog ( true ) ;
@@ -39,75 +44,52 @@ const useData = (initialMonth?: string, initialYear?: string, initialDays?: numb
3944 setShowDialog ( false ) ;
4045 } , [ ] ) ;
4146
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-
7847 return (
7948 < View flex padding-page >
8049 < Text h1 > Wheel Picker</ Text >
8150
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 >
9263
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 >
9866 < Incubator . WheelPicker
99- onChange = { onYearChange }
100- numberOfVisibleRows = { 3 }
101- initialValue = { 2021 }
67+ numberOfVisibleRows = { 3 }
68+ initialValue = { yearsValue }
10269 items = { yearItems }
70+ onChange = { onChange }
10371 />
10472 </ View >
10573 </ View >
10674
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 } />
10991 < 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 } />
11193 </ Dialog >
11294 </ View >
11395 </ View >
0 commit comments