Skip to content

Commit

Permalink
move validators to abstract / class (rather than array)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Mar 12, 2024
1 parent 28ac267 commit 247d7a9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 32 deletions.
42 changes: 10 additions & 32 deletions haxe/ui/core/InteractiveComponent.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import haxe.ui.events.FocusEvent;
import haxe.ui.events.UIEvent;
import haxe.ui.focus.FocusManager;
import haxe.ui.focus.IFocusable;
import haxe.ui.validators.IValidator;
import haxe.ui.validators.Validators;

/**
A component that can be interacted with and gain input focus via either mouse or keyboard
Expand Down Expand Up @@ -112,19 +112,20 @@ class InteractiveComponent extends Component implements IFocusable implements IV
return view;
}

private var _validators:Array<IValidator> = null;
@:clonable public var validators(get, set):Array<IValidator>;
private function get_validators():Array<IValidator> {
private var _validators:Validators = null;
@:clonable public var validators(get, set):Validators;
private function get_validators():Validators {
return _validators;
}
private function set_validators(value:Array<IValidator>):Array<IValidator> {
private function set_validators(value:Validators):Validators {
if (value == null) {
unregisterEvent(UIEvent.CHANGE, _onInteractiveChange);
}
_validators = value;
if (_validators != null) {
registerEvent(UIEvent.CHANGE, _onInteractiveChange);
applyValidators();
_validators.setup(this);
_validators.validate(this);
}
return value;
}
Expand All @@ -133,32 +134,9 @@ class InteractiveComponent extends Component implements IFocusable implements IV
// Internal
//***********************************************************************************************************

private function applyValidators() {
if (_validators == null) {
return;
}
for (v in _validators) {
if (v == null) {
continue;
}
v.setup(this);
validateComponentValue();
}
}

private function validateComponentValue() {
if (_validators == null) {
return;
}
for (v in _validators) {
if (v == null) {
continue;
}
v.validate(this);
}
}

private function _onInteractiveChange(event:UIEvent) {
validateComponentValue();
if (_validators != null) {
_validators.validate(this);
}
}
}
87 changes: 87 additions & 0 deletions haxe/ui/validators/Validators.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package haxe.ui.validators;

import haxe.ui.core.Component;

@:forward
@:forward.new
abstract Validators(ValidatorsImpl) from ValidatorsImpl {
@:arrayAccess
public inline function get(index:Int) {
return this.list[index];
}

@:from
static function fromArray(list:Array<IValidator>):Validators {
var ret = new ValidatorsImpl();
ret.list = list;
return ret;
}
}

private class ValidatorsImpl {
public var list:Array<IValidator> = [];

public function new() {
}

public var length(get, null):Int;
private function get_length():Int {
if (list == null) {
return 0;
}
return list.length;
}

public function setup(component:Component) {
for (item in list) {
if (item == null) {
continue;
}
item.setup(component);
}
}

private var _isValid:Bool = true;
public var isValid(get, null):Bool;
private function get_isValid():Bool {
return _isValid;
}

public function validate(component:Component) {
_isValid = true;
for (item in list) {
if (item == null) {
continue;
}
var r = item.validate(component);
if (r != null && r == false) {
_isValid = false;
}
}
}

public function iterator() {
return new ValidatorsIterator(list);
}
}

private class ValidatorsIterator {
private var i:Int;
private var list:Array<IValidator>;

public function new(list:Array<IValidator>) {
this.list = list;
this.i = 0;
}

public function hasNext() {
if (list == null) {
return false;
}
return i < list.length;
}

public function next() {
return list[i++];
}
}

0 comments on commit 247d7a9

Please sign in to comment.