-
Notifications
You must be signed in to change notification settings - Fork 687
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Refactor Header component to function #1241
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,77 @@ | ||
import React, { Component, Suspense } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import React, { Suspense } from 'react'; | ||
import { bool, func, shape, string } from 'prop-types'; | ||
import MenuIcon from 'react-feather/dist/icons/menu'; | ||
import SearchIcon from 'react-feather/dist/icons/search'; | ||
|
||
import classify from 'src/classify'; | ||
import { Link, resourceUrl, Route } from 'src/drivers'; | ||
import Icon from 'src/components/Icon'; | ||
import SearchIcon from 'react-feather/dist/icons/search'; | ||
import MenuIcon from 'react-feather/dist/icons/menu'; | ||
import Logo from 'src/components/Logo'; | ||
import { Link, resourceUrl, Route } from 'src/drivers'; | ||
|
||
import CartTrigger from './cartTrigger'; | ||
import NavTrigger from './navTrigger'; | ||
import SearchTrigger from './searchTrigger'; | ||
|
||
const SearchBar = React.lazy(() => import('src/components/SearchBar')); | ||
|
||
import { mergeClasses } from 'src/classify'; | ||
import defaultClasses from './header.css'; | ||
import Logo from '../Logo'; | ||
|
||
class Header extends Component { | ||
static propTypes = { | ||
classes: PropTypes.shape({ | ||
logo: PropTypes.string, | ||
primaryActions: PropTypes.string, | ||
root: PropTypes.string, | ||
open: PropTypes.string, | ||
closed: PropTypes.string, | ||
secondaryActions: PropTypes.string, | ||
toolbar: PropTypes.string | ||
}), | ||
searchOpen: PropTypes.bool, | ||
toggleSearch: PropTypes.func.isRequired | ||
}; | ||
|
||
get searchIcon() { | ||
return <Icon src={SearchIcon} />; | ||
} | ||
const SearchBar = React.lazy(() => import('src/components/SearchBar')); | ||
|
||
render() { | ||
const { searchOpen, classes, toggleSearch } = this.props; | ||
const Header = props => { | ||
// Props. | ||
const { searchOpen, toggleSearch } = props; | ||
|
||
const rootClass = searchOpen ? classes.open : classes.closed; | ||
// Members. | ||
const classes = mergeClasses(defaultClasses, props.classes); | ||
const rootClass = searchOpen ? classes.open : classes.closed; | ||
const searchIcon = <Icon src={SearchIcon} />; | ||
|
||
return ( | ||
<header className={rootClass}> | ||
<div className={classes.toolbar}> | ||
<Link to={resourceUrl('/')}> | ||
<Logo classes={{ logo: classes.logo }} /> | ||
</Link> | ||
<div className={classes.primaryActions}> | ||
<NavTrigger> | ||
<Icon src={MenuIcon} /> | ||
</NavTrigger> | ||
</div> | ||
<div className={classes.secondaryActions}> | ||
<SearchTrigger | ||
searchOpen={searchOpen} | ||
toggleSearch={toggleSearch} | ||
> | ||
{this.searchIcon} | ||
</SearchTrigger> | ||
<CartTrigger /> | ||
</div> | ||
return ( | ||
<header className={rootClass}> | ||
<div className={classes.toolbar}> | ||
<Link to={resourceUrl('/')}> | ||
<Logo classes={{ logo: classes.logo }} /> | ||
</Link> | ||
<div className={classes.primaryActions}> | ||
<NavTrigger> | ||
<Icon src={MenuIcon} /> | ||
</NavTrigger> | ||
</div> | ||
<div className={classes.secondaryActions}> | ||
<SearchTrigger | ||
searchOpen={searchOpen} | ||
toggleSearch={toggleSearch} | ||
> | ||
{searchIcon} | ||
</SearchTrigger> | ||
<CartTrigger /> | ||
</div> | ||
<Suspense fallback={this.searchIcon}> | ||
<Route | ||
render={({ history, location }) => ( | ||
<SearchBar | ||
isOpen={searchOpen} | ||
history={history} | ||
location={location} | ||
/> | ||
)} | ||
/> | ||
</Suspense> | ||
</header> | ||
); | ||
} | ||
} | ||
</div> | ||
<Suspense fallback={searchIcon}> | ||
<Route | ||
render={({ history, location }) => ( | ||
<SearchBar | ||
isOpen={searchOpen} | ||
history={history} | ||
location={location} | ||
/> | ||
)} | ||
/> | ||
</Suspense> | ||
</header> | ||
); | ||
}; | ||
|
||
Header.propTypes = { | ||
classes: shape({ | ||
closed: string, | ||
logo: string, | ||
open: string, | ||
primaryActions: string, | ||
secondaryActions: string, | ||
toolbar: string | ||
}), | ||
searchOpen: bool, | ||
toggleSearch: func.isRequired | ||
}; | ||
|
||
export default classify(defaultClasses)(Header); | ||
export default Header; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm new to
React.lazy
. How can I see that this is indeed lazily loading? What affect should it have in the browser? I'm not seeing a new chunk/request being made which is confusing me :D