-
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.
- Loading branch information
1 parent
d9c3c59
commit f490ca6
Showing
5 changed files
with
107 additions
and
68 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,66 @@ | ||
import { isEqual } from 'lodash-es'; | ||
import { isFunction } from '@pansy/shared'; | ||
import { useDeepCompareEffect, useUnmount, usePrevious } from '@pansy/react-hooks'; | ||
import { toCapitalString } from '@/utils'; | ||
|
||
interface Options { | ||
setterMap?: Record<string, Function>; | ||
converterMap?: Record<string, Function>; | ||
} | ||
|
||
export function usePropsReactive< | ||
Ins extends Record<string, any> = any, | ||
P extends Record<string, any> = {}, | ||
>(props: P, ins: Ins, { setterMap = {}, converterMap = {} }: Options = {}) { | ||
const prevProps = usePrevious(props) as P; | ||
|
||
const onInstanceCreated = (instance: Ins) => { | ||
reactivePropChange(props, false); | ||
}; | ||
|
||
/** | ||
* 对参数变化进行处理 | ||
* @param nextProps | ||
* @param shouldDetectChange | ||
* @returns | ||
*/ | ||
const reactivePropChange = (nextProps: P, shouldDetectChange: boolean = true) => { | ||
if (!ins) return; | ||
|
||
try { | ||
Object.keys(nextProps).forEach((key) => { | ||
/** 忽略事件绑定处理 */ | ||
if (isFunction(props[key]) && /^on[A-Z]/.test(key)) return; | ||
|
||
let willReactive = true; | ||
if (shouldDetectChange) { | ||
willReactive = !isEqual(nextProps[key], prevProps?.[key]); | ||
} | ||
if (!willReactive) return; | ||
|
||
let setterParam = nextProps[key]; | ||
// 对值进行转换 | ||
if (key in converterMap) { | ||
setterParam = converterMap[key](nextProps[key]); | ||
} | ||
|
||
// 设置值 | ||
if (key in setterMap) { | ||
setterMap[key](setterParam, ins); | ||
} else { | ||
const trySetterName = `set${toCapitalString(key)}`; | ||
|
||
if (trySetterName in ins) { | ||
ins[trySetterName](setterParam); | ||
} | ||
} | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
return { | ||
onInstanceCreated, | ||
}; | ||
} |
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 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