项目作者: oklas

项目描述 :
:house_with_garden: > breadcrumbs > extremely flexible > and > easy to use
高级语言: JavaScript
项目地址: git://github.com/oklas/react-breadcrumbs-dynamic.git
创建时间: 2017-06-28T19:31:37Z
项目社区:https://github.com/oklas/react-breadcrumbs-dynamic

开源协议:MIT License

下载


react-breadcrumbs-dynamic

Npm package
Npm downloads
Travis build status
Test Coverage
Dependency Status
Dependency Status
Dependency Status


🏠 > React dynamic breadcrumbs > extremely flexible > and > easy to use

This is completely router-independent solution. You can use it with any version
of React Router (2 or 3 or 4) or any other routing library for React or without
routing at all. All what you need is just to specify components for breadcrumbs
items and its props. However props and components should be specified
separately. Props should be specified
in intermediator component BreadcrumbsItem anywhere in your hierarchy of
components and routes.

Visit live DEMO (source code
of demo in example folder)

Synopsis

  1. const Profile = ({user}) => (
  2. <div>
  3. <BreadcrumbsItem
  4. to=`/user/${user.login}`
  5. icon='account-box'
  6. >
  7. {user.firstName} {user.lastName}
  8. </BreadcrumbsItem>
  9. <h1>
  10. {user.firstName} {user.lastName}
  11. </h1>
  12. </div>
  13. )

Installation

  1. npm install --save react-through react-breadcrumbs-dynamic
  2. # definitions may be installed if typescript is used
  3. # ( worked for 1.0.10, leave feedback if any )
  4. npm install --save @types/react-breadcrumbs-dynamic

Base configuration

Do you already use declarative communications through react tree with
react-through?
Just add <ThroughProvider></ThroughProvider> to the root of your React component tree:

  1. import {ThroughProvider} from 'react-through'
  2. const theApp = (
  3. <ThroughProvider>
  4. <App ></App>
  5. </ThroughProvider>
  6. )
  7. ReactDOM.render(theApp, document.getElementById('root'))

Instance configuration

In this example the react-router v4 <NavLink> is used as breadcrumbs item:

  1. import {Breadcrumbs} from 'react-breadcrumbs-dynamic'
  2. const Page = (props) => (
  3. return (
  4. <div className="App">
  5. <Header>
  6. <Breadcrumbs
  7. separator={<b> / </b>}
  8. item={NavLink}
  9. finalItem={'b'}
  10. finalProps={{
  11. style: {color: 'red'}
  12. }}
  13. />
  14. </Header>
  15. {props.children}
  16. <Footer>
  17. <Breadcrumbs/>
  18. </Footer>
  19. </div>
  20. )
  21. }

Please note that item and finalItem require react component (class) instead
of react element. However separator requires react element.

By default order of items is based on URL length. You can override the sort order
as you like just specify comparision function in compare prop which receive
pair of objects containing props of breadcrumbs item. For example:
<Breadcrumbs compare={(a,b)=>a.weight-b.weight} removeProps={{weight: true}} />.

If you use <a> tag based items then you will find renameProps or
duplicateProps useful to map prop to on prop href like this:
<Breadcrumbs renameProps={{to:"href"}} />.

Adding items to breadcrumbs

Each routed component in your react tree is generally associated with route
and with correspondent breadcrumbs. Each component may add its breadcrumbs
item by rendering <BreadcrumbsItem> with any props and children.

Example tree of routed components with breadcrumbs items:

  1. import {BreadcrumbsItem} from 'react-breadcrumbs-dynamic'
  2. const App = (props) => (
  3. <div>
  4. <BreadcrumbsItem to='/'>Main Page</BreadcrumbsItem>
  5. {props.children}
  6. <Route exact path="/user" component={User} ></Route>
  7. </div>
  8. )
  9. const User = (props) => (
  10. <div>
  11. <BreadcrumbsItem to='/user'>Home</BreadcrumbsItem>
  12. <h2>Home</h2>
  13. {props.children}
  14. <Route exact path="/user/contacts" component={Contacts} ></Route>
  15. </div>
  16. )
  17. const Contacts = (props) => (
  18. <div>
  19. <BreadcrumbsItem to='/user/contacts'>Contacts</BreadcrumbsItem>
  20. <h2>Contacts</h2>
  21. ...
  22. </div>
  23. )

You can declaratively pass props with any data, functions, components and so on
through react tree in any direction because
react-through allows to do that.

Result

The result of above code will represent breadcrumbs like this:

  1. <NavLink to='/'>Main Page</NavLink>
  2. <b> / </b>
  3. <NavLink to='/user'>Home</NavLink>
  4. <b> / </b>
  5. <b to='/user/contacts'>Contacts</b>

If you use library or if you think that it is good for use -
let people know about that - click the star.


The components and functions

Breadcrumbs component props

The breadcrumbs instance is implemented in the Breadcrumbs component, which
is the through container in terms of
react-through.

property type default description
separator element undefined separator between breadcrumbs items
item component a component of breadcrumbs items
finalItem component value of item prop component of final breadcrumbs item
finalProps object {} final item props - will override specified in BreadcrumbsItem
container component span wrapper component
containerProps object {} props for container component
compare function (a,b)=>a.to.length-b.to.length comparision function for sorting items
hideIfEmpty boolean false show or hide breadcrumbs container if items exist or not
renameProps object {} rename props passed from item BreadcrumbsItem to item
duplicateProps object {} duplicate props passed from item BreadcrumbsItem to item
removeProps object {} props aren’t passed from item BreadcrumbsItem to item

BreadcrumbsItem component props

The BreadcrumbsItem component is the through agent with bearing key in
prop to in terms of react-through.

The BreadcrumbsItem component may have any prop and may have children. Each prop
for BreadcrumbsItem will be passed to correspondent breadcrumb component specified
in item or finalItem prop of Breadcrumbs. Only one prop is mandatory.

property type default description
to string required mandatory required bearing key with URL
... any any properties - will be mapped to correspondent breadcrumb item

withBreadcrumbsItem() function

Advanced usage higher order component function. It acquire one argument with
your custom react component and return appropriate component which will have
breadcrumbs in its props with methods item() and items() like
throughAgent from react-through.

this.props.breadcrumbs.item() and this.props.breadcrumbs.items()

Advanced usage methods to configure breadcrumbs item of your react component.
These methods will be added to props by HOC of withBreadcrumbsItem function.
The function item() accepts one react component with props and the functions
items() accepts react component with children which may contain any number of
components to create correspondent number of breadcrumbs item. The breadcrumbs
item for these functions may be any react component and only props is
significant. The Dummy and the Item components is exported by library
for this case. Each function accepts null to reset breadcrumbs items to none if
current component is no more needed to represent any breadcrumbs items.

constants

The through area name used by this library is defined in
breadcrumbsThroughArea variable.

The prop name which contain bearing key is defined in
breadcrumbsBearingKey.

  1. import { breadcrumbsThroughArea } from 'react-breadcrumbs-dynamic'
  2. import { breadcrumbsBearingKey } from 'react-breadcrumbs-dynamic'

LICENSE

MIT