variable-dialog.tsx
Overview
variable-dialog.tsx is a React component file defining a modal dialog interface for creating or editing "variables" within a flow or query system. It leverages a form with validation to collect variable properties such as a unique key, a reference, and a value. The dialog encapsulates user input with immediate validation feedback and integrates with the broader UI through reusable components.
The main export is VariableDialog, a modal dialog component that wraps a VariableForm form component. VariableForm handles form state management, validation, and submission using react-hook-form and zod schema validation.
This file is intended to be used anywhere in the application where users need to configure or modify variables that are part of a flow, query, or similar construct.
Exports
VariableDialog (Function Component)
Description
The VariableDialog component provides a modal dialog UI to create or edit a variable. It displays a form (VariableForm) inside a dialog box with header, content, and footer sections. The footer contains a submit button that triggers form submission.
Props
VariableDialog accepts the following combined props:
Prop | Type | Description |
|---|---|---|
|
| Initial form values to populate the form (for editing). |
| Callback to close/hide the modal dialog. | |
|
| Array of other variables (except current) to validate uniqueness of keys. |
|
| Callback invoked on form submission with form values. |
Usage Example
<VariableDialog
initialValue={{ key: 'var1', ref: 'ref1', value: 'val1' }}
otherThanCurrentQuery={[{ key: 'var2', ref: 'ref2', value: 'val2' }]}
hideModal={() => setShowModal(false)}
submit={(values) => console.log('Submitted:', values)}
/>
Internal Components and Functions
VariableForm (Function Component)
Description
VariableForm renders the form inside the dialog. It manages form state, validation, and submission using react-hook-form integrated with zod for schema validation. It provides fields for key, ref, and value with respective validations and UI controls.
Props
Prop | Type | Description |
|---|---|---|
|
| Initial values used to reset the form on mount/update. |
|
| List of other variables to check for key uniqueness. |
|
| Callback invoked when form is successfully submitted. |
Form Fields & Validation
Field | Type | Validation |
|---|---|---|
| string | Required, trimmed, must be unique among |
| string | No explicit validation beyond string type. |
| string | No explicit validation beyond string type. |
Implementation Details
Uses
zodfor schema-based validation with a custom.refine()method to enforce uniquekey.Uses
react-hook-formwithzodResolverfor seamless form integration and validation on change.Resets form values whenever
initialValuechanges viauseEffect.Submits form data through
submitcallback passed from parent.Uses localized labels via
useTranslation()hook.Uses separate
QueryVariablecomponent for therefinput field, suggesting it might be a more complex or custom input.
Usage Example
<VariableForm
initialValue={{ key: '', ref: '', value: '' }}
otherThanCurrentQuery={[{ key: 'existingKey', ref: '', value: '' }]}
submit={(values) => console.log('Form submitted:', values)}
/>
Important Implementation Details
Form Validation Logic:
The key field must be unique and non-empty. This is enforced by azodschema with a.refine()check to ensure no duplicate keys exist among other variables (otherThanCurrentQuery).Form Reset on Prop Change:
The form resets its internal state when theinitialValueprop changes, allowing dynamic updates when editing different variable entries.Localization Support:
All user-facing labels and button texts are localized using thereact-i18nextlibrary'suseTranslationhook.Form Submission Trigger:
The submit button inDialogFooteris linked to the form viaformelement ID allowing the button outside the<form>tag to trigger submission.Component Composition:
The form fields use compound components (FormField,FormItem,FormControl, etc.) from the UI library to maintain consistent styling and behavior.External Components:
TheQueryVariablecomponent is used for thereffield, indicating it might provide additional functionality like suggestions, dropdowns, or complex input handling.
Interaction with Other System Parts
UI Library Components:
Uses shared UI components (Button,Dialog,Form,Input) from the application's UI library, ensuring consistency in look and feel.Form Schema Type:
Relies onVariableFormSchemaTypeimported from./schemafor typing, ensuring form data conforms to expected data structures.Modal Interface:
ImplementsIModalPropsinterface for modal-related props likehideModal, integrating with the application's modal management system.Localization:
Usesreact-i18nextto provide translated strings, which means it depends on the app's i18n setup.Utility Functions:
Useslodash'sisEmptyto check emptiness ofinitialValuesafely.
Visual Diagram
componentDiagram
VariableDialog <|-- VariableForm
VariableDialog --> Dialog
Dialog --> DialogContent
DialogContent --> DialogHeader
DialogContent --> DialogFooter
DialogHeader --> DialogTitle
DialogFooter --> Button
VariableForm --> Form
Form --> FormField_key
Form --> QueryVariable_ref
Form --> FormField_value
FormField_key --> Input_key
FormField_value --> Input_value
Summary
VariableDialog: Modal dialog wrapping a variable configuration form.
VariableForm: Form component managing state, validation, and submission.
Validates uniqueness of variable keys.
Integrates with i18n for localization.
Uses controlled form inputs and schema validation.
Relies on UI library components and a custom
QueryVariablecomponent.Designed for adding or editing variables related to flows or queries.
This file enables users to safely and intuitively manage variables within the app, ensuring data integrity and a consistent user experience.