• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

alpertuna/react-metismenu: A ready / simple to use, highly customizable, updatea ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

alpertuna/react-metismenu

开源软件地址(OpenSource Url):

https://github.com/alpertuna/react-metismenu

开源编程语言(OpenSource Language):

JavaScript 94.9%

开源软件介绍(OpenSource Introduction):

NOTE: This library is not under active development now.

Build Status Coverage Status npm version peerDependencies Status

NPM

react-metismenu

A ready / simple to use, highly customizable, updateable, ajax supported, animated and material designed menu component for React

Demo

Here is a simple demo to show animations and actions with standart theme. Go to demo

react-metismenu-router-link link component to use with react-router. Go to demo - Extension Page

Install

npm install --save react-metismenu

Usage

With Ecma Script 6 and React Loaders

import React from 'react';
import ReactDOM from 'react-dom';
import MetisMenu from 'react-metismenu';

ReactDOM.render(<MetisMenu />, document.getElementById('dom_id'));

Without Loaders (ES5)

const React = require('react');
const ReactDOM = require('react-dom');
const MetisMenu = require('react-metismenu');

ReactDOM.render(
    React.createElement(MetisMenu),
    document.getElementById('dom_id')
);

Also you should embed core css file to your html for material design and transition effects to work.

<link rel="stylesheet" type="text/css" href="https://cdn.rawgit.com/alpertuna/react-metismenu/master/dist/react-metismenu-standart.min.css" />

You can find this css in your node_modules/react-metismenu/dist to embed locally.

Properties

MetisMenu (React Component) Properties

  • Updateable Properties (by state)

    • Properties To Set Content (See Properties For Each Item In Content)
      • {Object[]} [content=[]] - It keeps all recursive structure of Metismenu
      • {string | Object[]} [ajax] - Url or ajax settings object to get menu as json from remote. (See Remote Contents)
    • Active Link Selectors (Higlights and drops down all parents if it is a submenu item)
      • {boolean} [activeLinkFromLocation] - Automatically highlights link matched item to and browser location.
      • {string | number} [activeLinkId] - Find and highlight according to item id.
      • {string} [activeLinkTo] - Find and highlight according to item to.
      • {string} [activeLinkLabel] - Find and highlight according to item label.
  • Non-Updateable Properties (by state)

    • Customizing Styles
      • {string} [className] - Class name for main metismenu wrapper

      • {string|func} [classNameContainer] - Class name or dynamic class name producer callback for item container (Affects ul)

      • {string} [classNameContainerVisible] - Additional class name when container is visible (Affects ul)

      • {string} [classNameItem] - Class name for items in container (Affects li)

      • {string} [classNameItemActive] - Additional class name when item link is active (Affects li)

      • {string} [classNameItemHasActiveChild] - Additional class name when any child or grandchild item link is active (Affects li)

      • {string} [classNameItemHasVisibleChild] - Additional class name when any child or grandchild item link is visible (Affects li)

      • {string} [classNameLink] - Class name for links in items (Affects a)

      • {string} [classNameLinkActive] - Additional class name when link is active (Affects a)

      • {string} [classNameLinkHasActiveChild] - Additional class name when any child or grandchild link is active (Affects a)

      • {string} [classNameIcon] - Class name for link icons

      • {string} [classNameStateIcon] - Class name for state indicators of submenu

      • {boolean} [noBuiltInClassNames=false] - When true, core css class names won't be used

      • {string} [iconNamePrefix="fa fa-"] - Prefix for all icon's style class name

      • {string} [iconNameStateHidden="caret-left"] - Icon name for state of collapsed containers

      • {string} [iconNameStateVisible="caret-left rotate-minus-90"] - Icon name for state of opened containers

    • Customizing Link Component
      • {React.Component} [LinkComponent=DefaultLink] - Handles link components of all items
    • Event Listeners
      • {callback} [onSelected] - Function is called when a menu is selected.
    • Using With Redux
      • {string} [reduxStoreName="metisMenuStore"] - Redux store field name for react-metismenu to use
      • {object} [useExternalReduxStore] - Created redux store

