-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced DropDownList and DropDownItem classes
- Loading branch information
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
|
||
namespace Skybrud.Forms.Models.Fields.DropDown{ | ||
public class DropDownItem { | ||
|
||
#region Properties | ||
|
||
[JsonProperty("value")] | ||
public string Value { get; set; } | ||
|
||
[JsonProperty("label")] | ||
public string Label { get; set; } | ||
|
||
[JsonProperty("checked")] | ||
public bool IsChecked { get; set; } | ||
|
||
#endregion | ||
|
||
#region Constructors | ||
|
||
public DropDownItem(int value, string label) { | ||
Value = value.ToString(); | ||
Label = label; | ||
} | ||
|
||
public DropDownItem(int value, string label, bool isChecked) { | ||
Value = value.ToString(); | ||
Label = label; | ||
IsChecked = isChecked; | ||
} | ||
|
||
public DropDownItem(Guid value, string label) { | ||
Value = value.ToString(); | ||
Label = label; | ||
} | ||
|
||
public DropDownItem(Guid value, string label, bool isChecked) { | ||
Value = value.ToString(); | ||
Label = label; | ||
IsChecked = isChecked; | ||
} | ||
|
||
public DropDownItem(string value, string label) { | ||
Value = value; | ||
Label = label; | ||
} | ||
|
||
public DropDownItem(string value, string label, bool isChecked) { | ||
Value = value; | ||
Label = label; | ||
IsChecked = isChecked; | ||
} | ||
|
||
#endregion | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
using Skybrud.Essentials.Reflection.Extensions; | ||
using Skybrud.Essentials.Strings.Extensions; | ||
|
||
namespace Skybrud.Forms.Models.Fields.DropDown { | ||
|
||
public class DropDownList : FieldBase { | ||
|
||
protected const string FieldType = "dropdown"; | ||
|
||
#region Properties | ||
|
||
[JsonProperty("value", Order = 500)] | ||
public new List<DropDownItem> Value { get; set; } | ||
|
||
#endregion | ||
|
||
#region Constructors | ||
|
||
public DropDownList() : base(FieldType, null) { | ||
Value = new List<DropDownItem>(); | ||
} | ||
|
||
public DropDownList(string name) : base(FieldType, name) { | ||
Value = new List<DropDownItem>(); | ||
} | ||
|
||
public DropDownList(string name, IEnumerable<DropDownItem> value) : base(FieldType, name) { | ||
Value = value?.ToList() ?? new List<DropDownItem>(); | ||
} | ||
|
||
public DropDownList(string name, params DropDownItem[] value) : base(FieldType, name) { | ||
Value = value?.ToList() ?? new List<DropDownItem>(); | ||
} | ||
|
||
public DropDownList(string name, string label) : base(FieldType, name) { | ||
Label = label; | ||
Value = new List<DropDownItem>(); | ||
} | ||
|
||
public DropDownList(string name, string label, IEnumerable<DropDownItem> value) : base(FieldType, name) { | ||
Label = label; | ||
Value = value?.ToList() ?? new List<DropDownItem>(); | ||
} | ||
|
||
public DropDownList(string name, string label, params DropDownItem[] value) : base(FieldType, name) { | ||
Label = label; | ||
Value = value?.ToList() ?? new List<DropDownItem>(); | ||
} | ||
|
||
#endregion | ||
|
||
#region Static methods | ||
|
||
public static DropDownList CreateFromEnum<T>(string name, string label) where T : Enum { | ||
return CreateFromEnum<T>(name, label, default); | ||
} | ||
|
||
public static DropDownList CreateFromEnum<T>(string name, string label, T defaultValue) where T : Enum { | ||
|
||
List<DropDownItem> values = new List<DropDownItem>(); | ||
|
||
foreach (T value in (T[]) Enum.GetValues(typeof(T))) { | ||
|
||
string valueName = value.ToString(); | ||
|
||
if (value.HasCustomAttribute(out DescriptionAttribute result)) { | ||
valueName = result.Description; | ||
} | ||
|
||
values.Add(new DropDownItem(value.ToCamelCase(), valueName, Equals(value, defaultValue))); | ||
|
||
} | ||
|
||
return new DropDownList(name, label, values); | ||
|
||
} | ||
|
||
#endregion | ||
|
||
} | ||
|
||
} |