在这种情况下,我会显示复选框列表。在这里,我希望整个div都充当一个复选框。当前,它仅适用于复选框和标签。相反,我希望整个div被选中/取消选中。
我们将不胜感激
沙箱:https://codesandbox.io/s/modest-meninsky-9lcmb
import React from "react";
import ReactDOM from "react-dom";
import { Checkbox } from "semantic-ui-react";
import "./styles.css";
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
data: [
{ display: "CB1", checked: false, name: "cb1" },
{ display: "CB2", checked: false, name: "cb2" },
{ display: "CB3", checked: false, name: "cb3" }
]
};
}
handleItemClick = (event, data) => {
const index = this.state.data.findIndex(item => item.name === data.name);
const optionsArr = this.state.data.map((prevState, i) =>
i === index
? {
display: prevState.display,
name: prevState.name,
checked: !prevState.checked
}
: prevState
);
this.setState({ data: optionsArr });
};
render() {
return (
<div>
<div className="menu-item-holder">
{this.state.data.map((item, i) => (
<div className="menu-item" key={i}>
<Checkbox
onChange={this.handleItemClick}
checked={item.checked}
label={item.display}
name={item.name}
/>
</div>
))}
</div>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
.menu-item-holder {
border: 1px solid #ccc;
width: 150px;
}
.menu-item {
padding: 5px 10px;
border-bottom: 1px solid #ccc;
cursor: pointer;
}
答案 0 :(得分:1)
只需用父宽度增加标签的宽度。在styles.css中的CSS下面添加
.ui.checkbox input.hidden+label {
width:150px;
}
答案 1 :(得分:1)
只需为.ui.checkbox
添加宽度。
.ui.checkbox {
width: 100%;
}