[react-native] Account for nativeOnly props in requireNativeComponent

Adjusted the prop types of the returned native component class so that (a) the `nativeOnly` set of props adds to the prop types and (b) not all the props from the JS component are required (using `Partial`).
This commit is contained in:
James Ide
2018-09-17 17:38:49 -07:00
parent b697ec8a1b
commit 954567e501
2 changed files with 24 additions and 3 deletions

View File

@@ -8970,11 +8970,13 @@ export interface ComponentInterface<P> {
* Common types are lined up with the appropriate prop differs with
* `TypeToDifferMap`. Non-scalar types not in the map default to `deepDiffer`.
*/
export function requireNativeComponent<P>(
export function requireNativeComponent<P, NP = {}>(
viewName: string,
componentInterface?: ComponentInterface<P>,
extraConfig?: { nativeOnly?: any }
): React.ComponentClass<PropTypes.InferProps<PropTypes.ValidationMap<P>>>;
extraConfig?: { nativeOnly?: NP }
): React.ComponentClass<
Partial<PropTypes.InferProps<PropTypes.ValidationMap<P>>> & { [K in keyof NP]?: any}
>;
export function findNodeHandle(
componentOrHandle: null | number | React.Component<any, any> | React.ComponentClass<any>

View File

@@ -11,6 +11,7 @@ The content of index.io.js could be something like
For a list of complete Typescript examples: check https://github.com/bgrieder/RNTSExplorer
*/
import * as PropTypes from "prop-types";
import * as React from "react";
import {
Alert,
@@ -75,6 +76,7 @@ import {
Modal,
TimePickerAndroid,
ViewPropTypes,
requireNativeComponent,
} from "react-native";
declare module "react-native" {
@@ -762,3 +764,20 @@ const TimePickerAndroidTest = () => (
mode: 'spinner'
})
)
class BridgedComponentTest extends React.Component {
static propTypes = {
jsProp: PropTypes.string.isRequired,
...ViewPropTypes,
}
render() {
return <NativeBridgedComponent {...this.props} nativeProp="test" />;
}
}
const NativeBridgedComponent = requireNativeComponent("NativeBridgedComponent", BridgedComponentTest, {
nativeOnly: {
nativeProp: true,
}
});