我想制作一个用于提交表单的对话框组件。
class SubmitDialog extends React.PureComponent {
toggleDialog() {
this.setState({isOpen:!this.state.isOpen})
}
handleSubmit() {
// children need have handleSubmit
this.props.children.props.handleSubmit()
this.toggleDialog()
}
render(){
const {children, title } = this.props;
// I want to pass a button to trigger open this dialog
let trigger = React.cloneElement(this.props.trigger);
trigger.onClick = this.toggleDialog;
return (
{trigger},
<Dialog
open={this.state.isOpen}
onClose={() => {this.toggleDialog()}}
className="dialog"
>
<DialogHeader>
<DialogTitle>{title}</DialogTitle>
</DialogHeader>
<DialogBody scrollable className="dialog-body">
{children}
</DialogBody>
<DialogFooter className="align-axis">
<Button raised className="cyan white-text" onClick={()=> { this.toggleDialog() }}>取消</Button>
<Button raised primary onClick={()=> { this.handleSubmit() }} >提交</Button>
</DialogFooter>
</Dialog>
)
}
};
我想传递
答案 0 :(得分:3)
尝试:
React.cloneElement(this.props.trigger, {
onClick: this.toggleDialog
});