Handle "month" input type (#7022)

Addresses #6615
This commit is contained in:
Jass Bagga 2017-11-09 10:25:36 -08:00 committed by GitHub
parent bcaee8df4e
commit bb07119b4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 1 deletions

View File

@ -36,6 +36,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
{ "DateTime-local", "datetime-local" },
{ nameof(DateTimeOffset), "text" },
{ "Time", "time" },
{ "Month", "month" },
{ nameof(Byte), "number" },
{ nameof(SByte), "number" },
{ nameof(Int16), "number" },
@ -378,7 +379,12 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
private string GetFormat(ModelExplorer modelExplorer, string inputTypeHint, string inputType)
{
string format;
if (string.Equals("decimal", inputTypeHint, StringComparison.OrdinalIgnoreCase) &&
if (string.Equals("month", inputType, StringComparison.OrdinalIgnoreCase))
{
// A new HTML5 input type that only will be rendered in Rfc3339 mode
format = "{0:yyyy-MM}";
}
else if (string.Equals("decimal", inputTypeHint, StringComparison.OrdinalIgnoreCase) &&
string.Equals("text", inputType, StringComparison.Ordinal) &&
string.IsNullOrEmpty(modelExplorer.Metadata.EditFormatString))
{

View File

@ -370,6 +370,14 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
return GenerateTextBox(htmlHelper, inputType: "time");
}
public static IHtmlContent MonthInputTemplate(IHtmlHelper htmlHelper)
{
// A new HTML5 input type that only will be rendered in Rfc3339 mode
htmlHelper.Html5DateRenderingMode = Html5DateRenderingMode.Rfc3339;
ApplyRfc3339DateFormattingIfNeeded(htmlHelper, "{0:yyyy-MM}");
return GenerateTextBox(htmlHelper, inputType: "month");
}
public static IHtmlContent NumberInputTemplate(IHtmlHelper htmlHelper)
{
return GenerateTextBox(htmlHelper, inputType: "number");

View File

@ -52,6 +52,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
{ "DateTime-local", DefaultEditorTemplates.DateTimeLocalInputTemplate },
{ nameof(DateTimeOffset), DefaultEditorTemplates.DateTimeOffsetTemplate },
{ "Time", DefaultEditorTemplates.TimeInputTemplate },
{ "Month", DefaultEditorTemplates.MonthInputTemplate },
{ typeof(byte).Name, DefaultEditorTemplates.NumberInputTemplate },
{ typeof(sbyte).Name, DefaultEditorTemplates.NumberInputTemplate },
{ typeof(short).Name, DefaultEditorTemplates.NumberInputTemplate },

View File

@ -1052,6 +1052,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
{ "text", null, "text" },
{ "TEXT", null, "text" },
{ "time", null, "time" },
{ "month", "{0:yyyy-MM}", "month" },
{ "UInt16", null, "number" },
{ "uint16", null, "number" },
{ "UInt32", null, "number" },
@ -1217,6 +1218,8 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
[InlineData("DateTimeLocal", Html5DateRenderingMode.Rfc3339, "{0:yyyy-MM-ddTHH:mm:ss.fff}", "datetime-local")]
[InlineData("Time", Html5DateRenderingMode.CurrentCulture, "{0:t}", "time")] // Format from [DataType].
[InlineData("Time", Html5DateRenderingMode.Rfc3339, "{0:HH:mm:ss.fff}", "time")]
[InlineData("Month", Html5DateRenderingMode.CurrentCulture, "{0:yyyy-MM}", "month")]
[InlineData("Month", Html5DateRenderingMode.Rfc3339, "{0:yyyy-MM}", "month")]
[InlineData("NullableDate", Html5DateRenderingMode.Rfc3339, "{0:yyyy-MM-dd}", "date")]
[InlineData("NullableDateTime", Html5DateRenderingMode.Rfc3339, "{0:yyyy-MM-ddTHH:mm:ss.fff}", "datetime-local")]
[InlineData("NullableDateTimeOffset", Html5DateRenderingMode.Rfc3339, "{0:yyyy-MM-ddTHH:mm:ss.fffK}", "text")]
@ -1377,6 +1380,9 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
[DataType(DataType.Time)]
public DateTimeOffset Time { get; set; }
[DataType("month")]
public DateTimeOffset Month { get; set; }
}
private class NestedModel

View File

@ -57,6 +57,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
{ "DateTimeOffset", "__TextBox__ class='text-box single-line' type='text'" },
{ "Time", "__TextBox__ class='text-box single-line' type='time'" },
{ "time", "__TextBox__ class='text-box single-line' type='time'" },
{ "Month", "__TextBox__ class='text-box single-line' type='month'" },
{ "month", "__TextBox__ class='text-box single-line' type='month'" },
{ "Byte", "__TextBox__ class='text-box single-line' type='number'" },
{ "BYTE", "__TextBox__ class='text-box single-line' type='number'" },
{ "SByte", "__TextBox__ class='text-box single-line' type='number'" },
@ -825,6 +827,7 @@ Environment.NewLine;
[InlineData("datetime-local", null, "2000-01-02T03:04:05.060", "datetime-local")]
[InlineData("DateTimeOffset", "{0:o}", "2000-01-02T03:04:05.060-05:00", "text")]
[InlineData("time", "{0:t}", "03:04:05.060", "time")]
[InlineData("month", "{0:yyyy-MM}", "2000-01", "month")]
public void Editor_FindsCorrectDateOrTimeTemplate(
string dataTypeName,
string editFormatString,
@ -886,6 +889,7 @@ Environment.NewLine;
[InlineData("datetime-local", null, "02/01/2000 03:04:05 -05:00", "datetime-local")]
[InlineData("DateTimeOffset", "{0:o}", "2000-01-02T03:04:05.0600000-05:00", "text")]
[InlineData("time", "{0:t}", "03:04", "time")]
[InlineData("month", "{0:yyyy-MM}", "2000-01", "month")]
[ReplaceCulture]
public void Editor_FindsCorrectDateOrTimeTemplate_NotRfc3339(
string dataTypeName,
@ -950,6 +954,7 @@ Environment.NewLine;
[InlineData("datetime-local", null, "2000-01-02T03:04:05.060", "datetime-local")]
[InlineData("DateTimeOffset", "{0:o}", "2000-01-02T03:04:05.060Z", "text")]
[InlineData("time", "{0:t}", "03:04:05.060", "time")]
[InlineData("month", "{0:yyyy-MM}", "2000-01", "month")]
public void Editor_FindsCorrectDateOrTimeTemplate_ForDateTime(
string dataTypeName,
string editFormatString,
@ -1010,6 +1015,7 @@ Environment.NewLine;
[InlineData("datetime-local", null, "02/01/2000 03:04:05", "datetime-local")]
[InlineData("DateTimeOffset", "{0:o}", "2000-01-02T03:04:05.0600000Z", "text")]
[InlineData("time", "{0:t}", "03:04", "time")]
[InlineData("month", "{0:yyyy-MM}", "2000-01", "month")]
[ReplaceCulture]
public void Editor_FindsCorrectDateOrTimeTemplate_ForDateTimeNotRfc3339(
string dataTypeName,
@ -1076,6 +1082,8 @@ Environment.NewLine;
[InlineData("datetime-local", Html5DateRenderingMode.Rfc3339, "datetime-local")]
[InlineData("time", Html5DateRenderingMode.CurrentCulture, "time")]
[InlineData("time", Html5DateRenderingMode.Rfc3339, "time")]
[InlineData("month", Html5DateRenderingMode.CurrentCulture, "month")]
[InlineData("month", Html5DateRenderingMode.Rfc3339, "month")]
public void Editor_AppliesNonDefaultEditFormat(string dataTypeName, Html5DateRenderingMode renderingMode, string expectedType)
{
// Arrange