-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add safeInvokeMember util * add Popover targetProps, using new util for overridden events * tests * only one spread * add note about ref * add notes about types
- Loading branch information
Showing
5 changed files
with
138 additions
and
5 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,85 @@ | ||
/* | ||
* Copyright 2018 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the terms of the LICENSE file distributed with this project. | ||
*/ | ||
|
||
import { isFunction } from "../utils"; | ||
|
||
/* | ||
Understanding the types here: | ||
`<Object, Key, ...Args, Return>` | ||
`{ [k in K]?: (a: A) => R }`: This is a MAPPED TYPE that enforces that keys `K` | ||
are all optional member functions with the given signature. The type `k` is only | ||
used in the mapping definition. | ||
`K extends keyof T`: A subset of the keys of the object `T`. The mapped type | ||
above then imposes a restriction on the _values_ of these keys in `T`. Note that | ||
`safeInvokeMember` only supports a single key, so the subset here has exactly | ||
one item. | ||
*/ | ||
|
||
/** | ||
* Safely invoke the member function with no arguments, if the object | ||
* exists and the given key is indeed a function, and return its value. | ||
* Otherwise, return `undefined`. | ||
*/ | ||
export function safeInvokeMember<T extends { [k in K]?: () => R }, K extends keyof T, R = void>( | ||
obj: T | undefined, | ||
key: K, | ||
): R | undefined; | ||
/** | ||
* Safely invoke the member function with one argument, if the object | ||
* exists and the given key is indeed a function, and return its value. | ||
* Otherwise, return `undefined`. | ||
* | ||
* ```js | ||
* // example usage | ||
* safeInvokeMember(this.props.inputProps, "onChange", evt); | ||
* ``` | ||
*/ | ||
export function safeInvokeMember<T extends { [k in K]?: (a: A) => R }, K extends keyof T, A, R = void>( | ||
obj: T | undefined, | ||
key: K, | ||
arg1: A, | ||
): R | undefined; | ||
/** | ||
* Safely invoke the member function with two arguments, if the object | ||
* exists and the given key is indeed a function, and return its value. | ||
* Otherwise, return `undefined`. | ||
*/ | ||
export function safeInvokeMember<T extends { [k in K]?: (a: A, b: B) => R }, K extends keyof T, A, B, R = void>( | ||
obj: T | undefined, | ||
key: K, | ||
arg1: A, | ||
arg2: B, | ||
): R | undefined; | ||
/** | ||
* Safely invoke the member function with three arguments, if the object | ||
* exists and the given key is indeed a function, and return its value. | ||
* Otherwise, return undefined. | ||
*/ | ||
export function safeInvokeMember< | ||
T extends { [k in K]?: (a: A, b: B, c: C) => R }, | ||
K extends keyof T, | ||
A, | ||
B, | ||
C, | ||
R = void | ||
>(obj: T | undefined, key: K, arg1: A, arg2: B, arg3: C): R | undefined; | ||
// tslint:disable-next-line:ban-types | ||
export function safeInvokeMember<T extends { [P in K]?: Function }, K extends keyof T>( | ||
obj: T | null | undefined, | ||
key: K, | ||
...args: any[] | ||
) { | ||
if (obj != null) { | ||
const member = obj[key]; | ||
if (isFunction(member)) { | ||
return member(...args); | ||
} | ||
} | ||
return undefined; | ||
} |
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
19d1b78
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Popover] add targetProps (#3213)
Previews: documentation | landing | table