PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.8.1
GiveWP – Donation Plugin and Fundraising Platform v4.16.8.1
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / DonationForms / resources / app / fields / SectionNode.tsx

SectionNode.tsx in GiveWP – Donation Plugin and Fundraising Platform 4.16.8.1, at src/DonationForms/resources/app/fields/SectionNode.tsx

67 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {isElement, isField, isGroup, Node} from '@givewp/forms/types';
2 import FieldNode from './FieldNode';
3 import ElementNode from './ElementNode';
4 import GroupNode from './GroupNode';
5 import GatewayFieldNode from '@givewp/forms/app/fields/GatewayFieldNode';
6 import {elementTemplateExists, fieldTemplateExists, groupTemplateExists} from '@givewp/forms/app/templates';
7 import useVisibilityCondition from '@givewp/forms/app/hooks/useVisibilityCondition';
8 import {useEffect} from '@wordpress/element';
9 import memoNode from '@givewp/forms/app/utilities/memoNode';
10
11 const formTemplates = window.givewp.form.templates;
12
13 /**
14 * Determine which node template to render and apply visibility conditions. It is important the visibility conditions
15 * occur here, instead of in the more specific components, as it prevents the subsequent hooks from firing, which can
16 * cause an infinite re-render loop.
17 *
18 * @since 3.0.0
19 */
20 function SectionNode({node}: {node: Node}) {
21 const showNode = useVisibilityCondition(node.visibilityConditions);
22 const {unregister} = window.givewp.form.hooks.useFormContext();
23
24 useEffect(() => {
25 if (showNode) {
26 return;
27 }
28
29 if (isField(node)) {
30 unregister(node.name, {
31 keepDefaultValue: true,
32 });
33 }
34
35 if (isGroup(node)) {
36 node.walkNodes((node) => {
37 unregister(node.name, {
38 keepDefaultValue: true,
39 });
40 }, isField);
41 }
42 }, [showNode, unregister]);
43
44 if (!showNode) {
45 return null;
46 }
47
48 if (isField(node) && fieldTemplateExists(node)) {
49 if (node.type === 'gateways') {
50 return <GatewayFieldNode node={node} />;
51 }
52 return <FieldNode node={node} />;
53 } else if (isElement(node) && elementTemplateExists(node)) {
54 return <ElementNode node={node} />;
55 } else if (isGroup(node) && groupTemplateExists(node)) {
56 return <GroupNode node={node} />;
57 } else {
58 console.error(`Node: ${JSON.stringify(node)} does not exist in Form Design: ${JSON.stringify(formTemplates)}`);
59
60 return null;
61 }
62 }
63
64 const MemoizedSectionNode = memoNode(SectionNode);
65
66 export default MemoizedSectionNode;
67