gpt4 book ai didi

javascript - 如何取消选中 Antd React.js 中的 Checkbox.Group

转载 作者:行者123 更新时间:2023-11-29 22:55:41 25 4
gpt4 key购买 nike

我有以下代码使用 https://ant.design/components/checkbox/ , 并在选中复选框时尝试取消选中。如果单击按钮,我不想检查所有内容,只需取消选中所有内容或选中一个复选框。

constructor(props) {
super(props);
this.state = {
checked: true,
}
this.onChange = this.onChange.bind(this);
}


onChange(value) {
this.props.onChangeSession(value)
}

onChangeBox = e => {
this.setState({
checked: e.target.checked,
});
};

unChecked = () => {
this.props.onChangeSession([])
this.setState({
checked: false
});
};


render () {
const {
data,
} = this.props
return (
<div>
<Button type="primary" size="small" onClick={this.unChecked}>
Uncheck
</Button>
<Checkbox.Group
style={{ width: '100%' }}
onChange={this.onChange}>
<div>
<div className="filter-subhead">Track</div>

{data.map(i =>
<div className="filter-item">
<Checkbox
checked={this.state.checked}
onChange={this.onChangeBox}
value={i}>{i}</Checkbox>
</div>
)}
</div>
</Checkbox.Group>
</div>

)
}

任何帮助将不胜感激!

最佳答案

Working Link

由于 Checkbox.Group,复选框上的切换不起作用,您可以简单地使用 Checkbox


关于复选框状态:

您不能为所有复选框设置一个state,因此您需要一个bool 数组作为每个复选框项的状态.

在示例中,我在 componentDidMount 上初始化了复选框状态,它创建了一个数组 ([false,false,false,...]) 并且完全相同thing 用于重置 Uncheck。 (重构我的代码的可能性)

用户分配的状态将决定是否选中该复选框。

import React from "react";
import ReactDOM from "react-dom";
import { Button, Checkbox } from "antd";
import "antd/dist/antd.css";
import "./index.css";

let data = [3423, 3231, 334234, 55345, 65446, 45237];

class App extends React.Component {
constructor(props) {
super(props);
this.state = {
checkboxArray: []
};
// this.onChange = this.onChange.bind(this);
}

componentDidMount() {
let createEmptyArray = new Array(data.length).fill(false);
this.setState({ checkboxArray: createEmptyArray });
}

onChange(e) {
console.log(e.target);
}

onChangeBox = (e, i) => {
let checkBoxCurrentState = this.state.checkboxArray;
checkBoxCurrentState[i] = !checkBoxCurrentState[i];
this.setState({
checkboxArray: checkBoxCurrentState
});
};

unChecked = e => {
let resetArray = new Array(data.length).fill(false);
this.setState({
checkboxArray: resetArray
});
};

render() {
const { data } = this.props;
return (
<div>
<Button type="primary" size="small" onClick={this.unChecked}>
Uncheck
</Button>

<div>
<div className="filter-subhead">Track</div>
{data.map((i, index) => (
<div className="filter-item">
<Checkbox
checked={this.state.checkboxArray[index] ? true : false}
onChange={e => this.onChangeBox(e, index)}
value={index}
>
{JSON.stringify(this.state.checkboxArray[index])}
</Checkbox>
</div>
))}
</div>
{JSON.stringify(this.state.checkboxArray)}
</div>
);
}
}

ReactDOM.render(<App data={data} />, document.getElementById("root"));

简单的复制粘贴上面的代码,在需要的地方添加 Prop 。

如果你想使用Checkbox.Group,需要更新CheckBox.Group的onChange方法

let data = ['Apple', 'Pancakes', 'Butter', 'Tea', 'Coffee'];
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
checkboxArray: []
};
// this.onChange = this.onChange.bind(this);
}

componentDidMount() {
let createEmptyArray = new Array(this.props.data.length).fill(false);
this.setState({ checkboxArray: createEmptyArray });
}

onChangeBox = (e, i) => {
let checkBoxCurrentState = this.state.checkboxArray;
checkBoxCurrentState[i] = !checkBoxCurrentState[i];
this.setState({
checkboxArray: checkBoxCurrentState
});
};

unChecked = () => {
let resetArray = new Array(data.length).fill(false);
this.setState({
checkboxArray: resetArray
});
};

render() {
const { data } = this.props;
return (
<div>
<button onClick={this.unChecked}>Clear All</button>
{this.props.data.map((single, index) => (
<div>
<input
type="checkbox"
id={index}
name="scales"
checked={this.state.checkboxArray[index]}
onChange={e => this.onChangeBox(e, index)}
/>
<label for={index}>{single}</label>
</div>
))}
</div>
);
}
}

ReactDOM.render(<App data={data} />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>

关于javascript - 如何取消选中 Antd React.js 中的 Checkbox.Group,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56694972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com