index.tsx
Overview
The index.tsx file defines a React component for a form named GoogleScholarForm. This form is designed to capture user input related to Google Scholar queries, such as sorting options, year ranges, patents filter, and query text. It leverages React Hook Form for form state management, Zod for schema validation, and integrates several custom UI components for a consistent UX.
The form is composed of multiple smaller components and widgets, including a custom YearPicker, sorting options, switches, and output display. It also implements hooks to watch form changes and build dynamic options.
This file's primary functionality is to provide a complex form interface for Google Scholar parameters, validate the input, and display computed outputs based on the input values.
Detailed Explanations
Components and Functions
1. YearPicker (Functional Component)
Purpose
A custom date picker component that allows users to select a year only, rather than a full date.
Parameters
onChange?: (val: number | undefined) => void
Callback invoked when the selected year changes. It receives the selected year as a number or undefined if cleared.value?: number | undefined
The current selected year as a number.
Returns
JSX for an Ant Design
DatePickerconfigured to select only years.
Implementation Details
Uses dayjs to convert between year numbers and date objects.
The
onChangehandler converts the selected date to a year number before calling the externalonChangecallback.
Usage Example
<YearPicker value={2023} onChange={(year) => console.log(year)} />
2. GoogleScholarFormWidgets (Functional Component)
Purpose
Renders specific form fields related to Google Scholar parameters including the top N items, sorting options, year range, and patents filter.
Usage
This component is used inside the main GoogleScholarForm and relies on React Hook Form's useFormContext to access the form state and controls.
Inside the component:
Uses
useTranslate('flow')for localization.Gets sorting options from
useBuildSortOptions().Renders the following form fields:
TopNFormField(number input for top N results)sort_by(select with search dropdown)year_lowandyear_high(year pickers for start/end year range)patents(boolean switch to include patents)
Each field uses FormField and related UI components for layout, labeling, and validation messaging.
Usage Example
function FormWrapper() {
return (
<FormProvider {...formMethods}>
<GoogleScholarFormWidgets />
</FormProvider>
);
}
3. GoogleScholarFormPartialSchema (Zod Schema Object)
Defines partial validation rules for the form fields related to Google Scholar parameters.
Fields
top_n: numbersort_by: stringyear_low: numberyear_high: numberpatents: boolean
4. FormSchema (Zod Schema Object)
Extends GoogleScholarFormPartialSchema by adding a required query string field.
Fields
All fields in
GoogleScholarFormPartialSchemaquery: string
Used to validate the entire form data on submission.
5. GoogleScholarForm (Functional Component)
Parameters
{ node }: INextOperatorForm
The form receives anodeprop which represents the current form node in a larger workflow or operator chain.
Functionality
Uses
useFormfrom React Hook Form with default values derived frominitialGoogleScholarValuesand thenode.Validates form data with
zodResolverusingFormSchema.Uses
useWatchFormChangehook to reactively track form changes and sync with the system based on the node ID.Renders the form UI:
Wraps fields in
FormWrapperandFormContainercomponents for layout.Includes
QueryVariablecomponent for entering the main query text.Embeds
GoogleScholarFormWidgetsfor additional parameters.Displays output using the
Outputcomponent, driven by a pre-builtoutputList.
Returns
JSX rendering the entire form and output section.
Usage Example
<GoogleScholarForm node={currentNode} />
Important Implementation Details
Form State Management and Validation: Uses
react-hook-formfor efficient form state and validation integration withzodschemas.Custom Year Selection: The
YearPickercomponent is a lightweight abstraction over Ant Design'sDatePickerconfigured for year-only selection, transforming the date object to a number.Dynamic Sorting Options: Uses a custom hook
useBuildSortOptionsto generate sorting options dynamically.Form Change Watching: The hook
useWatchFormChangeis used to propagate form changes upstream, likely to update the workflow or state machine.Output Generation: The
buildOutputListutility builds the list of outputs displayed in the form, based on initial constants.
Interaction with Other Parts of the System
Imports multiple shared UI components (
FormContainer,SelectWithSearch,TopNFormField,Form,Switch) from the component library, ensuring consistent UI styling.Uses hooks like
useTranslatefor i18n, and custom hooks for form values and options (useFormValues,useBuildSortOptions).The form data and changes are linked to a higher-level flow or operator system through the
nodeprop anduseWatchFormChange.Output rendering is handled by the
Outputcomponent, which likely visualizes the processed results of the form input.The form schema and constants are defined in external files (
../../constant,../../interface,../../utils/build-output-list), indicating modular configuration.
Visual Diagram
componentDiagram
component "GoogleScholarForm" {
+props: node: INextOperatorForm
+Renders: FormWrapper
+Renders: QueryVariable
+Renders: GoogleScholarFormWidgets
+Renders: Output
}
component "GoogleScholarFormWidgets" {
+Uses: useFormContext()
+Renders: TopNFormField
+Renders: FormField (sort_by)
+Renders: FormField (year_low)
+Renders: FormField (year_high)
+Renders: FormField (patents)
}
component "YearPicker" {
+props: onChange?, value?
+Renders: DatePicker (year-only)
}
"GoogleScholarForm" --> "GoogleScholarFormWidgets"
"GoogleScholarFormWidgets" --> "YearPicker"
"GoogleScholarFormWidgets" --> "TopNFormField"
"GoogleScholarForm" --> "QueryVariable"
"GoogleScholarForm" --> "Output"
Summary
This file implements a Google Scholar query form as a React component with robust form state management, validation, and UI composition. It abstracts year selection via a custom YearPicker, integrates sorting and filtering options, and displays outputs computed from the input. It interacts with a broader application workflow through props and hooks to maintain synchronization and dynamic behavior. The modular, reusable components and hooks used here promote maintainability and extensibility within the application.