Open SfDatePicker when clicked on component's field

Dear Syncfusion team,

thank you for this great component.

I can open a DatePicker once I click on the calendar icon. How can I open a DatePicker by clicking on the component's field?

Since AllowEdit is set to False (field is not editable), users keep clicking on the field and expect DatePicker to show up.example.png


blazor-datepicker-date-selection.png

This is my DatePickerCode:

<SfDatePicker TValue="DateTime" @bind-Value="@DueDate" AllowEdit="false" Min="DateTime.Now">
</SfDatePicker>


Best wishes,
Aleksandar


3 Replies

VJ Vinitha Jeyakumar Syncfusion Team September 20, 2021 09:28 AM UTC

Hi Aleksandar, 
 
 
Greetings from Sycnfusion support, 
 
 
We have validated your query “Open SfDatePicker when clicked on component's field”. 
 
Your requirement to open the DatePicker popup when clicking on the input field can be achieved by calling the ShowPopupAsync method of DatePicker Control in Focus event which triggers when clicking the input field. Please check the below code, 
 
Code snippet: 
<SfDatePicker TValue="DateTime?" Placeholder='Choose a Date' @ref="dateObj" AllowEdit="false"> 
    <DatePickerEvents TValue="DateTime?" Focus="@FocusHandler"></DatePickerEvents> 
</SfDatePicker> 
@code{ 
    SfDatePicker<DateTime?> dateObj; 
    public void FocusHandler() 
    { 
        this.dateObj.ShowPopupAsync(); 
    } 
} 
 
 
Please check the sample and code snippet and let us know if it satisfies your requirement. 
 
Regards, 
Vinitha. 



AL Aleksandar September 20, 2021 09:58 AM UTC

Thank you, this works fine!


Since I have many date pickers it would be great if I could eliminate additional functions and code. This is what I came up with so far Focus="@(() => dateObj.ShowPopupAsync())":


<SfDatePicker TValue="DateTime?" Placeholder='Choose a Date' @ref="dateObj" AllowEdit="false">
    <DatePickerEvents TValue="DateTime?" Focus="@(() => dateObj.ShowPopupAsync())"></DatePickerEvents>
</SfDatePicker>

@code{
    SfDatePicker<DateTime?> dateObj;
}


Is it possible to to eliminate the need for @ref="dateObj" and SfDatePicker dateObj; so I can only have something like this:


<SfDatePicker TValue="DateTime?" Placeholder='Choose a Date' AllowEdit="false">
    <DatePickerEvents TValue="DateTime?" Focus="@(() => this.ShowPopupAsync())"></DatePickerEvents>
</SfDatePicker>


Best wishes,
Aleksandar



VJ Vinitha Jeyakumar Syncfusion Team September 21, 2021 11:19 AM UTC

Hi Aleksandar, 
 
 
Good day to you 
 
 
We have further validated your query “Since I have many date pickers it would be great if I could eliminate additional functions and code 
 
We can’t eliminate the @ref Object from the DatePicker component. Since it is a necessary instance to call the ShowAsync method of the DatePicker control. Please check the below code, 
 
Code snippet: 
<SfDatePicker TValue="DateTime?" Placeholder='Choose a Date' @ref="dateObj" AllowEdit="false"> 
    <DatePickerEvents TValue="DateTime?" Focus="@(() => this.dateObj.ShowPopupAsync())"></DatePickerEvents> 
</SfDatePicker> 
@code{ 
    SfDatePicker<DateTime?> dateObj; 
} 
 
Please let us know if you have any concerns. 
 
Regards, 
Vinitha. 


Loader.
Up arrow icon