Skip to content

Commit 31eab01

Browse files
committed
Added Array support for Select and Tabs' methods.
1 parent 718a9dc commit 31eab01

File tree

7 files changed

+79
-16
lines changed

7 files changed

+79
-16
lines changed

BlazorMaterialWeb.Demo/Pages/Progress.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<ComponentDemoPage Name="Progress indicators"
44
DesignUrl="https://m3.material.io/components/progress-indicators/overview"
55
ComponentUrl="https://material-web.dev/components/progress/"
6-
ComponentSourcePath="elevation"
7-
BlazorComponentSourcePath="Elevation"
8-
BlazorDemoSourceName="Elevation">
6+
ComponentSourcePath="progress"
7+
BlazorComponentSourcePath="MdProgress.razor"
8+
BlazorDemoSourceName="Progress">
99
<Description>
1010
Progress indicators show the status of a process in real time
1111
</Description>

BlazorMaterialWeb.Demo/Pages/Tabs.razor

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@
9898
<MdButton @onclick="Select3rdTabs">
9999
Programmatically select the 3rd tabs
100100
</MdButton>
101+
102+
<MdButton @onclick="GetAndSelectTab">
103+
Programmatically get the 2nd tab then select it
104+
</MdButton>
101105
</Stack>
102106
</Tweaks>
103107
</ComponentDemoPage>
@@ -138,4 +142,15 @@
138142
}
139143
}
140144

145+
async Task GetAndSelectTab()
146+
{
147+
foreach (var t in tabRefs.Values)
148+
{
149+
var tabs = await t.GetTabsAsync();
150+
151+
var secondTab = await tabs.GetItemAsync(1);
152+
await t.SetActiveTabAsync(secondTab);
153+
}
154+
}
155+
141156
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace BlazorMaterialWeb.Common;
2+
3+
public interface IJSArrayReference<T>
4+
{
5+
IJSObjectReference JSObjectReference { get; }
6+
7+
Task<T> GetItemAsync(long index);
8+
Task<long> GetLengthAsync();
9+
}
10+
11+
class JSArrayReference<T> : IJSArrayReference<T>
12+
{
13+
readonly IJSObjectReference jsRef;
14+
readonly IJSRuntime js;
15+
16+
public JSArrayReference(IJSObjectReference jsRef, IJSRuntime js)
17+
{
18+
this.jsRef = jsRef;
19+
this.js = js;
20+
}
21+
22+
public IJSObjectReference JSObjectReference => jsRef;
23+
24+
public async Task<long> GetLengthAsync() =>
25+
await js.GetObjectPropertyAsync<long>(jsRef, "length");
26+
27+
public async Task<T> GetItemAsync(long index) =>
28+
await js.GetArrayElementAsync<T>(jsRef, index);
29+
30+
}

BlazorMaterialWeb/Extensions/JsExtensions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ internal static class JsExtensions
66

77
public static async Task<T> GetElementPropertyAsync<T>(this IJSRuntime js, ElementReference el, string propertyName) =>
88
await js.InvokeAsync<T>(Prefix("getElementProperty"), el, propertyName);
9-
9+
10+
public static async Task<T> GetObjectPropertyAsync<T>(this IJSRuntime js, IJSObjectReference obj, string propertyName) =>
11+
await js.InvokeAsync<T>(Prefix("getElementProperty"), obj, propertyName);
12+
1013
public static async Task SetElementPropertyAsync(this IJSRuntime js, ElementReference el, string propertyName, object? value) =>
1114
await js.InvokeVoidAsync(Prefix("setElementProperty"), el, propertyName, value);
1215

16+
public static async Task SetObjectPropertyAsync(this IJSRuntime js, IJSObjectReference obj, string propertyName, object? value) =>
17+
await js.InvokeVoidAsync(Prefix("setElementProperty"), obj, propertyName, value);
18+
1319
public static async Task InvokeElementMethodAsync(this IJSRuntime js, ElementReference el, string methodName, params object?[] parameters) =>
1420
await js.InvokeVoidAsync(
1521
Prefix("invokeElementMethodAsync"),

BlazorMaterialWeb/Select/MdSelect.razor.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,17 @@ async Task OnChangeAsync(MdSelectChangeEventArgs e)
114114
await OnChange.InvokeAsync(e);
115115
}
116116

