【发布时间】:2021-02-05 19:20:15
【问题描述】:
我目前正在处理一个渠道,在几个页面上,用户必须上传文件才能在此渠道中取得进展。我的问题是,用户在页脚中使用“下一个”和“上一个”按钮浏览页面,我希望我的应用在文件上传仍为空时单击“下一步”时显示“无效”反馈。
这是我的 page.js:
import React, { Component } from "react";
import { withRouter } from "react-router-dom";
//React-Bootstrap components import
import { Col, Container, Form, ProgressBar, Row } from "react-bootstrap";
import {
Check2Circle,
CloudUploadFill,
ShieldLockFill,
} from "react-bootstrap-icons";
import { Footer } from "../../components/footer";
import PageTitle from "../../components/page_title";
class IdPage extends Component {
constructor(props) {
super(props);
this.state = {
file: this.props.file, //So if the user go back and come back on this page, the file stay in memory
};
this.onFormChange = this.onFormChange.bind(this);
}
nextPath() {
if (this.state.file === null) { //here is my condition (quiet simple but efficient) to cancel the "next" button action
return;
}
this.props.history.replace("6"); //I know that the replace function can be weird but I'll fix that later
}
previousPath(type) {
this.props.history.replace("4");
}
onChange(e) {
this.setState({ file: e.target.files[0] });
this.props.onFileChange(e);
}
render() {
const { type, service } = this.props;
var label = (
<span>
<CloudUploadFill size={20} color={`${lightblue}`} />
{" "}Mon justificatif d’identité
</span>
);
if (this.state.file) {
label = this.state.file.name;
}
return (
<>
<Container style={{ marginBottom: "120px" }} fluid>
<Row className="justify-content-center">
<Col lg={5} md={8} className="my-auto pt-4 main-fontsize">
<PageTitle text={`Mon nouveau contrat ${service}`} />
//Some text
{/* Form input to upload id document */}
<Form
noValidate
validated={this.state.validated}
className="my-3"
>
<Form.File
id="custom-file"
label=label
data-browse="Choose"
onChange={(e) => this.onChange(e)}
custom
required
isInvalid={!this.state.validated}
>
<Form.Control.Feedback type="invalid">
//invalid message
</Form.Control.Feedback>
</Form.File>
<Form.Text style={{ color: `${blue}` }}>
This file needs to be a pdf file or an image
</Form.Text>
</Form>
</Col>
</Row>
</Container>
<Footer
nextPage={() => this.nextPath()}
previousPage={() => this.previousPath(type)}
/>
</>
);
}
}
export default withRouter(IdPage);
如果你需要,这里是我的 footer.js:
import React from "react";
import { Container, Button, Row } from "react-bootstrap";
export const Footer = (props) => {
let previousButtonValue =
props.previousButtonValue === undefined ? "Previous" : "" ;
let bluebuttonvalue =
props.buttonValue === undefined ? "Next" : props.buttonValue;
return (
<footer
className="site-footer shadow-lg"
style={{
position: "fixed",
bottom: 0,
width: "100%",
backgroundColor: "white",
}}
>
<Container className="py-3" fluid>
<Container>
<Row className="justify-content-between my-auto">
<p className="ml-2 my-auto">
<a
className="primary"
onClick={props.previousPage}
style={{ cursor: "pointer" }}
>
<u>{previousButtonValue}</u>
</a>
</p>
<Button
className="text-right rounded-pill"
onClick={props.nextPage}
>
{bluebuttonvalue}
</Button>
</Row>
</Container>
</Container>
</footer>
);
};
export default {
Footer,
};
【问题讨论】:
-
欢迎来到 SO!我有一个非常相似的问题的答案How to set one component's state from another component in React,它是关于分享
setState(),但你可以使用任何功能,真的。希望这会有所帮助!