我已经制作了一个https://github.com/johnwalley/allotment倍的可拖动拆分面板.

我想要认识到几件事:

  1. 点击Expand or collapse the allotment B将展开或折叠分配B.
  2. 我们可以拖动拆分器,拆分器的位置在折叠分配B之前被保存,这样我们可以在展开分配B时返回到该位置.
  3. 当我们第一次扩展分配B时,我希望分配B的高度是整个高度的五分之一.

我编写的代码如下(https://codesandbox.io/s/reset-forked-f2f386?file=/src/App.js).我无法同时意识到这一点.

有人知道为什么会是这样吗?

import React from "react";
import { Allotment } from "allotment";
import "allotment/dist/style.css";
import "./style.css";

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      toExpand: true,
      lastExpandedSize: [200, 50]
    };
    this.myRef = React.createRef();
  }

  handleChangeAllotment = (sizes) => {
    if (sizes.length > 1) {
      if (sizes[1] < 31) {
        this.setState({ toExpand: true });
      } else {
        this.setState({
          toExpand: false,
          lastExpandedSize: sizes // removing this line will set the initial sizes correctly, but will not be able to save last sizes.
        });
      }
    }
  };

  undertakeExpandOrCollapse = () => {
    if (this.state.toExpand) {
      this.myRef.current.resize(this.state.lastExpandedSize);
    } else {
      this.myRef.current.resize([10000, 0]);
    }
  };

  render() {
    return (
      <div
        style={{
          minWidth: 200,
          height: "100vh",
          overflowX: "auto",
          width: "auto",
          margin: "0px 0px 0px 0px"
        }}
      >
        <Allotment
          vertical
          onChange={this.handleChangeAllotment}
          ref={this.myRef}
        >
          <Allotment.Pane>
            <span onClick={this.undertakeExpandOrCollapse}>
              Expand or collapse the allotment B
            </span>
          </Allotment.Pane>
          <Allotment.Pane preferredSize="0%" minSize={30}>
            the allotment B
          </Allotment.Pane>
        </Allotment>
      </div>
    );
  }
}

推荐答案

我的快速 idea 是,默认情况下没有储存值,lastExpandedSize的值是null.感觉到这一点,我们传递屏幕高度的五分之一作为保存的高度.当关闭时,已经有一个保存值,因此useState函数将从那时起返回该值.

// ...

constructor(props) {
  super(props);
  this.state = {
    toExpand: true,
    lastExpandedSize: null // set null to default
  };
  this.containerRef = React.createRef();
  this.myRef = React.createRef();
}

/**
 * Check to has LastExpandedSize
 * @return {bool}
 */
hasLastExpandedSize = () => {
  return this.state.lastExpandedSize !== null;
}

/**
 * Get DefaultExpandedSize
 * @param {number} ratio
 * @param {number} height
 * @return {number[]}
 */
getDefaultExpandedSize = (ratio = 5, height = 0) => {
  if (height < 1) {
    height = this.containerRef.current.clientHeight;
  }

  return [
    height * (ratio - 1) / ratio, 
    height / ratio
  ];
}

handleChangeAllotment = (sizes) => {
  if (sizes.length > 1) {
    if (sizes[1] < 31) {
      this.setState({ toExpand: true });
    }
    else {
      this.setState({
        toExpand: false,
        lastExpandedSize: this.hasLastExpandedSize()
          ? sizes
          : this.getDefaultExpandedSize()
      });
    }
  }
};

// ...
<div
  ref={this.containerRef}
  style={{
    minWidth: 200,
    height: "100vh",
    overflowX: "auto",
    width: "auto",
    margin: "0px 0px 0px 0px"
  }}
>
  ...
</div>

Css相关问答推荐

如何将bslib::card()中的扩展按钮移到右上方,其中full_screen = TRUE?

JavaFX如何在Tableview中使用默认和自定义的列标题CSS样式?

Vue3日期 Select 器:禁用向左和向右箭头

本机CSS嵌套混乱

覆盖现有像素的混合

如何解决 Spotify 嵌入代码中的白色背景错误?

CSS Select 标签旁边的文本并输入

根据现有值计算CSS变量的新值

如何使按钮:之后像按钮:之前一样可见

创建一个CSS动画的对角线线条.

使用 flexbox 使图像与内容高度相同

为什么每次加载新页面时我的 React 侧边栏都会抖动?

包含tailwind 中固定纵横比框的内容

从 Angular SCSS 生成 CSS?

创建一条淡出/在各个方向变得透明的线

CSS nth-child 表示大于和小于

内联