@sydsoft/base 1.19.0 → 1.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Copyright (c) 2023
3
+ * @author: izzetseydaoglu
4
+ * @last-modified: 29.01.2024 04:09
5
+ */
6
+ export declare function useInterval(callback: any, delay: any): void;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Copyright (c) 2023
3
+ * @author: izzetseydaoglu
4
+ * @last-modified: 29.01.2024 04:09
5
+ */
6
+ // source: https://overreacted.io/making-setinterval-declarative-with-react-hooks/
7
+ import { useEffect, useRef } from "react";
8
+ export function useInterval(callback, delay) {
9
+ var savedCallback = useRef(null);
10
+ // Remember the latest callback.
11
+ useEffect(function () {
12
+ savedCallback.current = callback;
13
+ }, [callback]);
14
+ // Set up the interval.
15
+ useEffect(function () {
16
+ function tick() {
17
+ savedCallback && savedCallback.current && savedCallback.current();
18
+ }
19
+ if (delay !== null) {
20
+ var id_1 = setInterval(tick, delay);
21
+ return function () { return clearInterval(id_1); };
22
+ }
23
+ }, [delay]);
24
+ }
@@ -0,0 +1,22 @@
1
+ import React from "react";
2
+ type Props = {
3
+ targetTime: number | string;
4
+ timerType?: "countdown" | "datetime";
5
+ speed?: number;
6
+ countType?: "seconds" | "minutes:seconds" | "hours:minutes:seconds" | "days:hours:minutes:seconds";
7
+ hide?: boolean;
8
+ onComplete?: () => void;
9
+ getStatus?: ({ days, hours, minutes, seconds, timer }: any) => void;
10
+ autoStart?: boolean;
11
+ };
12
+ export declare const useCountDown: ({ autoStart, onComplete, getStatus, targetTime, timerType, countType, speed, hide }: Props) => {
13
+ ComponentCountDown: (children: any) => React.FunctionComponentElement<{
14
+ ref: React.MutableRefObject<any>;
15
+ }> | null;
16
+ startCountDown: () => void;
17
+ stopCountDown: () => void;
18
+ setTargetTime: (targetTime: number | string) => void;
19
+ setTimerSpeed: (timerSpeed: number) => void;
20
+ getChildrenRef: () => any;
21
+ };
22
+ export {};
@@ -0,0 +1,101 @@
1
+ import { useEffect, useRef, useState } from "react";
2
+ import React from "react";
3
+ import { useInterval } from "../_lib/useInterval";
4
+ export var useCountDown = function (_a) {
5
+ var _b = _a.autoStart, autoStart = _b === void 0 ? false : _b, onComplete = _a.onComplete, getStatus = _a.getStatus, targetTime = _a.targetTime, _c = _a.timerType, timerType = _c === void 0 ? "countdown" : _c, _d = _a.countType, countType = _d === void 0 ? "seconds" : _d, _e = _a.speed, speed = _e === void 0 ? 1000 : _e, hide = _a.hide;
6
+ var refCountDownRender = useRef(null);
7
+ var _f = useState(timerType === "datetime" || autoStart), enabled = _f[0], setEnabled = _f[1];
8
+ var _g = useState(0), timer = _g[0], setTimer = _g[1];
9
+ var _h = useState(typeof speed === "number" && speed > 0 ? speed : 1000), timerSpeed = _h[0], setTimerSpeed = _h[1];
10
+ useEffect(function () { return prepareTimer(targetTime); }, [targetTime]);
11
+ useEffect(function () {
12
+ if (!hide)
13
+ render();
14
+ }, [timer]);
15
+ useInterval(function () {
16
+ if (enabled) {
17
+ if (timer <= 1) {
18
+ stopCountDown();
19
+ }
20
+ else {
21
+ setTimer(timer - 1);
22
+ }
23
+ }
24
+ }, enabled ? timerSpeed : null);
25
+ var prepareTimer = function (timeORstring) {
26
+ if (timerType === "datetime") {
27
+ setTimer(Math.floor((new Date(timeORstring).getTime() - new Date().getTime()) / 1000));
28
+ setEnabled(true);
29
+ }
30
+ else {
31
+ setTimer(timeORstring);
32
+ }
33
+ };
34
+ var stopCountDown = function () {
35
+ setTimer(0);
36
+ setEnabled(false);
37
+ if ((enabled && onComplete) || (timerType === "datetime" && onComplete)) {
38
+ onComplete();
39
+ }
40
+ };
41
+ var padNumber = function (num, padLength, padString) {
42
+ if (padLength === void 0) { padLength = 2; }
43
+ if (padString === void 0) { padString = "0"; }
44
+ return (typeof targetTime === "number" && targetTime < 10 ? num : String(num).padStart(padLength, padString));
45
+ };
46
+ var render = function () {
47
+ var days = 0, hours = 0, minutes = 0, seconds = timer;
48
+ if (countType === "minutes:seconds") {
49
+ minutes = Math.floor(seconds / 60);
50
+ seconds -= minutes * 60;
51
+ }
52
+ else if (countType === "hours:minutes:seconds") {
53
+ hours = Math.floor(seconds / (60 * 60));
54
+ seconds -= hours * 60 * 60;
55
+ minutes = Math.floor(seconds / 60);
56
+ seconds -= minutes * 60;
57
+ }
58
+ else if (countType === "days:hours:minutes:seconds") {
59
+ days = Math.floor(seconds / (60 * 60 * 24));
60
+ seconds -= days * 60 * 60 * 24;
61
+ hours = Math.floor(seconds / (60 * 60));
62
+ seconds -= hours * 60 * 60;
63
+ minutes = Math.floor(seconds / 60);
64
+ seconds -= minutes * 60;
65
+ }
66
+ seconds = Math.floor(seconds);
67
+ if (getStatus && timer > 0)
68
+ getStatus({ days: days, hours: hours, minutes: minutes, seconds: seconds, timer: timer });
69
+ if (days <= 0 && hours <= 0 && minutes <= 0 && seconds <= 0) {
70
+ stopCountDown();
71
+ return;
72
+ }
73
+ if (refCountDownRender.current) {
74
+ var getPadValues = function (div) {
75
+ var length = div.dataset.padlength || 2;
76
+ var string = div.dataset.padstring || "0";
77
+ return { length: length, string: string };
78
+ };
79
+ var divGun = refCountDownRender.current.querySelector("[data-name='days']");
80
+ if (divGun)
81
+ divGun.innerHTML = padNumber(days, getPadValues(divGun).length, getPadValues(divGun).string).toString();
82
+ var divSaat = refCountDownRender.current.querySelector("[data-name='hours']");
83
+ if (divSaat)
84
+ divSaat.innerHTML = padNumber(hours, getPadValues(divSaat).length, getPadValues(divSaat).string).toString();
85
+ var divDakika = refCountDownRender.current.querySelector("[data-name='minutes']");
86
+ if (divDakika)
87
+ divDakika.innerHTML = padNumber(minutes, getPadValues(divDakika).length, getPadValues(divDakika).string).toString();
88
+ var divSaniye = refCountDownRender.current.querySelector("[data-name='seconds']");
89
+ if (divSaniye)
90
+ divSaniye.innerHTML = padNumber(seconds, getPadValues(divSaniye).length, getPadValues(divSaniye).string).toString();
91
+ }
92
+ };
93
+ return {
94
+ ComponentCountDown: function (children) { return (hide ? null : React.cloneElement(children, { ref: refCountDownRender })); },
95
+ startCountDown: function () { return setEnabled(true); },
96
+ stopCountDown: function () { return setEnabled(false); },
97
+ setTargetTime: function (targetTime) { return prepareTimer(targetTime); },
98
+ setTimerSpeed: function (timerSpeed) { return setTimerSpeed(timerSpeed); },
99
+ getChildrenRef: function () { return refCountDownRender.current || null; },
100
+ };
101
+ };
@@ -1,7 +1,8 @@
1
1
  export * from './_lib/baseFunctions';
2
2
  export * from './alert';
3
3
  export * from './box';
4
- export * from './datetime';
4
+ export * from './countDown';
5
+ export * from './dateTime';
5
6
  export * from './form';
6
7
  export * from './grid';
7
8
  export * from './icon';
package/dist/esm/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  export * from './_lib/baseFunctions';
2
2
  export * from './alert';
3
3
  export * from './box';
4
- export * from './datetime';
4
+ export * from './countDown';
5
+ export * from './dateTime';
5
6
  export * from './form';
6
7
  export * from './grid';
7
8
  export * from './icon';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sydsoft/base",
3
3
  "private": false,
4
- "version": "1.19.0",
4
+ "version": "1.21.0",
5
5
  "description": "",
6
6
  "main": "./dist/cjs/index.js",
7
7
  "module": "./dist/esm/index.js",
File without changes
File without changes