Skip to content

Commit 7ff55f3

Browse files
committed
Merge pull request #80 from TravisTheTechie/master
Adding `TabIndex` and `AutoFocus`.
2 parents 4b171fa + 45e93f3 commit 7ff55f3

File tree

6 files changed

+57
-4
lines changed

6 files changed

+57
-4
lines changed

ChameleonForms.Example/Views/ExampleForms/Form1.cshtml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@using ChameleonForms.Example.Forms.Components
22
@using ChameleonForms.Templates.TwitterBootstrap3
3+
@using ChameleonForms.Component.Config
34
@model ViewModelExample
45

56
@{
@@ -15,12 +16,12 @@
1516
@using (var f = Html.BeginChameleonForm(method: FormMethod.Post, enctype: EncType.Multipart))
1617
{
1718
<p>@f.LabelFor(m => m.SomeCheckbox).Label("Are you ready for: ") @f.FieldElementFor(m => m.SomeCheckbox) @f.ValidationMessageFor(m => m.SomeCheckbox)</p>
18-
<p>@f.FieldElementFor(m => m.RequiredStringField)</p>
19+
<p>@f.FieldElementFor(m => m.RequiredStringField).TabIndex(4)</p>
1920
using (var s = f.BeginSection("My Section!", InstructionalText(), new{@class = "aClass"}.ToHtmlAttributes()))
2021
{
21-
using (var ff = s.BeginFieldFor(m => m.RequiredStringField, Field.Configure().Attr("data-some-attr", "value")))
22+
using (var ff = s.BeginFieldFor(m => m.RequiredStringField, Field.Configure().Attr("data-some-attr", "value").TabIndex(3)))
2223
{
23-
@ff.FieldFor(m => m.NestedField).Attr("data-attr1", "value")
24+
@ff.FieldFor(m => m.NestedField).Attr("data-attr1", "value").TabIndex(2)
2425
@ff.FieldFor(m => m.SomeEnum).Attr("data-attr1", "value")
2526
}
2627
@s.FieldFor(m => m.SomeCheckbox).AsDropDown()
@@ -29,7 +30,7 @@
2930
@ss.FieldFor(m => m.FileUpload).Attr("data-attr1", "value")
3031
}
3132
@s.FieldFor(m => m.RequiredStringField).OverrideFieldHtml(new MvcHtmlString("Custom html <b>she-yeah</b>!"))
32-
@s.FieldFor(m => m.TextAreaField).Cols(60).Rows(5).Label("Some Label")
33+
@s.FieldFor(m => m.TextAreaField).Cols(60).Rows(5).Label("Some Label").AutoFocus().TabIndex(1)
3334
@s.FieldFor(m => m.SomeCheckbox).InlineLabel("Some label").WithHint("Format: XXX")
3435
@s.FieldFor(m => m.SomeCheckbox).AsRadioList().WithTrueAs("True").WithFalseAs("False")
3536
@s.FieldFor(m => m.ListId)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
 autofocus="autofocus"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
 tabindex="44"

ChameleonForms.Tests/Component/Config/FieldConfigurationTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ public void Set_placeholder_attribute()
9696
HtmlApprovals.VerifyHtml(fc.Attributes.ToHtmlString());
9797
}
9898

99+
[Test]
100+
public void Set_autofocus_attribute()
101+
{
102+
var fc = Field.Configure().AutoFocus();
103+
104+
HtmlApprovals.VerifyHtml(fc.Attributes.ToHtmlString());
105+
}
106+
107+
[Test]
108+
public void Set_tabindex_attribute()
109+
{
110+
var fc = Field.Configure().TabIndex(44);
111+
112+
HtmlApprovals.VerifyHtml(fc.Attributes.ToHtmlString());
113+
}
114+
99115
[Test]
100116
public void Set_inline_label()
101117
{

ChameleonForms/ChameleonForms.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<ItemGroup>
7676
<Compile Include="Attributes\ExistsInAttribute.cs" />
7777
<Compile Include="Component\Config\FieldConfiguration.cs" />
78+
<Compile Include="Component\Config\FieldConfigurationExtensions.cs" />
7879
<Compile Include="Component\Config\ReadonlyFieldConfiguration.cs" />
7980
<Compile Include="Component\FieldParent.cs" />
8081
<Compile Include="Component\Navigation.cs" />
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace ChameleonForms.Component.Config
2+
{
3+
/// <summary>
4+
/// Provides additional configuration options to FieldConfiguration
5+
/// </summary>
6+
public static class FieldConfigurationExtensions
7+
{
8+
/// <summary>
9+
/// Sets the tab index of a given field
10+
/// </summary>
11+
/// <param name="config">Field configuration to update</param>
12+
/// <param name="index">Tab index to be set</param>
13+
/// <returns>The instance of IFieldConfiguration passed in to continue chaining things</returns>
14+
public static IFieldConfiguration TabIndex(this IFieldConfiguration config, int index)
15+
{
16+
if (config != null)
17+
return config.Attr("tabindex", index);
18+
return null;
19+
}
20+
21+
/// <summary>
22+
/// Applys the autofocus attribute to a given field
23+
/// </summary>
24+
/// <param name="config">Field configuration to modify</param>
25+
/// <returns>The instance of IFieldConfiguration passed in to continue chaining things</returns>
26+
public static IFieldConfiguration AutoFocus(this IFieldConfiguration config)
27+
{
28+
if (config != null)
29+
return config.Attr("autofocus", "autofocus");
30+
return null;
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)