I need to add form.item to switch button value without using valuePropName="checked". If I used this valuepropName it was difficult to implement my task. In that case, I will comment on it. Then I try to use defaultChecked={this.props.productBenefitDetailsDto.benefitRequired} but it works for one switch button but the other switch button does not support this. Reason is I used checked={this.state.checkedType} and onChange method in switch I think that's the reason. I can use Defaultcheck using without checking but it didn't work for my task. In my code, all values send to the backend and save. no errors in this code. The issue came from when values get back and present in UI one switch button work properly second one not working properly.
I explain my task
I have two switch buttons. The first one is (A) the Second one is B as an example. firstly I want to disable the B switch button already. Then I press the A switch button(value - true) B switch button must be enabled. it was working on my code. Then I click the B switch button (value - true) and I click the A switch button (value - false) In that case B switch button must be (value - false).
This is work in this code but the issue is my form is saved without error but when I get back the data and show the UI A switch button working B switch button not working properly. A button has a blue color and it was "ON" but b switch button was not colored and "ON or "OFF". How to fix this issue I can't find a solution for this.
My Code
interface Prop {
productBenefitDetailsDto: ProductBenefitDetailsDto;
isAuth: boolean
}
interface State {
checkedType: boolean
applicableType:boolean
checkedType:boolean
}
export class BenefitComp extends React.Component<Prop, State> {
constructor(prop: Prop) {
super(prop);
this.state = {
loading: false,
dropdownLoading: false,
checkedType: false,
applicableType : true,
checkedType:true
};
}
openCollapse = () => {
this.setState({openCollapse: !this.state.openCollapse});
};
enableButton = (checked: boolean) => {
if (checked){
this.props.productBenefitDetailsDto.benefitRequired = true
console.log("value switch if : ",this.state.checkedType, this.props.productBenefitDetailsDto.benefitRequired)
this.setState({
checkedType:false
})
// this.setState(prevState => ({
// checkedType:!prevState.checkedType
// }))
}else {
this.props.productBenefitDetailsDto.benefitRequired = false
console.log("value switch else : ", this.state.checkedType,this.props.productBenefitDetailsDto.benefitRequired)
this.setState({
checkedType:false
})
// this.enableButton2(false)
}
};
enableButton2 = (checked:boolean) => {
if (checked){
this.setState({
checkedType:true
})
}else{
this.setState({
checkedType:false
})
}
};
render() {
return (
<>
<Row gutter={[8, 8]}>
<Col span={10}>
</Col>
<Col span={2}>
<Form.Item
{...this.props.field}
name={[this.props.field.name, 'benefitEnabled']}
fieldKey={[this.props.field.fieldKey, 'benefitEnabled']}
// valuePropName="checked"
>
<Switch
disabled={this.props.isAuth}
checkedChildren={<CheckOutlined/>}
unCheckedChildren={<CloseOutlined/>}
defaultChecked={this.props.productBenefitDetailsDto.benefitEnabled}
onChange={this.enableButton}
/>
</Form.Item>
</Col>
<Col span={2}>
<Form.Item
{...this.props.field}
name={[this.props.field.name, 'benefitRequired']}
fieldKey={[this.props.field.fieldKey, 'benefitRequired']}
// valuePropName="checked"
>
<Switch
disabled={!this.props.isAuth && this.props.productBenefitDetailsDto.benefitRequired ? false:true}
checkedChildren={<CheckOutlined/>}
unCheckedChildren={<CloseOutlined/>}
checked={this.state.checkedType}
// defaultChecked={this.props.productBenefitDetailsDto.benefitRequired}
onChange={this.enableButton2}
/>
</Form.Item>
</Col>