-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Follow up to comments on PR1559 #1756
Changes from 7 commits
959f393
14e9040
8c7070a
10225b8
ed09c13
50f4c94
ef73c14
3c54273
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import _ from 'underscore'; | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import {View} from 'react-native'; | ||
|
@@ -9,8 +10,11 @@ const propTypes = { | |
// Array of additional styles to add | ||
style: PropTypes.arrayOf(PropTypes.object), | ||
|
||
// Returns a function as a child to pass insets to | ||
children: PropTypes.func.isRequired, | ||
// Returns a function as a child to pass insets to or a node to render without insets | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I love how this solution turned out. It's much better than the dual components that I suggested. |
||
children: PropTypes.oneOfType([ | ||
PropTypes.node, | ||
PropTypes.func, | ||
]).isRequired, | ||
|
||
// Whether to include padding bottom | ||
includePaddingBottom: PropTypes.bool, | ||
|
@@ -47,7 +51,11 @@ const ScreenWrapper = props => ( | |
]} | ||
> | ||
<HeaderGap /> | ||
{props.children(insets)} | ||
{// If props.children is a function, call it to provide the insets to the children. | ||
_.isFunction(props.children) | ||
? props.children(insets) | ||
: props.children | ||
} | ||
</View> | ||
); | ||
}} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,16 +12,27 @@ import compose from '../../compose'; | |
import CONST from '../../../CONST'; | ||
|
||
const propTypes = { | ||
// Internal react-navigation stuff used to determine which view we should display | ||
// Navigation state for this navigator | ||
// See: https://reactnavigation.org/docs/navigation-state/ | ||
state: PropTypes.shape({ | ||
// Index of the focused route object in the routes array | ||
index: PropTypes.number, | ||
|
||
// List of route objects (screens) which are rendered in the navigator. It also represents the history in a | ||
// stack navigator. There should be at least one item present in this array. | ||
routes: PropTypes.arrayOf(PropTypes.shape({ | ||
|
||
// A unique key name for a screen. Created automatically by react-nav. | ||
key: PropTypes.string, | ||
})), | ||
}).isRequired, | ||
|
||
// The current view object that has a render method to call | ||
// Object containing descriptors for each route with the route keys as its properties | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I love these verbose comments! |
||
// See: https://reactnavigation.org/docs/custom-navigators/#usenavigationbuilder | ||
descriptors: PropTypes.objectOf(PropTypes.shape({ | ||
|
||
// A function which can be used to render the actual screen. Calling descriptors[route.key].render() will return | ||
// a React element containing the screen content. | ||
render: PropTypes.func, | ||
})).isRequired, | ||
|
||
|
@@ -38,6 +49,13 @@ const defaultProps = { | |
|
||
|
||
class ResponsiveView extends React.Component { | ||
/** | ||
* Returns the current descriptor for the focused screen in this navigators state. The descriptor has a function | ||
* called render() that we must call each time this navigator updates. It's important to use this method to render | ||
* a screen, otherwise any child navigators won't be connected to the navigation tree properly. | ||
* | ||
* @returns {Object} | ||
*/ | ||
getCurrentViewDescriptor() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remember I wanted to comment about this in the original PR, and I never did, and then when I remembered about it, I couldn't find the spot. So, sorry this is a late comment! My thought was that it looks like this could be a functional component and just have this logic as part of the function before the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, this is an easy enough change to make and enforces the preference to use function components. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
const currentRoute = this.props.state.routes[this.props.state.index]; | ||
const currentRouteKey = currentRoute.key; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,36 +5,33 @@ import { | |
import linkingConfig from './linkingConfig'; | ||
|
||
export default function linkTo(navigation, path) { | ||
if (!path.startsWith('/')) { | ||
throw new Error(`The path must start with '/' (${path}).`); | ||
} | ||
|
||
const normalizedPath = !path.startsWith('/') ? `/${path}` : path; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could have sworn we had a utility for adding slashes to the beginning of things, but for the life of me, I can't find it. I can't remember where it was used either... Does anything come to your mind? If not, then ignore this comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is one used in config.js I think, but it adds them to the end not the the front There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes! That's the one. It might be nice to add these to |
||
if (navigation === undefined) { | ||
throw new Error("Couldn't find a navigation object. Is your component inside a screen in a navigator?"); | ||
} | ||
|
||
const state = linkingConfig?.getStateFromPath | ||
? linkingConfig.getStateFromPath(path, linkingConfig.config) | ||
: getStateFromPath(path, linkingConfig?.config); | ||
const state = linkingConfig.getStateFromPath | ||
? linkingConfig.getStateFromPath(normalizedPath, linkingConfig.config) | ||
: getStateFromPath(normalizedPath, linkingConfig.config); | ||
|
||
if (state) { | ||
let root = navigation; | ||
let current; | ||
if (!state) { | ||
throw new Error('Failed to parse the path to a navigation state.'); | ||
} | ||
|
||
// Traverse up to get the root navigation | ||
// eslint-disable-next-line no-cond-assign | ||
while ((current = root.dangerouslyGetParent())) { | ||
root = current; | ||
} | ||
let root = navigation; | ||
let current; | ||
|
||
const action = getActionFromState(state, linkingConfig?.config); | ||
// Traverse up to get the root navigation | ||
// eslint-disable-next-line no-cond-assign | ||
while ((current = root.dangerouslyGetParent())) { | ||
root = current; | ||
} | ||
|
||
const action = getActionFromState(state, linkingConfig.config); | ||
|
||
if (action !== undefined) { | ||
root.dispatch(action); | ||
} else { | ||
root.reset(state); | ||
} | ||
if (action !== undefined) { | ||
root.dispatch(action); | ||
} else { | ||
throw new Error('Failed to parse the path to a navigation state.'); | ||
root.reset(state); | ||
} | ||
} |
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.
NAB: Random thought; these could be nested to make them a little more organized.
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.
Only problem with that is that
ROUTES.SETTINGS
is already'settings'
so what convention should we use then? Perhaps something like:I'm kind of partial to the underscore convention.
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.
Cool, works for me!