【问题标题】:Use Vertical Steps component from antd with in a ScrollPanel from PrimeReact在 PrimeReact 的 ScrollPanel 中使用 antd 的 Vertical Steps 组件
【发布时间】:2020-01-01 20:38:21
【问题描述】:

我想在 primeReact ScrollPanel(https://www.primefaces.org/primereact/#/scrollpanel) 中使用来自 antd(https://ant.design/components/steps/) 的 Vertical Steps。我还想使用“antd”示例中所示的“Next”和“Previous”按钮。到目前为止一切正常,但是当我有很多步骤并且单击“下一步”移动到下一步但滚动器没有移动时,即突出显示的步骤移出视图。如何使滚动条也移动以使所选步骤位于中心视图中?

ScrollPanel 中的垂直步骤代码:

class VerticalStepsWithContent extends Component {
  constructor(props) {
    super(props);
    this.state = { current: 0 };
  }

  next() {
    const current = this.state.current + 1;
    this.setState({ current });
  }

  prev() {
    const current = this.state.current - 1;
    this.setState({ current });
  }

  onChange = current => {
    console.log("onChange:", current);
    this.setState({ current });
  };

  render() {
    const { Step } = Steps;
    const steps = [
      {
        title: "First",
        content: "First-content"
      },
      {
        title: "Second",
        content: "Second-content"
      },
      {
        title: "Last",
        content: "Last-content"
      }
    ];
    const { current } = this.state;
    return (
      <div>
        <div className="steps-action">
          {current < steps.length - 1 && (
            <Button type="primary" onClick={() => this.next()}>
              Next
            </Button>
          )}
          {current === steps.length - 1 && (
            <Button
              type="primary"
              onClick={() => message.success("Processing complete!")}
            >
              Done
            </Button>
          )}
          {current > 0 && (
            <Button style={{ marginLeft: 8 }} onClick={() => this.prev()}>
              Previous
            </Button>
          )}
        </div>
        <ScrollPanel
          header="Summary"
          style={{ height: "400px" }}
          className="custom"
        >
          <Steps
            direction="vertical"
            current={current}
            onChange={this.onChange}
          >
            {steps.map(item => (
              <Step key={item.title} title={item.title} />
            ))}
          </Steps>
        </ScrollPanel>
        {/* <div className="steps-content">{steps[current].content}</div> */}
      </div>
    );
  }
}
export default VerticalStepsWithContent;

【问题讨论】:

标签: reactjs antd primereact


【解决方案1】:

你没有bind class functions to this instance(这是一个常见的错误):

class VerticalStepsWithContent extends Component {
  constructor(props) {
    super(props);
    this.state = { current: 0 };
    this.next = this.next.bind(this);
    // bind rest of the functions
  }
}

你应该避免使用constructor,而是使用类属性:

class VerticalStepsWithContent extends Component {
  state = { current: 0 };

  next = () => {
    const current = this.state.current + 1;
    this.setState({ current });
  };

  // ...
}
export default VerticalStepsWithContent;

【讨论】:

  • 感谢您的指点,但这并不能解决问题,关于如何让滚动面板工作的任何想法。 TIA
猜你喜欢
  • 2020-05-06
  • 2021-06-02
  • 2020-06-08
  • 1970-01-01
  • 2021-09-07
  • 2018-02-26
  • 1970-01-01
  • 2019-04-22
  • 1970-01-01
相关资源
最近更新 更多