Properties For Each Item In Content

  • {string} [icon] - Icon class name of item
  • {string} label - Label of item
  • {string} [to] - Href address to link (if item has submenu, to property will be ignored)
  • {boolean} [externalLink] - If true link opens page in new tab/window
  • {Object[]} [content] - Sub menu of item. (For Flat Contents you may use id and parentId properties instead.)
  • {string | number} [id] - Necessary for Flat Contents, or useful when activating a link of menu contains non-unique links. Possbile values are; not empty string, or greater than 0 if it is number.
  • {string | number} [parentId] - Necessary for Flat Contents. If item has no parent, top item, parentId should be falsy -one of false, undefined, null, empty string or number 0, not string "0".

Note for all properties

Curly brackets {...} refers to property type. After types, square brackets [...] means that property is optional. Equal sign = in square brackets shows its default value.

Examples

Simple Usage

Simple usage with recursive content object.

import React from 'react';
import ReactDOM from 'react-dom';
import MetisMenu from 'react-metismenu';

const content=[
    {
        icon: 'icon-class-name',
        label: 'Label of Item',
        to: '#a-link',
    },
    {
        icon: 'icon-class-name',
        label: 'Second Item',
        content: [
            {
                icon: 'icon-class-name',
                label: 'Sub Menu of Second Item',
                to: '#another-link',
            },
        ],
    },
];

ReactDOM.render(
  <MetisMenu content={content} activeLinkFromLocation />,
  document.getElementById('root')
);

See Properties For Each Item In Content.

See activeLinkFromLocation property.

Flat Contents

You may get menu content from a sql server. In this case, you can pass the content directly into react-metismenu without processing data. Here is flat json content example;

[
    {
        "id": 1,
        "icon": "icon-class-name",
        "label": "Label of Item",
        "to": "#a-link"
    },
    {
        "id": 2,
        "icon": "icon-class-name",
        "label": "Second Item"
    },
    {
        "id": 3,
        "parentId": 2,
        "icon": "icon-class-name",
        "label": "Sub Menu of Second Item",
        "to": "#another-link"
    }
]

Remote Contents

You are able to get json content from remote. Content may be recursive or flat. react-metismenu uses simple-ajax to send ajax request. You can pass just url string or object with these Available Options to ajax prop.

<MetisMenu ajax="/get-menu.php" />
<MetisMenu ajax={{
    url: "/service.php",
    method: "POST",
    data: {
        "method": "get-menu",
    },
}} />

Active Link Selectors

With Properties

Using properties make you able to update active link via state.

activeLinkFromLocation

Automatically highlight link matched item to and browser location.

It tries these posibilities to match location;

[
  window.location.pathname + window.location.search, // /path?search
  window.location.hash, // #hash
  window.location.pathname + window.location.search + window.location.hash, // /path?search#hash
]

Usage Example

<MetisMenu activeLinkFromLocation />
activeLinkId

Find and highlight according to item id. It should be not empty string, or greater than 0 if it is number.

Usage Example

<MetisMenu activeLinkId={this.state.activeLinkId} />
activeLinkTo

Find and highlight according to item to.

Usage Example

<MetisMenu activeLinkTo="/users" />
activeLinkLabel

Find and highlight according to item label.

Usage Example

<MetisMenu activeLinkLabel="User List" />

Note that, if you like to use more than one selector (activeLinkTo, activeLinkLabel, ...) at the same time, while setting the prop using one of them, you should set other props to null or undefined. For example; this.setState({ metisMenuActiveLinkId: null, metisMenuActiveLinkLabel: 'A Label' });. Otherwise, your component may not change active link.

With Methods

Also, you can update active links with methods accessed from reference

changeActiveLinkFromLocation()

Same with activeLinkFromLocation property.

Usage Example

class App extends React.Component {
    //...

    foo() {
        //...
        this.refs.menu.changeActiveLinkFromLocation();
        //...
    }

    render() {
        return (
            <div>
                ...
                <MetisMenu ref="menu" />
                ...
            </div>
        );
    }
}
changeActiveLinkId(id)

Same with activeLinkId property.

Usage Example

class App extends React.Component {
    //...

    foo() {
        //...
        this.refs.menu.changeActiveLinkId(3);
        //...
    }

    render() {
        return (
            <div>
                ...
                <MetisMenu ref="menu" />
                ...
            </div>
        );
    }
}
changeActiveLinkTo(to)

