Skip to content

Commit 201cb5f

Browse files
feat(Swiper): add touchable props (#565)
* feat(Swiper): add touchable props * feat(NoticeBar): add --td-notice-bar-height * chore: update _common * chore: update snapshot --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent adf5bf9 commit 201cb5f

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

src/_common

src/notice-bar/NoticeBar.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import useDefaultProps from '../hooks/useDefaultProps';
1414
import { noticeBarDefaultProps } from './defaultProps';
1515
import noop from '../_util/noop';
1616

17-
export interface NoticeBarProps extends TdNoticeBarProps, StyledProps {}
17+
export interface NoticeBarProps extends TdNoticeBarProps, StyledProps {
18+
touchable?: Boolean;
19+
}
1820

1921
type IconType = ReturnType<typeof InfoCircleFilledIcon>;
2022

@@ -108,6 +110,7 @@ const NoticeBar: React.FC<NoticeBarProps> = (props) => {
108110
theme = 'info',
109111
visible,
110112
defaultVisible,
113+
touchable = false,
111114
onClick,
112115
} = useDefaultProps(props, noticeBarDefaultProps);
113116

@@ -179,7 +182,7 @@ const NoticeBar: React.FC<NoticeBarProps> = (props) => {
179182
setTimeout(() => {
180183
const listDOMWidth = listDOM.current?.getBoundingClientRect().width;
181184
const itemDOMWidth = itemDOM.current?.getBoundingClientRect().width;
182-
if (itemDOMWidth > listDOMWidth) {
185+
if (marquee || itemDOMWidth > listDOMWidth) {
183186
updateAnimationFrame({
184187
offset: -itemDOMWidth,
185188
duration: itemDOMWidth / animationSettingValue.current.scroll.speed,
@@ -273,7 +276,8 @@ const NoticeBar: React.FC<NoticeBarProps> = (props) => {
273276
loop
274277
direction={direction}
275278
duration={2000}
276-
height={22}
279+
touchable={touchable}
280+
style={{ height: 'var(--td-notice-bar-height, 22px)' }}
277281
>
278282
{content.map((item, index) => (
279283
<SwiperItem key={index}>

src/swiper/Swiper.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import forwardRefWithStatics from '../_util/forwardRefWithStatics';
1010

1111
export interface SwiperProps extends TdSwiperProps, StyledProps {
1212
children?: React.ReactNode;
13+
touchable?: Boolean;
1314
}
1415

1516
const Swiper = forwardRefWithStatics(
@@ -29,6 +30,7 @@ const Swiper = forwardRefWithStatics(
2930
className,
3031
style,
3132
children,
33+
touchable = true, // 是否可以通过手势滑动
3234
} = props;
3335

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

182-
/** ******************************************************************
183-
* 触摸事件处理方法
184-
*/
185184
// 触摸滑动事件 - 开始
186185
const handleTouchStart = (e: React.TouchEvent) => {
186+
if (
187+
!touchable ||
188+
// avoid resetting position on multi-finger touch
189+
e.touches.length > 1
190+
)
191+
return;
192+
187193
e.stopPropagation();
188194
isHovering.current = true;
189195
clearTimer();
@@ -197,6 +203,7 @@ const Swiper = forwardRefWithStatics(
197203
// 触摸滑动事件 - 滑动中
198204
const handleTouchMove = useCallback(
199205
(e: React.TouchEvent) => {
206+
if (!touchable) return;
200207
e.stopPropagation();
201208

202209
if (moveStartSite) {
@@ -221,11 +228,12 @@ const Swiper = forwardRefWithStatics(
221228
}
222229
}
223230
},
224-
[setTouchMoveDistance, moveStartSite, direction, currentIndex, loop, childrenLength],
231+
[setTouchMoveDistance, moveStartSite, direction, currentIndex, loop, childrenLength, touchable],
225232
);
226233

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

0 commit comments

Comments
 (0)