Skip to content

Commit

Permalink
fix(useToggle): fix hook returned value
Browse files Browse the repository at this point in the history
  • Loading branch information
vikiboss committed Aug 9, 2024
1 parent 69b7f5d commit dd976b9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/use-toggle/demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Button, Card, KeyValue, Zone } from '@/components'
import { useCircularList, useToggle } from '@shined/react-use'

export function App() {
const [mode, toggleMode, actions] = useToggle(['light', 'dark'] as const)
const [mode, actions] = useToggle(['light', 'dark'] as const)
const [value, listActions] = useCircularList(['A', 'B', 'C', 'D', 'F', 'G'])

return (
Expand All @@ -12,7 +12,7 @@ export function App() {
<KeyValue label="value" value={value} />
</Zone>
<Zone>
<Button onClick={toggleMode}>Toggle mode</Button>
<Button onClick={actions.toggle}>Toggle mode</Button>
<Button onClick={actions.setLeft}>Set left</Button>
<Button onClick={actions.setRight}>Set right</Button>
<Button onClick={() => listActions.prev()}>Prev value</Button>
Expand Down
10 changes: 7 additions & 3 deletions src/use-toggle/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import { Source } from '@/components'
## API

```tsx
const [value, toggle, actions] = useToggle(bool)
const [value, toggle, actions] = useToggle(one, theOther)
const [value, actions] = useToggle(bool)
const [value, actions] = useToggle(one, theOther)
```

### Parameters
Expand All @@ -50,6 +50,10 @@ It accepts one or two parameters:

```tsx
export interface UseToggleReturnsActions<O, T> {
/**
* Toggle the state.
*/
toggle(): void
/**
* Set the state to the left value.
*/
Expand All @@ -64,5 +68,5 @@ export interface UseToggleReturnsActions<O, T> {
setState: ReactSetState<O | T>
}

export type UseToggleReturns<O, T> = [O | T, () => void, UseToggleReturnsActions<O, T>]
export type UseToggleReturns<O, T> = [O | T, UseToggleReturnsActions<O, T>]
```
10 changes: 7 additions & 3 deletions src/use-toggle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { unwrapArrayable } from '../utils/unwrap'
import type { ReactSetState } from '../use-safe-state'

export interface UseToggleReturnsActions<O, T> {
/**
* Toggle the state between the left and right values.
*/
toggle(): void
/**
* Set the state to the left value.
*/
Expand All @@ -22,7 +26,7 @@ export interface UseToggleReturnsActions<O, T> {
setState: ReactSetState<O | T>
}

export type UseToggleReturns<O, T> = readonly [O | T, () => void, UseToggleReturnsActions<O, T>]
export type UseToggleReturns<O, T> = readonly [O | T, UseToggleReturnsActions<O, T>]

/**
* A React Hook that helps to manage a togglable state.
Expand All @@ -45,7 +49,7 @@ export function useToggle<O, T>(maybeTuple: O | T | readonly [one: O, theOther:

const setLeft = useStableFn(() => setState(latest.current.one))
const setRight = useStableFn(() => setState(latest.current.theOther))
const actions = useCreation(() => ({ setLeft, setRight, setState }))
const actions = useCreation(() => ({ setLeft, toggle, setRight, setState }))

return [state, toggle, actions] as const
return [state, actions] as const
}
10 changes: 7 additions & 3 deletions src/use-toggle/index_zh-cn.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import { Source } from '@/components'
## API \{#api}

```tsx
const [value, toggle, actions] = useToggle(bool)
const [value, toggle, actions] = useToggle(one, theOther)
const [value, actions] = useToggle(bool)
const [value, actions] = useToggle(one, theOther)
```

### 参数 Parameters \{#parameters}
Expand All @@ -50,6 +50,10 @@ const [value, toggle, actions] = useToggle(one, theOther)

```tsx
export interface UseToggleReturnsActions<O, T> {
/**
* 切换状态。
*/
toggle(): void
/**
* 设置状态为左值。
*/
Expand All @@ -64,5 +68,5 @@ export interface UseToggleReturnsActions<O, T> {
setState: ReactSetState<O | T>
}

export type UseToggleReturns<O, T> = [O | T, () => void, UseToggleReturnsActions<O, T>]
export type UseToggleReturns<O, T> = [O | T, UseToggleReturnsActions<O, T>]
```

0 comments on commit dd976b9

Please sign in to comment.