Skip to content

Commit ff481d4

Browse files
committed
Implement a DateTimePicker widget
1 parent 7f8ca26 commit ff481d4

4 files changed

Lines changed: 68 additions & 9 deletions

File tree

docs/src/basic_widgets.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Basic Widgets
2-
This Package exports two basic widgets: `Editable` and `StringOnEnter`
2+
This package exports three basic widgets:
3+
4+
- `Editable` - a number input triggering on `Enter`
5+
- `StringOnEnter` - a string input triggering on `Enter`
6+
- `DateTimePicker` - the native date and time picker.
37

48
## Editable
59
```@docs
@@ -9,4 +13,9 @@ Editable
913
## StringOnEnter
1014
```@docs
1115
StringOnEnter
12-
```
16+
```
17+
18+
## DateTimePicker
19+
```@docs
20+
DateTimePicker
21+
```

docs/src/index.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
# PlutoExtras
1+
# PlutoExtras.jl - Overview
22

3-
Documentation for [PlutoExtras](https://github.com/disberd/PlutoExtras.jl).
3+
The [PlutoExtras.jl](https://github.com/disberd/PlutoExtras.jl) package provides several extensions to Pluto and PlutoUI, including additional widgets, support for LaTeX equations, an improved table of contents, and useful macros for binding structs and named tuples to widgets.
44

5-
## Outline
65
```@contents
76
Pages = [
87
"basic_widgets.md",
@@ -11,4 +10,4 @@ Pages = [
1110
"structbond.md",
1211
]
1312
Depth = 1
14-
```
13+
```

src/PlutoExtras.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module PlutoExtras
22
using HypertextLiteral
33
using AbstractPlutoDingetjes.Bonds
44
using AbstractPlutoDingetjes
5+
using Dates
56
import PlutoUI
67

78
# This is similar to `@reexport` but does not exports undefined names and can
@@ -16,13 +17,13 @@ function re_export(m::Module; modname = false)
1617
eval(:(export $(exprts...)))
1718
end
1819

19-
export Editable, StringOnEnter # from basic_widgets.jl
20-
2120
include("helpers.jl")
2221

2322
include("combine_htl/PlutoCombineHTL.jl")
2423

2524
include("basic_widgets.jl")
25+
export Editable, StringOnEnter, DateTimePicker
26+
2627
include("latex_equations.jl")
2728
module ExtendedToc include("extended_toc/extended_toc.jl") end
2829

src/basic_widgets.jl

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,54 @@ Bonds.possible_values(t::StringOnEnter) = Bonds.InfinitePossibilities()
310310

311311
function 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

Comments
 (0)