-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathForm.hx
More file actions
122 lines (103 loc) · 3.01 KB
/
Form.hx
File metadata and controls
122 lines (103 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Feathers UI
Copyright 2026 Bowler Hat LLC. All Rights Reserved.
This program is free software. You can redistribute and/or modify it in
accordance with the terms of the accompanying license agreement.
*/
package feathers.controls;
import feathers.events.FormEvent;
import feathers.events.TriggerEvent;
import openfl.display.DisplayObject;
import openfl.events.KeyboardEvent;
import openfl.ui.Keyboard;
/**
Groups a set of user-editable fields together. Supports the submission of
data when the Enter/Return key is pressed, or when a specific button is
triggered.
@event feathers.events.FormEvent.SUBMIT Dispatched when the form is
submitted. This event may be dispatched when the `Form.submitButton` is
triggered, or when `Keyboard.ENTER` is pressed while a UI control inside the
form has focus.
@see `feathers.controls.FormItem`
@see [Tutorial: How to use the Form and FormItem components](https://feathersui.com/learn/haxe-openfl/form/)
@since 1.0.0
**/
@:event(feathers.events.FormEvent.SUBMIT)
@:styleContext
class Form extends LayoutGroup {
/**
Creates a new `Form` object.
@since 1.0.0
**/
public function new() {
initializeFormTheme();
super();
this.addEventListener(KeyboardEvent.KEY_DOWN, form_keyDownHandler);
}
private var _submitButton:Button;
/**
An optional button that submits the form when triggered.
@since 1.0.0
**/
public var submitButton(get, set):Button;
private function get_submitButton():Button {
return this._submitButton;
}
private function set_submitButton(value:Button):Button {
if (this._submitButton == value) {
return this._submitButton;
}
if (this._submitButton != null) {
this._submitButton.removeEventListener(TriggerEvent.TRIGGER, form_submitButton_triggerHandler);
}
this._submitButton = value;
if (this._submitButton != null) {
this._submitButton.addEventListener(TriggerEvent.TRIGGER, form_submitButton_triggerHandler, false, 0, true);
}
return this._submitButton;
}
/**
Manually forces `FormEvent.SUBMIT` to be dispatched.
@since 1.0.0
**/
public function submit():Void {
FormEvent.dispatch(this, FormEvent.SUBMIT);
}
override public function dispose():Void {
if (this._submitButton != null) {
this.submitButton = null;
}
super.dispose();
}
private function initializeFormTheme():Void {
#if !feathersui_disable_default_theme
feathers.themes.steel.components.SteelFormStyles.initialize();
#end
}
private function form_keyDownHandler(event:KeyboardEvent):Void {
if (!this._enabled) {
return;
}
if (event.keyCode != Keyboard.ENTER) {
return;
}
if (event.isDefaultPrevented()) {
return;
}
var current = cast(event.target, DisplayObject);
while (current != this && current != null) {
if ((current is FormItem)) {
var formItem:FormItem = cast current;
if (!formItem.submitOnEnterEnabled) {
return;
}
}
current = current.parent;
}
event.preventDefault();
this.submit();
}
private function form_submitButton_triggerHandler(event:TriggerEvent):Void {
this.submit();
}
}