Skip to content

Commit ecbc6e0

Browse files
committed
change tab controller center selected logic (and move it to a helper hook)
1 parent 6b907ce commit ecbc6e0

File tree

5 files changed

+386
-212
lines changed

5 files changed

+386
-212
lines changed

demo/src/screens/componentScreens/TabControllerScreen/index.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,15 @@ class TabControllerScreen extends Component<{}, State> {
3535
this.state.items = this.generateTabItems();
3636
}
3737

38-
generateTabItems = (fewItems = this.state.fewItems, centerSelected = this.state.centerSelected): TabControllerItemProps[] => {
38+
generateTabItems = (fewItems = this.state.fewItems): TabControllerItemProps[] => {
3939
let items: TabControllerItemProps[] = _.chain(TABS)
4040
.take(fewItems ? 3 : TABS.length)
4141
.map<TabControllerItemProps>(tab => ({label: tab, key: tab}))
4242
.value();
4343

4444
const addItem: TabControllerItemProps = {icon: Assets.icons.demo.add, key: 'add', ignore: true, width: 60, onPress: this.onAddItem};
4545

46-
if (!centerSelected) {
47-
items = [...items, addItem];
48-
}
49-
return items;
46+
return [...items, addItem];
5047
};
5148

5249
componentDidMount() {
@@ -83,7 +80,7 @@ class TabControllerScreen extends Component<{}, State> {
8380
toggleCenterSelected = () => {
8481
const {fewItems, centerSelected} = this.state;
8582
this.setState({
86-
items: this.generateTabItems(fewItems, !centerSelected),
83+
items: this.generateTabItems(fewItems),
8784
centerSelected: !centerSelected,
8885
key: Date.now()
8986
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { RefObject } from 'react';
2+
import { LayoutChangeEvent, ScrollView, FlatList, NativeSyntheticEvent, NativeScrollEvent } from 'react-native';
3+
export declare enum OffsetType {
4+
CENTER = "CENTER",
5+
DYNAMIC = "DYNAMIC",
6+
LEFT = "LEFT",
7+
RIGHT = "RIGHT"
8+
}
9+
export declare type Props = {
10+
/**
11+
* A reference to the ScrollView (or FlatList) which the items are in
12+
*/
13+
scrollViewRef: RefObject<ScrollView | FlatList>;
14+
/**
15+
* The number of items
16+
*/
17+
itemsCount: number;
18+
/**
19+
* The selected item's index
20+
*/
21+
selectedIndex?: number;
22+
/**
23+
* Where would the item be located (default to CENTER)
24+
*/
25+
offsetType?: OffsetType;
26+
/**
27+
* Add a margin to the offset (default to true)
28+
* This gives a better UX
29+
* Not relevant to OffsetType.CENTER
30+
*/
31+
addOffsetMargin?: boolean;
32+
/**
33+
* How much space (padding \ margin) is there on he left\right of the items
34+
*/
35+
sideSpacing?: number;
36+
/**
37+
* How much space (padding \ margin) is there between each item
38+
*/
39+
innerSpacing?: number;
40+
};
41+
export declare type ResultProps = {
42+
/**
43+
* This should be called by each ot the items' onLayout
44+
*/
45+
onItemLayout: (event: LayoutChangeEvent, index: number) => void;
46+
/**
47+
* This should be called by the ScrollView (or FlatList)
48+
* If this is not used OffsetType.DYNAMIC will not work properly
49+
*/
50+
onScroll: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
51+
/**
52+
* The items' width
53+
*/
54+
itemsWidths: number[];
55+
/**
56+
* Use in order to focus the item with the specified index
57+
*/
58+
focusIndex: (index: number, animated?: boolean) => void;
59+
};
60+
declare const focusItemsHelper: (props: Props) => ResultProps;
61+
export default focusItemsHelper;

0 commit comments

Comments
 (0)