117-
public async Task<IJSObjectReference> GetOptionsAsync() =>
118-
await Js.GetElementPropertyAsync<IJSObjectReference>(el, "options");
117+
public async Task<IJSArrayReference<IJSObjectReference>> GetOptionsAsync()
118+
{
119+
var options = await Js.GetElementPropertyAsync<IJSObjectReference>(el, "options");
120+
return new JSArrayReference<IJSObjectReference>(options, Js);
121+
}
119122

120-
public async Task<IJSObjectReference> GetSelectedOptionsAsync() =>
121-
await Js.GetElementPropertyAsync<IJSObjectReference>(el, "selectedOptions");
123+
public async Task<IJSArrayReference<IJSObjectReference>> GetSelectedOptionsAsync()
124+
{
125+
var options = await Js.GetElementPropertyAsync<IJSObjectReference>(el, "selectedOptions");
126+
return new JSArrayReference<IJSObjectReference>(options, Js);
127+
}
122128

123129
public async Task ResetAsync() =>
124130
await InvokeElAsync("reset");

BlazorMaterialWeb/Tabs/MdTabs.razor.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace BlazorMaterialWeb;
1+
using BlazorMaterialWeb.Common;
2+
3+
namespace BlazorMaterialWeb;
24

35
/// <summary>
46
/// Tabs organize content across different screens and views.
@@ -17,22 +19,26 @@ partial class MdTabs
1719
[Parameter]
1820
public EventCallback<MdTabChangeEventArgs> OnTabChanged { get; set; }
1921

20-
public async Task<IJSObjectReference> GetTabsAsync() =>
21-
await GetPropertyAsync<IJSObjectReference>("tabs");
22+
public async Task<IJSArrayReference<IJSObjectReference>> GetTabsAsync()
23+
{
24+
var tabs = await GetPropertyAsync<IJSObjectReference>("tabs");
25+
26+
return new JSArrayReference<IJSObjectReference>(tabs, Js);
27+
}
2228

2329
public async Task<int> GetActiveTabIndexAsync() =>
2430
await GetPropertyAsync<int>("activeTabIndex");
2531

2632
public async Task SetActiveTabIndexAsync(int index) =>
2733
await SetPropertyAsync("activeTabIndex", index);
2834

29-
public async Task<ElementReference> GetActiveTabAsync() =>
30-
await GetPropertyAsync<ElementReference>("activeTab");
35+
public async Task<IJSObjectReference> GetActiveTabAsync() =>
36+
await GetPropertyAsync<IJSObjectReference>("activeTab");
3137

32-
public async Task SetActiveTabAsync(ElementReference tab) =>
38+
public async Task SetActiveTabAsync(IJSObjectReference tab) =>
3339
await SetPropertyAsync("activeTab", tab);
3440

35-
public async Task ScrollToTabAsync(ElementReference tab) =>
41+
public async Task ScrollToTabAsync(IJSObjectReference tab) =>
3642
await InvokeMethodAsync("scrollToTab", tab);
3743

3844
}

pack.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ foreach($file in $files) {
44
rm $file.FullName
55
}
66

7-
$version="--property:Version=1.0.3"
7+
$version="--property:Version=1.1.1"
88

99
dotnet pack .\BlazorMaterialWeb\BlazorMaterialWeb.csproj -c Release "--property:PackageOutputPath=.\bin\nuget" "$version"
1010
dotnet pack .\BlazorMaterialWeb.Bundled\BlazorMaterialWeb.Bundled.csproj -c Release "--property:PackageOutputPath=.\bin\nuget" "$version"

0 commit comments

Comments
 (0)