-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathForm.cs
142 lines (127 loc) · 3.59 KB
/
Form.cs
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.Linq;
using DUCK.Forms.Fields;
using UnityEngine;
using UnityEngine.UI;
namespace DUCK.Forms
{
public class Form : MonoBehaviour
{
[SerializeField]
private Button submitButton;
[SerializeField]
private List<FieldConfig> fieldConfigs;
public event Action<Dictionary<string, AbstractFormField>> OnSubmitForm;
private void Awake()
{
if (submitButton != null)
{
SetSubmitButton(submitButton);
}
}
/// <summary>
/// Validate the fields and if successful invoke the OnSubmitForm event.
/// Also handles clearing fields on submission if applicable.
/// </summary>
public void SubmitForm()
{
var isValid = true;
foreach (var fieldConfig in fieldConfigs)
{
var isFormValid = fieldConfig.field.Validate();
if (!isFormValid && isValid)
{
isValid = false;
}
}
if (isValid)
{
if (OnSubmitForm != null)
{
OnSubmitForm.Invoke(fieldConfigs.ToDictionary(config => config.field.FieldName, config => config.field));
}
}
// Fields need to be validated before the fields get cleared.
// This is because vildation components may require values from other fields.
foreach (var fieldConfig in fieldConfigs)
{
if (fieldConfig.clearOnSubmit)
{
fieldConfig.field.Clear();
}
}
}
/// <summary>
/// Set the submit button.
/// if a submit button is already set, it will unscribed and be replaced.
/// </summary>
/// <param name="button">The submit button to be set.</param>
public void SetSubmitButton(Button button)
{
if (submitButton != null)
{
submitButton.onClick.RemoveListener(SubmitForm);
}
submitButton = button;
submitButton.onClick.AddListener(SubmitForm);
}
/// <summary>
/// Add a form field to the form.
/// Intended for creating a form at runtime.
/// </summary>
/// <param name="formField">The form field to add</param>
/// <param name="clearOnSubmit">If the field should be cleared on submission</param>
public void AddFormField(AbstractFormField formField, bool clearOnSubmit = false)
{
fieldConfigs.Add(new FieldConfig()
{
field = formField,
clearOnSubmit = clearOnSubmit
});
}
/// <summary>
/// Remove a form field from the form.
/// Intended for creating a form at runtime.
/// </summary>
/// <param name="formField">The form field to remove.</param>
/// <returns>If the field was removed successfully or if the field was not in the form.</returns>
public bool RemoveFormField(AbstractFormField formField)
{
var fieldConfig = fieldConfigs.FirstOrDefault(config => config.field == formField);
return fieldConfig == null || fieldConfigs.Remove(fieldConfig);
}
/// <summary>
/// Get all fields in the form.
/// </summary>
/// <returns>An array of form fields</returns>
public AbstractFormField[] GetAllFields()
{
return fieldConfigs.Select(config => config.field).ToArray();
}
/// <summary>
/// Get a form field by field name.
/// </summary>
/// <param name="fieldName">The field name</param>
/// <returns>The form field matching the field name.</returns>
public AbstractFormField GetField(string fieldName)
{
var first = fieldConfigs.FirstOrDefault(config => config.field.FieldName == fieldName);
return first != null ? first.field : null;
}
/// <summary>
/// Set all fields to default state.
/// </summary>
public void ResetFields()
{
fieldConfigs.ForEach(config => config.field.Reset());
}
private void OnDestroy()
{
if (submitButton != null)
{
submitButton.onClick.RemoveListener(SubmitForm);
}
}
}
}