Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) Adding useDebounce to the react utils #756

Merged
merged 11 commits into from
Sep 15, 2023
55 changes: 55 additions & 0 deletions packages/framework/esm-framework/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
### Other Functions

- [ExtensionSlot](API.md#extensionslot)
- [useDebounce](API.md#usedebounce)
brandones marked this conversation as resolved.
Show resolved Hide resolved

### Store Functions

Expand Down Expand Up @@ -3751,6 +3752,60 @@ Passing a function as children

___

### useDebounce

▸ **useDebounce**<`T`\>(`value`, `delay?`): `T`

This hook debounces a state variable. That state variable can then be used as the
value of a controlled input, while the return value of this hook is used for making
a request.

**`example`**

```tsx
import { useDebounce } from "@openmrs/esm-react-utils";

function MyComponent() {
const [searchTerm, setSearchTerm] = useState('');
const debouncedSearchTerm = useDebounce(searchTerm);
const swrResult = useSWR(`/api/search?q=${debouncedSearchTerm}`)

return (
<Search
labelText={t('search', 'Search')}
onChange={(e) => setSearchTerm(e.target.value)}
value={searchTerm}
/>
)
}

```

#### Type parameters

| Name |
| :------ |
| `T` |

#### Parameters

| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `value` | `T` | `undefined` | - |
| `delay` | `number` | `300` | number = 300: The number of milliseconds to wait before updating `debounceValue` |

#### Returns

`T`

debounceValue: The debounced value
brandones marked this conversation as resolved.
Show resolved Hide resolved

#### Defined in

[packages/framework/esm-react-utils/src/useDebounce.ts:33](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useDebounce.ts#L33)

___

## Store Functions

### createGlobalStore
Expand Down
1 change: 1 addition & 0 deletions packages/framework/esm-framework/mock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,4 @@ export const showModal = jest.fn();
export const LeftNavMenu = jest.fn();
export const setLeftNav = jest.fn();
export const unsetLeftNav = jest.fn();
export const useDebounce = jest.fn().mockImplementation((value) => value);
1 change: 1 addition & 0 deletions packages/framework/esm-react-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export * from "./useStore";
export * from "./useVisit";
export * from "./useVisitTypes";
export * from "./usePagination";
export * from "./useDebounce";
1 change: 1 addition & 0 deletions packages/framework/esm-react-utils/src/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export * from "./useStore";
export * from "./useVisit";
export * from "./useVisitTypes";
export * from "./usePagination";
export * from "./useDebounce";
42 changes: 42 additions & 0 deletions packages/framework/esm-react-utils/src/useDebounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useEffect, useState } from "react";

/**
* This hook debounces a state variable. That state variable can then be used as the
* value of a controlled input, while the return value of this hook is used for making
* a request.
*
* @example
* ```tsx
* import { useDebounce } from "@openmrs/esm-react-utils";
*
* function MyComponent() {
* const [searchTerm, setSearchTerm] = useState('');
* const debouncedSearchTerm = useDebounce(searchTerm);
* const swrResult = useSWR(`/api/search?q=${debouncedSearchTerm}`)
*
* return (
* <Search
* labelText={t('search', 'Search')}
* onChange={(e) => setSearchTerm(e.target.value)}
* value={searchTerm}
* />
* )
* }
* ```
*
* @param value: The value that will be used to set `debounceValue`
* @param delay number = 300: The number of milliseconds to wait before updating `debounceValue`
* @returns debounceValue: The debounced value
*/
export function useDebounce<T>(value: T, delay: number = 300) {
const [debounceValue, setDebounceValue] = useState<T>(value);
useEffect(() => {
const timer = setTimeout(() => {
setDebounceValue(value);
}, delay);
return () => {
clearTimeout(timer);
};
}, [value, delay]);
return debounceValue;
}
Loading