onChange JavaScript for TextField.shinyInput
#52
-
|
Hello hello! @marekrogala @anirbanshaw24 I'm trying to implement some DOM JavaScript to I found some examples online, react specific and plain, but I haven't been able to get it working. Any suggestions or pointers to get me in the right direction? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
|
Hello @kyle-power, thanks for reaching out. A solution I found to be the most straightforward to implement is to use both CSS and server-side processing: The value on the browser side isn't uppercase, so unless you want to access uppercase value from JS, this solution could be sufficient. But even then you could make uppercase from within JS. Would this solution work for you? |
Beta Was this translation helpful? Give feedback.
-
|
Hi @kyle-power. Jakub's solution is awesome. Hope it has solved your issue. Here the action button's text is generated using the input from the text box. |
Beta Was this translation helpful? Give feedback.
-
|
Hey @kyle-power! This thread is somewhat old, but I thought it might be still useful to add something here. In general, with In this case you are asking for a fully browser-side transformation of the value. Unfortunately all solutions I can think of will be either somewhat hacky (the workarounds provided here) or complex. See below for a "proper", but complex solution: library(shiny.fluent)
CustomComponents <- tags$script(HTML("(function() {
const { InputAdapter } = jsmodule['@/shiny.react'];
const { TextField } = jsmodule['@fluentui/react'];
const CustomComponents = jsmodule['CustomComponents'] = {};
CustomComponents.UpperCaseTextField = InputAdapter(TextField, (value, setValue) => ({
value: value.toUpperCase(),
onChange: (e, v) => setValue(v.toUpperCase()),
}));
})();"))
UpperCaseTextField <- function(inputId, ..., value = "") shiny.react::reactElement(
module = "CustomComponents",
name = "UpperCaseTextField",
props = shiny.react::asProps(inputId = inputId, ..., value = value),
deps = shiny.fluent::shinyFluentDependency()
)
shinyApp(
ui = tagList(
CustomComponents,
UpperCaseTextField("text"),
textOutput("text")
),
server = function(input, output) {
output$text <- renderText(input$text)
}
) |
Beta Was this translation helpful? Give feedback.
Hey @kyle-power! This thread is somewhat old, but I thought it might be still useful to add something here.
In general, with
.shinyInput()wrappers you won't be able to use theonChange,valueanddefaultValueprops as defined by the component (sometimes called differently, e.g.checkedanddefaultChecked). These are replaced by the the.shinyInput()wrapper with its owninputIdandvalueprops.In this case you are asking for a fully browser-side transformation of the value. Unfortunately all solutions I can think of will be either somewhat hacky (the workarounds provided here) or complex. See below for a "proper", but complex solution: