代码之家  ›  专栏  ›  技术社区  ›  JAAulde

如何在面板标题中访问react bootstrap面板折叠状态?

  •  2
  • JAAulde  · 技术社区  · 8 年前

    当使用 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>
    

    我想字体很棒 <i> 标签位于 Panel.Title fa-angle-double-right fa-angle-double-down 当面板打开时,在关闭时切换回但我似乎无法从面板标题访问面板的折叠状态。

    1 回复  |  直到 8 年前
        1
  •  3
  •   Danny Delott    8 年前

    你链接到的文档似乎有你要找的答案。

    你应该能维持当地的一些州, 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>
        );
      }
    }