Skip to content

Commit

Permalink
feat: support border-radius on cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
huhuanming committed Oct 11, 2024
1 parent 750a406 commit 5cc676c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
8 changes: 6 additions & 2 deletions src/PageContentView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default class PageContentView extends Component<PageContentViewProps> {
},
],
{
useNativeDriver: true,
useNativeDriver: false,
}
);
private scrollView: RefObject<ContentFlatList> | null = React.createRef();
Expand All @@ -91,7 +91,11 @@ export default class PageContentView extends Component<PageContentViewProps> {
},
} = event;
const reloadWidth = PixelRatio.roundToNearestPixel(width);
if (reloadWidth !== this.state.scrollViewWidth && reloadWidth > 0) {
if (
reloadWidth !== this.state.scrollViewWidth &&
reloadWidth > 0 &&
(this?.props?.data?.length ?? 0) > 0
) {
this._scrollViewWidthValue.setValue(reloadWidth);
this.setState({ scrollViewWidth: reloadWidth }, () => {
this?.scrollView?.current?.reloadScrollContainerWidth?.(reloadWidth);
Expand Down
12 changes: 6 additions & 6 deletions src/PageHeaderCursor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class PageHeaderCursor extends Component<PageHeaderCursorProps> {
const width = item.width - left - right;
return isWidth
? (width * Number(percentWidth ?? 100)) / 100
: item.x + width / 2.0 + left;
: item.x + left;
}
} else {
return 0;
Expand Down Expand Up @@ -113,11 +113,11 @@ export default class PageHeaderCursor extends Component<PageHeaderCursorProps> {
override render() {
const fixCursorWidth = this._findFixCursorWidth();
const translateX = this._reloadPageIndexValue(false);
const scaleX = this._reloadPageIndexValue(true);
const widthX = this._reloadPageIndexValue(true);

const containerStyle: Animated.WithAnimatedObject<any> = {
transform: [{ translateX }, fixCursorWidth ? { scale: 1 } : { scaleX }],
width: fixCursorWidth ?? 1,
transform: [{ translateX }],
width: fixCursorWidth ?? widthX,
position: 'absolute',
top: 0,
bottom: 0,
Expand All @@ -127,7 +127,7 @@ export default class PageHeaderCursor extends Component<PageHeaderCursorProps> {

const contentStyle = [
this.props.cursorStyle,
{ left: null, right: null, width: fixCursorWidth ?? 1 },
{ left: null, right: null, width: fixCursorWidth ?? widthX },
];

return (
Expand All @@ -149,7 +149,7 @@ export default class PageHeaderCursor extends Component<PageHeaderCursorProps> {
{this.props.renderCursor ? (
this.props.renderCursor()
) : (
<View style={contentStyle}></View>
<Animated.View style={contentStyle}></Animated.View>
)}
</Animated.View>
</View>
Expand Down
29 changes: 18 additions & 11 deletions src/PageHeaderView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { Component } from 'react';
import type { RefObject } from 'react';
import {
View,
StyleSheet,
Pressable,
ScrollView,
Animated,
Expand Down Expand Up @@ -34,24 +33,28 @@ interface PageHeaderViewProps extends ScrollViewProps {
scrollContainerStyle?: object;
contentContainerStyle?: object;
containerStyle?: object;
itemContainerSelectedStyle?: object;
}

export default class PageHeaderView extends Component<PageHeaderViewProps> {
private scrollPageIndex = this.props.initialScrollIndex ?? 0;
private scrollPageIndexValue = new Animated.Value(this.scrollPageIndex);
private scrollPageIndexValue = new Animated.Value(this.scrollPageIndex, {
useNativeDriver: false,
});
private nextScrollPageIndex = -1;
private shouldHandlerAnimationValue = true;
private itemConfigList = this.props.data.map((_: any) => ({
_animtedEnabledValue: new Animated.Value(1),
_containerRef: React.createRef<View>(),
}));
private itemContainerStyle = () =>
private itemContainerStyle = (index?: number) =>
({
height: '100%',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
...this.props.itemContainerStyle,
...(this.props.itemContainerSelectedStyle)
} as { flex?: number } & object);
private itemTitleNormalStyle: () => {
fontSize: number;
Expand Down Expand Up @@ -80,6 +83,7 @@ export default class PageHeaderView extends Component<PageHeaderViewProps> {
});
private scrollView: RefObject<ScrollView> = React.createRef();
private cursor: RefObject<PageHeaderCursor> = React.createRef();
private scrollViewWidth = 0;

static defaultProps: PageHeaderViewProps = {
data: [],
Expand Down Expand Up @@ -128,7 +132,7 @@ export default class PageHeaderView extends Component<PageHeaderViewProps> {
const itemLayout = this?.cursor?.current?.state
?.itemContainerLayoutList?.[newPageIndex] ?? { x: 0, width: 0 };
this?.scrollView?.current?.scrollTo({
x: itemLayout.x - itemLayout.width,
x: itemLayout.x + itemLayout.width / 2.0 - this.scrollViewWidth / 2.0,
});
this.scrollPageIndex = newPageIndex;
}
Expand Down Expand Up @@ -217,7 +221,7 @@ export default class PageHeaderView extends Component<PageHeaderViewProps> {
this.scrollPageIndexValue,
index,
this.props.selectedPageIndexDuration,
true
false
);
}
this.props.onSelectedPageIndex?.(index);
Expand All @@ -232,7 +236,7 @@ export default class PageHeaderView extends Component<PageHeaderViewProps> {
<Pressable
key={index}
ref={this?.itemConfigList?.[index]?._containerRef}
style={this.itemContainerStyle()}
style={this.itemContainerStyle(index)}
onPress={() => this._itemDidTouch(item, index)}
>
{content}
Expand Down Expand Up @@ -265,8 +269,9 @@ export default class PageHeaderView extends Component<PageHeaderViewProps> {
}

override render() {
const { style, ...restProps } = this.props;
return (
<View style={this.props.style}>
<View style={style}>
<ScrollView
onContentSizeChange={(width, height) => {
this?.cursor?.current?.reloadItemListContainerLayout(
Expand All @@ -275,16 +280,18 @@ export default class PageHeaderView extends Component<PageHeaderViewProps> {
);
this?.props?.onContentSizeChange?.(width, height);
}}
{...this.props}
onLayout={(event) => {
this.scrollViewWidth = event.nativeEvent.layout.width;
this?.props?.onLayout?.(event);
}}
style={this.props.containerStyle}
{...restProps}
contentContainerStyle={[
{ width: this.itemContainerStyle()?.flex ? '100%' : null },
this.props.scrollContainerStyle,
]}
ref={this.scrollView}
>
<View
style={[StyleSheet.absoluteFill, this.props.containerStyle]}
></View>
<View
style={[
{ minWidth: '100%', flexDirection: 'row', alignItems: 'center' },
Expand Down

0 comments on commit 5cc676c

Please sign in to comment.