Skip to content

Commit bab33d3

Browse files
Merge pull request #64 from commitd/sh/setValue
feat(usemodal): re-exposes original setValue in boolean hooks
2 parents c5f35f5 + b29e2f7 commit bab33d3

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

src/useBoolean/useBoolean.stories.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export interface UseBooleanDocsProps {
1414
* returns the value, an object containing function for toggle, setTrue and setFalse.
1515
*
1616
* __Use `toggle` with caution__, attaching to buttons can cause unintended consequences from double clicks.
17+
* Also, re-exposes the `setValue` function in case required.
18+
*
1719
*/
1820
export const UseBooleanDocs: React.FC<UseBooleanDocsProps> = (
1921
_props: UseBooleanDocsProps

src/useBoolean/useBoolean.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function useBoolean(startState = false): [
1414
toggle: () => void
1515
setTrue: () => void
1616
setFalse: () => void
17+
setValue: React.Dispatch<React.SetStateAction<boolean>>
1718
}
1819
] {
1920
const [value, setValue] = useState(startState)
@@ -23,6 +24,7 @@ export function useBoolean(startState = false): [
2324
toggle: (): void => setValue((state) => !state),
2425
setTrue: (): void => setValue(true),
2526
setFalse: (): void => setValue(false),
27+
setValue,
2628
}),
2729
[setValue]
2830
)

src/useModal/useModal.stories.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface UseModalDocsProps {
1212
* Utility hook for modal state
1313
*
1414
* returns the visibility of the modal and functions to `show` and `hide`.
15+
* Also, re-exposes the `set` function in case required.
1516
*
1617
*/
1718
export const UseModalDocs: React.FC<UseModalDocsProps> = (

src/useModal/useModal.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import { useBoolean } from '../useBoolean'
99
*/
1010
export function useModal(
1111
startState = false
12-
): [visible: boolean, show: () => void, hide: () => void] {
13-
const [visible, { setTrue: show, setFalse: hide }] = useBoolean(startState)
14-
return [visible, show, hide]
12+
): [
13+
visible: boolean,
14+
show: () => void,
15+
hide: () => void,
16+
set: React.Dispatch<React.SetStateAction<boolean>>
17+
] {
18+
const [visible, { setTrue: show, setFalse: hide, setValue: set }] =
19+
useBoolean(startState)
20+
return [visible, show, hide, set]
1521
}

0 commit comments

Comments
 (0)