Skip to content

Commit

Permalink
onReady & onSubmit fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Rosenzweig committed Jul 5, 2018
1 parent eeb86f4 commit abb3823
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import HubspotForm from 'react-hubspot-form'
portalId='your_portal_id'
formId='your_form_id'
onSubmit={() => console.log('Submit!')}
onReady={(form) => console.log('Form ready!')}
loading={<div>Loading...</div>}
/>
```
Expand Down
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class HubspotForm extends React.Component {
}
delete props.loading
delete props.onSubmit
delete props.onReady
let options = {
...props,
target: `#${this.el.getAttribute(`id`)}`,
Expand Down Expand Up @@ -51,9 +52,10 @@ class HubspotForm extends React.Component {
}
}
onSubmit(){
let interval = setInterval(() => {
clearInterval(this.onSubmitInterval)
this.onSubmitInterval = setInterval(() => {
if(!this.el.querySelector(`form`)){
clearInterval(interval)
clearInterval(this.onSubmitInterval)
if(this.props.onSubmit){
this.props.onSubmit()
}
Expand All @@ -65,6 +67,14 @@ class HubspotForm extends React.Component {
this.createForm()
this.findFormElement()
}
componentWillUnmount() {
clearInterval(this.onSubmitInterval)
}
componentDidUpdate(prevProps, prevState) {
if (this.state.loaded && !prevState.loaded && this.props.onReady) {
this.props.onReady(this.el);
}
}
render() {
return (
<div>
Expand Down

0 comments on commit abb3823

Please sign in to comment.