@@ -310,4 +310,54 @@ Bonds.possible_values(t::StringOnEnter) = Bonds.InfinitePossibilities()
310310
311311function Bonds. validate_value (t:: StringOnEnter , val)
312312 val isa AbstractString
313- end
313+ end
314+
315+
316+
317+ # DateTimePicker #
318+ #=
319+ Create an element to utilize the HTML5 date-time input type.
320+ =#
321+
322+ # # Struct ##
323+
324+ """
325+ DateTimePicker(default::DateTime=now(), min::Union{DateTime,Nothing}=nothing, max::Union{DateTime,Nothing}=nothing, step::Dates.Period=Dates.Minute(1))
326+ Creates a Pluto widget that allows to provide a DateTime as output when used with `@bind`.
327+
328+ Due to the implementation of the HTML5 date-time input type, only date-times with a precision up to the minute are supported.
329+
330+ Options:
331+ - `default::DateTime`: The default value to show when the widget is created. Defaults to the current date-time floored to the minute.
332+ - `min::Union{DateTime,Nothing}`: The minimum date-time that can be selected. Defaults to `nothing` (no minimum)
333+ - `max::Union{DateTime,Nothing}`: The maximum date-time that can be selected. Defaults to `nothing` (no maximum)
334+ - `step::Dates.Period`: The step size, defaults to `Dates.Minute(1)`. This defines the increments in which the date-time can be changed.
335+
336+ When rendered in HTML, the widget will use the native date-time picker of the browser.
337+ """
338+ Base. @kwdef struct DateTimePicker
339+ default:: DateTime = floor (now (), Dates. Minute)
340+ min:: Union{DateTime,Nothing} = nothing
341+ max:: Union{DateTime,Nothing} = nothing
342+ step:: Dates.Period = Dates. Minute (1 )
343+
344+ function DateTimePicker (default, min, max, step)
345+ flrd_default = floor (default, Dates. Minute)
346+ flrd_min = isnothing (min) ? nothing : floor (min, Dates. Minute)
347+ flrd_max = isnothing (max) ? nothing : floor (max, Dates. Minute)
348+ new (flrd_default, flrd_min, flrd_max, step)
349+ end
350+ end
351+
352+ Base. show (io:: IO , mime:: MIME"text/html" , dtp:: DateTimePicker ) = show (io, mime, @htl """
353+ <input $((type= " datetime-local" , value= dtp. default, min= dtp. min, max= dtp. max, step= Dates. seconds (dtp. step))) ></input>
354+ """ )
355+
356+ Base. get (dtp:: DateTimePicker ) = dtp. default
357+ Bonds. initial_value (dtp:: DateTimePicker ) = dtp. default
358+ function Bonds. transform_value (dtp:: DateTimePicker , val)
359+ something (tryparse (DateTime, val, dateformat " YYYY-mm-ddTHH:MM" ), DateTime (1970 ,1 ,1 ))
360+ end
361+ function Bonds. validate_value (dtp:: DateTimePicker , val)
362+ ! isnothing (tryparse (DateTime, val, dateformat " YYYY-mm-ddTHH:MM" ))
363+ end
0 commit comments