mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-02-10 09:13:43 +08:00
add withNavigationFocus HOC (#3512)
* add withNavigationFocus HOC See: - https://github.com/react-navigation/react-navigation/issues/51#issuecomment-276003658 - https://github.com/react-navigation/react-navigation/issues/51#issuecomment-278761705 - https://github.com/react-navigation/react-navigation/pull/3345#issuecomment-360260067 * typos * remove unused import * Add withNavigationFocus export * add example TabsWithNavigationFocus * add example TabsWithNavigationFocus * withNavigationFocus: get navigation from context or props * subs => subscriptions * fix flow issues
This commit is contained in:
committed by
Brent Vatne
parent
7f4706e4cc
commit
c74f001b1c
3
src/react-navigation.js
vendored
3
src/react-navigation.js
vendored
@@ -88,4 +88,7 @@ module.exports = {
|
||||
get withNavigation() {
|
||||
return require('./views/withNavigation').default;
|
||||
},
|
||||
get withNavigationFocus() {
|
||||
return require('./views/withNavigationFocus').default;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -32,4 +32,7 @@ module.exports = {
|
||||
get withNavigation() {
|
||||
return require('./views/withNavigation').default;
|
||||
},
|
||||
get withNavigationFocus() {
|
||||
return require('./views/withNavigationFocus').default;
|
||||
},
|
||||
};
|
||||
|
||||
56
src/views/withNavigationFocus.js
Normal file
56
src/views/withNavigationFocus.js
Normal file
@@ -0,0 +1,56 @@
|
||||
import React from 'react';
|
||||
import propTypes from 'prop-types';
|
||||
import hoistStatics from 'hoist-non-react-statics';
|
||||
import invariant from '../utils/invariant';
|
||||
|
||||
export default function withNavigationFocus(Component) {
|
||||
class ComponentWithNavigationFocus extends React.Component {
|
||||
static displayName = `withNavigationFocus(${Component.displayName ||
|
||||
Component.name})`;
|
||||
|
||||
static contextTypes = {
|
||||
navigation: propTypes.object.isRequired,
|
||||
};
|
||||
|
||||
state = {
|
||||
isFocused: false,
|
||||
};
|
||||
|
||||
componentDidMount() {
|
||||
const navigation = this.getNavigation();
|
||||
this.subscriptions = [
|
||||
navigation.addListener('didFocus', () =>
|
||||
this.setState({ isFocused: true })
|
||||
),
|
||||
navigation.addListener('willBlur', () =>
|
||||
this.setState({ isFocused: false })
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this.subscriptions.forEach(sub => sub.remove());
|
||||
}
|
||||
|
||||
getNavigation = () => {
|
||||
const navigation = this.props.navigation || this.context.navigation;
|
||||
invariant(
|
||||
!!navigation,
|
||||
'withNavigationFocus can only be used on a view hierarchy of a navigator. The wrapped component is unable to get access to navigation from props or context.'
|
||||
);
|
||||
return navigation;
|
||||
};
|
||||
|
||||
render() {
|
||||
return (
|
||||
<Component
|
||||
{...this.props}
|
||||
isFocused={this.state.isFocused}
|
||||
ref={this.props.onRef}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return hoistStatics(ComponentWithNavigationFocus, Component);
|
||||
}
|
||||
Reference in New Issue
Block a user