-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
205 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Button, Card, KeyValue, Zone } from '@/components' | ||
import { useBoolean, useCircularList } from '@shined/react-use' | ||
|
||
export function App() { | ||
const [bool, actions] = useBoolean(true) | ||
const [value, listActions] = useCircularList(['A', 'B', 'C', 'D', 'F', 'G']) | ||
|
||
return ( | ||
<Card> | ||
<Zone> | ||
<KeyValue label="bool" value={bool} /> | ||
<KeyValue label="value" value={value} /> | ||
</Zone> | ||
<Zone> | ||
<Button onClick={actions.toggle}>Toggle bool</Button> | ||
<Button onClick={actions.setTrue}>Set true</Button> | ||
<Button onClick={actions.setFalse}>Set false</Button> | ||
<Button onClick={() => listActions.prev()}>Prev value</Button> | ||
<Button onClick={() => listActions.next()}>Next value</Button> | ||
</Zone> | ||
</Card> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# useBoolean | ||
|
||
import { HooksType } from '@/components' | ||
|
||
<HooksType category="State" /> | ||
|
||
A React Hook that helps to manage a `boolean` state. | ||
|
||
:::tip | ||
|
||
If you need to manage a state that alternates among several values, consider utilizing [useCircularList](/reference/use-circular-list) as an alternative. | ||
|
||
::: | ||
|
||
## Demo | ||
|
||
import { App } from './demo' | ||
|
||
<App /> | ||
|
||
## Usage | ||
|
||
import { RawCode } from '@/components' | ||
import source from '!!raw-loader!./demo' | ||
|
||
<RawCode>{source}</RawCode> | ||
|
||
## Source | ||
|
||
import { Source } from '@/components' | ||
|
||
<Source /> | ||
|
||
## API | ||
|
||
```tsx | ||
const [bool, actions] = useBoolean(initialBool) | ||
``` | ||
|
||
### InitialBool \{#initial-bool} | ||
|
||
A `boolean` value that represents the initial state. | ||
|
||
### Returns | ||
|
||
```tsx | ||
export type UseBooleanActions = { | ||
/** | ||
* Set the boolean state to `true`. | ||
*/ | ||
setTrue: () => void | ||
/** | ||
* Set the boolean state to `false`. | ||
*/ | ||
setFalse: () => void | ||
/** | ||
* Set the boolean state to the given value. | ||
*/ | ||
setState: ReactSetState<boolean> | ||
/** | ||
* Toggle the boolean state. | ||
*/ | ||
toggle: () => void | ||
} | ||
|
||
export type UseBooleanReturns = readonly [value: boolean, UseBooleanActions] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useCreation } from '../use-creation' | ||
import { useToggle } from '../use-toggle' | ||
|
||
import type { ReactSetState } from '../use-safe-state' | ||
|
||
export type UseBooleanActions = { | ||
/** | ||
* Set the boolean state to `true`. | ||
*/ | ||
setTrue: () => void | ||
/** | ||
* Set the boolean state to `false`. | ||
*/ | ||
setFalse: () => void | ||
/** | ||
* Set the boolean state to the given value. | ||
*/ | ||
setState: ReactSetState<boolean> | ||
/** | ||
* Toggle the boolean state. | ||
*/ | ||
toggle: () => void | ||
} | ||
|
||
export type UseBooleanReturns = readonly [value: boolean, UseBooleanActions] | ||
|
||
/** | ||
* A hook to manage a boolean state. | ||
* | ||
* @param {boolean} [initialValue=true] The initial value of the boolean state. | ||
* | ||
*/ | ||
export function useBoolean(initialValue: boolean = true): UseBooleanReturns { | ||
const [value, toggle, _actions] = useToggle(initialValue) | ||
|
||
const actions = useCreation(() => ({ | ||
setTrue: () => _actions.setState(true), | ||
setFalse: () => _actions.setState(false), | ||
setState: _actions.setState, | ||
toggle, | ||
})) | ||
|
||
return [value, actions] as const | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# useBoolean | ||
|
||
import { HooksType } from '@/components' | ||
|
||
<HooksType category="State" /> | ||
|
||
一个用于帮助管理 `boolean` 状态的 React Hook。 | ||
|
||
:::tip | ||
|
||
如果你需要管理在几个值之间交替的状态,请考虑使用 [useCircularList](/reference/use-circular-list) 作为替代方案。 | ||
|
||
::: | ||
|
||
## 演示 Demo \{#demo} | ||
|
||
import { App } from './demo' | ||
|
||
<App /> | ||
|
||
## 用法 Usage \{#usage} | ||
|
||
import { RawCode } from '@/components' | ||
import source from '!!raw-loader!./demo' | ||
|
||
<RawCode>{source}</RawCode> | ||
|
||
## 源码 Source \{#source} | ||
|
||
import { Source } from '@/components' | ||
|
||
<Source /> | ||
|
||
## API \{#api} | ||
|
||
```tsx | ||
const [bool, actions] = useBoolean(initialBool) | ||
``` | ||
|
||
### 初始布尔值 InitialBool \{#initial-bool} | ||
|
||
表示初始状态的 `boolean` 值。 | ||
|
||
### 返回值 Returns \{#returns} | ||
|
||
```tsx | ||
export type UseBooleanActions = { | ||
/** | ||
* 将布尔状态设置为 `true`。 | ||
*/ | ||
setTrue: () => void | ||
/** | ||
* 将布尔状态设置为 `false`。 | ||
*/ | ||
setFalse: () => void | ||
/** | ||
* 将布尔状态设置为给定的值。 | ||
*/ | ||
setState: ReactSetState<boolean> | ||
/** | ||
* 切换布尔状态。 | ||
*/ | ||
toggle: () => void | ||
} | ||
|
||
export type UseBooleanReturns = readonly [value: boolean, UseBooleanActions] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters