I want to create custom component for redux-form V6. It looks like buttons switcher.
component
import React, { Component } from 'react';
export default class ButtonSwitcher extends Component{
// props.buttons [{text: "Btn Text", value: "BtnValue"}]
render (){
return (
<div className="btn-group" style={{verticalAlign: 'top'}}>
{this.props.buttons.map((button, index)=>(
<a href="#" key={index} onClick={this.props.onChange} className={(this.props.value === button.value) ? 'active btn btn-default' : 'btn btn-default'}>{button.text}</a>
))}
</div>
);
}
}
I use this component in my form:
const renderButtonSwitcher = props => {
return (
<ButtonSwitcher value={props.input.value} onChange={props.input.onChange} buttons={props.data} />
)
};
<Field name="PODType" component={renderButtonSwitcher} data={[{text: '%', value: 'percent'}, {text: '$', value: 'amount'}]} />
How i can get selected button value? I can't find any advanced examples from redux-form V6
onSubmit(data) {
console.log("onSubmit", data);
}
onSubmit
showing empty data object
I find the solution
Now my component looks:
import React, { Component } from 'react';
export default class ButtonSwitcher extends Component{
// props.buttons [{text: "Btn Text", value: "BtnValue"}]
onClick(value){
this.props.onChange(value);
}
render (){
return (
<div className="btn-group" style={{verticalAlign: 'top'}}>
{this.props.buttons.map((button, index)=>(
<a href="#" key={index} onClick={this.onClick.bind(this, button.value)} className={(this.props.value === button.value) ? 'active btn btn-default' : 'btn btn-default'}>{button.text}</a>
))}
</div>
);
}
}
Usage in form component:
const renderButtonSwitcher = props => {
return (
<ButtonSwitcher {...props.input} buttons={props.data} />
)
};
<Field name="PODType" component={renderButtonSwitcher} data={[{text: '%', value: 'percent'}, {text: '₽', value: 'amount'}]} />
I find this discussion and that gives me some ideas
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With