Compare commits
2 Commits
5082995d1d
...
0f9bc1fb8a
Author | SHA1 | Date | |
---|---|---|---|
0f9bc1fb8a | |||
5e29994ed9 |
1
dist/__test__/index.d.ts
vendored
Normal file
1
dist/__test__/index.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
declare function sumTest(): void;
|
4
dist/__test__/index.js
vendored
Normal file
4
dist/__test__/index.js
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
"use strict";
|
||||||
|
function sumTest() {
|
||||||
|
console.log(1 + 2);
|
||||||
|
}
|
1
dist/__tests__/index.d.ts
vendored
Normal file
1
dist/__tests__/index.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
declare function sumTest(): number;
|
7
dist/__tests__/index.js
vendored
Normal file
7
dist/__tests__/index.js
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
"use strict";
|
||||||
|
function sumTest() {
|
||||||
|
return 1 + 4;
|
||||||
|
}
|
||||||
|
test("test sum with 1 + 4 -> 5", function () {
|
||||||
|
expect(1 + 4).toBe(5);
|
||||||
|
});
|
1
dist/src/index.d.ts
vendored
Normal file
1
dist/src/index.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
export { default as useCountdown } from './useCountdown';
|
8
dist/src/index.js
vendored
Normal file
8
dist/src/index.js
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
"use strict";
|
||||||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||||
|
};
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.useCountdown = void 0;
|
||||||
|
var useCountdown_1 = require("./useCountdown");
|
||||||
|
Object.defineProperty(exports, "useCountdown", { enumerable: true, get: function () { return __importDefault(useCountdown_1).default; } });
|
12
dist/src/useCountdown.d.ts
vendored
Normal file
12
dist/src/useCountdown.d.ts
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
interface useCountdownOptions {
|
||||||
|
now?: () => Date;
|
||||||
|
onEnd?: () => void;
|
||||||
|
onCount?: (timeLeft: number) => void;
|
||||||
|
step?: number;
|
||||||
|
}
|
||||||
|
interface CountdownHookResult {
|
||||||
|
timeleft: number;
|
||||||
|
start: (timeLeft: number) => void;
|
||||||
|
}
|
||||||
|
declare function useCountdown(options?: useCountdownOptions): CountdownHookResult;
|
||||||
|
export default useCountdown;
|
54
dist/src/useCountdown.js
vendored
Normal file
54
dist/src/useCountdown.js
vendored
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
"use strict";
|
||||||
|
var __assign = (this && this.__assign) || function () {
|
||||||
|
__assign = Object.assign || function(t) {
|
||||||
|
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||||||
|
s = arguments[i];
|
||||||
|
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
||||||
|
t[p] = s[p];
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
};
|
||||||
|
return __assign.apply(this, arguments);
|
||||||
|
};
|
||||||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||||
|
};
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
var react_1 = __importDefault(require("react"));
|
||||||
|
var DEFAULT_OPTIONS = {
|
||||||
|
step: 1000,
|
||||||
|
};
|
||||||
|
function useCountdown(options) {
|
||||||
|
var _a = react_1.default.useState(), timeleft = _a[0], setTimeleft = _a[1];
|
||||||
|
var _b = react_1.default.useState(), targetDatetime = _b[0], setTargetDatetime = _b[1];
|
||||||
|
var _c = __assign(__assign({}, DEFAULT_OPTIONS), options), step = _c.step, onCount = _c.onCount, onEnd = _c.onEnd;
|
||||||
|
var start = function (countAsM) {
|
||||||
|
setTimeleft(countAsM);
|
||||||
|
};
|
||||||
|
var varTimeout = null;
|
||||||
|
react_1.default.useEffect(function () {
|
||||||
|
return function () { return clearTimeout(varTimeout); };
|
||||||
|
}, []);
|
||||||
|
react_1.default.useEffect(function () {
|
||||||
|
if (timeleft === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var newTimeleft = timeleft - step;
|
||||||
|
if (newTimeleft < 0) {
|
||||||
|
setTimeleft(0);
|
||||||
|
onEnd === null || onEnd === void 0 ? void 0 : onEnd();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
onCount === null || onCount === void 0 ? void 0 : onCount(timeleft);
|
||||||
|
varTimeout = setTimeout(function () {
|
||||||
|
setTimeleft(newTimeleft);
|
||||||
|
}, step);
|
||||||
|
}
|
||||||
|
}, [timeleft]);
|
||||||
|
var result = {
|
||||||
|
timeleft: timeleft || 0,
|
||||||
|
start: start,
|
||||||
|
};
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
exports.default = useCountdown;
|
@ -6,7 +6,7 @@
|
|||||||
"@testing-library/jest-dom": "^4.2.4",
|
"@testing-library/jest-dom": "^4.2.4",
|
||||||
"@testing-library/react": "^9.3.2",
|
"@testing-library/react": "^9.3.2",
|
||||||
"@testing-library/user-event": "^7.1.2",
|
"@testing-library/user-event": "^7.1.2",
|
||||||
"cubetiq-react-use-countdown": "link:../",
|
"@cubetiq/react-use-countdown": "link:../",
|
||||||
"react": "^16.13.1",
|
"react": "^16.13.1",
|
||||||
"react-dom": "^16.13.1",
|
"react-dom": "^16.13.1",
|
||||||
"react-scripts": "3.4.3"
|
"react-scripts": "3.4.3"
|
||||||
|
7
src/__tests__/index.ts
Normal file
7
src/__tests__/index.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
function sumTest() {
|
||||||
|
return 1 + 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
test(`test sum with 1 + 4 -> 5`, () => {
|
||||||
|
expect(1 + 4).toBe(5);
|
||||||
|
});
|
@ -64,7 +64,7 @@
|
|||||||
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
|
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"src"
|
"src", "src/__tests__"
|
||||||
],
|
],
|
||||||
"exclude": ["node_modules", "**/*.spec.ts"]
|
"exclude": ["node_modules", "**/*.spec.ts"]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user