diff --git a/src/Components/Components/src/BindConverter.cs b/src/Components/Components/src/BindConverter.cs
index 30ded5fe73..0aadb8077e 100644
--- a/src/Components/Components/src/BindConverter.cs
+++ b/src/Components/Components/src/BindConverter.cs
@@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Components
//
// Perf: our conversion routines present a regular API surface that allows us to specialize on types to avoid boxing.
// for instance, many of these types could be cast to IFormattable to do the appropriate formatting, but that's going
- // to allocate.
+ // to allocate.
public static class BindConverter
{
private static object BoxedTrue = true;
@@ -158,6 +158,41 @@ namespace Microsoft.AspNetCore.Components
return value.Value.ToString(culture ?? CultureInfo.CurrentCulture);
}
+ ///
+ /// Formats the provided for inclusion in an attribute.
+ ///
+ /// The value to format.
+ ///
+ /// The to use while formatting. Defaults to .
+ ///
+ /// The formatted value.
+ public static string FormatValue(short value, CultureInfo culture = null) => FormatShortValueCore(value, culture);
+
+ private static string FormatShortValueCore(short value, CultureInfo culture)
+ {
+ return value.ToString(culture ?? CultureInfo.CurrentCulture);
+ }
+
+ ///
+ /// Formats the provided for inclusion in an attribute.
+ ///
+ /// The value to format.
+ ///
+ /// The to use while formatting. Defaults to .
+ ///
+ /// The formatted value.
+ public static string FormatValue(short? value, CultureInfo culture = null) => FormatNullableShortValueCore(value, culture);
+
+ private static string FormatNullableShortValueCore(short? value, CultureInfo culture)
+ {
+ if (value == null)
+ {
+ return null;
+ }
+
+ return value.Value.ToString(culture ?? CultureInfo.CurrentCulture);
+ }
+
///
/// Formats the provided for inclusion in an attribute.
///
@@ -649,6 +684,71 @@ namespace Microsoft.AspNetCore.Components
return true;
}
+ ///
+ /// Attempts to convert a value to a .
+ ///
+ /// The object to convert.
+ /// The to use for conversion.
+ /// The converted value.
+ /// true if conversion is successful, otherwise false.
+ public static bool TryConvertToShort(object obj, CultureInfo culture, out short value)
+ {
+ return ConvertToShortCore(obj, culture, out value);
+ }
+
+ ///
+ /// Attempts to convert a value to a nullable .
+ ///
+ /// The object to convert.
+ /// The to use for conversion.
+ /// The converted value.
+ /// true if conversion is successful, otherwise false.
+ public static bool TryConvertToNullableShort(object obj, CultureInfo culture, out short? value)
+ {
+ return ConvertToNullableShort(obj, culture, out value);
+ }
+
+ internal static BindParser ConvertToShort = ConvertToShortCore;
+ internal static BindParser ConvertToNullableShort = ConvertToNullableShortCore;
+
+ private static bool ConvertToShortCore(object obj, CultureInfo culture, out short value)
+ {
+ var text = (string)obj;
+ if (string.IsNullOrEmpty(text))
+ {
+ value = default;
+ return false;
+ }
+
+ if (!short.TryParse(text, NumberStyles.Number, culture ?? CultureInfo.CurrentCulture, out var converted))
+ {
+ value = default;
+ return false;
+ }
+
+ value = converted;
+ return true;
+ }
+
+ private static bool ConvertToNullableShortCore(object obj, CultureInfo culture, out short? value)
+ {
+ var text = (string)obj;
+ if (string.IsNullOrEmpty(text))
+ {
+ value = default;
+ return true;
+ }
+
+ if (!short.TryParse(text, NumberStyles.Number, culture ?? CultureInfo.CurrentCulture, out var converted))
+ {
+ value = default;
+ return false;
+ }
+
+ value = converted;
+ return true;
+ }
+
///
/// Attempts to convert a value to a .
///
@@ -1198,6 +1298,14 @@ namespace Microsoft.AspNetCore.Components
{
formatter = (BindFormatter)FormatNullableLongValueCore;
}
+ else if (typeof(T) == typeof(short))
+ {
+ formatter = (BindFormatter)FormatShortValueCore;
+ }
+ else if (typeof(T) == typeof(short?))
+ {
+ formatter = (BindFormatter)FormatNullableShortValueCore;
+ }
else if (typeof(T) == typeof(float))
{
formatter = (BindFormatter)FormatFloatValueCore;
@@ -1323,6 +1431,14 @@ namespace Microsoft.AspNetCore.Components
{
parser = ConvertToNullableLong;
}
+ else if (typeof(T) == typeof(short))
+ {
+ parser = ConvertToShort;
+ }
+ else if (typeof(T) == typeof(short?))
+ {
+ parser = ConvertToNullableShort;
+ }
else if (typeof(T) == typeof(float))
{
parser = ConvertToFloat;