Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Swiper): add touchable props #565

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/_common
10 changes: 7 additions & 3 deletions src/notice-bar/NoticeBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import useDefaultProps from '../hooks/useDefaultProps';
import { noticeBarDefaultProps } from './defaultProps';
import noop from '../_util/noop';

export interface NoticeBarProps extends TdNoticeBarProps, StyledProps {}
export interface NoticeBarProps extends TdNoticeBarProps, StyledProps {
touchable?: Boolean;
}

type IconType = ReturnType<typeof InfoCircleFilledIcon>;

Expand Down Expand Up @@ -108,6 +110,7 @@ const NoticeBar: React.FC<NoticeBarProps> = (props) => {
theme = 'info',
visible,
defaultVisible,
touchable = false,
onClick,
} = useDefaultProps(props, noticeBarDefaultProps);

Expand Down Expand Up @@ -179,7 +182,7 @@ const NoticeBar: React.FC<NoticeBarProps> = (props) => {
setTimeout(() => {
const listDOMWidth = listDOM.current?.getBoundingClientRect().width;
const itemDOMWidth = itemDOM.current?.getBoundingClientRect().width;
if (itemDOMWidth > listDOMWidth) {
if (marquee || itemDOMWidth > listDOMWidth) {
updateAnimationFrame({
offset: -itemDOMWidth,
duration: itemDOMWidth / animationSettingValue.current.scroll.speed,
Expand Down Expand Up @@ -273,7 +276,8 @@ const NoticeBar: React.FC<NoticeBarProps> = (props) => {
loop
direction={direction}
duration={2000}
height={22}
touchable={touchable}
style={{ height: 'var(--td-notice-bar-height, 22px)' }}
>
{content.map((item, index) => (
<SwiperItem key={index}>
Expand Down
16 changes: 12 additions & 4 deletions src/swiper/Swiper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import forwardRefWithStatics from '../_util/forwardRefWithStatics';

export interface SwiperProps extends TdSwiperProps, StyledProps {
children?: React.ReactNode;
touchable?: Boolean;
}

const Swiper = forwardRefWithStatics(
Expand All @@ -29,6 +30,7 @@ const Swiper = forwardRefWithStatics(
className,
style,
children,
touchable = true, // 是否可以通过手势滑动
} = props;

const { classPrefix } = useConfig();
Expand Down Expand Up @@ -179,11 +181,15 @@ const Swiper = forwardRefWithStatics(
}, duration + 50); // 多 50ms 的间隔时间防止动画未执行完就跳转了
}, [currentIndex, swiperItemLength, duration, direction]);

/** ******************************************************************
* 触摸事件处理方法
*/
// 触摸滑动事件 - 开始
const handleTouchStart = (e: React.TouchEvent) => {
if (
!touchable ||
// avoid resetting position on multi-finger touch
e.touches.length > 1
)
return;

e.stopPropagation();
isHovering.current = true;
clearTimer();
Expand All @@ -197,6 +203,7 @@ const Swiper = forwardRefWithStatics(
// 触摸滑动事件 - 滑动中
const handleTouchMove = useCallback(
(e: React.TouchEvent) => {
if (!touchable) return;
e.stopPropagation();

if (moveStartSite) {
Expand All @@ -221,11 +228,12 @@ const Swiper = forwardRefWithStatics(
}
}
},
[setTouchMoveDistance, moveStartSite, direction, currentIndex, loop, childrenLength],
[setTouchMoveDistance, moveStartSite, direction, currentIndex, loop, childrenLength, touchable],
);

// 触摸滑动事件 - 结束
const handleTouchEnd = (e: React.TouchEvent) => {
if (!touchable) return;
e.stopPropagation();
if (touchMoveDistance / swiperOuterWidth <= -0.3) {
// swiperTo(currentIndex + 1, { source: 'touch' });
Expand Down
Loading
Loading