-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(props): add mergeProps utility to resolve all defaultProps warnin…
…gs (#2581) * fix: defaultprops * fix: test about defaultProps --------- Co-authored-by: xiaoyatong <84436086+xiaoyatong@users.noreply.github.com>
- Loading branch information
1 parent
cec2549
commit 5574489
Showing
10 changed files
with
52 additions
and
29 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
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 |
---|---|---|
@@ -1,7 +1,33 @@ | ||
export function mergeProps<T, U>( | ||
defaultProps: T, | ||
props: U | ||
): { [Key in Extract<keyof T, keyof U>]: U[Key] } & | ||
{ [Key in Exclude<keyof U, keyof T>]?: U[Key] } { | ||
return { ...defaultProps, ...props } | ||
export function mergeProps<A, B>(a: A, b: B): B & A | ||
export function mergeProps<A, B, C>(a: A, b: B, c: C): C & B & A | ||
export function mergeProps<A, B, C, D>(a: A, b: B, c: C, d: D): D & C & B & A | ||
export function mergeProps(...items: any[]) { | ||
const ret: any = {} | ||
items.forEach((item) => { | ||
if (item) { | ||
Object.keys(item).forEach((key) => { | ||
if (item[key] !== undefined) { | ||
ret[key] = item[key] | ||
} | ||
}) | ||
} | ||
}) | ||
return ret | ||
} | ||
|
||
/** | ||
* Merge props and return the first non-undefined value. | ||
* The later has higher priority. e.g. (10, 1, 5) => 5 wins. | ||
* This is useful with legacy props that have been deprecated. | ||
*/ | ||
export function mergeProp<T, DefaultT extends T = T>( | ||
defaultProp: DefaultT, | ||
...propList: T[] | ||
): T | undefined { | ||
for (let i = propList.length - 1; i >= 0; i -= 1) { | ||
if (propList[i] !== undefined) { | ||
return propList[i] | ||
} | ||
} | ||
return defaultProp | ||
} |