use-edit-variable.ts


Overview

use-edit-variable.ts exports a custom React hook, useEditVariableRecord, designed to manage the editing lifecycle of a collection of variable records within a form context. It facilitates adding, updating, deleting, and selecting variable entries, as well as controlling modal visibility for editing these records.

Key functionalities include:

This hook is intended for use in React components that require seamless form-driven editing of an array of variable objects, typically within a modal interface.


Detailed Explanation

useEditVariableRecord

useEditVariableRecord({
  form,
}: INextOperatorForm & { form: UseFormReturn<FormSchemaType> })

Purpose

Handles the editing logic and modal state for variable records inside a form. It abstracts common operations like opening the modal, selecting a record, saving changes, and deleting records.

Parameters

Returns

An object with the following properties and functions:

Name

Type

Description

ok

(record: VariableFormSchemaType) => void

Called to save the current record (add or update) and close the modal.

currentRecord

VariableFormSchemaType

The currently selected variable record for editing.

setRecord

(record: VariableFormSchemaType) => void

Function to update the current record state.

visible

boolean

Indicates if the edit modal is currently visible.

hideModal

() => void

Function to hide the edit modal.

showModal

(idx?: number, record?: VariableFormSchemaType) => void

Function to show the edit modal, optionally with a record and its index to edit.

otherThanCurrentQuery

VariableFormSchemaType[]

List of all variable records except the one currently selected for editing.

handleDeleteRecord

(idx: number) => void

Deletes the variable record at the specified index from the form.


Internal Logic and Implementation Details


Usage Example

import { useEditVariableRecord } from './use-edit-variable';
import { useForm } from 'react-hook-form';

const MyComponent = () => {
  const form = useForm<FormSchemaType>({
    defaultValues: { variables: [] },
  });

  const {
    ok,
    currentRecord,
    visible,
    showModal,
    hideModal,
    handleDeleteRecord,
    otherThanCurrentQuery,
  } = useEditVariableRecord({ form });

  return (
    <>
      <button onClick={() => showModal()}>Add Variable</button>

      {visible && (
        <Modal onClose={hideModal}>
          <VariableForm
            record={currentRecord}
            onSave={(record) => ok(record)}
            otherVariables={otherThanCurrentQuery}
          />
        </Modal>
      )}

      <VariableList
        variables={form.getValues('variables')}
        onEdit={(idx, record) => showModal(idx, record)}
        onDelete={handleDeleteRecord}
      />
    </>
  );
};

Interaction with Other Parts of the System


Important Notes


Mermaid Diagram: Functional Flowchart of useEditVariableRecord

flowchart TD
    A[Start: useEditVariableRecord hook] --> B[Initialize state: index = -1]
    B --> C[Watch form variables using useWatch]
    C --> D{User triggers showModal?}
    D -- Yes --> E[Set index and currentRecord]
    E --> F[Set modal visible]
    F --> G{User edits record and saves?}
    G -- Yes --> H[Update variables array]
    H --> I[Set variables in form]
    I --> J[Hide modal]
    D -- No --> K{User deletes record?}
    K -- Yes --> L[Filter out record by index]
    L --> M[Set updated variables in form]
    K -- No --> N[Return current state & handlers]
    J --> N
    M --> N

Summary

The use-edit-variable.ts file provides a specialized React hook for managing variable records within a form, including modal control and CRUD operations. It encapsulates complex state logic, enabling components to easily handle variable editing workflows while keeping form state consistent and modal visibility controlled. This hook integrates with form libraries and state hooks to create a streamlined editing experience in React applications.