QuoteForm
The QuoteForm
is a comprehensive component for creating and updating
a quote. It serves as a primary interface during the quoting process,
dynamically assembling various sections—product-specific fields, general
policy details, advanced settings, and coverage terms—into a single,
cohesive form. It is designed to be highly configurable and supports
complex, backend-driven field dependencies through constraint
evaluation.
Usage
To use the QuoteForm
, you must provide the quote
object to be
modified, the full dataModel
, and a handleSubmit
function. The
form’s appearance and behavior can be extensively customized through
various hide...
props and by optionally enabling constraint
evaluation.
import { QuoteForm } from '@socotra/ec-react-components';
import {
DataModel,
QuoteResponse,
EvaluateConstraintsRequest,
ConstraintEvaluationResponse,
} from '@socotra/ec-react-schemas';
// Example Usage
const MyQuotePage = ({
quote,
dataModel,
dependencyMap,
}: {
quote: QuoteResponse,
dataModel: DataModel,
dependencyMap: QuoteDependencyMapResponse,
}) => {
const handleSubmit = (quoteRequest) => {
console.log('Quote Update Request:', quoteRequest);
// Handle API call to update the quote
};
const handleEvaluateConstraints = async (
request: EvaluateConstraintsRequest,
): Promise<ConstraintEvaluationResponse | undefined> => {
console.log('Evaluating constraints:', request);
// API call to the backend to get calculated values
return fetch(/*...- /constraints/evaluate ...*/).then((res) => res.json());
};
return (
<QuoteForm
quote={quote}
dataModel={dataModel}
handleSubmit={handleSubmit}
dependencyMap={dependencyMap}
getEvaluatedConstraints={handleEvaluateConstraints}
submitButtonText='Update Quote'
/>
);
};
Props
Prop |
Type |
Description |
Default |
---|---|---|---|
|
|
The quote object being edited. |
Required |
|
|
The entire resolved data model for the tenant. |
Required |
|
|
Callback triggered on submit,
receiving the |
Required |
|
|
Optional map of field dependencies, required to enable constraint evaluation. |
|
|
|
Optional async function to call the backend for constraint evaluation. Required for the feature to be active. |
|
|
|
If |
|
|
|
If |
|
|
|
If |
|
|
|
If |
|
|
|
When |
|
|
|
An object to override default labels for form sections and fields. See “Labels and Translations” below. |
|
…plus other standard form props (isSubmitting
, disabled
,
submitButtonText
, etc.).
State Management
The form manages its state internally using useState
and
useEffect
.
Data Initialization: The form’s data is initialized by
getDefaultQuoteValues
, which combines data from thequote
object, the relevantproductModel
, and the overalldataModel
.Prop Synchronization: A
useEffect
hook tracks changes to the incomingquote
prop. If the prop is updated externally (e.g., after a save operation), the form’s internal state is re-initialized to reflect the latest data.Constraint State: The component maintains an
evaluatedConstraints
state. This state is populated by the response from thegetEvaluatedConstraints
function and is cleared whenever the user modifies the form data, ensuring that constraints are re-evaluated on the next relevant change.
Constraint Evaluation
This form supports a powerful constraint evaluation system to create dynamic relationships between fields.
Purpose: To automatically calculate or update the values of certain fields based on user input in other fields, without requiring a full form submission. For example, selecting a specific “Region” could automatically populate a “Tax Rate” field.
Activation: The feature is enabled by providing both the
dependencyMap
andgetEvaluatedConstraints
props.Mechanism:
The
dependencyMap
tells the form which fields depend on others.When a user changes a field that is a dependency for another, a
useEffect
hook is triggered.This hook calls the
getEvaluatedConstraints
function with a payload of the changed data.The backend service evaluates the data and returns a
ConstraintEvaluationResponse
containing the new values for any dependent fields.This response is stored in the
evaluatedConstraints
state, which triggers a re-render.The
dataModelToJSONSchema
utility uses this response to update the JSON Schema, making the calculated fields read-only and displaying their new values.
Validation
Client-side validation is handled by Ajv. The
schema used for validation is highly dynamic, built on the fly by the
dataModelToJSONSchema
utility. It combines definitions for default
fields, advanced fields, coverage terms, and product-specific fields.
The hide...
props directly affect which sections are included in the
schema. When constraint evaluation is active, the schema is further
modified to reflect the calculated values.
Labels and Translations
The text for UI labels and validation messages can be customized.
UI Labels
Labels for sections and fields can be overridden by passing a titles
object prop.
Key |
Description |
Default Value |
---|---|---|
|
Title for the main “Details” section. |
|
|
Title for the collapsible “Advanced” section. |
|
|
Title for the “Coverage Terms” section. |
|
|
Label for the “Currency” field. |
|
|
Label for the “Timezone” field. |
|
|
Label for the “Billing Level” field. |
|
|
Label for the “Billing Trigger” field. |
|
|
Label for the “Duration Basis” field. |
|
|
Label for the “Delinquency Plan” field. |
|
|
Label for the “Auto-Renewal Plan” field. |
|
|
Label for the “Installment Plan” field. |
|
|
Text for a “true” boolean value. |
|
|
Text for a “false” boolean value. |
|