From fa283b49cdeff177fee339df7646ac5ae5b1f917 Mon Sep 17 00:00:00 2001 From: friessn Date: Mon, 23 Sep 2024 23:54:47 +0200 Subject: [PATCH 1/6] add editors --- NAMESPACE | 1 + R/editors.R | 33 +++++++++++++++++++++++++++++ examples/editors/editor_input.R | 4 ++++ man/set_column_editor_input.Rd | 37 +++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 R/editors.R create mode 100644 examples/editors/editor_input.R create mode 100644 man/set_column_editor_input.Rd diff --git a/NAMESPACE b/NAMESPACE index 98b88bf..85a992c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,6 +7,7 @@ export(redo) export(renderTabulator) export(set_calculation) export(set_column_defaults) +export(set_column_editor_input) export(set_editor) export(set_formatter_color) export(set_formatter_datetime) diff --git a/R/editors.R b/R/editors.R new file mode 100644 index 0000000..8920e17 --- /dev/null +++ b/R/editors.R @@ -0,0 +1,33 @@ +#' The input editor allows entering of a single line of plain text +#' +#' @param widget A [tabulator()] HTML widget. +#' @param column The name of the column the formatter is applied to. +#' @param search Use search type input element with clear button +#' @param mask Apply a mask to the input to allow characters to be entered only in a certain order +#' @param select_contents When the editor is loaded select its text content +#' @param elementAttributes Set attributes directly on the input element +#' +#' @return The updated [tabulator()] HTML widget +#' @export +#' +#' @examples examples/editors/editor_input +set_column_editor_input <- function(widget, + columns, + search = TRUE, + mask = "AAA-999", + select_contents = TRUE, + elementAttributes = list( + maxlength = 10 + )){ + editor = "input" + editorParams = list( + search = search, + mask = mask, + selectContents = select_contents, + elementAttributes = elementAttributes + ) + + set_column_editor(widget, columns, editor, editorParams) +} + + diff --git a/examples/editors/editor_input.R b/examples/editors/editor_input.R new file mode 100644 index 0000000..d366046 --- /dev/null +++ b/examples/editors/editor_input.R @@ -0,0 +1,4 @@ +df <- data.frame(values = c(1,2,3), + names = c("a","b","c")) +tabulator(df) |> + set_column_editor_input("names") diff --git a/man/set_column_editor_input.Rd b/man/set_column_editor_input.Rd new file mode 100644 index 0000000..a17bd0c --- /dev/null +++ b/man/set_column_editor_input.Rd @@ -0,0 +1,37 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/editors.R +\name{set_column_editor_input} +\alias{set_column_editor_input} +\title{Title} +\usage{ +set_column_editor_input( + widget, + columns, + search = TRUE, + mask = "", + select_contents = TRUE, + elementAttributes = list(maxlength = 10) +) +} +\arguments{ +\item{widget}{A \code{\link[=tabulator]{tabulator()}} HTML widget.} + +\item{search}{Use search type input element with clear button} + +\item{mask}{Apply a mask to the input to allow characters to be entered only in a certain order} + +\item{select_contents}{When the editor is loaded select its text content} + +\item{elementAttributes}{Set attributes directly on the input element} + +\item{column}{The name of the column the formatter is applied to.} +} +\value{ +The updated \code{\link[=tabulator]{tabulator()}} HTML widget +} +\description{ +Title +} +\examples{ +examples/editors/editor_input +} From 3e33c23ff7bbb14ed82786216784e3b1d8940d86 Mon Sep 17 00:00:00 2001 From: friessn Date: Mon, 23 Sep 2024 23:55:03 +0200 Subject: [PATCH 2/6] some tests --- tests/testthat/test-editor.R | 29 +++++++++++++++++++ ...-formatter-html.R => test-set-formatter.R} | 0 2 files changed, 29 insertions(+) create mode 100644 tests/testthat/test-editor.R rename tests/testthat/{test-set-formatter-html.R => test-set-formatter.R} (100%) diff --git a/tests/testthat/test-editor.R b/tests/testthat/test-editor.R new file mode 100644 index 0000000..ca16c20 --- /dev/null +++ b/tests/testthat/test-editor.R @@ -0,0 +1,29 @@ +test_that("set editor input", { + # Prepare + col = "input_var" + df <- data.frame(input_var = c("a","b","c")) + + + # Act + t <- tabulator(df) |> + set_column_editor_input(col) + + # Assert + expected_column_def <- list( + title = "input_var", + field = "input_var", + hozAlign = "left", + editor = "input", + editorParams = list( + search = TRUE, + mask = "AAA-999", + selectContents = TRUE, + elementAttributes = list( + maxlength = 10 + ) + ) + ) + expect_mapequal(t$x$options$columns[[1]], expected_column_def) + expect_s3_class(t, "rtabulator") + expect_s3_class(t, "htmlwidget") +}) diff --git a/tests/testthat/test-set-formatter-html.R b/tests/testthat/test-set-formatter.R similarity index 100% rename from tests/testthat/test-set-formatter-html.R rename to tests/testthat/test-set-formatter.R From 34df87456b23631fb76efb2b4b00027816438d3d Mon Sep 17 00:00:00 2001 From: friessn Date: Mon, 23 Sep 2024 23:55:25 +0200 Subject: [PATCH 3/6] minor update --- man/set_formatter_color.Rd | 2 +- man/set_formatter_datetime.Rd | 2 +- man/set_formatter_html.Rd | 2 +- man/set_formatter_image.Rd | 2 +- man/set_formatter_link.Rd | 2 +- man/set_formatter_money.Rd | 2 +- man/set_formatter_plaintext.Rd | 2 +- man/set_formatter_progress.Rd | 2 +- man/set_formatter_star.Rd | 2 +- man/set_formatter_textarea.Rd | 2 +- man/set_formatter_tick_cross.Rd | 2 +- man/set_formatter_toggle_switch.Rd | 2 +- man/set_formatter_traffic_light.Rd | 2 +- 13 files changed, 13 insertions(+), 13 deletions(-) diff --git a/man/set_formatter_color.Rd b/man/set_formatter_color.Rd index 4377b50..d366c9e 100644 --- a/man/set_formatter_color.Rd +++ b/man/set_formatter_color.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/columns.R +% Please edit documentation in R/formatters.R \name{set_formatter_color} \alias{set_formatter_color} \title{Set color formatter} diff --git a/man/set_formatter_datetime.Rd b/man/set_formatter_datetime.Rd index 848bfa7..eecc477 100644 --- a/man/set_formatter_datetime.Rd +++ b/man/set_formatter_datetime.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/columns.R +% Please edit documentation in R/formatters.R \name{set_formatter_datetime} \alias{set_formatter_datetime} \title{Set datetime formatter} diff --git a/man/set_formatter_html.Rd b/man/set_formatter_html.Rd index cdec68e..5ee5f3a 100644 --- a/man/set_formatter_html.Rd +++ b/man/set_formatter_html.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/columns.R +% Please edit documentation in R/formatters.R \name{set_formatter_html} \alias{set_formatter_html} \title{Set HTML formatter} diff --git a/man/set_formatter_image.Rd b/man/set_formatter_image.Rd index 2d4fa79..2fd14a9 100644 --- a/man/set_formatter_image.Rd +++ b/man/set_formatter_image.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/columns.R +% Please edit documentation in R/formatters.R \name{set_formatter_image} \alias{set_formatter_image} \title{Set image formatter} diff --git a/man/set_formatter_link.Rd b/man/set_formatter_link.Rd index 1af240a..03fe5e2 100644 --- a/man/set_formatter_link.Rd +++ b/man/set_formatter_link.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/columns.R +% Please edit documentation in R/formatters.R \name{set_formatter_link} \alias{set_formatter_link} \title{Set link formatter} diff --git a/man/set_formatter_money.Rd b/man/set_formatter_money.Rd index 8594ca1..1156cca 100644 --- a/man/set_formatter_money.Rd +++ b/man/set_formatter_money.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/columns.R +% Please edit documentation in R/formatters.R \name{set_formatter_money} \alias{set_formatter_money} \title{Set money formatter} diff --git a/man/set_formatter_plaintext.Rd b/man/set_formatter_plaintext.Rd index 8c9b53d..e6432b2 100644 --- a/man/set_formatter_plaintext.Rd +++ b/man/set_formatter_plaintext.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/columns.R +% Please edit documentation in R/formatters.R \name{set_formatter_plaintext} \alias{set_formatter_plaintext} \title{Set plain text formatter} diff --git a/man/set_formatter_progress.Rd b/man/set_formatter_progress.Rd index 3f1afc8..5e2c5f2 100644 --- a/man/set_formatter_progress.Rd +++ b/man/set_formatter_progress.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/columns.R +% Please edit documentation in R/formatters.R \name{set_formatter_progress} \alias{set_formatter_progress} \title{Set progress formatter} diff --git a/man/set_formatter_star.Rd b/man/set_formatter_star.Rd index 0d44886..1551dcf 100644 --- a/man/set_formatter_star.Rd +++ b/man/set_formatter_star.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/columns.R +% Please edit documentation in R/formatters.R \name{set_formatter_star} \alias{set_formatter_star} \title{Set star rating formatter} diff --git a/man/set_formatter_textarea.Rd b/man/set_formatter_textarea.Rd index 075103b..860a96c 100644 --- a/man/set_formatter_textarea.Rd +++ b/man/set_formatter_textarea.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/columns.R +% Please edit documentation in R/formatters.R \name{set_formatter_textarea} \alias{set_formatter_textarea} \title{Set text area formatter} diff --git a/man/set_formatter_tick_cross.Rd b/man/set_formatter_tick_cross.Rd index 2c39479..d0d3d15 100644 --- a/man/set_formatter_tick_cross.Rd +++ b/man/set_formatter_tick_cross.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/columns.R +% Please edit documentation in R/formatters.R \name{set_formatter_tick_cross} \alias{set_formatter_tick_cross} \title{Set tick cross formatter} diff --git a/man/set_formatter_toggle_switch.Rd b/man/set_formatter_toggle_switch.Rd index c8350a0..db21c2d 100644 --- a/man/set_formatter_toggle_switch.Rd +++ b/man/set_formatter_toggle_switch.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/columns.R +% Please edit documentation in R/formatters.R \name{set_formatter_toggle_switch} \alias{set_formatter_toggle_switch} \title{Set toggle switch formatter} diff --git a/man/set_formatter_traffic_light.Rd b/man/set_formatter_traffic_light.Rd index f2592c4..9cb6b85 100644 --- a/man/set_formatter_traffic_light.Rd +++ b/man/set_formatter_traffic_light.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/columns.R +% Please edit documentation in R/formatters.R \name{set_formatter_traffic_light} \alias{set_formatter_traffic_light} \title{Set traffic light formatter} From f4a6f667796b7d018499fd32420d9e7365aa3a03 Mon Sep 17 00:00:00 2001 From: friessn Date: Tue, 24 Sep 2024 00:15:38 +0200 Subject: [PATCH 4/6] add editor functions --- NAMESPACE | 2 ++ R/editors.R | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 85a992c..4ac543e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,6 +8,8 @@ export(renderTabulator) export(set_calculation) export(set_column_defaults) export(set_column_editor_input) +export(set_column_editor_numeric) +export(set_column_editor_text_area) export(set_editor) export(set_formatter_color) export(set_formatter_datetime) diff --git a/R/editors.R b/R/editors.R index 8920e17..186c428 100644 --- a/R/editors.R +++ b/R/editors.R @@ -1,4 +1,4 @@ -#' The input editor allows entering of a single line of plain text +#' The \strong{input} editor allows entering of a single line of plain text #' #' @param widget A [tabulator()] HTML widget. #' @param column The name of the column the formatter is applied to. @@ -14,7 +14,7 @@ set_column_editor_input <- function(widget, columns, search = TRUE, - mask = "AAA-999", + mask = "", select_contents = TRUE, elementAttributes = list( maxlength = 10 @@ -30,4 +30,74 @@ set_column_editor_input <- function(widget, set_column_editor(widget, columns, editor, editorParams) } +#' The \strong{textarea} editor allows entering of multiple lines of plain text +#' @inherit set_column_editor_input params return +#' @param elementAttributes Set attributes directly on the textarea element +#' @param mask Apply a mask to the input to allow characters to be entered only in a certain order +#' @param select_contents When the editor is loaded select its text content +#' @param vertical_navigation Determine how use of the up/down arrow keys will affect the editor, +#' this can take three different types of value: *hybrid*, *editor* and *table* +#' @param shift_enter_submit Submit the cell value when the shift and enter keys are pressed +#' +#' @export +#' +set_column_editor_text_area <- function(widget, + columns, + elementAttributes = list( + maxlength = 10 + ), + mask = "", + select_contents = TRUE, + vertical_navigation = "editor", + shift_enter_submit = TRUE +){ + editor = "textarea" + editorParams = list( + elementAttributes = elementAttributes, + mask = mask, + selectContents = select_contents, + verticalNavigation = vertical_navigation, + shiftEnterSubmit = shift_enter_submit + ) + + set_column_editor(widget, columns, editor, editorParams) +} + +#' The \strong{numeric} editor allows for numeric entry with a number type input element with increment and decrement buttons. +#' @inherit set_column_editor_input params return +#' @param min the maximum allowed value +#' @param max the minimum allowed value +#' @param step the step size when incrementing/decrementing the value (default 1) +#' @param elementAttributes Set attributes directly on the element +#' @param mask Apply a mask to the input to allow characters to be entered only in a certain order +#' @param select_contents When the editor is loaded select its text content +#' @param vertical_navigation determine how use of the up/down arrow keys will affect the editor, +#' this can take two different types of value: : *editor* and *table* +#' @export +#' +set_column_editor_numeric <- function(widget, + columns, + min = 0, + max = 100, + step = 1, + elementAttributes = list( + maxlength = 10 + ), + mask = "", + select_contents = TRUE, + vertical_navigation = "table" +){ + editor = "number" + editorParams = list( + min = min, + max = max, + elementAttributes = elementAttributes, + mask = mask, + selectContents = select_contents, + verticalNavigation = vertical_navigation + ) + + set_column_editor(widget, columns, editor, editorParams) +} + From c3c7ae997832089102e64c2717f383c9e65b7a5f Mon Sep 17 00:00:00 2001 From: friessn Date: Tue, 24 Sep 2024 00:15:49 +0200 Subject: [PATCH 5/6] add documentation --- man/set_column_editor_input.Rd | 4 +-- man/set_column_editor_numeric.Rd | 42 ++++++++++++++++++++++++++++++ man/set_column_editor_text_area.Rd | 36 +++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 man/set_column_editor_numeric.Rd create mode 100644 man/set_column_editor_text_area.Rd diff --git a/man/set_column_editor_input.Rd b/man/set_column_editor_input.Rd index a17bd0c..274791d 100644 --- a/man/set_column_editor_input.Rd +++ b/man/set_column_editor_input.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/editors.R \name{set_column_editor_input} \alias{set_column_editor_input} -\title{Title} +\title{The \strong{input} editor allows entering of a single line of plain text} \usage{ set_column_editor_input( widget, @@ -30,7 +30,7 @@ set_column_editor_input( The updated \code{\link[=tabulator]{tabulator()}} HTML widget } \description{ -Title +The \strong{input} editor allows entering of a single line of plain text } \examples{ examples/editors/editor_input diff --git a/man/set_column_editor_numeric.Rd b/man/set_column_editor_numeric.Rd new file mode 100644 index 0000000..aec73f9 --- /dev/null +++ b/man/set_column_editor_numeric.Rd @@ -0,0 +1,42 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/editors.R +\name{set_column_editor_numeric} +\alias{set_column_editor_numeric} +\title{The \strong{numeric} editor allows for numeric entry with a number type input element with increment and decrement buttons.} +\usage{ +set_column_editor_numeric( + widget, + columns, + min = 0, + max = 100, + step = 1, + elementAttributes = list(maxlength = 10), + mask = "", + select_contents = TRUE, + vertical_navigation = "table" +) +} +\arguments{ +\item{widget}{A \code{\link[=tabulator]{tabulator()}} HTML widget.} + +\item{min}{the maximum allowed value} + +\item{max}{the minimum allowed value} + +\item{step}{the step size when incrementing/decrementing the value (default 1)} + +\item{elementAttributes}{Set attributes directly on the element} + +\item{mask}{Apply a mask to the input to allow characters to be entered only in a certain order} + +\item{select_contents}{When the editor is loaded select its text content} + +\item{vertical_navigation}{determine how use of the up/down arrow keys will affect the editor, +this can take two different types of value: : \emph{editor} and \emph{table}} +} +\value{ +The updated \code{\link[=tabulator]{tabulator()}} HTML widget +} +\description{ +The \strong{numeric} editor allows for numeric entry with a number type input element with increment and decrement buttons. +} diff --git a/man/set_column_editor_text_area.Rd b/man/set_column_editor_text_area.Rd new file mode 100644 index 0000000..6ad9617 --- /dev/null +++ b/man/set_column_editor_text_area.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/editors.R +\name{set_column_editor_text_area} +\alias{set_column_editor_text_area} +\title{The \strong{textarea} editor allows entering of multiple lines of plain text} +\usage{ +set_column_editor_text_area( + widget, + columns, + elementAttributes = list(maxlength = 10), + mask = "AAA-999", + select_contents = TRUE, + vertical_navigation = "editor", + shift_enter_submit = TRUE +) +} +\arguments{ +\item{widget}{A \code{\link[=tabulator]{tabulator()}} HTML widget.} + +\item{elementAttributes}{Set attributes directly on the textarea element} + +\item{mask}{Apply a mask to the input to allow characters to be entered only in a certain order} + +\item{select_contents}{When the editor is loaded select its text content} + +\item{vertical_navigation}{Determine how use of the up/down arrow keys will affect the editor, +this can take three different types of value: \emph{hybrid}, \emph{editor} and \emph{table}} + +\item{shift_enter_submit}{Submit the cell value when the shift and enter keys are pressed} +} +\value{ +The updated \code{\link[=tabulator]{tabulator()}} HTML widget +} +\description{ +The \strong{textarea} editor allows entering of multiple lines of plain text +} From 9bb6e84dd68502406646bbd324b147b5e71f3da8 Mon Sep 17 00:00:00 2001 From: friessn Date: Tue, 24 Sep 2024 00:15:56 +0200 Subject: [PATCH 6/6] add tests --- tests/testthat/test-editor.R | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-editor.R b/tests/testthat/test-editor.R index ca16c20..2021715 100644 --- a/tests/testthat/test-editor.R +++ b/tests/testthat/test-editor.R @@ -16,7 +16,7 @@ test_that("set editor input", { editor = "input", editorParams = list( search = TRUE, - mask = "AAA-999", + mask = "", selectContents = TRUE, elementAttributes = list( maxlength = 10 @@ -27,3 +27,36 @@ test_that("set editor input", { expect_s3_class(t, "rtabulator") expect_s3_class(t, "htmlwidget") }) + +test_that("set editor area input", { + # Prepare + col = "input_var" + df <- data.frame(input_var = c("a","b","c")) + + + # Act + t <- tabulator(df) |> + set_column_editor_text_area(col) + + # Assert + expected_column_def <- list( + title = "input_var", + field = "input_var", + hozAlign = "left", + editor = "textarea", + editorParams = list( + elementAttributes = list( + maxlength = 10 + ), + mask = "", + selectContents = TRUE, + verticalNavigation = "editor", + shiftEnterSubmit = TRUE + ) + ) + expect_mapequal(t$x$options$columns[[1]], expected_column_def) + expect_s3_class(t, "rtabulator") + expect_s3_class(t, "htmlwidget") +}) + +