forked from radzenhq/radzen-blazor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadzenCheckBoxList.razor.cs
290 lines (252 loc) · 9.34 KB
/
RadzenCheckBoxList.razor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Radzen.Blazor.Rendering;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Radzen.Blazor
{
/// <summary>
/// RadzenCheckBoxList component.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <example>
/// <code>
/// <RadzenCheckBoxList @bind-Value=@checkedValues TValue="int" >
/// <Items>
/// <RadzenCheckBoxListItem Text="Orders" Value="1" />
/// <RadzenCheckBoxListItem Text="Employees" Value="2" />
/// </Items>
/// </RadzenCheckBoxList>
/// </code>
/// </example>
public partial class RadzenCheckBoxList<TValue> : FormComponent<IEnumerable<TValue>>
{
ClassList ItemClassList(RadzenCheckBoxListItem<TValue> item) => ClassList.Create("rz-chkbox-box")
.Add("rz-state-active", IsSelected(item))
.AddDisabled(Disabled || item.Disabled);
ClassList IconClassList(RadzenCheckBoxListItem<TValue> item) => ClassList.Create("rz-chkbox-icon")
.Add("notranslate rzi rzi-check", IsSelected(item));
/// <summary>
/// Gets or sets the value property.
/// </summary>
/// <value>The value property.</value>
[Parameter]
public string ValueProperty { get; set; }
/// <summary>
/// Gets or sets the text property.
/// </summary>
/// <value>The text property.</value>
[Parameter]
public string TextProperty { get; set; }
/// <summary>
/// Gets or sets the disabled property.
/// </summary>
/// <value>The disabled property.</value>
[Parameter]
public string DisabledProperty { get; set; }
/// <summary>
/// Gets or sets the read-only property.
/// </summary>
/// <value>The read-only property.</value>
[Parameter]
public string ReadOnlyProperty { get; set; }
IEnumerable<RadzenCheckBoxListItem<TValue>> allItems
{
get
{
return items.Concat((Data != null ? Data.Cast<object>() : Enumerable.Empty<object>()).Select(i =>
{
var item = new RadzenCheckBoxListItem<TValue>();
item.SetText((string)PropertyAccess.GetItemOrValueFromProperty(i, TextProperty));
item.SetValue((TValue)PropertyAccess.GetItemOrValueFromProperty(i, ValueProperty));
if (DisabledProperty != null && PropertyAccess.TryGetItemOrValueFromProperty<bool>(i, DisabledProperty, out var disabledResult))
{
item.SetDisabled(disabledResult);
}
if (ReadOnlyProperty != null && PropertyAccess.TryGetItemOrValueFromProperty<bool>(i, ReadOnlyProperty, out var readOnlyResult))
{
item.SetReadOnly(readOnlyResult);
}
return item;
}));
}
}
/// <summary>
/// Gets or sets a value indicating whether the user can select all values. Set to <c>false</c> by default.
/// </summary>
/// <value><c>true</c> if select all values is allowed; otherwise, <c>false</c>.</value>
[Parameter]
public bool AllowSelectAll { get; set; } = false;
/// <summary>
/// Gets or sets the select all text.
/// </summary>
/// <value>The select all text.</value>
[Parameter]
public string SelectAllText { get; set; }
async Task SelectAll(bool? value)
{
if (Disabled || ReadOnly)
{
return;
}
if (value == true)
{
Value = allItems.Where(i => !i.Disabled).Select(i => i.Value);
}
else if (value == false)
{
Value = null;
}
await ValueChanged.InvokeAsync(Value);
if (FieldIdentifier.FieldName != null) { EditContext?.NotifyFieldChanged(FieldIdentifier); }
await Change.InvokeAsync(Value);
StateHasChanged();
}
bool? IsAllSelected()
{
Func<RadzenCheckBoxListItem<TValue>, bool> predicate = i => Value != null && Value.Contains(i.Value);
var all = allItems.All(predicate);
var any = allItems.Any(predicate);
if (all)
{
return true;
}
else
{
return any ? null : (bool?)false;
}
}
IEnumerable _data = null;
/// <summary>
/// Gets or sets the data used to generate items.
/// </summary>
/// <value>The data.</value>
[Parameter]
public virtual IEnumerable Data
{
get
{
return _data;
}
set
{
if (_data != value)
{
_data = value;
StateHasChanged();
}
}
}
/// <summary>
/// Gets or sets a value indicating whether is read only.
/// </summary>
/// <value><c>true</c> if is read only; otherwise, <c>false</c>.</value>
[Parameter]
public bool ReadOnly { get; set; }
/// <inheritdoc />
protected override string GetComponentCssClass()
{
return GetClassList(Orientation == Orientation.Horizontal ? "rz-checkbox-list-horizontal" : "rz-checkbox-list-vertical").ToString();
}
/// <summary>
/// Gets a value indicating whether this instance has value.
/// </summary>
/// <value><c>true</c> if this instance has value; otherwise, <c>false</c>.</value>
public override bool HasValue
{
get
{
return Value != null && Value.Any();
}
}
/// <summary>
/// Gets or sets the orientation.
/// </summary>
/// <value>The orientation.</value>
[Parameter]
public Orientation Orientation { get; set; } = Orientation.Horizontal;
/// <summary>
/// Gets or sets the items that will be concatenated with generated items from Data.
/// </summary>
/// <value>The items.</value>
[Parameter]
public RenderFragment Items { get; set; }
List<RadzenCheckBoxListItem<TValue>> items = new List<RadzenCheckBoxListItem<TValue>>();
/// <summary>
/// Adds the item.
/// </summary>
/// <param name="item">The item.</param>
public void AddItem(RadzenCheckBoxListItem<TValue> item)
{
if (items.IndexOf(item) == -1)
{
items.Add(item);
StateHasChanged();
}
}
/// <summary>
/// Removes the item.
/// </summary>
/// <param name="item">The item.</param>
public void RemoveItem(RadzenCheckBoxListItem<TValue> item)
{
if (items.Contains(item))
{
items.Remove(item);
if (!disposed)
{
try { InvokeAsync(StateHasChanged); } catch { }
}
}
}
/// <summary>
/// Determines whether the specified item is selected.
/// </summary>
/// <param name="item">The item.</param>
/// <returns><c>true</c> if the specified item is selected; otherwise, <c>false</c>.</returns>
protected bool IsSelected(RadzenCheckBoxListItem<TValue> item)
{
return Value != null && Value.Contains(item.Value);
}
/// <summary>
/// Selects the item.
/// </summary>
/// <param name="item">The item.</param>
protected async System.Threading.Tasks.Task SelectItem(RadzenCheckBoxListItem<TValue> item)
{
if (Disabled || item.Disabled || ReadOnly || item.ReadOnly)
return;
List<TValue> selectedValues = new List<TValue>(Value != null ? Value : Enumerable.Empty<TValue>());
if (!selectedValues.Contains(item.Value))
{
selectedValues.Add(item.Value);
}
else
{
selectedValues.Remove(item.Value);
}
Value = selectedValues;
await ValueChanged.InvokeAsync(Value);
if (FieldIdentifier.FieldName != null) { EditContext?.NotifyFieldChanged(FieldIdentifier); }
await Change.InvokeAsync(Value);
StateHasChanged();
}
bool preventKeyPress = true;
async Task OnKeyPress(KeyboardEventArgs args, Task task)
{
var key = args.Code != null ? args.Code : args.Key;
if (key == "Space" || key == "Enter")
{
preventKeyPress = true;
await task;
}
else
{
preventKeyPress = false;
}
}
}
}