Skip to content

Commit 961767b

Browse files
committed
feat: 增加 IsEditable 参数
1 parent acaa8d6 commit 961767b

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

src/BootstrapBlazor/Components/Select/MultiSelect.razor.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ public partial class MultiSelect<TValue>
9696

9797
/// <summary>
9898
/// 获得/设置 编辑模式下输入选项更新后回调方法 默认 null
99-
/// <para>返回 true 时输入选项生效,返回 false 时选项不生效进行舍弃操作,建议在回调方法中自行提示</para>
99+
/// <para>返回 <see cref="SelectedItem"/> 实例时输入选项生效,返回 null 时选项不生效进行舍弃操作,建议在回调方法中自行提示</para>
100100
/// </summary>
101101
/// <remarks>设置 <see cref="IsEditable"/> 后生效</remarks>
102102
[Parameter]
103-
public Func<string, Task<bool>>? OnEditCallback { get; set; }
103+
public Func<string, Task<SelectedItem>>? OnEditCallback { get; set; }
104104

105105
/// <summary>
106106
/// 获得/设置 编辑提交按键 默认 Enter
@@ -287,19 +287,28 @@ public async Task ToggleRow(string val)
287287
/// <param name="val"></param>
288288
/// <returns></returns>
289289
[JSInvokable]
290-
public async Task TriggerEditTag(string val)
290+
public async Task<bool> TriggerEditTag(string val)
291291
{
292-
var ret = true;
292+
SelectedItem? ret = null;
293+
val = val.Trim();
293294
if (OnEditCallback != null)
294295
{
295296
ret = await OnEditCallback.Invoke(val);
296297
}
297-
298-
if (ret)
298+
else if (!string.IsNullOrEmpty(val))
299+
{
300+
ret = GetData().Find(i => i.Text.Equals(val, StringComparison.OrdinalIgnoreCase)) ?? new SelectedItem(val, val);
301+
if (SelectedItems.Find(i => i.Text.Equals(val, StringComparison.OrdinalIgnoreCase)) == null)
302+
{
303+
SelectedItems.Add(ret);
304+
}
305+
}
306+
if (ret != null)
299307
{
300308
// 更新选中值
301309
await SetValue();
302310
}
311+
return ret != null;
303312
}
304313

305314
private string? GetValueString(SelectedItem item) => IsPopover ? item.Value : null;
@@ -433,14 +442,14 @@ private bool CheckCanSelect(SelectedItem item)
433442
return !ret;
434443
}
435444

436-
private IEnumerable<SelectedItem> GetData()
445+
private List<SelectedItem> GetData()
437446
{
438447
var data = Items;
439448
if (ShowSearch && !string.IsNullOrEmpty(SearchText))
440449
{
441450
data = OnSearchTextChanged(SearchText);
442451
}
443-
return data;
452+
return data.ToList();
444453
}
445454

446455
/// <summary>

src/BootstrapBlazor/Components/Select/MultiSelect.razor.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,28 @@ export function init(id, invoke, method) {
2323
popover
2424
}
2525

26-
EventHandler.on(itemsElement, 'change', '.multi-select-input', e => {
27-
invoke.invokeMethodAsync('TriggerEditTag', e.target.value)
28-
});
29-
3026
EventHandler.on(itemsElement, 'click', '.multi-select-input', e => {
3127
const handler = setTimeout(() => {
3228
clearTimeout(handler);
3329
e.target.focus();
3430
}, 50);
3531
});
3632

37-
EventHandler.on(itemsElement, 'keyup', '.multi-select-input', e => {
38-
const key = e.target.getAttribute('data-bb-trigger-key');
39-
if (key === 'space') {
40-
invoke.invokeMethodAsync('TriggerEditTag', e.target.value)
33+
EventHandler.on(itemsElement, 'keyup', '.multi-select-input', async e => {
34+
const triggerSpace = e.target.getAttribute('data-bb-trigger-key') === 'space';
35+
let submit = false;
36+
if (triggerSpace && e.code === 'Space') {
37+
submit = true;
38+
}
39+
else if (e.code === 'Enter' || e.code === 'NumPadEnter') {
40+
submit = true;
41+
}
42+
43+
if (submit) {
44+
const ret = await invoke.invokeMethodAsync('TriggerEditTag', e.target.value);
45+
if (ret) {
46+
e.target.value = '';
47+
}
4148
}
4249
});
4350

src/BootstrapBlazor/Components/Select/MultiSelect.razor.scss

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
--bb-multi-select-button-hover-bg-color: rgba(var(--bs-body-color-rgb), .3);
66
--bb-multi-select-item-margin-x: 3px;
77
--bb-multi-select-item-margin-y: 3px;
8-
--bb-multi-select-item-padding: 2px 6px;
98
--bb-multi-select-item-max-width: 130px;
9+
--bb-multi-select-item-padding: 2px 6px;
1010
width: 100%;
1111
position: relative;
1212

@@ -81,6 +81,10 @@
8181
.multi-select-item-group {
8282
display: inline-flex;
8383
position: relative;
84+
85+
+ .multi-select-input {
86+
padding: 3px 6px;
87+
}
8488
}
8589

8690
.multi-select-close {
@@ -106,7 +110,7 @@
106110
border: none;
107111
outline: none;
108112
appearance: none;
109-
padding: 0;
113+
padding: 3px 12px;
110114
background-color: transparent;
111115
flex: 1;
112116
width: 1%;

0 commit comments

Comments
 (0)