I am looking to develop a component that enables users to edit data for a mathematical classification model.
The interface concept may resemble the following:
interface MathModel {
name: string;
article: ArticleDescription;
criteria: Criteria;
coefficients: Coefficient[];
interpretation: ResultInterpretation;
}
interface Coefficient {
value: number; // not equal to 0! how to validate?
}
interface Interpretation
{
description: string
interpretationItems: InterpretationItem[]
// ...
}
interface InterpretationItem {
scoreMin: number
scoreMax: number
}
In my initial attempt, I tried creating separate components to manage each property of MathModel
. For example: MathModelCoefficientList
, MathModelInterpretation
components.
However, the interpretation component requires additional information that is not included in the Interpretation type. It requires knowledge of the sum of all coefficient values. This is because InterpretationItem scoreMax should not exceed the total sum of coefficients.
Additionally, InterpretationItems are interconnected - each InterpretationItem should represent a distinct score range.
Therefore, simple v-model components cannot be used.
Potential solutions:
- Within
MathModelComponent
, create a computed variable
const coefficientsValuesSum = computed( () => ...)
const nonOverlappingInterpretationItems = ...
then
<MathModelInterpretation v-model=modelValue.interpretation :coefficientsValuesSum=coefficientsValuesSum :nonOverlappingInterpretationItems=nonOverlappingInterpretationItems>
<MathModelInterpretationList v-model=modelValue.interpretationItems :coefficientsValuesSum=coefficientsValuesSum :nonOverlappingInterpretationItems=nonOverlappingInterpretationItems>
<MathModelInterpretationItem v-model=modelValue[i] :coefficientsValuesSum=coefficientsValuesSum :nonOverlappingInterpretationItems=nonOverlappingInterpretationItems[i]>
While it allows us to see dependencies of subcomponents, handling numerous props raises concerns.
- Create a store containing MathModel. Dependencies are less explicit. Moreover, is it acceptable to pass an index to a component which then retrieves data from the store?
<MathModelInterpretationItem :interpretationItemIndex=i>
- It's not such a complicated component. Avoid dividing it. Maintain Everything in a Single Component.
Extra:
Extra 1) Validation. After splitting into subcomponents, validating before submission becomes more challenging, correct?
Extra 2) I could construct a TypeScript class MathModel with a method sumAllCoefficientValues()
. How can TypeScript classes and their methods connect with the Vue reactivity system? Does this approach make sense?