Skip to content

Commit 37b75d3

Browse files
authored
feat(HubspotForm): add ref forwarding for HS form (#332)
1 parent 4cc47ae commit 37b75d3

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

src/internal-typings/global.d.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,17 @@ declare module '*.svg' {
1010
export default path;
1111
}
1212

13-
interface CreateFormProps {
14-
portalId: string;
15-
formId: string;
16-
region?: string;
17-
target?: string;
18-
cssClass?: string;
19-
formInstanceId?: string;
20-
}
13+
declare module '*.md';
2114

22-
interface Window {
23-
hbspt?: {
24-
forms: {
25-
create: (args: CreateFormProps) => unknown;
26-
};
27-
};
28-
ymaps: Ymaps;
15+
declare namespace Hbspt {
16+
interface CreateFormProps {
17+
portalId: string;
18+
formId: string;
19+
region?: string;
20+
target?: string;
21+
cssClass?: string;
22+
formInstanceId?: string;
23+
}
2924
}
3025

3126
declare namespace Ymaps {
@@ -63,4 +58,11 @@ declare namespace Ymaps {
6358
}
6459
}
6560

66-
declare module '*.md';
61+
interface Window {
62+
hbspt?: {
63+
forms: {
64+
create: (args: Hbspt.CreateFormProps) => unknown;
65+
};
66+
};
67+
ymaps: Ymaps;
68+
}

src/sub-blocks/HubspotForm/HubspotFormContainer.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useRef} from 'react';
1+
import React, {forwardRef, useImperativeHandle, useRef} from 'react';
22

33
import {useMount} from '../../hooks';
44
import {HubspotFormProps} from '../../models';
@@ -16,7 +16,7 @@ type HubspotFormContainerPropsKeys =
1616

1717
type HubspotFormContainerProps = Pick<HubspotFormProps, HubspotFormContainerPropsKeys>;
1818

19-
const HubspotFormContainer = (props: HubspotFormContainerProps) => {
19+
const HubspotFormContainer = forwardRef<HTMLDivElement, HubspotFormContainerProps>((props, ref) => {
2020
const {
2121
className,
2222

@@ -31,6 +31,9 @@ const HubspotFormContainer = (props: HubspotFormContainerProps) => {
3131
const containerRef = useRef<HTMLDivElement>(null);
3232
const hsContainerRef = useRef<HTMLDivElement>();
3333

34+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
35+
useImperativeHandle(ref, () => containerRef.current!);
36+
3437
const containerId = formInstanceId
3538
? `hubspot-form-${formId}-${formInstanceId}`
3639
: `hubspot-form-${formId}`;
@@ -74,6 +77,8 @@ const HubspotFormContainer = (props: HubspotFormContainerProps) => {
7477
});
7578

7679
return <div className={className} id={containerId} ref={containerRef} />;
77-
};
80+
});
81+
82+
HubspotFormContainer.displayName = 'HubspotFormContainer';
7883

7984
export default HubspotFormContainer;

src/sub-blocks/HubspotForm/index.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useContext, useMemo} from 'react';
1+
import React, {forwardRef, useContext, useMemo} from 'react';
22

33
import {MobileContext} from '../../context/mobileContext';
44
import {ThemeValueContext} from '../../context/theme/ThemeValueContext';
@@ -14,7 +14,7 @@ import './HubspotForm.scss';
1414

1515
const b = block('hubspot-form');
1616

17-
const HubspotForm: React.FunctionComponent<HubspotFormProps> = (props) => {
17+
const HubspotForm = forwardRef<HTMLDivElement, HubspotFormProps>((props, ref) => {
1818
const {
1919
className,
2020
theme: themeProp,
@@ -49,10 +49,10 @@ const HubspotForm: React.FunctionComponent<HubspotFormProps> = (props) => {
4949
onBeforeSubmit,
5050
onLoad,
5151
onSubmitError,
52-
onSubmit: (e) => {
52+
onSubmit: (event) => {
5353
handleMetrika?.({pixelEvents});
5454
handleAnalytics(analyticsEvents);
55-
onSubmit?.(e);
55+
onSubmit?.(event);
5656
},
5757
}),
5858
[
@@ -69,7 +69,6 @@ const HubspotForm: React.FunctionComponent<HubspotFormProps> = (props) => {
6969
);
7070

7171
useHandleHubspotEvents(handlers, formId);
72-
7372
return (
7473
<HubspotFormContainer
7574
createDOMElement={createDOMElement}
@@ -80,8 +79,11 @@ const HubspotForm: React.FunctionComponent<HubspotFormProps> = (props) => {
8079
portalId={portalId}
8180
formInstanceId={formInstanceId}
8281
region={region}
82+
ref={ref}
8383
/>
8484
);
85-
};
85+
});
86+
87+
HubspotForm.displayName = 'HubspotForm';
8688

8789
export default HubspotForm;

0 commit comments

Comments
 (0)