11@page " /select"
22
3+ @inject IJSRuntime Js ;
4+
35<ComponentDemoPage Name =" Select"
46 DesignUrl =" @(null)"
57 ComponentUrl =" https://github.com/material-components/material-web/blob/main/docs/components/select.md"
1012 Select menus display a list of choices on temporary surfaces and display the currently selected menu item above the menu.
1113 </Description >
1214 <ComponentDemo >
13- <div class =" alert alert-warning" >
14- This component is still under development by Material Web.
15- It is not working yet.
15+ <div class =" row" >
16+ <div class =" col" >
17+ <MdSelect @ref =" cboNum" @bind-Value =" selectedValue" @bind-SelectedIndex =" selectedIndexes[0]"
18+ class =" w-100"
19+ Required =" required"
20+ Quick =" quick"
21+ Disabled =" disabled"
22+ Error =" hasError"
23+ ErrorText =" @(hasError ? " Did you divide by 0? " : null)"
24+ Label =" @(hasLabel ? " Select a constant " : null)"
25+ SupportingText =" @(hasSupportingText ? " Custom validity only accepts values smaller than 4 " : null)"
26+ HasLeadingIcon =" leadingIcon" >
27+ @if (leadingIcon )
28+ {
29+ <MdIcon slot =" @(MdSelect.LeadingIconSlot)" >function </MdIcon >
30+ }
31+
32+ @foreach ( var (value , text , symbol , desc , url ) in Values )
33+ {
34+ <MdSelectOption Value =" @(value)" >
35+ <div slot =" @(MdSelectOption.HeadlineSlot)" >@( text ) </div >
36+ <div slot =" @(MdSelectOption.StartSlot)" >@( symbol ) </div >
37+ <div slot =" @(MdSelectOption.OverlineSlot)" >@( value ) </div >
38+ <div slot =" @(MdSelectOption.SupportingTextSlot)" >@( desc ) </div >
39+
40+ <MdIconButton slot =" @(MdSelectOption.EndSlot)" Href =" @(url)" Target =" _blank" >
41+ <MdIcon >open_in_new </MdIcon >
42+ </MdIconButton >
43+ </MdSelectOption >
44+ }
45+ </MdSelect >
46+
47+ <p >Selected value: @( selectedValue ) , selected index: @( selectedIndexes [0 ]) </p >
48+ </div >
49+ <div class =" col" >
50+ <MdSelect @ref =" cbo" @bind-Value =" selectedStrValue" @bind-SelectedIndex =" selectedIndexes[1]"
51+ class =" w-100"
52+ SelectStyle =" MdSelectStyle.Filled"
53+ Required =" required"
54+ Quick =" quick"
55+ Disabled =" disabled"
56+ Error =" hasError"
57+ ErrorText =" @(hasError ? " Oops, someone doesn ' t like fruit" : null)"
58+ Label="@(hasLabel ? "Select a fruit" : null)"
59+ SupportingText="@(hasSupportingText ? "May be empty" : null)"
60+ HasLeadingIcon="leadingIcon">
61+
62+ @if (leadingIcon)
63+ {
64+ <MdIcon slot="@(MdSelect.LeadingIconSlot)">nutrition</MdIcon>
65+ }
66+
67+ <MdSelectOption Value="@("")">No thanks</MdSelectOption>
68+ <MdDivider />
69+ @for (int i = 0; i < StrValues.Length; i++)
70+ {
71+ var z = i;
72+ var value = StrValues[z];
73+
74+ <MdSelectOption Value="@(value)" @bind-Selected="strSelected[z]">
75+ <div slot="@(MdSelectOption.HeadlineSlot)">@(value)</div>
76+ </MdSelectOption>
77+ }
78+ </MdSelect>
79+
80+ <p>Selected String value: @(selectedStrValue), selected index: @(selectedIndexes[1])</p>
81+ <p><code>MdSelectOption</code> selected: @(string.Join(", ", strSelected))</p>
82+ </div>
1683 </div>
1784
18- <MdSelect SelectStyle =" style" >
19- <MdSelectOption Headline =" Apple" Selected />
20- <MdSelectOption Headline =" Apricot" />
21- </MdSelect >
2285 </ComponentDemo>
2386 <Tweaks>
87+ <div class="hstack gap-3 flex-wrap">
88+ <LabeledSwitch @bind-Selected="required" Label="Required" />
89+ <LabeledSwitch @bind-Selected="quick" Label="Quick (skip animation)" />
90+ <LabeledSwitch @bind-Selected="disabled" Label="Disabled" />
91+ <LabeledSwitch @bind-Selected="hasError" Label="Error" />
92+ <LabeledSwitch @bind-Selected="hasLabel" Label="Label" />
93+ <LabeledSwitch @bind-Selected="hasSupportingText" Label="Supporting text" />
94+ <LabeledSwitch @bind-Selected="leadingIcon" Label="Leading icon" />
95+ </div>
2496
97+ <div class="hstack gap-3">
98+ <MdButton @onclick="() => cbo.ReportValidityAsync()">Report validity</MdButton>
99+ <MdButton @onclick="SetCustomValidityAsync">Set custom validity</MdButton>
100+ <MdButton @onclick="PrintOptionsAsync">Print options</MdButton>
101+ <MdButton @onclick="PrintSelectedOptionsAsync">Print selected options</MdButton>
102+ </div>
25103 </Tweaks>
26104</ComponentDemoPage>
27105
28106@code {
107+ bool required = true;
108+ bool quick;
109+ bool disabled;
110+ bool hasError;
111+ bool hasLabel = true;
112+ bool hasSupportingText = true;
113+ bool leadingIcon = true;
114+
115+ int[] selectedIndexes = new int[2];
116+
117+ double selectedValue = Values[0].Value;
118+ static readonly (double Value, string Text, string Symbol, string Desc, string url)[] Values =
119+ {
120+ (
121+ Math.PI, "Pi", "π",
122+ "The ratio of a circle' s circumference to its diameter " ,
123+ " https: //en.wikipedia.org /wiki /Pi "
124+ ),
125+ (
126+ Math.E, " Euler ' s number", "e",
127+ "The base of natural logarithms",
128+ "https://en.wikipedia.org/wiki/E_(mathematical_constant)"
129+ ),
130+ (
131+ Math.Tau, "Tau", "τ",
132+ "Ratio of a circle' s circumference to its radius " ,
133+ " https: //en.wikipedia.org /wiki /Turn_(angle)#Tau_proposals "
134+ ),
135+ };
136+
137+ string? selectedStrValue = StrValues[0];
138+ static readonly string[] StrValues = { " Apple " , " Banana " , " Cucumber " , };
139+ bool[] strSelected = { true, false, false };
140+
141+ MdSelect<double> cboNum = null!;
142+ MdSelect<string?> cbo = null!;
143+
144+ async Task PrintOptionsAsync()
145+ {
146+ var options = await cbo.GetOptionsAsync();
147+ await Js.InvokeVoidAsync(" console.log " , options);
148+ }
149+
150+ async Task PrintSelectedOptionsAsync()
151+ {
152+ var options = await cbo.GetSelectedOptionsAsync();
153+ await Js.InvokeVoidAsync(" console.log " , options);
154+ }
155+
156+ async Task SetCustomValidityAsync()
157+ {
158+ await cboNum.SetCustomValidityAsync(selectedValue < 4 ?
159+ " " : " Please pick a constant smaller than 4 " );
29160
30- MdSelectStyle style = MdSelectStyle .Outlined ;
161+ // Must call this. See https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement/setCustomValidity
162+ await cboNum.ReportValidityAsync();
163+ }
31164
32165}
0 commit comments