Same with activeLinkTo property.

Usage Example

class App extends React.Component {
    //...

    foo() {
        //...
        this.refs.menu.changeActiveLinkTo('/users');
        //...
    }

    render() {
        return (
            <div>
                ...
                <MetisMenu ref="menu" />
                ...
            </div>
        );
    }
}
changeActiveLinkLabel(label)

Same with activeLinkLabel property.

Usage Example

class App extends React.Component {
    //...

    foo() {
        //...
        this.refs.menu.changeActiveLinkLabel('User List');
        //...
    }

    render() {
        return (
            <div>
                ...
                <MetisMenu ref="menu" />
                ...
            </div>
        );
    }
}

Customizing Styles

After rendering react-metismenu with recursive content, output dom structure will be like this;

<div>             - main wrapper
  ====================================== Top container
  <ul>            - container
    <li>          - item
      <a>         - link
        <i />     - icon
        " "       - label
        <i />     - state icon (caret icon)
      </a>
      ---------------------------------- First depth sub container
      <ul>        - container
        <li>      - item
          <a>     - link
            <i /> - icon
            " "   - label
          </a>
        </li>
        ...
      </ul>
      ----------------------------------
    </li>
    ...
  </ul>
  ======================================
</div>

Overriding Styles

Metismenu with default setting adds built-in css class names.

These class names are, according to figure above;

  • main wrapper - metismenu
  • container - metismenu-container and visible for opened containers
  • item - metismenu-item
  • link - metismenu-link, active for active links, and has-active-child for links has active child or grandchild
  • icon - metismenu-icon
  • state icon - metismenu-state-icon

You can overide these class names to customize with your own css.

Note: Containers' default state is hidden and there is no assigned class to tell.

Using Your Own Class Names

You can tell metismenu to add your own class names by sending them as props.

Property names are, according to figure above;

  • main wrapper - className
  • container - classNameContainer and classNameContainerVisible for opened containers
  • item - classNameItem, classNameItemActive for active items, and classNameItemHasActiveChild for items has active child or grandchild
  • link - classNameLink, classNameLinkActive for active links, and classNameLinkHasActiveChild for links has active child or grandchild
  • icon - classNameIcon
  • state icon - classNameStateIcon

Using these props not overwrites built-in class names, just appends.

Note: Containers' default state is hidden and there is no prop to tell.

Not Using Built-in Styles

If you don't want use core styles you can remove them completely by setting noBuiltInClassNames prop true. In this case you are responsable for all styling including visibility states of containers.

Icon Framework

By default, metismenu uses Font Awesome for icons and prepends all icon names with fa fa-.

To use another icon framework, you can change prefix with iconNamePrefix prop.

To change state icons (shows submenu is visible or not) you can use these props;

  • iconNameStateVisible
  • iconNameStateHidden

These icons are also prepended by iconNamePrefix.

Customizing Style Example

<MetisMenu
  className="my-menu"
  clasNameLink="my-menu-link"
  iconNameStateVisible="minus"
  iconNameStateHidden="plus"
/>

Customizing Link Component

You are able to change the link component of each item. You may use another html tag, want to inject some properties or change operation logic. In this case, you can customize and use your own link component sending to react-metismenu component as LinkComponent property.

Props to use in your Link Component

  • {string} className - Passes built-in class name and classNameLink prop of top component
  • {string} classNameActive - Passes built-in class name and classNameLinkActive prop of top component
  • {string} classNameHasActiveChild - Passes built-in class name and classNameLinkHasActiveChild prop of top component
  • {boolean} active - Active link or not state
  • {boolean} hasActiveChild - Has active child or grand child state
  • {boolean} hasSubMenu - Has sub menu or not state
  • {function} toggleSubMenu - If item has submenu, toggle sub menu callback. Otherwise dead function.
  • {function} activateMe - If it is normal link, callback that activates link (to assign active class name) and makes all parents beware they have active link. Also triggers onSelected and given parameters are passed to listener.
  • {string} [to] - Contains to info of the item comes from menu content object
  • {boolean} [externalLink] - Destination is external or not
  • {React.Component} children - Ready to render content of link - contains icon, label and other stuff

An Example

Defining CustomLink Component


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap