Skip to content

Commit

Permalink
Introduced DropDownList and DropDownItem classes
Browse files Browse the repository at this point in the history
  • Loading branch information
abjerner committed Mar 15, 2021
1 parent 2292662 commit c4d8bd9
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/Skybrud.Forms/Models/Fields/DropDown/DropDownItem.cs
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

}

}
87 changes: 87 additions & 0 deletions src/Skybrud.Forms/Models/Fields/DropDown/DropDownList.cs
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

}

}

0 comments on commit c4d8bd9

Please sign in to comment.