Skip to content

Commit dc33e53

Browse files
authored
Fix CodeInput Doesn't Work Properly When Pressed Backspace on Empty Component (#621)
1 parent a997c8b commit dc33e53

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

src/CodeBeam.MudBlazor.Extensions/Components/CodeInput/MudCodeInput.razor.cs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,12 @@ private async Task OnInputHandler()
162162
_skipInputEvent = false;
163163
return;
164164
}
165-
await FocusNext();
165+
166+
var current = _elementReferences[_lastFocusedIndex];
167+
var val = current.GetState(x => x.Value)?.ToString();
168+
169+
if (!string.IsNullOrEmpty(val))
170+
await FocusNext();
166171
}
167172

168173
/// <summary>
@@ -173,36 +178,49 @@ private async Task OnInputHandler()
173178
protected async Task HandleKeyDown(KeyboardEventArgs arg)
174179
{
175180
if (Disabled || ReadOnly)
176-
{
177181
return;
178-
}
179182

180-
if (arg.Key == "Backspace" || arg.Key == "ArrowLeft" || arg.Key == "Delete")
183+
if (arg.Key == "Backspace")
181184
{
182-
_skipInputEvent = true;
183-
_skipRefocus = true;
184-
if (arg.Key == "Delete")
185+
var current = _elementReferences[_lastFocusedIndex];
186+
var currentValue = current.GetState(x => x.Value)?.ToString();
187+
188+
if (!string.IsNullOrEmpty(currentValue))
185189
{
186-
await _elementReferences[_lastFocusedIndex].Clear();
190+
_skipInputEvent = true;
191+
await current.Clear();
187192
_skipInputEvent = false;
193+
return;
188194
}
195+
196+
_skipRefocus = true;
197+
189198
if (RuntimeLocation.IsClientSide)
190-
{
191199
await Task.Delay(10);
192-
}
200+
201+
await FocusPrevious();
202+
return;
203+
}
204+
205+
if (arg.Key == "Delete")
206+
{
207+
_skipInputEvent = true;
208+
await _elementReferences[_lastFocusedIndex].Clear();
209+
_skipInputEvent = false;
210+
return;
211+
}
212+
213+
if (arg.Key == "ArrowLeft")
214+
{
193215
await FocusPrevious();
194216
return;
195217
}
196218

197219
if (arg.Key == "ArrowRight")
198220
{
199-
if (RuntimeLocation.IsClientSide)
200-
{
201-
await Task.Delay(10);
202-
}
203221
await FocusNext();
222+
return;
204223
}
205-
206224
}
207225

208226
private int _lastFocusedIndex = 0;

0 commit comments

Comments
 (0)