This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add useToggle hook to react-hooks (#1220)
- Loading branch information
Showing
11 changed files
with
115 additions
and
9 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
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,5 +1,6 @@ | ||
export {useLazyRef} from './lazy-ref'; | ||
export {default as useTimeout} from './timeout'; | ||
export {default as useOnValueChange} from './on-value-change'; | ||
export {useMountedRef} from './mounted-ref'; | ||
export {default as usePrevious} from './previous'; | ||
export {useOnValueChange} from './on-value-change'; | ||
export {usePrevious} from './previous'; | ||
export {useTimeout} from './timeout'; | ||
export {useToggle} from './toggle'; |
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
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,62 @@ | ||
import React from 'react'; | ||
import {mount} from '@shopify/react-testing'; | ||
|
||
import {useToggle} from '../toggle'; | ||
|
||
describe('useToggle', () => { | ||
function MockComponent({initialValueIsTrue = false}) { | ||
const {value, toggle, setTrue, setFalse} = useToggle(initialValueIsTrue); | ||
|
||
const activeText = value ? 'true' : 'false'; | ||
|
||
return ( | ||
<> | ||
<p>Value: {activeText}</p> | ||
<button type="button" id="toggle" onClick={toggle} /> | ||
<button type="button" id="forceTrue" onClick={setTrue} /> | ||
<button type="button" id="forceFalse" onClick={setFalse} /> | ||
</> | ||
); | ||
} | ||
|
||
it('starts with an initial value', () => { | ||
const wrapperInitallyFalse = mount(<MockComponent />); | ||
expect(wrapperInitallyFalse).toContainReactText('Value: false'); | ||
|
||
const wrapperInitallyTrue = mount(<MockComponent initialValueIsTrue />); | ||
expect(wrapperInitallyTrue).toContainReactText('Value: true'); | ||
}); | ||
|
||
it('toggles the value when the toggle callback is triggered', () => { | ||
const wrapper = mount(<MockComponent />); | ||
expect(wrapper).toContainReactText('Value: false'); | ||
|
||
wrapper.find('button', {id: 'toggle'}).trigger('onClick'); | ||
expect(wrapper).toContainReactText('Value: true'); | ||
|
||
wrapper.find('button', {id: 'toggle'}).trigger('onClick'); | ||
expect(wrapper).toContainReactText('Value: false'); | ||
}); | ||
|
||
it('forces the value to true when the forceTrue callback is triggered', () => { | ||
const wrapper = mount(<MockComponent />); | ||
expect(wrapper).toContainReactText('Value: false'); | ||
|
||
wrapper.find('button', {id: 'forceTrue'}).trigger('onClick'); | ||
expect(wrapper).toContainReactText('Value: true'); | ||
|
||
wrapper.find('button', {id: 'forceTrue'}).trigger('onClick'); | ||
expect(wrapper).toContainReactText('Value: true'); | ||
}); | ||
|
||
it('forces the value to false when the forceFalse callback is triggered', () => { | ||
const wrapper = mount(<MockComponent initialValueIsTrue />); | ||
expect(wrapper).toContainReactText('Value: true'); | ||
|
||
wrapper.find('button', {id: 'forceFalse'}).trigger('onClick'); | ||
expect(wrapper).toContainReactText('Value: false'); | ||
|
||
wrapper.find('button', {id: 'forceFalse'}).trigger('onClick'); | ||
expect(wrapper).toContainReactText('Value: false'); | ||
}); | ||
}); |
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,16 @@ | ||
import {useState, useCallback} from 'react'; | ||
|
||
/** | ||
* Returns a stateful value, and a set of memoized functions to toggle it, | ||
* set it to true and set it to false | ||
*/ | ||
export function useToggle(initialState: boolean) { | ||
const [value, setState] = useState(initialState); | ||
|
||
return { | ||
value, | ||
toggle: useCallback(() => setState(state => !state), []), | ||
setTrue: useCallback(() => setState(true), []), | ||
setFalse: useCallback(() => setState(false), []), | ||
}; | ||
} |