【问题标题】:Highlight the selected item of a navigation pane突出显示导航窗格的选定项目
【发布时间】:2020-05-08 14:48:31
【问题描述】:

参考https://developer.microsoft.com/en-us/fabric#/controls/web/nav 下的示例“带有嵌套链接的导航”,单击导航项时,我想突出显示该项。我已将 url 设置为 ' ',以便单击某个项目不会执行任何操作。但是,我希望在点击时突出显示该项目。我该怎么做呢? 任何指针都会有所帮助。

import * as React from 'react';
import { Nav,INavStyles } from 'office-ui-fabric-react/lib/Nav';
import { initializeIcons } from '@uifabric/icons';
initializeIcons();

    const navStyles: INavStyles = {
  root:{
      boxSizing: 'border-box',
      border: '1px solid lightgrey',
      overflowY: 'auto',
      height: 300
  },
  chevronButton: {
      height: 30
  },
  chevronIcon:{
      height: 30,
      lineHeight: 30
  },
  compositeLink: {}, 
  group:{}, 
  groupContent: {},
  link: {},
  linkText:{},
  navItem:{}, 
  navItems:{
    margin: 0
  },
};

export const NavNestedExample1: React.FunctionComponent = () => {
  return (
    <Nav
      styles={navStyles}
      ariaLabel="Nav example with nested links"
      groups={[
        {
          links: [
            {
              name: 'Parent link 1',
              url: '',
              target: '_blank',
              expandAriaLabel: 'Expand Parent link 1',
              collapseAriaLabel: 'Collapse Parent link 1',
              links: [
                {
                  name: 'Child link 1',
                  url: '',
                  target: '_blank'
                },
                {
                  name: 'Child link 2',
                  url: '',
                  target: '_blank',
                  expandAriaLabel: 'Expand Child link 2',
                  collapseAriaLabel: 'Collapse Child link 2',
                  links: [
                    {
                      name: '3rd level link 1',
                      url: '',
                      target: '_blank'
                    },
                    {
                      name: '3rd level link 2',
                      url: '',
                      target: '_blank'
                    }
                  ]
                },
                {
                  name: 'Child link 3',
                  url: '',
                  target: '_blank'
                }
              ]
            },
            {
              name: 'Parent link 2',
              url: '',
              target: '_blank',
              expandAriaLabel: 'Expand Parent link 2',
              collapseAriaLabel: 'Collapse Parent link 2',
              links: [
                {
                  name: 'Child link 4',
                  url: '',
                  target: '_blank'
                }
              ]
            }
          ]
        }
      ]}
    />
  );
};

【问题讨论】:

  • 你能提供一些代码吗?
  • 代码添加到主线程。
  • 我从来没有使用过这个库,但是在单击一个项目时它可能会触发一个事件,根据doc,您应该将 selectedKey 保持在状态

标签: reactjs office-ui-fabric react-tsx


【解决方案1】:

使用className 属性为导航应用额外的 CSS 类,INavProps interface

export const App: React.FunctionComponent = () => {
  return (
    <Nav
      className='nav' //here
      ariaLabel="Nav example with nested links"
      groups={[
            ....


 //App.css
    .nav :focus{ 
      color: brown;
      background-color: darksalmon ;
    }
   .nav :hover{ 
      color: .....;
      background-color: ......;
    }
   .nav :active{ 
      color: .....;
      background-color: ......;
    }


使用INavStylesselectors

const navStyles: INavStyles = {
  root: {
    boxSizing: 'border-box',
    border: '1px solid lightgrey',
    overflowY: 'auto',
    height: 300
  },
  linkText: {
    color: 'green',
    selectors: { '&:hover': { color: 'red' } }
  },
compositeLink: {
   selectors: {
      '&:active ,&:focus-within': { backgroundColor: 'orange' }
    }
  },
...

【讨论】:

  • 这确实解决了它。但是,我从使用 INavStyles 类型的 'styles' 属性中寻找更多。我的导航组件已经定义了“样式”,我想在“样式”本身中处理这种突出显示,而不是添加“className”属性。感谢您的投入,亚历克斯。
  • 我已经编辑了代码,现在使用“Nav”组件的“styles”属性。
  • 这只是在将鼠标悬停在导航项上时将文本颜色从绿色更改为红色。我正在寻找的是......当我点击一个项目说“儿童链接1”时,这个项目应该保持突出显示(基本上改变背景颜色)。目前,当您将鼠标悬停在某个项目上时,我们可以看到每个项目的背景颜色发生了变化。我想突出显示当前点击的项目。
  • What I am looking for is... when I click on an item say "Child link1" 这与您的问题不同,这就是我要求创建一个新问题的原因。要突出显示当前单击的项目,您可以使用compositeLink,正如我在更新的项目中所展示的那样。此外,您可以尝试与linklinkTextnavItem,...等其他人一起实现不同的风格。祝你好运。
  • 谢谢亚历克斯。会试试的。
猜你喜欢
  • 1970-01-01
  • 2020-06-03
  • 2016-10-12
  • 2017-04-01
  • 2019-07-07
  • 2018-12-23
  • 1970-01-01
  • 2014-10-09
  • 2014-12-21
相关资源
最近更新 更多