[英]How to scroll to the top of a wizard form on page change
so I have a 3 page redux wizard form, whenever a new page is opened the view starts at the middle of the form, meaning the user must scroll to the top themselves.所以我有一个 3 页 redux 向导表单,每当打开一个新页面时,视图都会从表单的中间开始,这意味着用户必须自己滚动到顶部。 My form is in a
react-modal
.我的表格是
react-modal
。 I played around with window.scrollTo
but this of course won't work as I don't want to change the window location, rather the form in the modal.我玩过
window.scrollTo
但这当然行不通,因为我不想更改 window 的位置,而是模式中的表单。
I can't find many similar situations as mine as I have a form inside a modal.我找不到很多与我类似的情况,因为我在模态中有一个表单。
This problem only applies to the mobile view and tablet view of my site.此问题仅适用于我网站的移动视图和平板电脑视图。 The desktop view is fine.
桌面视图很好。
Would anyone have any hints as to what I should look at?任何人都会对我应该看什么有任何提示吗?
modal component模态组件
import React, { Component } from "react";
import Modal from "react-modal";
//Components
import QuoteForm from "./form.js";
class GetQuote extends Component {
constructor(props) {
super(props);
this.state = {
showModal: false
};
}
componentDidMount() {
Modal.setAppElement("body");
}
toggleModal = () => {
this.setState(prevState => {
return { showModal: !prevState.showModal };
});
};
render() {
return (
<div className="modalBtn-container">
<button className={this.props.type} onClick={this.toggleModal}>
{this.props.text}
</button>
<Modal
className={this.props.modalStyle}
isOpen={this.state.showModal}
onRequestClose={this.toggleModal}
contentLabel="popup modal form"
>
<QuoteForm toggleModal={this.toggleModal} />
</Modal>
</div>
);
}
}
export default GetQuote;
form component表单组件
import React, { Component } from "react";
//Components
import QuoteFormFirstPage from "./formFirstPage.js";
import QuoteFormSecondPage from "./formSecondPage.js";
class QuoteForm extends Component {
constructor(props) {
super(props);
this.state = {
page: 1
};
this.myRef = React.createRef();
this.nextPage = this.nextPage.bind(this);
this.previousPage = this.previousPage.bind(this);
}
nextPage() {
this.setState({ page: this.state.page + 1 });
this.scrollToMyRef();
}
previousPage() {
this.setState({ page: this.state.page - 1 });
}
scrollToMyRef = () => {
console.log(this.myRef.current.offsetTop);
window.scrollTo(0, this.myRef.current.offsetTop);
};
render() {
const { page } = this.state;
return (
<div className="quote-container" ref={this.myRef}>
{page === 1 && (
<QuoteFormFirstPage
close={this.props.toggleModal}
onSubmit={this.nextPage}
currentPage={page}
initialValues={this.props.initialValues}
enableReinitialize={true}
/>
)}
{page === 2 && (
<QuoteFormSecondPage
close={this.props.toggleModal}
previousPage={this.previousPage}
onSubmit={this.nextPage}
currentPage={page}
/>
)}
</div>
);
}
}
export default QuoteForm;
form first page表格第一页
import React from "react";
import { Field, reduxForm } from "redux-form";
const QuoteFormFirstPage = props => {
return (
<div className="quote-form">
<form onSubmit={props.handleSubmit}>
<div className="cancel-button-container">
<button className="cancel-btn" onClick={props.close}>
x
</button>
</div>
<div className="containerForm-row">
<div className="containerForm-col">
<div className="field-container">
<Field
name={`your_details.title`}
component={selectField}
labelClass=" hidden required"
className="small"
>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Mrs">Mrs</option>
<option value="Dr">Dr</option>
</Field>
<Field
name="your_details.first_name"
type="text"
placeholder="Forename"
containerClass="field-group medium"
component={dgInput}
label="What is your forename?"
labelClass="title-left required"
validate={[required]}
/>
</div>
</div>
</div>
<div className="form-container-button-first">
<button type="close" onClick={props.close} className="form-back">
Cancel
</button>
<button type="submit" className="btn-quote">
Next
</button>
</div>
</form>
</div>
);
};
export default reduxForm({
form: "quote", // <------ same form name
destroyOnUnmount: false, // <------ preserve form data
forceUnregisterOnUnmount: true // <------ unregister fields on unmount
})(QuoteFormFirstPage);
form second page表格第二页
import React from "react";
import { Field, reduxForm } from "redux-form";
const QuoteFormSecondPage = props => {
const { close, currentPage, handleSubmit, previousPage } = props;
return (
<div className="quote-form">
<form onSubmit={handleSubmit}>
<div className="cancel-button-container">
<button className="cancel-btn" onClick={close}>
x
</button>
</div>
<div className="form-row">
<Field
name="your_insurance_details.policy_start_date"
type="date"
placeholder={getCurrentDate()}
containerClass="field-group large"
component={dgInput}
label="When would you like your policy to start?"
labelClass="required"
validate={[required]}
/>
</div>
<div className="form-container-button">
<button type="button" className="form-back" onClick={previousPage}>
Back to property details
</button>
<button type="submit" className="btn-quote">
Next
</button>
</div>
</form>
</div>
);
};
export default reduxForm({
form: "quote", //Form name is same
destroyOnUnmount: false, // <------ preserve form data
forceUnregisterOnUnmount: true // <------ unregister fields on unmount
})(QuoteFormSecondPage);
I trimmed a lot of the form to try and make it more readable.我修剪了很多表格以尝试使其更具可读性。 As you can see in the form component, I tried using
createRef
and scrolling to it on lick of next page.正如您在表单组件中看到的那样,我尝试使用
createRef
并在下一页上滚动到它。 But that uses window.scrollTo
which won't work as I need to scroll to the top of the modal I'm in not the window!但这使用
window.scrollTo
这将不起作用,因为我需要滚动到模态的顶部我不在窗口中!
Could you provide a stackblitz or anything else so we could implement something?您能否提供一个 stackblitz 或其他任何东西,以便我们可以实施一些东西?
I would suggest to use an anchor and scroll to the element as described in How to scroll HTML page to given anchor?我建议使用锚点并滚动到元素,如如何将 HTML 页面滚动到给定锚点?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.