jy-headless 0.2.26 → 0.2.27

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.
@@ -1,3 +1,3 @@
1
1
  import type { ButtonProps } from '../../types';
2
- declare const Button: ({ onClick, debounce, loading, readOnly, disabled, children, ...props }: ButtonProps) => import("react/jsx-runtime").JSX.Element;
2
+ declare const Button: ({ onClick, loading, readOnly, disabled, useDebounce, timeout, children, ...props }: ButtonProps) => import("react/jsx-runtime").JSX.Element;
3
3
  export default Button;
@@ -1,19 +1,8 @@
1
1
  import { jsx } from 'react/jsx-runtime';
2
- import { useRef } from 'react';
2
+ import useDebouncing from '../../hooks/useDebouncing.js';
3
3
 
4
- const Button = ({ onClick, debounce = false, loading = false, readOnly = false, disabled = false, children, ...props }) => {
5
- const timer = useRef(0);
6
- const handleClick = (() => {
7
- if (!onClick)
8
- return;
9
- if (debounce) {
10
- return () => {
11
- clearTimeout(timer.current);
12
- timer.current = setTimeout(onClick, 300);
13
- };
14
- }
15
- return onClick;
16
- })();
4
+ const Button = ({ onClick, loading = false, readOnly = false, disabled = false, useDebounce = false, timeout = 300, children, ...props }) => {
5
+ const handleClick = onClick && useDebounce ? useDebouncing(onClick, timeout || 300) : onClick;
17
6
  return (jsx("button", { onClick: handleClick, disabled: disabled || loading || readOnly, ...props, children: children }));
18
7
  };
19
8
 
@@ -1,3 +1,3 @@
1
1
  import type { ButtonProps } from '../../types';
2
- declare const Button: ({ onClick, debounce, loading, readOnly, disabled, children, ...props }: ButtonProps) => import("react/jsx-runtime").JSX.Element;
2
+ declare const Button: ({ onClick, loading, readOnly, disabled, useDebounce, timeout, children, ...props }: ButtonProps) => import("react/jsx-runtime").JSX.Element;
3
3
  export default Button;
@@ -1,21 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  var jsxRuntime = require('react/jsx-runtime');
4
- var react = require('react');
4
+ var useDebouncing = require('../../hooks/useDebouncing.js');
5
5
 
6
- const Button = ({ onClick, debounce = false, loading = false, readOnly = false, disabled = false, children, ...props }) => {
7
- const timer = react.useRef(0);
8
- const handleClick = (() => {
9
- if (!onClick)
10
- return;
11
- if (debounce) {
12
- return () => {
13
- clearTimeout(timer.current);
14
- timer.current = setTimeout(onClick, 300);
15
- };
16
- }
17
- return onClick;
18
- })();
6
+ const Button = ({ onClick, loading = false, readOnly = false, disabled = false, useDebounce = false, timeout = 300, children, ...props }) => {
7
+ const handleClick = onClick && useDebounce ? useDebouncing(onClick, timeout || 300) : onClick;
19
8
  return (jsxRuntime.jsx("button", { onClick: handleClick, disabled: disabled || loading || readOnly, ...props, children: children }));
20
9
  };
21
10
 
@@ -0,0 +1,2 @@
1
+ export * from './useDebouncing';
2
+ export * from './useThrottling';
@@ -0,0 +1,2 @@
1
+ declare const useDebouncing: <T extends (...args: any[]) => void>(callback: T, delay: number) => T;
2
+ export default useDebouncing;
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+
5
+ const useDebouncing = (callback, delay) => {
6
+ const timer = react.useRef(null);
7
+ return react.useCallback(() => {
8
+ if (timer.current)
9
+ clearTimeout(timer.current);
10
+ timer.current = setTimeout(() => {
11
+ callback();
12
+ }, delay);
13
+ }, [callback, delay]);
14
+ };
15
+
16
+ module.exports = useDebouncing;
@@ -0,0 +1,2 @@
1
+ declare const useThrottling: <T extends (...args: any[]) => void>(callback: T, delay: number) => T;
2
+ export default useThrottling;
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+
5
+ const useThrottling = (callback, delay) => {
6
+ const lastCalled = react.useRef(0);
7
+ return react.useCallback((...args) => {
8
+ const now = Date.now();
9
+ if (now - lastCalled.current >= delay) {
10
+ lastCalled.current = now;
11
+ callback(...args);
12
+ }
13
+ }, [callback, delay]);
14
+ };
15
+
16
+ module.exports = useThrottling;
package/cjs/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export { default as Button } from './buttons/Button/Button';
2
- export { default as Input } from './inputs/Input/Input';
3
- export * as Types from './types';
4
- export * as Utils from './utils';
1
+ export { default as Button } from './buttons';
2
+ export { default as Input } from './inputs';
3
+ export * from './types';
4
+ export * from './utils';
5
+ export * from './hooks';
package/cjs/index.js CHANGED
@@ -2,16 +2,9 @@
2
2
 
3
3
  var Button = require('./buttons/Button/Button.js');
4
4
  var Input = require('./inputs/Input/Input.js');
5
- var index = require('./types/index.js');
6
- var index$1 = require('./utils/index.js');
7
-
8
- function _interopNamespaceDefaultOnly (e) { return Object.freeze({ __proto__: null, default: e }); }
9
-
10
- var index__namespace = /*#__PURE__*/_interopNamespaceDefaultOnly(index$1);
5
+ require('react');
11
6
 
12
7
 
13
8
 
14
9
  exports.Button = Button;
15
10
  exports.Input = Input;
16
- exports.Types = index;
17
- exports.Utils = index__namespace;
@@ -1,3 +1,3 @@
1
- import { InputProps } from '../../types/inputs/types';
2
- declare const Input: ({ id, suffixElement, prefixElement, wrapperStyle, wrapperClass, children, ...props }: InputProps) => import("react/jsx-runtime").JSX.Element;
1
+ import { InputProps } from '../../types';
2
+ declare const Input: ({ id, suffixElement, prefixElement, wrapperStyle, wrapperClass, onChange, timeout, useThrottle, children, ...props }: InputProps) => import("react/jsx-runtime").JSX.Element;
3
3
  export default Input;
@@ -1,10 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  var jsxRuntime = require('react/jsx-runtime');
4
- var ArrayUtils = require('../../utils/ArrayUtils.js');
4
+ var useThrottling = require('../../hooks/useThrottling.js');
5
5
 
6
- const Input = ({ id, suffixElement, prefixElement, wrapperStyle, wrapperClass, children, ...props }) => {
7
- return (jsxRuntime.jsxs("span", { "data-testid": 'input-wrapper', id: [id, 'input-wrapper'].join('-'), className: wrapperClass && ArrayUtils.toFlat(wrapperClass).join(' '), style: wrapperStyle, children: [prefixElement, jsxRuntime.jsx("input", { id: id, ...props }), suffixElement] }));
6
+ const Input = ({ id, suffixElement, prefixElement, wrapperStyle, wrapperClass = [], onChange, timeout = 300, useThrottle = false, children, ...props }) => {
7
+ const handleChange = onChange && useThrottle ? useThrottling(onChange, timeout) : onChange;
8
+ return (jsxRuntime.jsxs("span", { "data-testid": 'input-wrapper', id: [id, 'input-wrapper'].join('-'), className: wrapperClass?.join(' '), style: wrapperStyle, children: [prefixElement, jsxRuntime.jsx("input", { id: id, onChange: handleChange, ...props }), suffixElement] }));
8
9
  };
9
10
 
10
11
  module.exports = Input;
@@ -0,0 +1,7 @@
1
+ import { ButtonHTMLAttributes } from 'react';
2
+ export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
3
+ loading?: boolean;
4
+ readOnly?: boolean;
5
+ useDebounce?: boolean;
6
+ timeout?: number;
7
+ }
@@ -1,2 +1,2 @@
1
- export * from './buttons/types';
2
- export * from './inputs/types';
1
+ export * from './buttons';
2
+ export * from './inputs';
@@ -0,0 +1,9 @@
1
+ import { CSSProperties, InputHTMLAttributes, ReactElement, ReactNode } from 'react';
2
+ export interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
3
+ suffixElement?: ReactElement | ReactNode;
4
+ prefixElement?: ReactElement | ReactNode;
5
+ wrapperStyle?: CSSProperties;
6
+ wrapperClass?: string[];
7
+ useThrottle?: boolean;
8
+ timeout?: number;
9
+ }
@@ -1,4 +1,2 @@
1
- declare const ArrayUtils: {
2
- toFlat(...args: (string | string[])[]): string[];
3
- };
1
+ declare const ArrayUtils: {};
4
2
  export default ArrayUtils;
@@ -0,0 +1,2 @@
1
+ export * from './useDebouncing';
2
+ export * from './useThrottling';
@@ -0,0 +1,2 @@
1
+ declare const useDebouncing: <T extends (...args: any[]) => void>(callback: T, delay: number) => T;
2
+ export default useDebouncing;
@@ -0,0 +1,14 @@
1
+ import { useRef, useCallback } from 'react';
2
+
3
+ const useDebouncing = (callback, delay) => {
4
+ const timer = useRef(null);
5
+ return useCallback(() => {
6
+ if (timer.current)
7
+ clearTimeout(timer.current);
8
+ timer.current = setTimeout(() => {
9
+ callback();
10
+ }, delay);
11
+ }, [callback, delay]);
12
+ };
13
+
14
+ export { useDebouncing as default };
@@ -0,0 +1,2 @@
1
+ declare const useThrottling: <T extends (...args: any[]) => void>(callback: T, delay: number) => T;
2
+ export default useThrottling;
@@ -0,0 +1,14 @@
1
+ import { useRef, useCallback } from 'react';
2
+
3
+ const useThrottling = (callback, delay) => {
4
+ const lastCalled = useRef(0);
5
+ return useCallback((...args) => {
6
+ const now = Date.now();
7
+ if (now - lastCalled.current >= delay) {
8
+ lastCalled.current = now;
9
+ callback(...args);
10
+ }
11
+ }, [callback, delay]);
12
+ };
13
+
14
+ export { useThrottling as default };
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export { default as Button } from './buttons/Button/Button';
2
- export { default as Input } from './inputs/Input/Input';
3
- export * as Types from './types';
4
- export * as Utils from './utils';
1
+ export { default as Button } from './buttons';
2
+ export { default as Input } from './inputs';
3
+ export * from './types';
4
+ export * from './utils';
5
+ export * from './hooks';
package/index.js CHANGED
@@ -1,6 +1,3 @@
1
1
  export { default as Button } from './buttons/Button/Button.js';
2
2
  export { default as Input } from './inputs/Input/Input.js';
3
- import * as index from './types/index.js';
4
- export { index as Types };
5
- import * as index$1 from './utils/index.js';
6
- export { index$1 as Utils };
3
+ import 'react';
@@ -1,3 +1,3 @@
1
- import { InputProps } from '../../types/inputs/types';
2
- declare const Input: ({ id, suffixElement, prefixElement, wrapperStyle, wrapperClass, children, ...props }: InputProps) => import("react/jsx-runtime").JSX.Element;
1
+ import { InputProps } from '../../types';
2
+ declare const Input: ({ id, suffixElement, prefixElement, wrapperStyle, wrapperClass, onChange, timeout, useThrottle, children, ...props }: InputProps) => import("react/jsx-runtime").JSX.Element;
3
3
  export default Input;
@@ -1,8 +1,9 @@
1
1
  import { jsxs, jsx } from 'react/jsx-runtime';
2
- import ArrayUtils from '../../utils/ArrayUtils.js';
2
+ import useThrottling from '../../hooks/useThrottling.js';
3
3
 
4
- const Input = ({ id, suffixElement, prefixElement, wrapperStyle, wrapperClass, children, ...props }) => {
5
- return (jsxs("span", { "data-testid": 'input-wrapper', id: [id, 'input-wrapper'].join('-'), className: wrapperClass && ArrayUtils.toFlat(wrapperClass).join(' '), style: wrapperStyle, children: [prefixElement, jsx("input", { id: id, ...props }), suffixElement] }));
4
+ const Input = ({ id, suffixElement, prefixElement, wrapperStyle, wrapperClass = [], onChange, timeout = 300, useThrottle = false, children, ...props }) => {
5
+ const handleChange = onChange && useThrottle ? useThrottling(onChange, timeout) : onChange;
6
+ return (jsxs("span", { "data-testid": 'input-wrapper', id: [id, 'input-wrapper'].join('-'), className: wrapperClass?.join(' '), style: wrapperStyle, children: [prefixElement, jsx("input", { id: id, onChange: handleChange, ...props }), suffixElement] }));
6
7
  };
7
8
 
8
9
  export { Input as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jy-headless",
3
- "version": "0.2.26",
3
+ "version": "0.2.27",
4
4
  "description": "A lightweight and customizable headless UI library for React components",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/yCZwIqY/jy-headless",
@@ -23,6 +23,16 @@
23
23
  "require": "./cjs/cjs/index.js",
24
24
  "types": "./cjs/index.d.ts"
25
25
  },
26
+ "./useDebouncing": {
27
+ "import": "./hooks/useDebouncing.js",
28
+ "require": "./cjs/hooks/useDebouncing.js",
29
+ "types": "./hooks/useDebouncing.d.ts"
30
+ },
31
+ "./useThrottling": {
32
+ "import": "./hooks/useThrottling.js",
33
+ "require": "./cjs/hooks/useThrottling.js",
34
+ "types": "./hooks/useThrottling.d.ts"
35
+ },
26
36
  "./index": {
27
37
  "import": "./utils/index.js",
28
38
  "require": "./cjs/utils/index.js",
@@ -0,0 +1,7 @@
1
+ import { ButtonHTMLAttributes } from 'react';
2
+ export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
3
+ loading?: boolean;
4
+ readOnly?: boolean;
5
+ useDebounce?: boolean;
6
+ timeout?: number;
7
+ }
package/types/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from './buttons/types';
2
- export * from './inputs/types';
1
+ export * from './buttons';
2
+ export * from './inputs';
@@ -0,0 +1,9 @@
1
+ import { CSSProperties, InputHTMLAttributes, ReactElement, ReactNode } from 'react';
2
+ export interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
3
+ suffixElement?: ReactElement | ReactNode;
4
+ prefixElement?: ReactElement | ReactNode;
5
+ wrapperStyle?: CSSProperties;
6
+ wrapperClass?: string[];
7
+ useThrottle?: boolean;
8
+ timeout?: number;
9
+ }
@@ -1,4 +1,2 @@
1
- declare const ArrayUtils: {
2
- toFlat(...args: (string | string[])[]): string[];
3
- };
1
+ declare const ArrayUtils: {};
4
2
  export default ArrayUtils;
package/version.txt CHANGED
@@ -1 +1 @@
1
- 0.2.26
1
+ 0.2.27