Skip to content

Commit 950a339

Browse files
committed
refactor: 重构代码
1 parent 4223011 commit 950a339

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

src/BootstrapBlazor/Components/Textarea/Textarea.razor

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@
77
<BootstrapLabel required="@Required" for="@Id" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" />
88
}
99
<textarea @attributes="AdditionalAttributes" placeholder="@PlaceHolder" id="@Id" class="@ClassName" disabled="@Disabled"
10-
@bind-value="@CurrentValueAsString" @bind-value:event="@EventString"
11-
@onkeyup="OnKeyUp"
12-
@onblur="OnBlur" data-bb-scroll="@AutoScrollString" @ref="FocusElement"></textarea>
10+
@bind-value="CurrentValueAsString" @bind-value:event="@EventString" @onblur="@OnBlur"
11+
data-bb-shift-enter="@_shiftEnterString" data-bb-scroll="@AutoScrollString" @ref="FocusElement"></textarea>

src/BootstrapBlazor/Components/Textarea/Textarea.razor.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,24 @@ public partial class Textarea
3838

3939
/// <summary>
4040
/// 获得/设置 文本框按键回调委托方法 默认为 null
41+
/// <para>返回真时阻止按键</para>
4142
/// </summary>
4243
[Parameter]
43-
public Func<KeyboardEventArgs, Task>? OnKeyUpAsync { get; set; }
44+
public Func<KeyboardEventArgs, Task<bool>>? OnKeyDownAsync { get; set; }
45+
46+
/// <summary>
47+
/// 获得/设置 是否使用 Shift + Enter 代替原回车按键行为 默认为 false
48+
/// </summary>
49+
[Parameter]
50+
public bool UseShiftEnter { get; set; }
4451

4552
/// <summary>
4653
/// 获得 客户端是否自动滚屏标识
4754
/// </summary>
4855
private string? AutoScrollString => IsAutoScroll ? "auto" : null;
4956

57+
private string? _shiftEnterString => UseShiftEnter ? "true" : null;
58+
5059
/// <summary>
5160
/// <inheritdoc/>
5261
/// </summary>
@@ -61,20 +70,4 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
6170
await InvokeVoidAsync("execute", Id, "update");
6271
}
6372
}
64-
65-
private async Task OnKeyUp(KeyboardEventArgs args)
66-
{
67-
if (args.Key == "Enter" && OnEnterAsync != null)
68-
{
69-
await OnEnterAsync(Value);
70-
}
71-
if (args.Key == "Escape" && OnEscAsync != null)
72-
{
73-
await OnEscAsync(Value);
74-
}
75-
if (OnKeyUpAsync != null)
76-
{
77-
await OnKeyUpAsync(args);
78-
}
79-
}
8073
}

src/BootstrapBlazor/Components/Textarea/Textarea.razor.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import { select, handleKeyUp, selectAllByFocus, selectAllByEnter } from "../Input/BootstrapInput.razor.js"
22
import Data from "../../modules/data.js"
3+
import EventHandler from "../../modules/event-handler.js"
34

45
export function init(id) {
6+
var el = document.getElementById(id);
57
const text = {
68
prevMethod: '',
7-
element: document.getElementById(id)
9+
element: el
810
}
911

10-
Data.set(id, text)
12+
Data.set(id, text);
13+
EventHandler.on(el, 'keydown', e => {
14+
if (e.key === "Enter" || e.key === "NumpadEnter") {
15+
const useShiftEnter = el.getAttribute('data-bb-shift-enter') === 'true';
16+
const shiftKey = e.shiftKey;
17+
if (!shiftKey || !useShiftEnter) {
18+
e.preventDefault();
19+
}
20+
}
21+
});
1122
}
1223

1324
export function execute(id, method, position) {
@@ -36,7 +47,12 @@ export function execute(id, method, position) {
3647
}
3748

3849
export function dispose(id) {
39-
Data.remove(id)
50+
const text = Data.get(id);
51+
Data.remove(id);
52+
53+
if (text) {
54+
EventHandler.off(text.element);
55+
}
4056
}
4157

4258
export { select, handleKeyUp, selectAllByFocus, selectAllByEnter }

0 commit comments

Comments
 (0)