@antmjs/vantui
Version:
一套适用于Taro3及React的vantui组件库
150 lines (148 loc) • 5.47 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
var _excluded = ["time", "format", "autoStart", "millisecond", "onChange", "onFinish", "children", "style", "className"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import { useEffect, useState, useRef, forwardRef, useImperativeHandle, useCallback } from 'react';
import { View } from '@tarojs/components';
import { get } from '../default-props';
import { isSameSecond, parseFormat, parseTimeData } from './util';
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsx as _jsx } from "react/jsx-runtime";
var d = get().CountDown;
function simpleTick(fn) {
return setTimeout(fn, 30);
}
function Index(props, ref) {
var _ref = useRef({
tid: null,
counting: false
});
var _useState = useState({
formattedTime: '0'
}),
_useState2 = _slicedToArray(_useState, 2),
state = _useState2[0],
setState = _useState2[1];
var formattedTime = state.formattedTime;
var _d$props = _objectSpread(_objectSpread({}, d), props),
time = _d$props.time,
_d$props$format = _d$props.format,
format = _d$props$format === void 0 ? 'HH:mm:ss' : _d$props$format,
_d$props$autoStart = _d$props.autoStart,
autoStart = _d$props$autoStart === void 0 ? true : _d$props$autoStart,
millisecond = _d$props.millisecond,
onChange = _d$props.onChange,
onFinish = _d$props.onFinish,
children = _d$props.children,
style = _d$props.style,
className = _d$props.className,
others = _objectWithoutProperties(_d$props, _excluded);
// 暂停
var _pause = useCallback(function () {
_ref.current.counting = false;
clearTimeout(_ref.current.tid);
}, []);
var getRemain = useCallback(function () {
return Math.max(_ref.current.endTime - Date.now(), 0);
}, []);
var setRemain = useCallback(function (remain) {
_ref.current.remain = remain;
var timeData = parseTimeData(remain);
if (children) {
onChange === null || onChange === void 0 ? void 0 : onChange({
detail: timeData
});
}
setState(function (pre) {
return _objectSpread(_objectSpread({}, pre), {}, {
formattedTime: parseFormat(format, timeData)
});
});
if (remain === 0) {
_pause();
onFinish === null || onFinish === void 0 ? void 0 : onFinish();
}
}, [children, format, onChange, onFinish, _pause]);
var microTick = useCallback(function () {
_ref.current.tid = simpleTick(function () {
setRemain(getRemain());
if (_ref.current.remain !== 0) {
microTick();
}
});
}, [getRemain, setRemain]);
var macroTick = useCallback(function () {
_ref.current.tid = simpleTick(function () {
var remain = getRemain();
if (!isSameSecond(remain, _ref.current.remain) || remain === 0) {
setRemain(remain);
}
if (_ref.current.remain !== 0) {
macroTick();
}
});
}, [getRemain, setRemain]);
var tick = useCallback(function () {
if (millisecond) {
microTick();
} else {
macroTick();
}
}, [macroTick, microTick, millisecond]);
// 开始
var _start = useCallback(function () {
if (_ref.current.counting) {
return;
}
_ref.current.counting = true;
_ref.current.endTime = Date.now() + _ref.current.remain;
tick();
}, [tick]);
// 重置
var _reset = useCallback(function () {
_pause();
_ref.current.remain = time;
setRemain(_ref.current.remain);
if (autoStart) {
_start();
}
}, [autoStart, _pause, setRemain, _start, time]);
useImperativeHandle(ref, function () {
return {
start: function start() {
_start();
},
pause: function pause() {
_pause();
},
reset: function reset() {
_reset();
}
};
});
useEffect(function () {
_reset();
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[time]);
useEffect(function () {
var _refCurrent = _ref.current;
return function () {
_refCurrent.tid && clearTimeout(_refCurrent.tid);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return /*#__PURE__*/_jsx(View, _objectSpread(_objectSpread({
className: "van-count-down ".concat(className),
style: style
}, others), {}, {
children: children ? children : /*#__PURE__*/_jsx(_Fragment, {
children: formattedTime
})
}));
}
var CountDown = /*#__PURE__*/forwardRef(Index);
export { CountDown };
export default CountDown;