【问题标题】:How to access react-bootstrap panel collapse state in panel title?如何在面板标题中访问 react-bootstrap 面板折叠状态?
【发布时间】:2018-07-23 22:20:17
【问题描述】:

在使用react-bootstrap Collapsible Panel 时,有没有办法从面板标题访问面板的折叠状态?在以下示例中:

<Panel>
    <Panel.Heading>
        <Panel.Title toggle>
            This is the title of the panel
            <i className="fa fa-angle-double-right" />
        </Panel.Title>
    </Panel.Heading>
    <Panel.Collapse>
        <Panel.Body>
            This is the body of the panel
        </Panel.Body>
    </Panel.Collapse>
</Panel>

我希望 Panel.Title 中的字体 awesome &lt;i&gt; 在面板打开时自动在 fa-angle-double-rightfa-angle-double-down 之间切换,并在面板关闭时切换回来。但我似乎无法从面板标题访问面板的折叠状态。

【问题讨论】:

    标签: reactjs react-bootstrap


    【解决方案1】:

    您链接到的文档似乎有您正在寻找的答案。

    您应该能够维护一些本地状态 this.state.open,并将其作为 expanded 属性传递给面板。

    class Example extends React.Component {
      constructor(props, context) {
        super(props, context);
    
        this.state = {
          // to share the state, we must maintain it ourselves
          open: true
        };
      }
    
    
      render() {
        const iconClass = this.state.open
          ? 'fa-angle-double-down'
          : 'fa-angle-double-right';
    
        return (
          <Panel id="example-1" expanded={this.state.open}>
            <Panel.Heading> 
              <Panel.Title
                onClick={() => this.setState({ open: !this.state.open})}>
                This is the title of the panel
                <i className={iconClass} />
              </Panel.Title>
            </Panel.Heading>
            <Panel.Collapse>
              <Panel.Body>
                This is the body of the panel
              </Panel.Body>
            </Panel.Collapse>
          </Panel>
        );
      }
    }
    

    【讨论】:

    • 我确实在文档中看到了这一点,但在我看来,这更多是关于从外部控制扩展状态。我不想重复面板所做的检查以确定它是否应该在导致箭头反映打开状态的检查中打开。 IMO,应该有一种方法可以在反映该状态时询问面板其状态(这是面板确实知道的)。不过,我很欣赏你的想法,除非有其他建议,否则我会开始应用它。谢谢!
    • 我想在 React 中你能做的就是用你自己的组件来包装组件,并让较低的组件反映包装器的状态。所以最后,我最终做了类似那个例子的事情。谢谢
    猜你喜欢
    • 2017-01-06
    • 2016-02-16
    • 2016-11-06
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    相关资源
    最近更新 更多