- Export Signal type (signal → Signal) so subpackages can reference it - Expose viewport signals as public fields (CenterLng, CenterLat, Zoom, Bearing, Pitch) for .Text() display and .Bind() usage - Add signal-backed marker positions (LngSignal/LatSignal) with data-effect reactivity for server push and dragend writeback - Add event system (MapEvent, OnClick, OnLayerClick, OnMouseMove, OnContextMenu) using hidden inputs + action triggers - Add Source interface replacing type-switch, with RawSource escape hatch - Add CameraOptions for FlyTo/EaseTo/JumpTo/FitBounds/Stop - Add Control interface with NavigationControl, ScaleControl, GeolocateControl, FullscreenControl - Expand Options with interaction toggles, MaxBounds, and Extra map - Add effectspike example to validate data-effect with server-pushed signals - Update maplibre example to showcase all new features
59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package via
|
|
|
|
// Field is a signal with built-in validation rules and error state.
|
|
// It embeds *Signal, so all signal methods (Bind, String, Int, Bool, SetValue, Text, ID)
|
|
// work transparently.
|
|
type Field struct {
|
|
*Signal
|
|
rules []Rule
|
|
errors []string
|
|
initialVal any
|
|
}
|
|
|
|
// Validate runs all rules against the current value.
|
|
// Clears previous errors, populates new ones, returns true if all rules pass.
|
|
func (f *Field) Validate() bool {
|
|
f.errors = nil
|
|
val := f.String()
|
|
for _, r := range f.rules {
|
|
if err := r.validate(val); err != nil {
|
|
f.errors = append(f.errors, err.Error())
|
|
}
|
|
}
|
|
return len(f.errors) == 0
|
|
}
|
|
|
|
// HasError returns true if this field has any validation errors.
|
|
func (f *Field) HasError() bool {
|
|
return len(f.errors) > 0
|
|
}
|
|
|
|
// FirstError returns the first validation error message, or "" if valid.
|
|
func (f *Field) FirstError() string {
|
|
if len(f.errors) > 0 {
|
|
return f.errors[0]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Errors returns all current validation error messages.
|
|
func (f *Field) Errors() []string {
|
|
return f.errors
|
|
}
|
|
|
|
// AddError manually adds an error message (useful for server-side or cross-field validation).
|
|
func (f *Field) AddError(msg string) {
|
|
f.errors = append(f.errors, msg)
|
|
}
|
|
|
|
// ClearErrors removes all validation errors from this field.
|
|
func (f *Field) ClearErrors() {
|
|
f.errors = nil
|
|
}
|
|
|
|
// Reset restores the field value to its initial value and clears all errors.
|
|
func (f *Field) Reset() {
|
|
f.SetValue(f.initialVal)
|
|
f.errors = nil
|
|
}
|