Skip to content

Commit

Permalink
refactor: update memoNode types
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Waldstein committed Dec 12, 2023
1 parent 460514e commit c471a2a
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Field} from '@givewp/forms/types';
import type {Field} from '@givewp/forms/types';
import registerFieldAndBuildProps from '../utilities/registerFieldAndBuildProps';
import {useDonationFormState} from '@givewp/forms/app/store';
import {withTemplateWrapper} from '@givewp/forms/app/templates';
Expand Down
1 change: 0 additions & 1 deletion src/DonationForms/resources/app/fields/GroupNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function GroupNode({node}: {node: Group}) {
const Group = useTemplateWrapper<GroupProps>(formTemplates.groups[node.type], 'div', node.name);

const nodeComponents = node.reduceNodes((nodes, node: Node) => {
// @ts-ignore
nodes[node.name] = (props: Node) => <SectionNode node={{...node, ...props}} />;

return nodes;
Expand Down
4 changes: 0 additions & 4 deletions src/DonationForms/resources/app/fields/SectionNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,12 @@ function SectionNode({node}: {node: Node}) {

if (isField(node) && fieldTemplateExists(node)) {
if (node.type === 'gateways') {
// @ts-ignore
return <GatewayFieldNode node={node} />;
}
// @ts-ignore
return <FieldNode node={node} />;
} else if (isElement(node) && elementTemplateExists(node)) {
// @ts-ignore
return <ElementNode node={node} />;
} else if (isGroup(node) && groupTemplateExists(node)) {
// @ts-ignore
return <GroupNode node={node} />;
} else {
console.error(`Node: ${JSON.stringify(node)} does not exist in Form Design: ${JSON.stringify(formTemplates)}`);
Expand Down
5 changes: 1 addition & 4 deletions src/DonationForms/resources/app/form/MultiStepForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ const convertSectionsToSteps = (sections: Section[], hasFirstStep: boolean) => {
<FormSectionTemplate key={section.name} section={section} hideLabel>
{section.nodes.map((node) => (
<DonationFormErrorBoundary key={node.name}>
{
// @ts-ignore
<SectionNode key={node.name} node={node} />
}
<SectionNode key={node.name} node={node} />
</DonationFormErrorBoundary>
))}
</FormSectionTemplate>
Expand Down
5 changes: 1 addition & 4 deletions src/DonationForms/resources/app/form/Section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ export default function Section({section}: {section: SectionType}) {
<FormSectionTemplate section={section}>
{section.nodes.map((node) => (
<DonationFormErrorBoundary key={node.name}>
{
// @ts-ignore
<SectionNode node={node} />
}
<SectionNode node={node} />
</DonationFormErrorBoundary>
))}
</FormSectionTemplate>
Expand Down
6 changes: 4 additions & 2 deletions src/DonationForms/resources/app/utilities/memoNode.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {memo} from '@wordpress/element';
import {Node} from '@givewp/forms/types';
import {JSX} from 'react/jsx-runtime';

/**
* This is used for memoizing Node components. Node props come from the server and are never intended to change. The
* state of a Node may change, triggering a re-render, but the props should never change.
*
* @unreleased updated param and return types
* @since 3.0.0
*/
export default function memoNode(NodeComponent) {
return memo(NodeComponent, compareNodeProps);
export default function memoNode(NodeComponent: { ({node}: {node: Node}): JSX.Element; }): typeof NodeComponent {
return memo(NodeComponent, compareNodeProps) as typeof NodeComponent;
}

type NodeProp = { node: Node };
Expand Down

0 comments on commit c471a2a

Please sign in to comment.