-
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.
feat: new API for useToggle hook (#96)
The hook structure and logic has been completely modified. It no longer toggles a boolean value by default, but now requires an array of values to toggle.
- Loading branch information
Showing
8 changed files
with
156 additions
and
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@raddix/use-toggle': major | ||
'@raddix/switch': minor | ||
--- | ||
|
||
New API for useToggle hook [#96](https://github.com/vintach/raddix/pull/96) |
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
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 |
---|---|---|
@@ -1 +1,17 @@ | ||
export { useToggle } from './use-toggle'; | ||
import { useState, useCallback } from 'react'; | ||
import type { Dispatch, SetStateAction } from 'react'; | ||
type ReturnValue<T> = [T, () => void, Dispatch<SetStateAction<T>>]; | ||
|
||
export const useToggle = <T>(values: T[], initValue?: T): ReturnValue<T> => { | ||
const [value, setValue] = useState(initValue ?? values[0]); | ||
|
||
const toggle = useCallback(() => { | ||
setValue(prev => { | ||
const currentIndex = values.indexOf(prev); | ||
const nextIndex = (currentIndex + 1) % values.length; | ||
return values[nextIndex]; | ||
}); | ||
}, [values]); | ||
|
||
return [value, toggle, setValue]; | ||
}; |
This file was deleted.
Oops, something went wrong.
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