Skip to content

Commit 9824789

Browse files
authored
Fix/incubator wheel picker layouts (#1296)
* wrap FlatList * fix sectionsWheelPicker * update sectionsWheelPickerScreen
1 parent 933a860 commit 9824789

File tree

3 files changed

+52
-36
lines changed

3 files changed

+52
-36
lines changed

demo/src/screens/componentScreens/SectionsWheelPickerScreen.tsx

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {Alert} from 'react-native';
44
import {Text, View, SectionsWheelPicker, SegmentedControl, Button, Incubator} from 'react-native-ui-lib';
55

66
const SectionsWheelPickerScreen = () => {
7-
const [showMinutes, setShowMinutes] = useState(false);
7+
const [numOfSections, setNumOfSections] = useState(1);
88

99
const [selectedDays, setSelectedDays] = useState(0);
1010
const [selectedHours, setSelectedHours] = useState(0);
@@ -35,7 +35,7 @@ const SectionsWheelPickerScreen = () => {
3535
const hours = selectedHours === 1 ? 'hour' : 'hours';
3636
const minutes = selectedMinutes === 1 ? 'minute' : 'minutes';
3737

38-
showMinutes
38+
numOfSections === 3
3939
? Alert.alert('Your chosen duration is:\n' +
4040
selectedDays +
4141
' ' +
@@ -48,7 +48,9 @@ const SectionsWheelPickerScreen = () => {
4848
selectedMinutes +
4949
' ' +
5050
minutes)
51-
: Alert.alert('Your chosen duration is:\n' + selectedDays + ' ' + days + ' and ' + selectedHours + ' ' + hours);
51+
: numOfSections === 2
52+
? Alert.alert('Your chosen duration is:\n' + selectedDays + ' ' + days + ' and ' + selectedHours + ' ' + hours)
53+
: Alert.alert('Your chosen duration is:\n' + selectedDays + ' ' + days);
5254
};
5355

5456
const onResetPress = () => {
@@ -58,20 +60,33 @@ const SectionsWheelPickerScreen = () => {
5860
};
5961

6062
const sections: Incubator.WheelPickerProps[] = [
61-
{items: getItems(days), onChange: onDaysChange, selectedValue: selectedDays, label: 'Days'},
62-
{items: getItems(hours), onChange: onHoursChange, selectedValue: selectedHours, label: 'Hrs'},
63+
{
64+
items: getItems(days),
65+
onChange: onDaysChange,
66+
selectedValue: selectedDays,
67+
label: 'Days',
68+
style: numOfSections === 1 ? {flex: 1} : {flex: 1, alignItems: 'flex-end'}
69+
},
70+
{
71+
items: getItems(hours),
72+
onChange: onHoursChange,
73+
selectedValue: selectedHours,
74+
label: 'Hrs',
75+
style: numOfSections === 2 ? {flex: 1, alignItems: 'flex-start'} : undefined
76+
},
6377
{
6478
items: getItems(minutes),
6579
onChange: onMinutesChange,
6680
selectedValue: selectedMinutes,
67-
label: 'Mins'
81+
label: 'Mins',
82+
style: {flex: 1, alignItems: 'flex-start'}
6883
}
6984
];
7085

71-
const sectionsToPresent = showMinutes ? sections : _.slice(sections, 0, 2);
86+
const sectionsToPresent = _.slice(sections, 0, numOfSections);
7287

7388
const onChangeIndex = (index: number) => {
74-
return index === 0 ? setShowMinutes(false) : setShowMinutes(true);
89+
return setNumOfSections(index + 1);
7590
};
7691

7792
return (
@@ -80,13 +95,16 @@ const SectionsWheelPickerScreen = () => {
8095
Sections Wheel Picker
8196
</Text>
8297
<View centerH marginT-40>
83-
<SegmentedControl segments={[{label: '2 sections'}, {label: '3 sections'}]} onChangeIndex={onChangeIndex}/>
98+
<SegmentedControl
99+
segments={[{label: '1 section'}, {label: '2 sections'}, {label: '3 sections'}]}
100+
onChangeIndex={onChangeIndex}
101+
/>
84102
<Text text50 marginV-20>
85103
Pick a duration
86104
</Text>
87105
</View>
88106
<SectionsWheelPicker sections={sectionsToPresent}/>
89-
<Button marginH-150 marginT-300 label={'Save'} onPress={onSavePress}/>
107+
<Button marginH-150 marginT-40 label={'Save'} onPress={onSavePress}/>
90108
<Button marginH-150 marginT-15 label={'Reset'} onPress={onResetPress}/>
91109
</View>
92110
);

src/components/sectionsWheelPicker/index.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,11 @@ const SectionsWheelPicker = (props: SectionsWheelPickerProps) => {
5353

5454
const renderSections = () =>
5555
_.map(sections, (section, index) => {
56-
return (
57-
<View height={1} key={index} testID={testID}>
58-
<WheelPicker testID={`${testID}.${index}`} {...wheelPickerProps} {...section}/>
59-
</View>
60-
);
56+
return <WheelPicker key={index} testID={`${testID}.${index}`} {...wheelPickerProps} {...section}/>;
6157
});
6258

6359
return (
64-
<View flex row centerH>
60+
<View row centerH testID={testID}>
6561
{renderSections()}
6662
</View>
6763
);

src/incubator/WheelPicker/index.tsx

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import React, {useCallback, useRef, useMemo, useEffect, useState} from 'react';
33
import {TextStyle, ViewStyle, FlatList, NativeSyntheticEvent, NativeScrollEvent, StyleSheet} from 'react-native';
44
import Animated from 'react-native-reanimated';
55
import {onScrollEvent, useValues} from 'react-native-redash';
6-
import {Colors, Spacings} from '../../../src/style';
6+
import {Colors, Spacings} from 'style';
77
import View from '../../components/view';
88
import Fader, {FaderPosition} from '../../components/fader';
9-
import {Constants} from '../../helpers';
9+
import {Constants} from 'helpers';
1010
import Item, {ItemProps} from './Item';
1111
import usePresenter from './usePresenter';
1212
import Text, {TextProps} from '../../components/text';
@@ -168,7 +168,7 @@ const WheelPicker = React.memo(({
168168

169169
const renderLabel = () => {
170170
return (
171-
<View centerV flexG>
171+
<View centerV>
172172
<Text marginL-s2 text80M {...labelProps} color={activeTextColor} style={labelStyle}>
173173
{label}
174174
</Text>
@@ -188,23 +188,25 @@ const WheelPicker = React.memo(({
188188
return (
189189
<View testID={testID} bg-white style={style}>
190190
<View row marginH-s5 centerH>
191-
<AnimatedFlatList
192-
height={height}
193-
data={items}
194-
// @ts-ignore reanimated2
195-
keyExtractor={keyExtractor}
196-
scrollEventThrottle={100}
197-
onScroll={onScroll}
198-
onMomentumScrollEnd={onValueChange}
199-
showsVerticalScrollIndicator={false}
200-
onLayout={scrollToPassedIndex}
201-
// @ts-ignore
202-
ref={scrollView}
203-
contentContainerStyle={contentContainerStyle}
204-
snapToInterval={itemHeight}
205-
decelerationRate={Constants.isAndroid ? 0.98 : 'normal'}
206-
renderItem={renderItem}
207-
/>
191+
<View>
192+
<AnimatedFlatList
193+
height={height}
194+
data={items}
195+
// @ts-ignore reanimated2
196+
keyExtractor={keyExtractor}
197+
scrollEventThrottle={100}
198+
onScroll={onScroll}
199+
onMomentumScrollEnd={onValueChange}
200+
showsVerticalScrollIndicator={false}
201+
onLayout={scrollToPassedIndex}
202+
// @ts-ignore
203+
ref={scrollView}
204+
contentContainerStyle={contentContainerStyle}
205+
snapToInterval={itemHeight}
206+
decelerationRate={Constants.isAndroid ? 0.98 : 'normal'}
207+
renderItem={renderItem}
208+
/>
209+
</View>
208210
{label && renderLabel()}
209211
</View>
210212
{fader(FaderPosition.BOTTOM)}

0 commit comments

Comments
 (0)