作为标题,
我想在页面完全加载时将复选框默认设置为“选中”,
但是在有关Material-UI Checkbox component的教程中,
没有defaultChecked
道具。
当我添加checked={true}
道具时,就会出现错误
像
“一个组件正在更改要控制的复选框类型的不受控制的输入等”
我该怎么办?
有我的密码
我要列出所有组,包括默认组,其他
让用户选择多组。
export default class AcceptButton extends Component {
constructor(props) {
super(props)
this.handleChange = this.handleChange.bind(this)
this.state = {open: true}
}
handleChange = event => {
this.setState({checked: event.target.checked})
}
render() {
return (
<Fragment>
<div>
group :
</div>
<div className="f-col">
{
group.data.map(g => {
if (data.filter(d => d.gid == g.gid).length != 0) {
return (
<FormControlLabel
key={g.gid}
control={
<Checkbox
disabled={true}
onChange = {this.handleChange}
color="primary"
checked={this.state.open}
/>
}
label={g.gname}
/>
)}
else {
return (
<FormControlLabel
key={g.gid}
control={
<Checkbox
onChange={this.handleChange}
value={g.gname}
color="primary"
/>
}
label={g.gname}
/>
)}
})
}
</div>
<button className="btn" onClick={this.postassignhandle}>update</button>
</Fragment>
)
答案 0 :(得分:5)
它确实具有 defaultChecked 道具,您可以像这样使用它:
<Checkbox defaultChecked={true}
onChange={this.handleChange}
color="primary"
/>
答案 1 :(得分:1)
选中复选框可指定地图类型
const checkboxstatemap = new Map()
export default class AcceptButton extends Component {
constructor(props) {
super(props)
this.handleChange = this.handleChange.bind(this)
this.state = {checkboxstate: group.data.map((item) => checkboxstatemap.set(item.gid, true ))}
}
componentDidMount() {
handleChange = event => {
this.setState({checked: event.target.checked})
}
render() {
return (
<Fragment>
<div>
group :
</div>
<div className="f-col">
{
group.data.map(g => {
if (data.filter(d => d.gid == g.gid).length != 0) {
return (
<FormControlLabel
key={g.gid}
control={
<Checkbox
disabled={true}
onChange = {this.handleChange}
color="primary"
checked={this.state.checkboxstate.get(g.gid)}
/>
}
label={g.gname}
/>
)}
else {
return (
<FormControlLabel
key={g.gid}
control={
<Checkbox
onChange={this.handleChange}
value={g.gname}
color="primary"
/>
}
label={g.gname}
/>
)}
})
}
</div>
<button className="btn" onClick={this.postassignhandle}>update</button>
</Fragment>
)
答案 2 :(得分:0)
import React from 'react';
import Checkbox from '@material-ui/core/Checkbox';
class Checkboxes extends React.Component {
state = {
checkedBox: true,
};
handleChange = name => event => {
this.setState({ [name]: event.target.checked });
};
render() {
return (
<div>
<Checkbox
checked={this.state.checkedBox}
onChange={this.handleChange('checkedBox')}
value="checkedBox"
/>
</div>
);
}
}
尝试一下。
答案 3 :(得分:0)
import React, { useCallback, useState } from 'react';
import { Checkbox, FormControlLabel } from '@material-ui/core';
const defaultProps = {
margin: 'normal',
onChange: (e) => {}
};
const InputCheckbox = (props) => {
const { onChange, label, value=false, name, checkedIcon, icon, className, style } = props;
const [ isChecked, setChecked ] = useState(value);
const _onChange = useCallback(
(e) => {
setChecked((value) => {
onChange(e, { [e.target.name]: !value });
return !value;
});
},
[ onChange ]
);
return <FormControlLabel className={className} style={style} label={label} control={<Checkbox name={name} icon={icon} checkedIcon={checkedIcon} checked={isChecked} onChange={_onChange} />} />;
};
InputCheckbox.defaultProps = defaultProps;
export default InputCheckbox;
使用onChange回调值并使用value进行初始化