usePositioningTargetSelector
The usePositioningTargetSelector
is a custom React hook designed to handle positioning algorithms and their respective targets and options within a user interface. This hook utilizes various side effects and callbacks to manage state and interactions with a positioning controller.
Overview
This hook provides functions to:
- Enable or disable specific positioning target types.
- Clear and select algorithms.
- Update options for the algorithm.
- Retrieve and manage targets and options for a specified positioning algorithm.
Import Statements
import {
ITargetAndOptionsForPositioningAlgorithm,
PositioningControllerAlgorithm,
PositioningTargetType,
} from "@shared/interfaces";
import { useCallback, useEffect, useState } from "react";
Hook: usePositioningTargetSelector
Function Signature
eort function usePositioningTargetSelector<TAlgorithmOptions>(
algorithm: PositioningControllerAlgorithm,
options: TAlgorithmOptions
);
Parameters
algorithm
: The positioning algorithm to be controlled. This is of typePositioningControllerAlgorithm
.options
: The initial options for the targeting algorithm. This is a generic typeTAlgorithmOptions
.
States
targetsAndOptionsForAlgorithm
: This state holds an object that contains targets and options associated with the specified algorithm.
Callbacks
-
handleOnPositioningTargetTypeChange
:- Purpose: Enable or disable a specific target type.
- Parameters:
targetType: PositioningTargetType
,enabled: boolean
- Usage: Invokes an action to change the target type's enabled state.
-
handleOnClear
:- Purpose: Clears the current algorithm's settings.
- Logs: Outputs "clear" followed by the algorithm to the console.
-
handleOnSelection
:- Purpose: Activates the algorithm for selection.
- Logs: Outputs "select" followed by the algorithm to the console.
-
updateOptionsForAlgorithm
:- Purpose: Updates the options for the current algorithm.
- Parameters:
updateOptions: Partial<TAlgorithmOptions>
Effects
useEffect
:- Purpose: On component mount, retrieves targets and options for the specified algorithm from a server method and updates the state accordingly.
Returns
The hook returns an object containing:
Property | Type | Description |
---|---|---|
handleOnPositioningTargetTypeChange | Function | Callback to change target type state. |
handleOnClear | Function | Callback to clear algorithm settings. |
handleOnSelection | Function | Callback to select the algorithm. |
targetsAndOptionsForAlgorithm | ITargetAndOptionsForPositioningAlgorithm<TAlgorithmOptions> | State containing targets and options. |
updateOptionsForAlgorithm | Function | Function to update algorithm options. |
Example Usage
import { usePositioningTargetSelector } from "./usePositioningTargetSelector";
const MyComponent = () => {
const {
handleOnPositioningTargetTypeChange,
handleOnClear,
handleOnSelection,
targetsAndOptionsForAlgorithm,
updateOptionsForAlgorithm,
} = usePositioningTargetSelector(myAlgorithm, myOptions);
// Usage of handlers and state
};
Notes
- The hook uses
window.kweenb
actions and methods, implying an interaction with a global object or library designed for position handling. - The use of
useEffect
ensures that the targets and options are fetched and set when the component mounts, providing an initial setup for the positioning logic.
This documentation provides a comprehensive overview and usage guide for the usePositioningTargetSelector
hook, encapsulating its purpose, structure, and usage within a React application.