Skip to content

Commit 8483642

Browse files
authored
feat(HubspotForm): default values (#1166)
1 parent ee401eb commit 8483642

File tree

6 files changed

+80
-2
lines changed

6 files changed

+80
-2
lines changed

src/internal-typings/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ declare namespace Hbspt {
2020
target?: string;
2121
cssClass?: string;
2222
formInstanceId?: string;
23+
onFormReady?: (form: HTMLFormElement) => void;
2324
}
2425
}
2526

src/models/constructor-items/sub-blocks.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import {ClassNameProps, QuoteType} from '../../models';
22
import {ThemeSupporting} from '../../utils';
3-
import {HubspotEventData, HubspotEventHandlers} from '../../utils/hubspot';
3+
import {
4+
HubspotEventData,
5+
HubspotEventHandlers,
6+
HubspotFormDefaultValues,
7+
} from '../../utils/hubspot';
48
import {AnalyticsEventsBase} from '../common';
59

610
import {ContentBlockProps} from './blocks';
@@ -103,6 +107,7 @@ export interface HubspotFormProps extends HubspotEventHandlers, AnalyticsEventsB
103107
onLoad?: (arg: HubspotEventData) => void;
104108
hubspotEvents?: string[];
105109
createDOMElement?: boolean;
110+
defaultValues?: HubspotFormDefaultValues;
106111
}
107112

108113
//cards

src/sub-blocks/HubspotForm/HubspotFormContainer.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {useMount} from '../../hooks';
44
import {HubspotFormProps} from '../../models';
55

66
import loadHubspotScript from './loadHubspotScript';
7+
import {setHubspotDefaultValues} from './setHubspotDefaultValues';
78

89
type HubspotFormContainerPropsKeys =
910
| 'className'
@@ -12,7 +13,8 @@ type HubspotFormContainerPropsKeys =
1213
| 'portalId'
1314
| 'region'
1415
| 'formClassName'
15-
| 'createDOMElement';
16+
| 'createDOMElement'
17+
| 'defaultValues';
1618

1719
type HubspotFormContainerProps = Pick<HubspotFormProps, HubspotFormContainerPropsKeys>;
1820

@@ -27,6 +29,7 @@ const HubspotFormContainer = React.forwardRef<HTMLDivElement, HubspotFormContain
2729
region,
2830
formClassName,
2931
createDOMElement,
32+
defaultValues,
3033
} = props;
3134

3235
const containerRef = React.useRef<HTMLDivElement>(null);
@@ -56,6 +59,9 @@ const HubspotFormContainer = React.forwardRef<HTMLDivElement, HubspotFormContain
5659
target: `#${containerId}`,
5760
cssClass: formClassName,
5861
formInstanceId,
62+
onFormReady: defaultValues
63+
? (form) => setHubspotDefaultValues(form, defaultValues)
64+
: undefined,
5965
});
6066
}
6167
}

src/sub-blocks/HubspotForm/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const HubspotForm = React.forwardRef<HTMLDivElement, HubspotFormProps>((props, r
3030
onLoad,
3131
createDOMElement,
3232
onSubmitError,
33+
defaultValues,
3334
} = props;
3435

3536
const themeValue = useTheme();
@@ -72,6 +73,7 @@ const HubspotForm = React.forwardRef<HTMLDivElement, HubspotFormProps>((props, r
7273
portalId={portalId}
7374
formInstanceId={formInstanceId}
7475
region={region}
76+
defaultValues={defaultValues}
7577
ref={ref}
7678
/>
7779
);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {HubspotFormDefaultValues} from '../../utils/hubspot';
2+
3+
/* eslint-disable no-not-accumulator-reassign/no-not-accumulator-reassign */
4+
/* eslint-disable no-param-reassign */
5+
6+
type HubspotInputValue = HubspotFormDefaultValues[string];
7+
8+
const setInputValue = (inputs: HTMLInputElement[], value: HubspotInputValue) => {
9+
const input = inputs[0];
10+
const type = input.type;
11+
12+
switch (type) {
13+
case 'checkbox':
14+
input.checked = Boolean(value);
15+
return;
16+
case 'radio':
17+
inputs.forEach((radio) => {
18+
if (radio.value === String(value)) {
19+
input.checked = true;
20+
}
21+
});
22+
return;
23+
default:
24+
input.value = String(value);
25+
}
26+
};
27+
28+
const setSelectValue = (select: HTMLSelectElement, value: HubspotInputValue) => {
29+
const options = Array.from(select.querySelectorAll('option'));
30+
31+
options.forEach((option) => {
32+
if (option.value === String(value)) {
33+
option.selected = true;
34+
}
35+
});
36+
};
37+
38+
const setValue = (elements: Element[], value: HubspotInputValue) => {
39+
const element = elements[0];
40+
41+
switch (element.tagName.toLowerCase()) {
42+
case 'input':
43+
setInputValue(elements as HTMLInputElement[], value);
44+
return;
45+
case 'textarea':
46+
(element as HTMLTextAreaElement).value = String(value);
47+
return;
48+
case 'select':
49+
setSelectValue(element as HTMLSelectElement, value);
50+
return;
51+
}
52+
};
53+
54+
export const setHubspotDefaultValues = (
55+
form: HTMLFormElement,
56+
defaultValues: HubspotFormDefaultValues,
57+
) => {
58+
Object.entries(defaultValues).forEach(([name, value]) => {
59+
const inputs = Array.from(form.querySelectorAll(`[name="${name}"]`));
60+
setValue(inputs, value);
61+
});
62+
};

src/utils/hubspot.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export interface HubspotEventData {
1313
data?: unknown;
1414
}
1515

16+
export type HubspotFormDefaultValues = Record<string, string | number | boolean>;
17+
1618
export function isHubspotEventData(maybeData: unknown): maybeData is HubspotEventData {
1719
return (
1820
typeof maybeData === 'object' &&

0 commit comments

Comments
 (0)