Skip to content

Commit

Permalink
feat: add swiper ref
Browse files Browse the repository at this point in the history
  • Loading branch information
taoyage committed Jul 3, 2022
1 parent f7210d6 commit b6fc9df
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@taoyage/react-mobile-ui",
"version": "1.0.7",
"version": "1.0.8",
"description": "A react mobile components lib",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
22 changes: 22 additions & 0 deletions packages/hooks/useUpdateEffect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';

const useUpdateEffect = (callback: React.EffectCallback, deep?: React.DependencyList) => {
const isMounted = React.useRef<boolean>(false);

React.useEffect(() => {
return () => {
isMounted.current = false;
};
}, []);

React.useEffect(() => {
if (!isMounted.current) {
isMounted.current = true;
} else {
callback();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deep);
};

export default useUpdateEffect;
2 changes: 1 addition & 1 deletion packages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export { default as Tabs } from '@/tabs';
export type { TabsProps, TabProps } from '@/tabs';

export { default as Swiper } from '@/swiper';
export type { SwiperProps, SwiperItemProps } from '@/swiper';
export type { SwiperProps, SwiperItemProps, SwiperRef } from '@/swiper';

export { default as Grid } from '@/grid';
export type { GridProps, GridItemProps } from '@/grid';
2 changes: 1 addition & 1 deletion packages/swiper/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import InternalSwiper from './swiper';
import SwiperItem from './swiper-item';

export type { SwiperProps } from './swiper';
export type { SwiperProps, SwiperRef } from './swiper';
export type { SwiperItemProps } from './swiper-item';

type InternalSwiperType = typeof InternalSwiper;
Expand Down
22 changes: 20 additions & 2 deletions packages/swiper/swiper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ export interface SwiperProps {
children: React.ReactElement | React.ReactElement[];
showIndicator?: boolean;
indicatorClassName?: string;
onIndexChange?: (index: number) => void;
}

const Swiper: React.FC<SwiperProps> = React.memo((props) => {
export interface SwiperRef {
swipeTo: (index: number) => void;
swipeNext: () => void;
swipePrev: () => void;
}

const Swiper = React.forwardRef<SwiperRef, SwiperProps>((props, ref) => {
const [currentIndex, setCurrentIndex] = React.useState<number>(props.defaultIndex || 0);
const [dragging, setDragging] = React.useState<boolean>(false);

Expand Down Expand Up @@ -80,10 +87,15 @@ const Swiper: React.FC<SwiperProps> = React.memo((props) => {
(index: number) => {
const targetIndex = modulus(index, count);
setCurrentIndex(targetIndex);
props.onIndexChange?.(targetIndex);
},
[count]
[count, props]
);

const swipePrev = React.useCallback(() => {
swipeTo(currentIndex - 1);
}, [currentIndex, swipeTo]);

const swipeNext = React.useCallback(() => {
swipeTo(currentIndex + 1);
}, [swipeTo, currentIndex]);
Expand Down Expand Up @@ -131,6 +143,12 @@ const Swiper: React.FC<SwiperProps> = React.memo((props) => {
[onTouchEnd, onTouchMove]
);

React.useImperativeHandle(ref, () => ({
swipeTo,
swipeNext,
swipePrev,
}));

React.useEffect(() => {
if (!props.autoplay || dragging) return;
intervalRef.current = window.setInterval(() => {
Expand Down
6 changes: 5 additions & 1 deletion packages/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ const Tabs: React.FC<TabsProps> = React.memo((props) => {
{panes.map(
(child) =>
activeKey === child.key && (
<div key={child.key} className={cx(`${classPrefix}-content`, props.tabContentClassName)}>
<div
key={child.key}
className={cx(`${classPrefix}-content`, props.tabContentClassName)}
style={{ display: activeKey === child.key ? 'block' : 'none' }}
>
{child}
</div>
)
Expand Down

0 comments on commit b6fc9df

Please sign in to comment.