【问题标题】:How to expand Reactstrap Collapse component horizontally?如何水平展开 Reactstrap Collapse 组件?
【发布时间】:2018-11-29 17:38:20
【问题描述】:

默认情况下,reactstrap 折叠组件总是垂直折叠, 有什么让它水平折叠的技巧吗?

也许我错过了什么...... https://reactstrap.github.io/components/collapse/

【问题讨论】:

    标签: reactjs collapse reactstrap


    【解决方案1】:

    所以,我创建了一个可以水平或垂直折叠两种方式的组件。 垂直折叠仍在测试中,但它适用于水平折叠。

    export default class Collapse extends Component {
    
        static props = {       
            isOpen: PropTypes.bool.isRequired,        
            vertical: PropTypes.bool,
            elementMaxLength: PropTypes.string,
    
            onOpen: PropTypes.func,
            onClose: PropTypes.func,
        }
    
        static defaultProps = {
            vertical: false,
            elementMaxLength: '300px',
            onOpen: () => console.log('Opened'),
            onClose: () => console.log('Closed'),
        }
    
       constructor(props) {
           super(props);
    
           this.state = {
            cssTarget: '_collapseH'
           }
       }
    
       componentDidMount() {
            if (this.props.vertical)
                this.setState({cssTarget: '_collapseV'})
    
            if (this.props.isOpen)
                this.collapse();
       }
    
        componentDidUpdate(prevProps, prevState, snapshot) {
            if (prevProps.isOpen !== this.props.isOpen)
                this.collapse();
       }
    
       collapse() {
        var content = this.refs.collapseDiv;
        if (content)
           if (this.decide(content))
              this.close(content)
           else
              this.open(content)    
       }
    
       decide(content) {
          if (this.props.vertical)
            return content.style.maxHeight;
    
          return content.style.maxWidth;
       }
    
       open(content) {
          this.assign(content, this.props.elementMaxLength);      
          this.props.onOpen();
       }
    
       close(content) {
          this.assign(content, null)
          this.props.onClose();
      }
    
      assign(content, value) {
        if (this.props.vertical)      
          content.style.maxHeight = value;
        else
          content.style.maxWidth = value;
      }
    
       render() {
        return (
              <div ref='collapseDiv' target={this.state.cssTarget}> 
                {this.props.children}
              </div>
        );
      }
    }
    

    所以基本上我们渲染一个 DIV 并引用它,以便我们可以在我们的组件中使用this.refs 访问它。我们在这个 DIV 中渲染所有传递给这个组件的孩子。

    为了控制我们是否应该展开或折叠,我们使用了 isOpen 属性,它通过父组件中的this.setState 从 TRUE 变为 FALSE。

    当我们在父级中使用this.setState 时,它将触发对父级的重新渲染,同时也会触发对 Collapse 组件的重新渲染。这也会触发 componentDidUpdate 我们将在此处开始动画。

    为了控制动画,我使用了 CSS:

    div[target='_collapseV'] {
      display: flex;
      flex: 1;  
      overflow: hidden;
      background-color: maroon;
    
      max-height: 0;
      transition: max-height 1s ease-out;
    }
    
    div[target='_collapseH'] {
      display: flex;
      flex: 1;
      overflow: hidden;
      background-color: maroon;
    
      max-width: 0;    
      transition: max-width 1s ease;
    }
    

    目标属性设置在与我们设置ref 属性相同的DIV 中。如果道具 vertical 设置为 true,然后我们的目标 att 将更改为 _collapseV 使组件垂直折叠。

    为了触发动画,我们在assign 函数内部更改max-widthmax-height 的值,该函数在componentDidUpdate 内部调用。

    唯一的缺点是您必须知道内容的最大长度(宽度或高度) 在这个组件中渲染,并设置在 prop elementMaxLength 中。它不必是相同的值,但 elementMaxLength 应该大于内容长度。

    就是这样。

    我真的不知道这是否是最好的方法,我确信还有很大的改进空间。但我认为这是一个很好的解决方案,工作正常,您不必安装任何软件包。

    正如我之前所说的,垂直折叠仍然需要一些测试,但关键是要创建一些水平折叠的东西。

    【讨论】:

      猜你喜欢
      • 2020-12-22
      • 2021-01-14
      • 1970-01-01
      • 2012-02-26
      • 1970-01-01
      • 2019-03-11
      • 2018-02-03
      • 2021-07-13
      • 1970-01-01
      相关资源
      最近更新 更多