-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#243 add use last changed hook, add demo for hook
- Loading branch information
Showing
4 changed files
with
75 additions
and
0 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,21 @@ | ||
import { useState } from 'react'; | ||
|
||
import { useLastChanged } from './useLastChanged'; | ||
|
||
const Demo = () => { | ||
const [inputValue, setInputValue] = useState(''); | ||
const value = useLastChanged(inputValue); | ||
|
||
return ( | ||
<div> | ||
<input | ||
placeholder='Type something...' | ||
onChange={(event) => setInputValue(event.target.value)} | ||
value={inputValue} | ||
/> | ||
<p>Last changed: {value}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Demo; |
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,25 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
|
||
import { useLastChanged } from './useLastChanged'; | ||
|
||
it('Should use last changed', () => { | ||
const { result } = renderHook(useLastChanged); | ||
|
||
expect(result.current).toBeNull(); | ||
}); | ||
|
||
it('Should return timestamp when value changes', () => { | ||
const { result, rerender } = renderHook((text = 'init text') => useLastChanged(text)); | ||
|
||
rerender('changed text'); | ||
|
||
expect(result.current).toBeCloseTo(Date.now(), -1); | ||
}); | ||
|
||
it('Should return initial value', () => { | ||
const initialValue = Date.now(); | ||
|
||
const { result } = renderHook(() => useLastChanged(null, { initialValue })); | ||
|
||
expect(result.current).toBe(initialValue); | ||
}); |
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,28 @@ | ||
import { useState } from 'react'; | ||
|
||
import { useDidUpdate } from '../useDidUpdate/useDidUpdate'; | ||
|
||
/** The use last changed options type */ | ||
export interface UseLastChangedOptions { | ||
initialValue?: number; | ||
} | ||
|
||
/** | ||
* @name useLastChanged | ||
* @description - Hook for records the timestamp of the last change | ||
* @category Time | ||
* | ||
* @param {any} source The source of the last change | ||
* @param {number | null} [options.initialValue=null] The initial value | ||
* @returns {number | null} Return timestamp of the last change | ||
* | ||
* @example | ||
* const lastChanged = useLastChanged(source); | ||
*/ | ||
export const useLastChanged = (source: any, options?: UseLastChangedOptions): number | null => { | ||
const [lastChanged, setLastChanged] = useState(options?.initialValue ?? null); | ||
|
||
useDidUpdate(() => setLastChanged(Date.now()), [source]); | ||
|
||
return lastChanged; | ||
}; |