asp.net-coreblazor

Blazor InputDate Control Display Problem when the Bound Date is Empty


Here is the markup:

<InputDate @bind-Value="@EndTime" Type="InputDateType.DateTimeLocal" bind-Value:format="@DateTimeFormat" style="width:250px" />

The DateTimeFormat = "dd/MM/yyyy HH:mm".

When the control is displayed on the page while EndTime is null, it displays "mm/dd/yyyy --:-- --" as a placeholder. I want the control to be empty when the bound value is null. How can I achieve this? There isn't a placeholder property on the control.

enter image description here

Another bugaboo is that the "AM/PM" is used even though the format calls for a 24 hour time.


Solution

  • It's default behaviour of <input type="date"/> and you will need to override the built-in css of input which is hard to achieve.
    You could try a workaround to make it transparent when value is null.

    <style>
        .null-date {
            width: 250px;
            color: transparent; 
        }
        .non-null-date {
            width: 250px;
            color: black; 
        }   
    </style>
    
    
    <InputDate id="input3" @bind-Value="@EndTime" Type="InputDateType.DateTimeLocal" class="@((EndTime!=null)||(Focus) ?"non-null-date":"null-date")" @onfocus="()=>{Focus=true;}" @onfocusout="()=>{Focus=false;}" />
    
    @code{
        private DateTime? EndTime { get; set; } = null;
    
        private bool Focus = false;
    }