Я новичок в реакции и использую response-select для выбора соответствующих отделов. Я пытаюсь проверить раскрывающийся список выбора, так что, если раскрывающийся список пуст, он выдаст сообщение об ошибке. Я не знаю, как именно это сделать. Он показывает эту ошибку:
Departments.continue
D:/react_bpms1/src/components/Departments.js:69
66 | this.setState({ selectedOptions });
67 | };
68 | continue = e => {
> 69 | if (document.getElementById("Departments").value.length < 1) {
| ^ 70 | document.getElementById("departments").style.visibility = "visible";
71 | document.getElementById("Departments").style.border = "1px solid red";
72 | } else {
But here is what I am doing:
const Departments = [
{ label: "OneIT", value: "OneIT" },
{ label: "HR", value: "HR" },
{ label: "Vigilance", value: "Vigilance" },
{ label: "Ethics", value: "Ethics" },
{ label: "Corporate Services", value: "Corporate Services" },
{ label: "Legal", value: "Legal" },
{ label: "Sports", value: "Sports" },
{ label: "TQM", value: "TQM" },
{ label: "Iron Making", value: "Iron Making" },
{ label: "TMH", value: "TMH" }
];
class MultiSelect2 extends Component {
state = {
selectedOptions: []
};
handleChangeField = selectedOptions => {
this.setState({ selectedOptions });
};
render() {
const { selectedOption } = this.state;
return (
<div className="container">
<div className="row">
<div className="col-md-2"></div>
<div className="col-md-8">
<span>Select Department</span>
<Select
id="Departments"
htmlFor="Departments"
value={selectedOption}
options={Departments}
onChange={this.handleChangeField}
isMulti
/>
{this.state.selectedOptions.map(o => (
<p>{o.value}</p>
))}
</div>
<div className="col-md-4"></div>
</div>
</div>
);
}
}
export default MultiSelect2;
Вот где я вызываю опцию Multiselect:
export class Departments extends Component {
state = {
rows: [],
idx: [],
selectedOptions: []
};
handleChangeField = selectedOptions => {
this.setState({ selectedOptions });
};
continue = e => {
if (document.getElementById("Departments").value.length < 1) {
document.getElementById("departments").style.visibility = "visible";
document.getElementById("Departments").style.border = "1px solid red";
} else {
e.preventDefault();
this.props.nextStep();
}
};
back = e => {
e.preventDefault();
this.props.prevStep();
};
render() {
const { values, handleChange } = this.props;
const { selectedOption } = this.state;
const {
values: {
Title,
Details,
What,
Why,
How,
Status,
Cost,
Benefits,
Kpi_Before,
Kpi_After,
UOM_Before,
UOM_After,
Base_After,
Target_After,
dateTime_After,
Base_Before,
Target_Before,
Time,
dateTime,
departments,
Departments,
selectedOptions
}
} = this.props;
return (
<MuiThemeProvider theme={theme}>
<React.Fragment>
<div className={useStyles.root}>
<AppBar position="static">
<Toolbar>
<Typography
gutterBottom
align="center"
style={{ width: "100%", alignItems: "center" }}
>
Select Departments
</Typography>
</Toolbar>
</AppBar>
</div>
<br />
<br />
<Grid container>
<Grid item xs={6}>
<MultiSelect2
style={{ width: "80%" }}
id="Departments"
onChange={this.handleChangeField}
value={selectedOption}
/>
<label
id="departments"
style={{ visibility: "hidden", color: "red" }}
>
Select Applicable departments
</label>
</Grid>