@trackunit/react-components 0.1.96 → 0.1.97

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.
package/index.cjs CHANGED
@@ -1159,18 +1159,28 @@ const useClickOutside = (el, options = {}, onClick) => {
1159
1159
  *
1160
1160
  * @param {any} value The value to set
1161
1161
  * @param {number} delay The debounce time
1162
- * @returns {any} value The latest values received
1162
+ * @param {"in" | "out" | "both"} direction Considers truthiness of value. If set to "in", the value will only be debounced if it is truthy. If set to "out", the value will only be debounced if it is falsy. If set to "both" or if not defined, the value will be debounced regardless of truthiness.
1163
+ * @returns {any} The latest value received
1163
1164
  */
1164
- const useDebounce = (value, delay = 500) => {
1165
+ const useDebounce = (value, delay = 500, direction) => {
1165
1166
  const [debouncedValue, setDebouncedValue] = React.useState(value);
1166
1167
  React.useEffect(() => {
1167
- const handler = setTimeout(() => {
1168
+ let handler;
1169
+ if ((direction === "in" && !debouncedValue && value) ||
1170
+ (direction === "out" && debouncedValue && !value) ||
1171
+ direction === "both" ||
1172
+ !direction) {
1173
+ handler = setTimeout(() => {
1174
+ setDebouncedValue(value);
1175
+ }, delay);
1176
+ }
1177
+ else {
1168
1178
  setDebouncedValue(value);
1169
- }, delay);
1179
+ }
1170
1180
  return () => {
1171
1181
  clearTimeout(handler);
1172
1182
  };
1173
- }, [value, delay]);
1183
+ }, [value, delay, direction, debouncedValue]);
1174
1184
  return debouncedValue;
1175
1185
  };
1176
1186
 
package/index.js CHANGED
@@ -1132,18 +1132,28 @@ const useClickOutside = (el, options = {}, onClick) => {
1132
1132
  *
1133
1133
  * @param {any} value The value to set
1134
1134
  * @param {number} delay The debounce time
1135
- * @returns {any} value The latest values received
1135
+ * @param {"in" | "out" | "both"} direction Considers truthiness of value. If set to "in", the value will only be debounced if it is truthy. If set to "out", the value will only be debounced if it is falsy. If set to "both" or if not defined, the value will be debounced regardless of truthiness.
1136
+ * @returns {any} The latest value received
1136
1137
  */
1137
- const useDebounce = (value, delay = 500) => {
1138
+ const useDebounce = (value, delay = 500, direction) => {
1138
1139
  const [debouncedValue, setDebouncedValue] = useState(value);
1139
1140
  useEffect(() => {
1140
- const handler = setTimeout(() => {
1141
+ let handler;
1142
+ if ((direction === "in" && !debouncedValue && value) ||
1143
+ (direction === "out" && debouncedValue && !value) ||
1144
+ direction === "both" ||
1145
+ !direction) {
1146
+ handler = setTimeout(() => {
1147
+ setDebouncedValue(value);
1148
+ }, delay);
1149
+ }
1150
+ else {
1141
1151
  setDebouncedValue(value);
1142
- }, delay);
1152
+ }
1143
1153
  return () => {
1144
1154
  clearTimeout(handler);
1145
1155
  };
1146
- }, [value, delay]);
1156
+ }, [value, delay, direction, debouncedValue]);
1147
1157
  return debouncedValue;
1148
1158
  };
1149
1159
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-components",
3
- "version": "0.1.96",
3
+ "version": "0.1.97",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "dependencies": {
@@ -3,6 +3,7 @@
3
3
  *
4
4
  * @param {any} value The value to set
5
5
  * @param {number} delay The debounce time
6
- * @returns {any} value The latest values received
6
+ * @param {"in" | "out" | "both"} direction Considers truthiness of value. If set to "in", the value will only be debounced if it is truthy. If set to "out", the value will only be debounced if it is falsy. If set to "both" or if not defined, the value will be debounced regardless of truthiness.
7
+ * @returns {any} The latest value received
7
8
  */
8
- export declare const useDebounce: <T>(value: T, delay?: number) => T;
9
+ export declare const useDebounce: <T>(value: T, delay?: number, direction?: "in" | "out" | "both") => T;