0

I'm trying to get my textAreaInput -- inside a renderUI called from a module -- to resize so that it displays the entire area's contents by default. Building off of Auto-resize textAreaInput created through renderUI, a textAreaInput inside a renderUI now resizes automatically as desired, but not when I put the renderUI into a module.

In both cases, using:

jc <- "document.addEventListener('DOMContentLoaded', function(event) {
  var observe;
  if (window.attachEvent) {
    observe = function (element, event, handler) {
      element.attachEvent('on'+event, handler);
    };
  } else {
    observe = function (element, event, handler) {
      element.addEventListener(event, handler, false);
    };
  }
  function init () {
    var text = document.getElementById('text');
    function resize () {
      text.style.height = 'auto';
      text.style.height = text.scrollHeight+'px';
    }
    /* 0-timeout to get the already changed text */
    function delayedResize () {
      window.setTimeout(resize, 0);
    }
    observe(text, 'change',  resize);
    observe(text, 'cut',     delayedResize);
    observe(text, 'paste',   delayedResize);
    observe(text, 'drop',    delayedResize);
    observe(text, 'keydown', delayedResize);

    text.focus();
    text.select();
    resize();
  };
  setTimeout(function(){init();}, 500);
})
"

Works (resizes automatically):

ui <- shiny::fluidPage(
  shiny::tags$script(jc),
  shiny::uiOutput("tai")
)

server <- function(input, output, session) {
  output$tai <- shiny::renderUI({
    shiny::textAreaInput(inputId = "text", label = "a", value = "b")
  })
}

shiny::shinyApp(ui, server)

Does not work:

taiUI <- function(id) {
  ns <- shiny::NS(id)
  shiny::uiOutput(ns("tai"))
}

tai <- function(input, output, session) {
  ns <- session$ns
  output$tai <- shiny::renderUI({ 
    shiny::textAreaInput(inputId = ns("text"), label = "a", value = "b") 
  })
}

ui <- shiny::fluidPage(
  shiny::tags$script(jc),
  taiUI("taimodule")
)

server <- function(input, output, session) {
  shiny::callModule(tai, "taimodule")
}

shiny::shinyApp(ui, server)
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132

1 Answers1

0

That's because the ID is ns("text"), not "text". So you have to replace this line:

var text = document.getElementById('text');

with

var text = document.getElementById('taimodule-text');
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225