Skip to content

Commit df5ce42

Browse files
docs. fill in jsdocs, replace interface to type, add eslint (#29)
1 parent c86a646 commit df5ce42

File tree

22 files changed

+251
-53
lines changed

22 files changed

+251
-53
lines changed

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export default [
4949
...hooksPlugin.configs.recommended.rules,
5050
'@typescript-eslint/no-explicit-any': 'warn',
5151
'@typescript-eslint/strict-boolean-expressions': 'error',
52+
'@typescript-eslint/consistent-type-definitions': ['warn', 'type'],
5253
'react/react-in-jsx-scope': 'off',
5354
'import/extensions': [
5455
'error',

src/components/Separated/Separated.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
```tsx
1313
<Separated with={<Border type="padding24" />}>
1414
{LIST.map(item => (
15-
<div>item.title</div>
15+
<div>{item.title}</div>
1616
))}
1717
</Separated>
1818
```

src/components/Separated/Separated.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@ interface Props {
55
children: ReactNode;
66
}
77

8+
/**
9+
* @description
10+
* `Separated` is a component that can be used when you want to insert a component that repeats between each element.
11+
*
12+
* @param children - The component to insert between each element.
13+
* @param with - The child elements to render.Each child will be filtered using `React.isValidElement` function.
14+
* @returns A React component that separates children with a separator
15+
*
16+
* @example
17+
* function App() {
18+
* return (
19+
* <Separated with={<Border type="padding24" />}>
20+
* {['hello', 'react', 'world'].map(item => (
21+
* <div>{item}</div>
22+
* ))}
23+
* </Separated>
24+
* );
25+
* // expected output:
26+
* // <div>hello</div>
27+
* // <Border type="padding24" />
28+
* // <div>react</div>
29+
* // <Border type="padding24" />
30+
* // <div>world</div>
31+
* }
32+
*/
833
export function Separated({ children, with: separator }: Props) {
934
const childrenArray = Children.toArray(children).filter(isValidElement);
1035

src/components/SwitchCase/SwitchCase.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@ interface Props<Case> {
1414
defaultComponent?: () => ReactNode;
1515
}
1616

17+
/**
18+
* @description
19+
* `SwitchCase` is a component that allows you to use switch-case statements declaratively.
20+
*
21+
* @param value - The case value
22+
* @param caseBy - An object that defines the components to render based on the case.
23+
* @param defaultComponent - The component to render if the case does not match.
24+
* @returns A React component that conditionally rendered based on cases
25+
*
26+
* @example
27+
* function App() {
28+
* return <SwitchCase
29+
* value={status}
30+
* // component is rendered based on the status value
31+
* caseBy={{
32+
* a: <TypeA />,
33+
* b: <TypeB />,
34+
* c: <TypeC />,
35+
* }}
36+
* // component is rendered when the status value is not matched
37+
* defaultComponent={<Default />}
38+
* />;
39+
* }
40+
*/
1741
export function SwitchCase<Case>({ value, caseBy, defaultComponent = () => null }: Props<Case>) {
1842
const stringifiedValue = String(value) as StringifiedValue<Case>;
1943
return (caseBy[stringifiedValue] ?? defaultComponent)();

src/hooks/useAsyncEffect/useAsyncEffect.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { DependencyList, useEffect } from 'react';
55
* useAsyncEffect is a hook for handling async effects in React components.
66
* Follows the same cleanup pattern as useEffect.
77
*
8+
* @param effect - An async function to be executed in useEffect pattern.
9+
* @param deps - A dependency array.
10+
*
811
* @example
912
* useAsyncEffect(async () => {
1013
* const data = await fetchData();

src/hooks/useBooleanState/useBooleanState.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ import { useCallback, useState } from 'react';
33
/**
44
*
55
* @description
6-
* boolean 타입으로 useState를 쉽게 사용할 수 있는 hook 입니다.
6+
* useBooleanState is a React hook that helps manage a boolean state easily. It provides functionalities to set the state to true, set it to false, and toggle the state.
77
*
8-
* ```ts
9-
* function useBooleanState(defaultValue = false): readonly [bool, setTrue, setFalse, toggle];
10-
* ```
8+
* @param defaultValue - It's the initial value of the state. The default value is false.
9+
*
10+
* @returns Returns a `readonly [boolean, () => void, () => void, () => void]` tuple:
11+
*
12+
* 1. boolean: The current state value
13+
* 2. () => void: A function to set the state to true
14+
* 3. () => void: A function to set the state to false
15+
* 4. () => void: A function to toggle the state
1116
*
1217
* @example
1318
* const [open, openBottomSheet, closeBottomSheet, toggleBottomSheet] = useBooleanState(false);

src/hooks/useCallbackOnce/useCallbackOnce.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DependencyList, useEffect, useRef } from 'react';
44
import { usePreservedCallback } from '../usePreservedCallback/index.ts';
55

66
/**
7+
* @descrption
78
* A React hook that ensures a callback function is executed only once, regardless of
89
* how many times it's called. This is useful for one-time operations that should not
910
* be repeated, even if the component re-renders.

src/hooks/useDebounce/debounce.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
// Simplified version of https://github.com/toss/es-toolkit/blob/main/src/function/debounce.ts
33

4-
export interface DebouncedFunction<F extends (...args: any[]) => void> {
4+
export type DebouncedFunction<F extends (...args: any[]) => void> = {
55
(...args: Parameters<F>): void;
66
cancel: () => void;
77
}

src/hooks/useDebounce/useDebounce.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ import { usePreservedCallback } from '../usePreservedCallback/index.ts';
55

66
import { debounce } from './debounce.ts';
77

8-
interface DebounceOptions {
8+
type DebounceOptions = {
99
leading?: boolean;
1010
trailing?: boolean;
11-
}
11+
};
1212

1313
/**
14+
* @description
1415
* `useDebounce` is a hook that returns a debounced version of the provided callback function
1516
*
1617
* @param callback Function to debounce
1718
* @param wait Time in milliseconds to delay
1819
* @param options Configuration options for debounce behavior
20+
*
1921
* @returns Debounced function that will delay invoking the callback
2022
*
2123
* @example
@@ -46,7 +48,7 @@ export function useDebounce<F extends (...args: unknown[]) => unknown>(
4648
) {
4749
const preservedCallback = usePreservedCallback(callback);
4850

49-
const { leading = true, trailing = true } = options;
51+
const { leading = false, trailing = true } = options;
5052

5153
const edges = useMemo(() => {
5254
const _edges: Array<'leading' | 'trailing'> = [];

src/hooks/useInterval/useInterval.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,33 @@ type IntervalOptions =
1010
enabled?: boolean;
1111
};
1212

13+
/**
14+
* @description
15+
* `useInterval` is a React hook that executes a function periodically.
16+
*
17+
* @param callback - The function to be executed periodically.
18+
* @param options - Configure the interval timing and behavior.
19+
* @param options.delay - The interval duration in milliseconds. When null, the interval won't be executed.
20+
* @param options.immediate - When true, executes immediately. When false, waits for the first delay before execution.
21+
* @param options.enabled - When false, the interval won't be executed.
22+
*
23+
* @example
24+
* import { useInterval } from 'reactive-kit';
25+
*
26+
* function Timer() {
27+
* const [time, setTime] = useState(0);
28+
*
29+
* useInterval(() => {
30+
* setTime(prev => prev + 1);
31+
* }, 1000);
32+
*
33+
* return (
34+
* <div>
35+
* <p>{time} seconds</p>
36+
* </div>
37+
* );
38+
* }
39+
*/
1340
export function useInterval(callback: () => void, options: IntervalOptions) {
1441
const delay = typeof options === 'number' ? options : options.delay;
1542
const immediate = typeof options === 'number' ? false : options.immediate;

0 commit comments

Comments
 (0)