Skip to content

Commit

Permalink
Added additional AddCheckboxList, AddDropDownList and AddRadioList ex…
Browse files Browse the repository at this point in the history
…tension methods

The new extension methods allow specifying an IEnumerable<T> collection and a callback function for converting each item into ListItem instances.
  • Loading branch information
abjerner committed Jul 6, 2022
1 parent e7a0ee3 commit 2bd806d
Showing 1 changed file with 100 additions and 10 deletions.
110 changes: 100 additions & 10 deletions src/Skybrud.Forms/Models/FormExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Skybrud.Forms.Models.Fields;

// ReSharper disable InconsistentNaming
Expand Down Expand Up @@ -220,16 +221,6 @@ public static T AddCheckbox<T>(this T form, string name, string value = null, st
return form;
}











/// <summary>
/// Appends a new checkbox list to <paramref name="form"/>'s list of fields.
/// </summary>
Expand Down Expand Up @@ -284,6 +275,39 @@ public static T AddCheckboxList<T>(this T form, string name, string label, param
return form;
}

/// <summary>
/// Appends a new checkbox list to <paramref name="form"/>'s list of fields.
/// </summary>
/// <typeparam name="T">The type of the form.</typeparam>
/// <typeparam name="TItem">The type of the items.</typeparam>
/// <param name="form">The form.</param>
/// <param name="name">The name of the field.</param>
/// <param name="items">The items that should make up the índividual checkboxes of the list.</param>
/// <param name="function">At callback function used for converting each <typeparamref name="TItem"/> item to <see cref="ListItem"/>.</param>
/// <returns><paramref name="form"/> - which may be used for mehtod chaining.</returns>
public static T AddCheckboxList<T, TItem>(this T form, string name, IEnumerable<TItem> items, Func<TItem, ListItem> function) where T : Form {
if (form == null) return null;
form.Fields.Add(new CheckboxList(name, items?.Select(function)));
return form;
}

/// <summary>
/// Appends a new checkbox list to <paramref name="form"/>'s list of fields.
/// </summary>
/// <typeparam name="T">The type of the form.</typeparam>
/// <typeparam name="TItem">The type of the items.</typeparam>
/// <param name="form">The form.</param>
/// <param name="name">The name of the field.</param>
/// <param name="label">The label of the field.</param>
/// <param name="items">The items that should make up the índividual checkboxes of the list.</param>
/// <param name="function">At callback function used for converting each <typeparamref name="TItem"/> item to <see cref="ListItem"/>.</param>
/// <returns><paramref name="form"/> - which may be used for mehtod chaining.</returns>
public static T AddCheckboxList<T, TItem>(this T form, string name, string label, IEnumerable<TItem> items, Func<TItem, ListItem> function) where T : Form {
if (form == null) return null;
form.Fields.Add(new CheckboxList(name, label, items?.Select(function)));
return form;
}

/// <summary>
/// Appends a new checkbox list to <paramref name="form"/>'s list of fields with the values based on <typeparamref name="TEnum"/>.
/// </summary>
Expand Down Expand Up @@ -367,6 +391,39 @@ public static T AddDropDownList<T>(this T form, string name, string label, param
return form;
}

/// <summary>
/// Appends a new drop-down list to <paramref name="form"/>'s list of fields.
/// </summary>
/// <typeparam name="T">The type of the form.</typeparam>
/// <typeparam name="TItem">The type of the items.</typeparam>
/// <param name="form">The form.</param>
/// <param name="name">The name of the field.</param>
/// <param name="items">The items that should make up the índividual checkboxes of the list.</param>
/// <param name="function">At callback function used for converting each <typeparamref name="TItem"/> item to <see cref="ListItem"/>.</param>
/// <returns><paramref name="form"/> - which may be used for mehtod chaining.</returns>
public static T AddDropDownList<T, TItem>(this T form, string name, IEnumerable<TItem> items, Func<TItem, ListItem> function) where T : Form {
if (form == null) return null;
form.Fields.Add(new DropDownList(name, items?.Select(function)));
return form;
}

/// <summary>
/// Appends a new drop-down list to <paramref name="form"/>'s list of fields.
/// </summary>
/// <typeparam name="T">The type of the form.</typeparam>
/// <typeparam name="TItem">The type of the items.</typeparam>
/// <param name="form">The form.</param>
/// <param name="name">The name of the field.</param>
/// <param name="label">The label of the field.</param>
/// <param name="items">The items that should make up the índividual checkboxes of the list.</param>
/// <param name="function">At callback function used for converting each <typeparamref name="TItem"/> item to <see cref="ListItem"/>.</param>
/// <returns><paramref name="form"/> - which may be used for mehtod chaining.</returns>
public static T AddDropDownList<T, TItem>(this T form, string name, string label, IEnumerable<TItem> items, Func<TItem, ListItem> function) where T : Form {
if (form == null) return null;
form.Fields.Add(new DropDownList(name, label, items?.Select(function)));
return form;
}

/// <summary>
/// Appends a new drop-down list to <paramref name="form"/>'s list of fields with the values based on <typeparamref name="TEnum"/>.
/// </summary>
Expand Down Expand Up @@ -450,6 +507,39 @@ public static T AddRadioList<T>(this T form, string name, string label, params L
return form;
}

/// <summary>
/// Appends a new radio list to <paramref name="form"/>'s list of fields.
/// </summary>
/// <typeparam name="T">The type of the form.</typeparam>
/// <typeparam name="TItem">The type of the items.</typeparam>
/// <param name="form">The form.</param>
/// <param name="name">The name of the field.</param>
/// <param name="items">The items that should make up the índividual checkboxes of the list.</param>
/// <param name="function">At callback function used for converting each <typeparamref name="TItem"/> item to <see cref="ListItem"/>.</param>
/// <returns><paramref name="form"/> - which may be used for mehtod chaining.</returns>
public static T AddRadioList<T, TItem>(this T form, string name, IEnumerable<TItem> items, Func<TItem, ListItem> function) where T : Form {
if (form == null) return null;
form.Fields.Add(new RadioList(name, items?.Select(function)));
return form;
}

/// <summary>
/// Appends a new radio list to <paramref name="form"/>'s list of fields.
/// </summary>
/// <typeparam name="T">The type of the form.</typeparam>
/// <typeparam name="TItem">The type of the items.</typeparam>
/// <param name="form">The form.</param>
/// <param name="name">The name of the field.</param>
/// <param name="label">The label of the field.</param>
/// <param name="items">The items that should make up the índividual checkboxes of the list.</param>
/// <param name="function">At callback function used for converting each <typeparamref name="TItem"/> item to <see cref="ListItem"/>.</param>
/// <returns><paramref name="form"/> - which may be used for mehtod chaining.</returns>
public static T AddRadioList<T, TItem>(this T form, string name, string label, IEnumerable<TItem> items, Func<TItem, ListItem> function) where T : Form {
if (form == null) return null;
form.Fields.Add(new RadioList(name, label, items?.Select(function)));
return form;
}

/// <summary>
/// Appends a new radio list list to <paramref name="form"/>'s list of fields with the values based on <typeparamref name="TEnum"/>.
/// </summary>
Expand Down

0 comments on commit 2bd806d

Please sign in to comment.