Navigation 组件是一个已弃用的实验性组件,用于渲染带可选分组和层级的导航列表。建议开发者使用 Navigator 作为替代。
import {
__experimentalNavigation as Navigation,
__experimentalNavigationGroup as NavigationGroup,
__experimentalNavigationItem as NavigationItem,
__experimentalNavigationMenu as NavigationMenu,
} from '@wordpress/components';
const MyNavigation = () => (
<Navigation>
<NavigationMenu title="Home">
<NavigationGroup title="Group 1">
<NavigationItem item="item-1" title="Item 1" />
<NavigationItem item="item-2" title="Item 2" />
</NavigationGroup>
<NavigationGroup title="Group 2">
<NavigationItem
item="item-3"
navigateToMenu="category"
title="Category"
/>
</NavigationGroup>
</NavigationMenu>
<NavigationMenu
backButtonLabel="Home"
menu="category"
parentMenu="root"
title="Category"
>
<NavigationItem badge="1" item="child-1" title="Child 1" />
<NavigationItem item="child-2" title="Child 2" />
</NavigationMenu>
</Navigation>
);Render a navigation list with optional groupings and hierarchy.
import {
__experimentalNavigation as Navigation,
__experimentalNavigationGroup as NavigationGroup,
__experimentalNavigationItem as NavigationItem,
__experimentalNavigationMenu as NavigationMenu,
} from '@wordpress/components';
const MyNavigation = () => (
<Navigation>
<NavigationMenu title="Home">
<NavigationGroup title="Group 1">
<NavigationItem item="item-1" title="Item 1" />
<NavigationItem item="item-2" title="Item 2" />
</NavigationGroup>
<NavigationGroup title="Group 2">
<NavigationItem
item="item-3"
navigateToMenu="category"
title="Category"
/>
</NavigationGroup>
</NavigationMenu>
<NavigationMenu
backButtonLabel="Home"
menu="category"
parentMenu="root"
title="Category"
>
<NavigationItem badge="1" item="child-1" title="Child 1" />
<NavigationItem item="child-2" title="Child 2" />
</NavigationMenu>
</Navigation>
);
Navigation supports the following props.
activeItemstringThe active item slug.
activeMenustringThe active menu slug.
stringOptional className for the Navigation component.
onActivateMenufunctionSync the active menu between the external state and the Navigation’s internal state.
NavigationMenu supports the following props.
backButtonLabelstringThe back button label used in nested menus. If not provided, the label will be inferred from the parent menu’s title.
If for some reason the parent menu’s title is not available then it will default to “Back”.
onBackButtonClickfunctionA callback to handle clicking on the back button. If this prop is provided then the back button will be shown.
stringOptional className for the NavigationMenu component.
booleanEnable the search feature on the menu title.
menustringThe unique identifier of the menu. The root menu can omit this, and it will default to “root”; all other menus need to specify it.
( searchString: string ) => void;When hasSearch is active, this function handles the search input’s onChange event, making it controlled from the outside. It requires setting the search prop as well.
booleanIndicates whether the search is debouncing or not. In case of true the “No results found.” text is omitted. Used to prevent showing “No results found.” text between debounced searches.
parentMenustringThe parent menu slug; used by nested menus to indicate their parent menu.
stringWhen hasSearch is active and onSearch is provided, this controls the value of the search input. Required when the onSearch prop is provided.
isEmptybooleanIndicates whether the menu is empty or not. Used together with the hideIfTargetMenuEmpty prop of Navigation Item.
titlestringThe menu title. It’s also the field used by the menu search function.
titleActionReact.ReactNodeUse this prop to render additional actions in the menu title.
NavigationGroup supports the following props.
stringOptional className for the NavigationGroup component.
titlestringThe group title.
NavigationItem supports the following props.
badgestring|NumberThe item badge content.
stringOptional className for the NavigationItem component.
hrefstringIf provided, renders a instead of button.
iconReact.JSX.ElementIf no children are passed, this prop allows to specify a custom icon for the menu item.
itemstringThe unique identifier of the item.
navigateToMenustringThe child menu slug. If provided, clicking on the item will navigate to the target menu.
hideIfTargetMenuEmptybooleanIndicates whether this item should be hidden if the menu specified in navigateToMenu is marked as empty in the isEmpty prop. Used together with the isEmpty prop of Navigation Menu.
onClickReact.MouseEventHandlerA callback to handle clicking on a menu item.
isTextbooleanIf set to true then the menu item will only act as a text-only item rather than a button.
titlestringThe item title.