Skip to main content
Version: Next

Hooks

useDatasourceSub​

A hook that subscribes to changes in datasources and performs replacements in the Page based on specified actions.

useSources​

A hook that facilitates managing datasources and current elements associated with a web form. It provides methods for setting, fetching, and listening to changes in datasource values.

Parameters​

paramdescription
optionsUseSourcesOptions An object allowing configuration of the hook behavior. Includes options such as datasourceChange, currentElementChange and acceptIteratorSel.

Returns​

{object}

useEnhancedNode​

A hook that enhances the functionality of a Craft.js node, providing features for handling data transfer, managing datasources, and styling components.

Parameters​

paramdescription
collector(optional) (args: Node) => K
options(optional) { stopPropagation?: (data: IDataTransfer) => boolean; onDrop?: (e: any) => void; }

Returns​

{UseEnhancedNodeReturnType} - An object containing enhanced node information, connectors, and actions.

Basic usage:

import { useEnhancedNode } from '@ws-ui/webform-editor';
import { useState, useEffect } from 'react';

const ExampleDelayedState = () => {
const { actions, connectors, linkedNodes } = useEnhancedNode((node) => ({
nodes: node.data.nodes,
linkedNodes: node.data.linkedNodes,
node,
}));

const [delayedState, setDelayedState] = useState(null);

useEffect(() => {
const fetchData = async () => {
// Fetch your data, e.g., from an API
const result = await fetch('https://api.example.com/data');
const data = await result.json();

setDelayedState(data);

actions.setDatasourceValue({ key: 'yourDatasourceKey', value: data });
};

fetchData();
}, [actions]);

return (
<div ref={connectors.connect}>

{delayedState && (
<div>
{JSON.stringify(delayedState)}
</div>
)}
</div>
);
};
info

The useEnhancedNode function returns an object with the following structure:

  • id: The ID of the node.
  • store: The store associated with the node.
  • connectors: Object containing connectors to interact with the Craft.js editor.
  • ...collected: Additional data collected by the collector function.
  • actions: An object containing methods for manipulating node values, including setProp, setStyle, setDatasource, and setIterator.

useEnhancedEditor​

A Hook that provides methods and state information associated with the entire editor.

Parameters​

paramdescription
collectA function that collects relevant state information from the editor state. The component will re-render when the values returned by this function changes.

Returns​

{useEditorReturnType<S>}

Basic usage:

import { useEnhancedEditor } from "@ws-ui/webform-editor";

const BasicUsageExample = () => {
const { isActive, enabled, canvas } = useEnhancedEditor();

// Your component logic based on the editor state
return (
<div>
<p>Is Active: {isActive ? 'Yes' : 'No'}</p>
<p>Editor Enabled: {enabled ? 'Yes' : 'No'}</p>
<p>Canvas Element: {canvas}</p>

// Additional logic and components based on the editor state
</div>
);
};