Skip to content

Commit 228e9d9

Browse files
authored
useScrollToItem - add offsetType to statics (#1144)
* useScrollToItem - add offsetType to statics * Fix dynamic focusing when there's inner/outer spacing * Fix doc for focusIndex
1 parent 6535f87 commit 228e9d9

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

generatedTypes/hooks/useScrollToItem/index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,8 @@ export declare type ScrollToItemResultProps<T extends ScrollToSupportedViews> =
4848
*/
4949
focusIndex: (index: number, animated?: boolean) => void;
5050
};
51-
declare const useScrollToItem: <T extends ScrollToSupportedViews>(props: ScrollToItemProps<T>) => ScrollToItemResultProps<T>;
51+
declare const useScrollToItem: {
52+
<T extends ScrollToSupportedViews>(props: ScrollToItemProps<T>): ScrollToItemResultProps<T>;
53+
offsetType: typeof OffsetType;
54+
};
5255
export default useScrollToItem;

src/components/tabController/TabBar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {Constants} from '../../helpers';
1414
import {LogService} from '../../services';
1515
import FadedScrollView from './FadedScrollView';
1616

17-
import useScrollToItem, {OffsetType} from '../../hooks/useScrollToItem';
17+
import {useScrollToItem} from '../../hooks';
1818

1919
const {Code, Value, interpolate, block, set} = Reanimated;
2020

@@ -201,7 +201,7 @@ const TabBar = (props: Props) => {
201201
const {scrollViewRef: tabBar, onItemLayout, itemsWidths, focusIndex} = useScrollToItem({
202202
itemsCount: itemsCount.current,
203203
selectedIndex,
204-
offsetType: centerSelected ? OffsetType.CENTER : OffsetType.DYNAMIC
204+
offsetType: centerSelected ? useScrollToItem.offsetType.CENTER : useScrollToItem.offsetType.DYNAMIC
205205
});
206206

207207
const indicatorOffsets = useMemo((): number[] => {

src/hooks/useScrollToItem/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export type ScrollToItemResultProps<T extends ScrollToSupportedViews> = Pick<Scr
5757
*/
5858
itemsWidths: number[];
5959
/**
60-
* Use in order to focus the item with the specified index
60+
* Use in order to focus the item with the specified index (use when the selectedIndex is not changed)
6161
*/
6262
focusIndex: (index: number, animated?: boolean) => void;
6363
};
@@ -95,22 +95,22 @@ const useScrollToItem = <T extends ScrollToSupportedViews>(props: ScrollToItemPr
9595
const centeredOffsets = [];
9696
let currentCenterOffset = outerSpacing;
9797
const leftOffsets = [];
98-
leftOffsets.push(0);
98+
leftOffsets.push(outerSpacing - innerSpacing);
9999
const rightOffsets = [];
100-
rightOffsets.push(-Constants.screenWidth + itemsWidths[0]);
100+
rightOffsets.push(-Constants.screenWidth + itemsWidths[0] + outerSpacing + innerSpacing);
101101
while (index < itemsCount) {
102102
centeredOffsets[index] = currentCenterOffset - screenCenter + itemsWidths[index] / 2;
103103
++index;
104104
currentCenterOffset += itemsWidths[index - 1] + innerSpacing;
105-
leftOffsets[index] = leftOffsets[index - 1] + itemsWidths[index - 1];
106-
rightOffsets[index] = rightOffsets[index - 1] + itemsWidths[index];
105+
leftOffsets[index] = leftOffsets[index - 1] + itemsWidths[index - 1] + innerSpacing;
106+
rightOffsets[index] = rightOffsets[index - 1] + itemsWidths[index] + innerSpacing;
107107
}
108108

109109
if (addOffsetMargin) {
110-
index = 1;
110+
index = 1;
111111
while (index < itemsCount - 1) {
112112
leftOffsets[index] -= itemsWidths[index - 1];
113-
rightOffsets[index] += itemsWidths[index + 1];
113+
rightOffsets[index] += itemsWidths[index + 1] + innerSpacing;
114114
++index;
115115
}
116116
}
@@ -155,4 +155,6 @@ const useScrollToItem = <T extends ScrollToSupportedViews>(props: ScrollToItemPr
155155
};
156156
};
157157

158+
useScrollToItem.offsetType = OffsetType;
159+
158160
export default useScrollToItem;

0 commit comments

Comments
 (0)