From bf1f7d688d731c47273b3f554ccb2f4bbf16e25d Mon Sep 17 00:00:00 2001 From: "d.krokhmal" Date: Sun, 10 Nov 2019 19:59:19 +0300 Subject: [PATCH] update index with new props --- src/index.js | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index 3e7bcf3..ceae532 100644 --- a/src/index.js +++ b/src/index.js @@ -4,39 +4,51 @@ import PropTypes from 'prop-types'; import { useUnmount } from './useUnmount'; -export const CustomRefreshControl = ({ callbackError, callback, callbackParams }) => { +export const CustomRefreshControl = ({ + callbackError, + callback, + callbackParams, + delay, + delayCallback, + controlParams, +}) => { const [refreshing, setRefreshing] = useState(false); - const cancelRefreshing = useCallback(() => setRefreshing(false), []); - const setClearedInterval = useCallback(async () => { + if (delay) { + setTimeout(delayCallback, delay) + } + if (callback) { try { - await callback(callbackParams); + await callback(callbackParams) } catch (e) { - callbackError(e); + callbackError(e) } } - cancelRefreshing(); - }, [callback, callbackError, callbackParams, cancelRefreshing]); + cancelRefreshing() + }, [callback, callbackError, callbackParams, cancelRefreshing, delay, delayCallback]); const handleOnRefresh = useCallback(async () => { setRefreshing(true); - await setClearedInterval(); + await setClearedInterval() }, [setClearedInterval]); + const cancelRefreshing = useCallback(() => setRefreshing(false), []); + useUnmount(cancelRefreshing); - return ( - - ) + return }; CustomRefreshControl.defaultProps = { callback: null, callbackError: () => {}, callbackParams: undefined, + controlParams: {}, + delay: null, + delayCallback: () => {}, }; CustomRefreshControl.propTypes = { @@ -47,4 +59,7 @@ CustomRefreshControl.propTypes = { PropTypes.object, PropTypes.oneOf([undefined]), ]), + controlParams: PropTypes.object, + delay: PropTypes.number, + delayCallback: PropTypes.func, };