-
Notifications
You must be signed in to change notification settings - Fork 4
/
inputs.R
208 lines (191 loc) · 5.19 KB
/
inputs.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#' @keywords internal
args_js <- function(...) {
args <- rlang::list2(...)
if (!is.null(args$class))
args$className <- args$class
if (length(args) == 0) {
return("")
}
paste0(
", ",
paste0(
names(args), ": ", "'", as.character(args), "'", collapse = ", "
)
)
}
#' Define the unique id to use when passing values to shiny
#'
#' @keywords internal
define_key <- function(key) {
if (!is.null(key)) {
key <- paste0("cellInfo.row.", key)
} else {
key <- paste(
"cellInfo.row['.internal_uuid'] ?",
"cellInfo.row['.internal_uuid'] :",
"(Number(cellInfo.id) + 1)"
)
}
return(key)
}
#' Button input for reactable column cell
#'
#' @param id id of the button input
#' @param key alternative unique id for server side processing
#' @param ... parameters of button, only `class` is supported for now
#'
#' @examples
#' reactable::colDef(cell = button_extra("click", class = "table-button"))
#'
#' @return Custom JS button renderer for reactable
#'
#' @export
button_extra <- function(id, key = NULL, ...) {
key <- define_key(key)
reactable::JS(
htmltools::doRenderTags(
htmltools::htmlTemplate(
text_ = "function(cellInfo) {
return React.createElement(ButtonExtras,
{id: '{{id}}', label: cellInfo.value,
uuid: {{key}}, column: cellInfo.column.id {{args}}}, cellInfo.id)
}",
id = id,
key = key,
args = args_js(...)
)
)
)
}
#' Checkbox input for reactable column cell
#'
#' @param id id of the checkbox input
#' @param key alternative unique id for server side processing
#' @param ... parameters of checkbox, only `class` is supported for now
#'
#' @examples
#' reactable::colDef(cell = checkbox_extra("check", class = "table-check"))
#'
#' @return Custom JS checkbox renderer for reactable
#'
#' @export
checkbox_extra <- function(id, key = NULL, ...) {
key <- define_key(key)
reactable::JS(
htmltools::doRenderTags(
htmltools::htmlTemplate(
text_ = "function(cellInfo) {
return React.createElement(checkboxExtras,
{id: '{{id}}', value: cellInfo.value, uuid: {{key}},
column: cellInfo.column.id {{args}}}, cellInfo.id)
}",
id = id,
key = key,
args = args_js(...)
)
)
)
}
#' Date input for reactable column cell
#'
#' @param id id of the date input
#' @param key alternative unique id for server side processing
#' @param ... parameters of date input, only `class` is supported for now
#'
#' @examples
#' reactable::colDef(cell = date_extra("date", class = "table-date"))
#'
#' @return Custom JS date input renderer for reactable
#'
#' @export
date_extra <- function(id, key = NULL, ...) {
key <- define_key(key)
reactable::JS(
htmltools::doRenderTags(
htmltools::htmlTemplate(
text_ = "function(cellInfo) {
return React.createElement(dateExtras,
{id: '{{id}}', value: cellInfo.value, uuid: {{key}},
column: cellInfo.column.id {{args}}}, cellInfo.id)
}",
id = id,
key = key,
args = args_js(...)
)
)
)
}
build_dropdown_extra_choices <- function(choices) {
if (length(choices) == 0) {
choices_js <- ""
} else {
choices_js <- paste0(", choices: ", rjson::toJSON(choices))
}
return(choices_js)
}
#' Select input for reactable column cell
#'
#' @param id id of the select input
#' @param choices vector of choices
#' @param key alternative unique id for server side processing
#' @param ... parameters of date input, only `class` is supported for now
#'
#' @examples
#' reactable::colDef(
#' cell = dropdown_extra("dropdown",
#' choices = letters[1:5],
#' class = "table-dropdown"
#' )
#' )
#'
#' @return Custom JS dropdown renderer for reactable
#'
#' @export
dropdown_extra <- function(id, choices, key = NULL, ...) {
choices_js <- build_dropdown_extra_choices(choices)
key <- define_key(key)
reactable::JS(
htmltools::doRenderTags(
htmltools::htmlTemplate(
text_ = "function(cellInfo) {
return React.createElement(dropdownExtras,
{id: '{{id}}', value: cellInfo.value,
uuid: {{key}}, column: cellInfo.column.id {{args}} {{choices}}}, cellInfo.id)
}",
id = id,
key = key,
choices = choices_js,
args = args_js(...)
)
)
)
}
#' Text input for reactable column cell
#'
#' @param id id of the text input
#' @param ... parameters of text input, only `class` is supported for now
#' @param key alternative unique id for server side processing
#'
#' @examples
#' reactable::colDef(cell = text_extra("text", class = "table-text"))
##'
#' @return Custom JS text input renderer for reactable
#'
#' @export
text_extra <- function(id, key = NULL, ...) {
key <- define_key(key)
reactable::JS(
htmltools::doRenderTags(
htmltools::htmlTemplate(
text_ = "function(cellInfo) {
return React.createElement(textExtras,
{id: '{{id}}', value: cellInfo.value, uuid: {{key}},
column: cellInfo.column.id {{args}}}, cellInfo.id)
}",
id = id,
key = key,
args = args_js(...)
)
)
)
}