Lesson 2 — FormField: a base class for all form primitives
Every input primitive shares the same set of concerns:
- A
fieldidentifier linking the input to its label - An optional
labelrendered above the input - An optional
hintrendered below the input - An
errorboolean that switches styling to danger colours - A
requiredboolean that adds a visual indicator and the HTMLrequiredattribute
Rather than repeating these props and the label/hint rendering in every primitive, we define them once in a base class:
|
|
Each subclass inherits all of this and only needs to:
- Declare its own specific props
- Implement
render_input
Any future change to label rendering, hint styling, or ARIA attributes happens in one place and applies to every primitive automatically.