diff --git a/NAMESPACE b/NAMESPACE index bdd1b02..bdb25e4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,6 +8,9 @@ export(redo) 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 new file mode 100644 index 0000000..186c428 --- /dev/null +++ b/R/editors.R @@ -0,0 +1,103 @@ +#' 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. +#' @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 = "", + 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) +} + +#' 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) +} + + diff --git a/man/set_column_editor_input.Rd b/man/set_column_editor_input.Rd new file mode 100644 index 0000000..274791d --- /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{The \strong{input} editor allows entering of a single line of plain text} +\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{ +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 +} diff --git a/tests/testthat/test-editor.R b/tests/testthat/test-editor.R new file mode 100644 index 0000000..2021715 --- /dev/null +++ b/tests/testthat/test-editor.R @@ -0,0 +1,62 @@ +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 = "", + 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") +}) + +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") +}) + + 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