-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: useMergeState instead of effect sync (#261)
* refactor: simplify controlled value * test: update test case
- Loading branch information
Showing
5 changed files
with
170 additions
and
161 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
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,41 +1,20 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useMergedState } from 'rc-util'; | ||
import { useMemo } from 'react'; | ||
import type { Color } from '../color'; | ||
import type { ColorGenInput } from '../interface'; | ||
import { generateColor } from '../util'; | ||
|
||
type ColorValue = ColorGenInput | undefined; | ||
|
||
function hasValue(value: ColorValue) { | ||
return value !== undefined; | ||
} | ||
|
||
const useColorState = ( | ||
defaultStateValue: ColorValue, | ||
option: { | ||
defaultValue?: ColorValue; | ||
value?: ColorValue; | ||
}, | ||
defaultValue: ColorValue, | ||
value?: ColorValue, | ||
): [Color, React.Dispatch<React.SetStateAction<Color>>] => { | ||
const { defaultValue, value } = option; | ||
const [colorValue, setColorValue] = useState(() => { | ||
let mergeState; | ||
if (hasValue(value)) { | ||
mergeState = value; | ||
} else if (hasValue(defaultValue)) { | ||
mergeState = defaultValue; | ||
} else { | ||
mergeState = defaultStateValue; | ||
} | ||
return generateColor(mergeState); | ||
}); | ||
const [mergedValue, setValue] = useMergedState(defaultValue, { value }); | ||
|
||
useEffect(() => { | ||
if (value) { | ||
setColorValue(generateColor(value)); | ||
} | ||
}, [value]); | ||
const color = useMemo(() => generateColor(mergedValue), [mergedValue]); | ||
|
||
return [colorValue, setColorValue]; | ||
return [color, setValue]; | ||
}; | ||
|
||
export default useColorState; |
Oops, something went wrong.