Skip to content

Commit f79351f

Browse files
committed
update
1 parent f61af60 commit f79351f

File tree

28 files changed

+1202
-844
lines changed

28 files changed

+1202
-844
lines changed

R/dependencies.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#' @return An [htmltools::htmlDependency] for SurveyJS v2.2.6
77
#' @importFrom htmltools htmlDependency
88
#' @keywords internal
9-
# @noRd
9+
#' @noRd
1010
dep_react <- function() {
1111
htmltools::htmlDependency(
1212
name = "react",
@@ -22,7 +22,7 @@ dep_react <- function() {
2222
#' @return An [htmltools::htmlDependency] for SurveyJS v2.2.6
2323
#' @importFrom htmltools htmlDependency
2424
#' @keywords internal
25-
# @noRd
25+
#' @noRd
2626
dep_reactdom <- function() {
2727
htmltools::htmlDependency(
2828
name = "react-dom",
@@ -38,7 +38,7 @@ dep_reactdom <- function() {
3838
#' @return An [htmltools::htmlDependency] for SurveyJS v2.2.6
3939
#' @importFrom htmltools htmlDependency
4040
#' @keywords internal
41-
# @noRd
41+
#' @noRd
4242
dep_surveyjs_core <- function() {
4343
htmltools::htmlDependency(
4444
name = "surveyjs", version = "2.2.6",

R/surveyjs.R

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,57 @@
1212
#' @param schema List or JSON string; must follow
1313
#' [SurveyJS JSON Schema](https://surveyjs.io/form-library/documentation/json-schema).
1414
#' @param data Initial values.
15-
#' @param readOnly Render in read-only mode.
15+
#' @param read_only Render in read-only mode.
1616
#' @param live Live update responses?
1717
#' @param theme Theme name.
1818
#' @param theme_vars Named list of CSS variables (e.g. `--sjs-primary-backcolor`).
1919
#' @param locale Language code (e.g. `"en"`, `"de"`)
20-
#' @param width,height Optional CSS size or number.
21-
#' @param elementId Optional element id for the container.
2220
#' @param pre_render_hook JavaScript code (as a string) to run before rendering the survey.
2321
#' Supply only the body of a JavaScript function — do not wrap it in `function(...) {}`.
2422
#' @param post_render_hook JavaScript code (as a string) to run after the survey is rendered.
2523
#' Also supply only the body of a JavaScript function — not a full `function(...) {}` wrapper.
26-
#' @param width,height Optional size specs.
27-
#' @param elementId Optional element id for the container.
24+
#' @param complete_hook JavaScript function body to run on survey completion
25+
#' @param width,height Optional CSS size or number.
26+
#' @param element_id Optional element ID. Leave NULL in Shiny (it's auto-assigned).
27+
#' @details
28+
#' The SurveyJS widget is responsive by default. The `resize()` method is a placeholder
29+
#' and does not implement manual resizing logic, as SurveyJS adapts to container size
30+
#' via CSS flexbox rules.
2831
#' @export
29-
surveyjs <- function(schema, data = NULL, readOnly = FALSE, live = FALSE,
30-
theme = "DefaultLight", theme_vars = NULL, locale = NULL,
31-
pre_render_hook = NULL, post_render_hook = NULL,
32-
width = NULL, height = NULL, elementId = NULL) {
32+
surveyjs <- function(schema, data = NULL, read_only = FALSE, live = FALSE,
33+
theme = "DefaultLight", theme_vars = NULL, locale = NULL,
34+
pre_render_hook = NULL, post_render_hook = NULL,
35+
complete_hook = NULL, width = NULL, height = NULL,
36+
element_id = NULL) {
3337

34-
schema_json <- if (is.character(schema)) schema
35-
else jsonlite::toJSON(schema, auto_unbox = TRUE)
38+
if (shiny::isRunning() && !is.null(element_id)) {
39+
warning("In Shiny, `element_id` is ignored. The widget ID is automatically set from the output ID in `surveyjsOutput(id)`.")
40+
}
3641

42+
schema_json <- if (is.character(schema)) schema
43+
else jsonlite::toJSON(schema, auto_unbox = TRUE)
3744

3845
x <- list(
3946
schema = schema_json,
4047
data = data,
41-
readOnly = readOnly,
48+
read_only = read_only,
4249
live = live,
4350
theme = theme,
4451
theme_vars = theme_vars,
4552
locale = locale,
4653
pre_render_hook = pre_render_hook,
47-
post_render_hook = post_render_hook
54+
post_render_hook = post_render_hook,
55+
complete_hook = complete_hook,
56+
element_id = element_id
4857
)
4958

5059
htmlwidgets::createWidget(
5160
name = "surveyjs",
5261
x = x,
5362
width = width,
5463
height = height,
64+
elementId = element_id,
5565
package = "rsurveyjs",
56-
elementId = elementId,
5766
dependencies = list(
5867
dep_react(),
5968
dep_reactdom(),

R/update.R

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#' @param session Shiny session object.
88
#' @param id Output ID used in `surveyjsOutput()`.
99
#' @param data Optional named list of answers to set.
10-
#' @param readOnly Optional logical; toggle read-only mode.
10+
#' @param read_only Optional logical; toggle read-only mode.
1111
#' @param schema Optional new schema (list or JSON string).
1212
#' @param theme Optional theme name (e.g. `"modern"`).
1313
#' @param locale Optional locale code (e.g. `"en"`, `"de"`).
@@ -20,19 +20,22 @@
2020
#'
2121
#' @export
2222
updateSurveyjs <- function(session, id,
23-
data = NULL,
24-
readOnly = NULL,
25-
schema = NULL,
26-
theme = NULL,
27-
locale = NULL) {
23+
data = NULL, read_only = NULL, schema = NULL,
24+
theme = NULL, locale = NULL) {
2825

29-
payload <- list(
30-
id = id,
31-
data = data,
32-
readOnly = readOnly,
33-
schema = if (!is.null(schema)) if (is.character(schema)) schema else jsonlite::toJSON(schema, auto_unbox = TRUE),
34-
theme = theme,
35-
locale = locale
36-
)
37-
session$sendCustomMessage("rsurveyjs:update", payload)
26+
if (!is.null(data)) {
27+
session$sendCustomMessage("surveyjs-data", list(el = id, data = data))
28+
}
29+
if (!is.null(read_only)) {
30+
session$sendCustomMessage("surveyjs-mode", list(el = id, mode = if (read_only) "display" else "edit"))
31+
}
32+
if (!is.null(schema)) {
33+
session$sendCustomMessage("surveyjs-schema", list(el = id, schema = schema))
34+
}
35+
if (!is.null(theme)) {
36+
session$sendCustomMessage("surveyjs-theme", list(el = id, theme = theme))
37+
}
38+
if (!is.null(locale)) {
39+
session$sendCustomMessage("surveyjs-locale", list(el = id, locale = locale))
40+
}
3841
}

README.Rmd

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,22 @@ This avoids applying potentially mismatched styles and helps keep behavior predi
144144

145145
## Known limitations so far
146146

147-
* consistent argument naming e.g. theme_vars vs readOnly...> snake_case
148-
* work over vignettes (including editing, consistency, cross-linking, examples)
149-
* does the high level structure for vignettes make sense?
150-
* what are multi-locale surveys?
151-
* switch language on runtime (e.g. via dropown; supproted by surveyjs) (could add a message handler like "surveyjs-locale" - not yet done)
152147
* Live results tracking example doesnt work yet > work with onValueChanged > Shiny.setInputValue(...)
153148
* Completion example does not work or is not meaningful > Covered by onComplete + input&<id>_data
154149
* Dynamic Update example does not work or is not meaningful
155-
* tests/unit tests for all examples are needed
156150
* error handling
157-
* Embedding in other widgets or layouts
158-
* Extending with custom question types
159-
* Custom widgets / question renderers (needs js-level support, potentially surveyjs plugin, not sure if possible)
160151
* Provide file upload example - e.g. what to do then with that file?
161152
* provide a shiny database example including testing with db-connection
162153

163-
* In surveyjs.js den Comment noch auflösen. You can add resizing suppert here if needed later
154+
* Custom widgets / question renderers (needs js-level support, potentially surveyjs plugin, not sure if possible)
155+
* Extending with custom question types
156+
* Embedding in other widgets or layouts
157+
* tests/unit tests for all examples are needed
158+
* switch language on runtime (e.g. via dropown; supproted by surveyjs) (could add a message handler like "surveyjs-locale" - not yet done)
159+
* what are multi-locale surveys?
160+
* does the high level structure for vignettes make sense?
164161
* PDF/Excel/CSV/JSON Export
162+
* work over vignettes (including editing, consistency, cross-linking, examples)
165163

166164
* DSLs e.g. surveyjs_text, surveyjs_number, .. to user friendly, standardizing, encourage best practices
167165
* validation utilities or DSLs like survejs_validator would output the correct pre_render_hook string for that validator, e.g.,
@@ -256,35 +254,35 @@ Shiny.addCustomMessageHandler("surveyjs-clear", function(message) {
256254
}
257255
});
258256

259-
In R:
257+
<!-- In R: -->
260258

261-
session$sendCustomMessage("surveyjs-clear", list(el = "survey_id"))
259+
<!-- session$sendCustomMessage("surveyjs-clear", list(el = "survey_id")) -->
262260

263-
✅ Events to Add for Shiny Interop
264-
Event Purpose When to Use
265-
onComplete Send results to Shiny when survey is done Capture responses in server
266-
onValueChanged Live-update an input binding Track form progress reactively
267-
Example: Live updates
261+
<!-- ✅ Events to Add for Shiny Interop -->
262+
<!-- Event Purpose When to Use -->
263+
<!-- onComplete Send results to Shiny when survey is done Capture responses in server -->
264+
<!-- onValueChanged Live-update an input binding Track form progress reactively -->
265+
<!-- Example: Live updates -->
268266

269-
survey.onValueChanged.add(function(sender, options) {
270-
Shiny.setInputValue(el.id + "_data_live", sender.data, { priority: "event" });
271-
});
267+
<!-- survey.onValueChanged.add(function(sender, options) { -->
268+
<!-- Shiny.setInputValue(el.id + "_data_live", sender.data, { priority: "event" }); -->
269+
<!-- }); -->
272270

273-
And final submission:
271+
<!-- And final submission: -->
274272

275-
survey.onComplete.add(function(sender) {
276-
Shiny.setInputValue(el.id + "_data", sender.data);
277-
});
273+
<!-- survey.onComplete.add(function(sender) { -->
274+
<!-- Shiny.setInputValue(el.id + "_data", sender.data); -->
275+
<!-- }); -->
278276

279-
Then in R:
277+
<!-- Then in R: -->
280278

281-
input$survey_id_data # final completed data
282-
input$survey_id_data_live # updated continuously
279+
<!-- input$survey_id_data # final completed data -->
280+
<!-- input$survey_id_data_live # updated continuously -->
283281

284282

285-
onComplete, onValueChanged
283+
<!-- onComplete, onValueChanged -->
286284

287-
Maybe nextPage()/prevPage() for custom buttons
285+
<!-- Maybe nextPage()/prevPage() for custom buttons -->
288286

289287

290288
---

README.md

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,6 @@ behavior predictable.
161161

162162
## Known limitations so far
163163

164-
- consistent argument naming e.g. theme_vars vs readOnly…\> snake_case
165-
166-
- work over vignettes (including editing, consistency, cross-linking,
167-
examples)
168-
169-
- does the high level structure for vignettes make sense?
170-
171-
- what are multi-locale surveys?
172-
173-
- switch language on runtime (e.g. via dropown; supproted by surveyjs)
174-
(could add a message handler like “surveyjs-locale” - not yet done)
175-
176164
- Live results tracking example doesnt work yet \> work with
177165
onValueChanged \> Shiny.setInputValue(…)
178166

@@ -181,26 +169,33 @@ behavior predictable.
181169

182170
- Dynamic Update example does not work or is not meaningful
183171

184-
- tests/unit tests for all examples are needed
185-
186172
- error handling
187173

188-
- Embedding in other widgets or layouts
174+
- Provide file upload example - e.g. what to do then with that file?
189175

190-
- Extending with custom question types
176+
- provide a shiny database example including testing with db-connection
191177

192178
- Custom widgets / question renderers (needs js-level support,
193179
potentially surveyjs plugin, not sure if possible)
194180

195-
- Provide file upload example - e.g. what to do then with that file?
181+
- Extending with custom question types
196182

197-
- provide a shiny database example including testing with db-connection
183+
- Embedding in other widgets or layouts
198184

199-
- In surveyjs.js den Comment noch auflösen. You can add resizing suppert
200-
here if needed later
185+
- tests/unit tests for all examples are needed
186+
187+
- switch language on runtime (e.g. via dropown; supproted by surveyjs)
188+
(could add a message handler like “surveyjs-locale” - not yet done)
189+
190+
- what are multi-locale surveys?
191+
192+
- does the high level structure for vignettes make sense?
201193

202194
- PDF/Excel/CSV/JSON Export
203195

196+
- work over vignettes (including editing, consistency, cross-linking,
197+
examples)
198+
204199
- DSLs e.g. surveyjs_text, surveyjs_number, .. to user friendly,
205200
standardizing, encourage best practices
206201

@@ -294,32 +289,43 @@ Shiny.addCustomMessageHandler(“surveyjs-clear”, function(message) {
294289
const el = document.getElementById(message.el); if (el &&
295290
el.surveyModel) { el.surveyModel.clear(); } });
296291

297-
In R:
292+
<!-- In R: -->
298293

299-
session\$sendCustomMessage(surveyjs-clear, list(el = survey_id”))
294+
<!-- session$sendCustomMessage("surveyjs-clear", list(el = "survey_id")) -->
300295

301-
✅ Events to Add for Shiny Interop Event Purpose When to Use onComplete
302-
Send results to Shiny when survey is done Capture responses in server
303-
onValueChanged Live-update an input binding Track form progress
304-
reactively Example: Live updates
296+
<!-- ✅ Events to Add for Shiny Interop -->
305297

306-
survey.onValueChanged.add(function(sender, options) {
307-
Shiny.setInputValue(el.id + “\_data_live”, sender.data, { priority:
308-
“event” }); });
298+
<!-- Event Purpose When to Use -->
309299

310-
And final submission:
300+
<!-- onComplete Send results to Shiny when survey is done Capture responses in server -->
311301

312-
survey.onComplete.add(function(sender) { Shiny.setInputValue(el.id +
313-
\_data”, sender.data); });
302+
<!-- onValueChanged Live-update an input binding Track form progress reactively -->
303+
304+
<!-- Example: Live updates -->
305+
306+
<!-- survey.onValueChanged.add(function(sender, options) { -->
307+
308+
<!-- Shiny.setInputValue(el.id + "_data_live", sender.data, { priority: "event" }); -->
309+
310+
<!-- }); -->
311+
312+
<!-- And final submission: -->
313+
314+
<!-- survey.onComplete.add(function(sender) { -->
315+
316+
<!-- Shiny.setInputValue(el.id + "_data", sender.data); -->
317+
318+
<!-- }); -->
319+
320+
<!-- Then in R: -->
314321

315-
Then in R:
322+
<!-- input$survey_id_data # final completed data -->
316323

317-
input$survey_id_data # final completed data
318-
input$survey_id_data_live \# updated continuously
324+
<!-- input$survey_id_data_live # updated continuously -->
319325

320-
onComplete, onValueChanged
326+
<!-- onComplete, onValueChanged -->
321327

322-
Maybe nextPage()/prevPage() for custom buttons
328+
<!-- Maybe nextPage()/prevPage() for custom buttons -->
323329

324330
------------------------------------------------------------------------
325331

0 commit comments

Comments
 (0)