@mpen/react-basic-inputs 0.2.3 → 0.2.4

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/README.md CHANGED
@@ -17,6 +17,7 @@ npm install @mpen/react-basic-inputs
17
17
  ## Links
18
18
 
19
19
  - Npm: https://www.npmjs.com/package/@mpen/react-basic-inputs
20
+ - https://www.npmjs.com/package/@mpen/react-basic-inputs?activeTab=code
20
21
  - Yarn: https://yarnpkg.com/package/@mpen/react-basic-inputs
21
22
  - pkg-size: https://pkg-size.dev/@mpen%2Freact-basic-inputs
22
23
  - Bundlephobia: https://bundlephobia.com/package/@mpen/react-basic-inputs
@@ -9,6 +9,18 @@ import { ReactNode } from 'react';
9
9
  import { RefAttributes } from 'react';
10
10
  import { TextareaHTMLAttributes } from 'react';
11
11
 
12
+ export declare function DebouncedInput({ value: initialValue, onChange, debounce, ...props }: DebouncedInputProps): JSX_2.Element;
13
+
14
+ export declare type DebouncedInputChangeEvent = {
15
+ value: string;
16
+ };
17
+
18
+ export declare type DebouncedInputProps = OverrideProps<'input', {
19
+ value: string;
20
+ onChange: (ev: DebouncedInputChangeEvent) => void;
21
+ debounce?: number;
22
+ }>;
23
+
12
24
  declare type EventCallback<T = never> = (ev: T) => void;
13
25
 
14
26
  declare type HtmlInputElement = HTMLElementTagNameMap['input'];
@@ -107,7 +119,7 @@ export declare type SelectChangeEventHandler<T> = EventCallback<SelectChangeEven
107
119
  export declare type SelectOption<T> = OverrideProps<'option', {
108
120
  value: T;
109
121
  text: ReactNode;
110
- key?: Resolvable<Key, [SelectOption<T>, number]>;
122
+ key?: Resolvable<StrictKey, [SelectOption<T>, number]>;
111
123
  }, 'children' | 'selected'>;
112
124
 
113
125
  export declare type SelectProps<T extends NonNil> = OverrideProps<'select', {
@@ -126,6 +138,8 @@ export declare type SelectProps<T extends NonNil> = OverrideProps<'select', {
126
138
  placeholder?: ReactNode;
127
139
  }, 'children' | 'defaultValue'>;
128
140
 
141
+ declare type StrictKey = string | number;
142
+
129
143
  export declare const TextArea: ForwardRefExoticComponent<Omit<Omit<DetailedHTMLProps<TextareaHTMLAttributes<HTMLTextAreaElement>, HTMLTextAreaElement>, "ref">, "initialHeight"> & {
130
144
  /** Initial/minimum height. "0" or "auto" are good choices. Defaults to "auto" */
131
145
  initialHeight?: string | undefined;
@@ -280,7 +280,33 @@ function RadioMenu(menu) {
280
280
  ] }) }, fixedKey);
281
281
  }) });
282
282
  }
283
+ function useBox(value) {
284
+ const ref = useRef(value);
285
+ ref.current = value;
286
+ return ref;
287
+ }
288
+ function DebouncedInput({
289
+ value: initialValue,
290
+ onChange,
291
+ debounce = 500,
292
+ ...props
293
+ }) {
294
+ const [value, setValue] = useState(initialValue);
295
+ const onChangeRef = useBox(onChange);
296
+ const debounceRef = useBox(debounce);
297
+ useEffect(() => {
298
+ setValue(initialValue);
299
+ }, [initialValue]);
300
+ useEffect(() => {
301
+ const timeout = setTimeout(() => {
302
+ onChangeRef.current({ value });
303
+ }, debounceRef.current);
304
+ return () => clearTimeout(timeout);
305
+ }, [debounceRef, onChangeRef, value]);
306
+ return /* @__PURE__ */ jsx("input", { ...props, value, onChange: (e) => setValue(e.target.value) });
307
+ }
283
308
  export {
309
+ DebouncedInput,
284
310
  Input,
285
311
  RadioMenu,
286
312
  Select,
@@ -282,6 +282,32 @@ var __publicField = (obj, key, value) => {
282
282
  ] }) }, fixedKey);
283
283
  }) });
284
284
  }
285
+ function useBox(value) {
286
+ const ref = React.useRef(value);
287
+ ref.current = value;
288
+ return ref;
289
+ }
290
+ function DebouncedInput({
291
+ value: initialValue,
292
+ onChange,
293
+ debounce = 500,
294
+ ...props
295
+ }) {
296
+ const [value, setValue] = React.useState(initialValue);
297
+ const onChangeRef = useBox(onChange);
298
+ const debounceRef = useBox(debounce);
299
+ React.useEffect(() => {
300
+ setValue(initialValue);
301
+ }, [initialValue]);
302
+ React.useEffect(() => {
303
+ const timeout = setTimeout(() => {
304
+ onChangeRef.current({ value });
305
+ }, debounceRef.current);
306
+ return () => clearTimeout(timeout);
307
+ }, [debounceRef, onChangeRef, value]);
308
+ return /* @__PURE__ */ jsxRuntime.jsx("input", { ...props, value, onChange: (e) => setValue(e.target.value) });
309
+ }
310
+ exports2.DebouncedInput = DebouncedInput;
285
311
  exports2.Input = Input;
286
312
  exports2.RadioMenu = RadioMenu;
287
313
  exports2.Select = Select;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mpen/react-basic-inputs",
3
3
  "private": false,
4
- "version": "0.2.3",
4
+ "version": "0.2.4",
5
5
  "type": "module",
6
6
  "files": ["dist"],
7
7
  "main": "./dist/react-basic-inputs.umd.cjs",
@@ -15,8 +15,10 @@
15
15
  },
16
16
  "scripts": {
17
17
  "dev": "bun run --bun vite",
18
- "build": "bun run --bun tsc && bun run --bun vite build",
19
- "lint": "bun run --bun eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
18
+ "build": "bun run --bun vite build",
19
+ "lint": "run-s 'lint:*'",
20
+ "lint:ts": "bun run --bun tsc",
21
+ "lint:es": "bun run --bun eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
20
22
  "preview": "bun run --bun vite preview",
21
23
  "_prepublishOnly": "bun run --bun build",
22
24
  "_publish": "npm publish --access=public"