Skip to content

Commit

Permalink
allow selectedItem to be used to set the value
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Dec 14, 2023
1 parent cc724d0 commit 1e40adb
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions haxe/ui/components/OptionStepper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class OptionStepper extends InteractiveComponent implements IDataComponent imple
* `value` is used as a universal way to access the "core" value a component is based on.
* in this case its the index of the selected option inside this stepper.
*/
@:clonable @:value(selectedIndex) public var value:Dynamic;
@:clonable @:value(selectedItem) public var value:Dynamic;

@:call(IncrementValue) public function incrementValue():Void;
@:call(DeincrementValue) public function deincrementValue():Void;
Expand Down Expand Up @@ -101,12 +101,51 @@ private class SelectedIndexBehaviour extends DataBehaviour {
}

@:dox(hide) @:noCompletion
private class SelectedItemBehaviour extends Behaviour {
private class SelectedItemBehaviour extends DataBehaviour {
public override function getDynamic():Dynamic {
var stepper:OptionStepper = cast(_component, OptionStepper);
var ds = stepper.dataSource;
return ds.get(stepper.selectedIndex);
}

private override function validateData() {
var stepper:OptionStepper = cast(_component, OptionStepper);
var ds = stepper.dataSource;
if (ds == null) {
return;
}

var indexToSelect = -1;
for (i in 0...ds.size) {
var v:Dynamic = ds.get(i);
#if hl
if (Reflect.hasField(v, "value")) {
v = Std.string(v.value);
} else if (Reflect.hasField(v, "text")) {
v = Std.string(v.text);
}
#else
if (v.value != null) {
v = Std.string(v.value);
} else if (v.text != null) {
v = Std.string(v.text);
}
#end

if (v == _value.toString()) {
indexToSelect = i;
break;
}
}

if (indexToSelect != -1) {
stepper.selectedIndex = indexToSelect;
} else { // lets also allow selectedItem to be an index _if_ it wasnt found in the datasource
if (_value.isInt) {
stepper.selectedIndex = _value.toInt();
}
}
}
}

private class IncrementValue extends Behaviour {
Expand Down

0 comments on commit 1e40adb

Please sign in to comment.