-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
317 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
|
||
#' @title Time input | ||
#' | ||
#' @description | ||
#' This widget allow to select hour and minute using the default browser time input. | ||
#' See [developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/time) for more. | ||
#' | ||
#' | ||
#' @param inputId The input slot that will be used to access the value. | ||
#' @param label Display label for the control, or NULL for no label. | ||
#' @param value Initial value, foramtted as `"HH:MM"` or `"HH:MM:SS"`. | ||
#' @param min,max Minimal and maximal value, use same format as in `value`. | ||
#' @param step It takes an integer value that equates to the number of seconds you want to increment by; | ||
#' the default value is 60 seconds, or one minute. If you specify a value of less than 60 seconds (1 minute), | ||
#' the time input will show a seconds input area alongside the hours and minutes. | ||
#' This property has some strange effects across browsers, so is not completely reliable. | ||
#' @param width The width of the input, e.g. `400px`, or `100%`. | ||
#' | ||
#' @return A time input control that can be added to a UI definition. | ||
#' @export | ||
#' | ||
#' @name time-input | ||
#' | ||
#' @example examples/time-input.R | ||
timeInput <- function(inputId, | ||
label, | ||
value = NULL, | ||
min = NULL, | ||
max = NULL, | ||
step = NULL, | ||
width = NULL) { | ||
tags$div( | ||
class = "form-group shiny-input-container", | ||
label_input(inputId, label), | ||
style = css(width = validateCssUnit(width)), | ||
tags$input( | ||
id = inputId, | ||
type = "time", | ||
class = "form-control sw-time-input", | ||
value = value, | ||
min = min, | ||
max = max, | ||
step = step | ||
), | ||
html_dependency_input_time() | ||
) | ||
} | ||
|
||
#' @param session Default Shiny session. | ||
#' @export | ||
#' | ||
#' @rdname time-input | ||
updateTimeInput <- function(session = getDefaultReactiveDomain(), | ||
inputId, | ||
label = NULL, | ||
value = NULL, | ||
min = NULL, | ||
max = NULL, | ||
step = NULL) { | ||
message <- dropNulls(list( | ||
label = label, | ||
value = value, | ||
min = min, | ||
max = max, | ||
step = step | ||
)) | ||
session$sendInputMessage(inputId, message) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
library(shiny) | ||
library(shinyWidgets) | ||
|
||
ui <- fluidPage( | ||
tags$h2("Time Input"), | ||
fluidRow( | ||
column( | ||
width = 6, | ||
timeInput( | ||
inputId = "time1", | ||
label = "Time:" | ||
), | ||
verbatimTextOutput("res1"), | ||
timeInput( | ||
inputId = "time2", | ||
label = "Time (default value):", | ||
value = "09:30" | ||
), | ||
verbatimTextOutput("res2"), | ||
timeInput( | ||
inputId = "time3", | ||
label = "Time (with seconds):", | ||
step = 1 | ||
), | ||
verbatimTextOutput("res3") | ||
), | ||
column( | ||
width = 6, | ||
timeInput(inputId = "time4", label = "Time:"), | ||
verbatimTextOutput("res4"), | ||
numericInput("up_h", "Update hour;", value = 0), | ||
numericInput("up_m", "Update minute;", value = 0) | ||
) | ||
) | ||
) | ||
|
||
server <- function(input, output, session) { | ||
|
||
output$res1 <- renderPrint(input$time1) | ||
output$res2 <- renderPrint(input$time2) | ||
output$res3 <- renderPrint(input$time3) | ||
output$res4 <- renderPrint(input$time4) | ||
|
||
observe({ | ||
updateTimeInput( | ||
inputId = "time4", | ||
value = paste( | ||
# Hour and minute need to be a field of minimum width 2, | ||
# with zero-padding on the left | ||
sprintf("%02d", input$up_h), | ||
sprintf("%02d", input$up_m), | ||
sep = ":" | ||
) | ||
) | ||
}) | ||
} | ||
|
||
if (interactive()) | ||
shinyApp(ui, server) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import $ from "jquery"; | ||
import "shiny"; | ||
import { updateLabel } from "../modules/utils"; | ||
|
||
var timeInputBinding = new Shiny.InputBinding(); | ||
$.extend(timeInputBinding, { | ||
find: function find(scope) { | ||
return $(scope).find(".sw-time-input"); | ||
}, | ||
getValue: function getValue(el) { | ||
return el.value; | ||
}, | ||
setValue: function setValue(el, value) { | ||
el.value = value; | ||
}, | ||
subscribe: function subscribe(el, callback) { | ||
$(el).on("change.timeInputBinding", function(event) { | ||
callback(); | ||
}); | ||
}, | ||
unsubscribe: function unsubscribe(el) { | ||
$(el).off(".timeInputBinding"); | ||
}, | ||
receiveMessage: function receiveMessage(el, data) { | ||
if (data.hasOwnProperty("label")) { | ||
var label = $("#" + el.id + "-label"); | ||
updateLabel(data.label, label); | ||
} | ||
if (data.hasOwnProperty("value")) this.setValue(el, data.value); | ||
if (data.hasOwnProperty("min")) el.min = data.min; | ||
if (data.hasOwnProperty("max")) el.max = data.max; | ||
if (data.hasOwnProperty("step")) el.step = data.step; | ||
$(el).trigger("change"); | ||
}, | ||
getState: function getState(el) { | ||
return { | ||
label: this._getLabelNode(el).text(), | ||
value: el.value, | ||
placeholder: el.placeholder | ||
}; | ||
}, | ||
getRatePolicy: function getRatePolicy() { | ||
return { | ||
policy: "debounce", | ||
delay: 250 | ||
}; | ||
}, | ||
_getLabelNode: function _getLabelNode(el) { | ||
return $(el) | ||
.parent() | ||
.find('label[for="' + Shiny.$escape(el.id) + '"]'); | ||
} | ||
}); | ||
Shiny.inputBindings.register(timeInputBinding, "shinyWidgets.timeInput"); |