Completed
Last Updated: 15 Aug 2024 10:17 by ADMIN
Release 4.0.1 (27/01/2023)
Paul
Created on: 09 Dec 2022 07:04
Category: DateInput
Type: Bug Report
1
Deleting a date segment clears the entire date and automatically focuses on the last date segment
Deleting a date segment clears the entire date and automatically focuses on the last date segment.
1 comment
ADMIN
Dimo
Posted on: 09 Dec 2022 11:19

Here is an example that shows how to select the day portion of the DateInput after the user hits Backspace. The code can be adjusted to handle other key presses or other textbox values.

@inject IJSRuntime js

<span @onkeydown="@OnKeyDown">
    <TelerikDatePicker @bind-Value="@DateValue"
                       DebounceDelay="1500"
                       Format="dd-MM-yyyy"
                       Width="300px"
                       Id="d1">
        <DatePickerFormatPlaceholder Year="jjjj" Month="mm" Day="dd"></DatePickerFormatPlaceholder>
    </TelerikDatePicker>
</span>

<!-- move this script to a separate file in a real app -->
<script suppress-error="BL9992">//

    function selectDaySegment(inputId) {
        setTimeout(function() {
            var focusedElement = document.activeElement;
            var dateInput = document.querySelector("#" + inputId);

            if (focusedElement == dateInput && dateInput.value == "dd-mm-jjjj") {
                dateInput.setSelectionRange(0, 2);
            }
        });
    }

//</script>

@code {
    DateTime? DateValue { get; set; } = DateTime.Now;

    async Task OnKeyDown(KeyboardEventArgs args)
    {
        if (args.Key == "Backspace")
        {
            await js.InvokeVoidAsync("selectDaySegment", "d1");
        }
    }
}