-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathf7-inputs.R
2692 lines (2484 loc) · 72.4 KB
/
f7-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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' Framework7 autocomplete input
#'
#' \code{f7AutoComplete} generates a Framework7 autocomplete input.
#'
#' @param inputId Autocomplete input id.
#' @param label Autocomplete label.
#' @param placeholder Text to write in the container.
#' @param value Autocomplete initial value, if any.
#' @param choices Autocomplete choices.
#' @param openIn Defines how to open Autocomplete,
#' can be page or popup (for Standalone) or dropdown.
#' @param typeahead Enables type ahead, will prefill input
#' value with first item in match. Only if openIn is "dropdown".
#' @param expandInput If TRUE then input which is used as
#' item-input in List View will be expanded to full
#' screen wide during dropdown visible. Only if openIn is "dropdown".
#' @param closeOnSelect Set to true and autocomplete will be closed when user picks value.
#' Not available if multiple is enabled. Only works
#' when openIn is 'popup' or 'page'.
#' @param dropdownPlaceholderText Specify dropdown placeholder text.
#' Only if openIn is "dropdown".
#' @param multiple Whether to allow multiple value selection. Only works
#' when openIn is 'popup' or 'page'.
#'
#' @rdname autocomplete
#'
#' @examples
#' # Autocomplete input
#' if(interactive()){
#' library(shiny)
#' library(shinyMobile)
#'
#' shinyApp(
#' ui = f7Page(
#' title = "My app",
#' f7SingleLayout(
#' navbar = f7Navbar(title = "f7AutoComplete"),
#' f7AutoComplete(
#' inputId = "myautocomplete1",
#' placeholder = "Some text here!",
#' dropdownPlaceholderText = "Try to type Apple",
#' label = "Type a fruit name",
#' openIn = "dropdown",
#' choices = c('Apple', 'Apricot', 'Avocado', 'Banana', 'Melon',
#' 'Orange', 'Peach', 'Pear', 'Pineapple')
#' ),
#' textOutput("autocompleteval1"),
#' f7AutoComplete(
#' inputId = "myautocomplete2",
#' placeholder = "Some text here!",
#' openIn = "popup",
#' multiple = TRUE,
#' label = "Type a fruit name",
#' choices = c('Apple', 'Apricot', 'Avocado', 'Banana', 'Melon',
#' 'Orange', 'Peach', 'Pear', 'Pineapple')
#' ),
#' verbatimTextOutput("autocompleteval2")
#' )
#' ),
#' server = function(input, output) {
#' observe({
#' print(input$myautocomplete1)
#' print(input$myautocomplete2)
#' })
#' output$autocompleteval1 <- renderText(input$myautocomplete1)
#' output$autocompleteval2 <- renderPrint(input$myautocomplete2)
#' }
#' )
#' }
#'
#' @author David Granjon, \email{dgranjon@@ymail.com}
#'
#' @export
f7AutoComplete <- function(inputId, label, placeholder = NULL,
value = choices[1], choices,
openIn = c("popup", "page", "dropdown"),
typeahead = TRUE, expandInput = TRUE, closeOnSelect = FALSE,
dropdownPlaceholderText = NULL, multiple = FALSE) {
type <- match.arg(openIn)
if(is.null(value)) value <- character()
value <- jsonlite::toJSON(value)
choices <- jsonlite::toJSON(choices)
# autocomplete common props
autoCompleteCommon <- list(
id = inputId,
class = "autocomplete-input",
`data-choices` = choices,
`data-value` = value,
`data-open-in` = type
)
# specific props
autoCompleteProps <- if (!(type %in% c("page", "popup"))) {
list(
type = "text",
placeholder = placeholder,
`data-typeahead` = typeahead,
`data-expand-input` = expandInput,
`data-dropdown-placeholder-text` = dropdownPlaceholderText
)
} else {
list(
class = "item-link item-content",
href = "#",
`data-multiple` = multiple,
`data-close-on-select` = closeOnSelect
)
}
# merge props
autoCompleteProps <- c(autoCompleteCommon, autoCompleteProps)
# remove NULL elements
autoCompleteProps <- dropNulls(autoCompleteProps)
# replace TRUE and FALSE by true and false for javascript
autoCompleteProps <- lapply(autoCompleteProps, function(x) {
if (identical(x, TRUE)) "true"
else if (identical(x, FALSE)) "false"
else x
})
# wrap props
autoCompleteProps <- if (!(type %in% c("page", "popup"))) {
do.call(shiny::tags$input, autoCompleteProps)
} else {
tempTag <- do.call(shiny::tags$a, autoCompleteProps)
tempTag <- shiny::tagAppendChildren(
tempTag,
shiny::tags$input(type = "hidden"),
shiny::tags$div(
class = "item-inner",
# label
shiny::tags$div(class = "item-title", label),
# input
shiny::tags$div(class = "item-after")
)
)
}
# input tag + label wrapper
if (!(type %in% c("page", "popup"))) {
shiny::tags$div(
class = "list no-hairlines-md",
shiny::tags$ul(
shiny::tags$li(
class = "item-content item-input inline-label",
shiny::tags$div(
class = "item-inner",
# label
shiny::tags$div(class = "item-title item-label", label),
# input
shiny::tags$div(
class = "item-input-wrap",
autoCompleteProps
)
)
)
)
)
} else {
shiny::tags$div(class = "list", shiny::tags$ul(autoCompleteProps))
}
}
#' Update Framework7 autocomplete
#'
#' \code{updateF7AutoComplete} changes the value of an autocomplete input on the client.
#'
#' @param inputId The id of the input object.
#' @param value New value.
#' @param session The Shiny session object.
#'
#' @note You cannot update choices yet.
#'
#' @export
#' @rdname autocomplete
#'
#' @examples
#' # Update autocomplete
#' if (interactive()) {
#' library(shiny)
#' library(shinyMobile)
#' shinyApp(
#' ui = f7Page(
#' title = "My app",
#' f7SingleLayout(
#' navbar = f7Navbar(title = "Update autocomplete"),
#' f7Card(
#' f7Button(inputId = "update", label = "Update autocomplete"),
#' f7AutoComplete(
#' inputId = "myautocomplete",
#' placeholder = "Some text here!",
#' openIn = "dropdown",
#' label = "Type a fruit name",
#' choices = c('Apple', 'Apricot', 'Avocado', 'Banana', 'Melon',
#' 'Orange', 'Peach', 'Pear', 'Pineapple')
#' ),
#' verbatimTextOutput("autocompleteval")
#' )
#' )
#' ),
#' server = function(input, output, session) {
#'
#' observe({
#' print(input$myautocomplete)
#' })
#'
#' output$autocompleteval <- renderText(input$myautocomplete)
#'
#' observeEvent(input$update, {
#' updateF7AutoComplete(
#' inputId = "myautocomplete",
#' value = "Banana"
#' )
#' })
#' }
#' )
#' }
updateF7AutoComplete <- function(inputId, value = NULL,
session = shiny::getDefaultReactiveDomain()) {
message <- dropNulls(
list(
value = I(value)
)
)
session$sendInputMessage(inputId, message)
}
#' Framework7 picker input
#'
#' \code{f7Picker} generates a picker input.
#'
#' @param inputId Picker input id.
#' @param label Picker label.
#' @param placeholder Text to write in the container.
#' @param value Picker initial value, if any.
#' @param choices Picker choices.
#' @param rotateEffect Enables 3D rotate effect. Default to TRUE.
#' @param openIn Can be auto, popover (to open picker in popover), sheet (to open in sheet modal).
#' In case of auto will open in sheet modal on small screens and in popover on large screens. Default
#' to auto.
#' @param scrollToInput Scroll viewport (page-content) to input when picker opened. Default
#' to FALSE.
#' @param closeByOutsideClick If enabled, picker will be closed by clicking outside of picker or related input element.
#' Default to TRUE.
#' @param toolbar Enables picker toolbar. Default to TRUE.
#' @param toolbarCloseText Text for Done/Close toolbar button.
#' @param sheetSwipeToClose Enables ability to close Picker sheet with swipe. Default to FALSE.
#'
#' @rdname picker
#' @examples
#' # Picker input
#' if(interactive()){
#' library(shiny)
#' library(shinyMobile)
#'
#' shinyApp(
#' ui = f7Page(
#' title = "My app",
#' f7SingleLayout(
#' navbar = f7Navbar(title = "f7Picker"),
#' f7Picker(
#' inputId = "mypicker",
#' placeholder = "Some text here!",
#' label = "Picker Input",
#' choices = c('a', 'b', 'c')
#' ),
#' textOutput("pickerval")
#' )
#' ),
#' server = function(input, output) {
#' output$pickerval <- renderText(input$mypicker)
#' }
#' )
#' }
#'
#' @author David Granjon, \email{dgranjon@@ymail.com}
#'
#' @export
f7Picker<- function(inputId, label, placeholder = NULL, value = choices[1], choices,
rotateEffect = TRUE, openIn = "auto", scrollToInput = FALSE,
closeByOutsideClick = TRUE, toolbar = TRUE, toolbarCloseText = "Done",
sheetSwipeToClose = FALSE) {
# for JS
if (is.null(value)) stop("value cannot be NULL.")
value <- jsonlite::toJSON(value)
choices <- jsonlite::toJSON(choices)
# picker props
pickerProps <- dropNulls(
list(
id = inputId,
class = "picker-input",
type = "text",
placeholder = placeholder,
`data-choices` = choices,
`data-value` = value,
`data-rotate-effect` = rotateEffect,
`data-open-in` = openIn,
`data-scroll-to-input` = scrollToInput,
`data-close-by-outside-click` = closeByOutsideClick,
`data-toolbar` = toolbar,
`data-toolbar-close-text` = toolbarCloseText,
`data-sheet-swipe-to-close` = sheetSwipeToClose
)
)
# replace TRUE and FALSE by true and false for javascript
pickerProps <- lapply(pickerProps, function(x) {
if (identical(x, TRUE)) "true"
else if (identical(x, FALSE)) "false"
else x
})
# wrap props
pickerProps <- do.call(shiny::tags$input, pickerProps)
# input tag
inputTag <- shiny::tags$div(
class = "item-content item-input",
shiny::tags$div(
class = "item-inner",
shiny::tags$div(
class = "item-input-wrap",
pickerProps
)
)
)
# tag wrapper
shiny::tagList(
shiny::tags$div(
class = "block-title",
label
),
shiny::tags$div(
class = "list no-hairlines-md",
shiny::tags$ul(
shiny::tags$li(
inputTag
)
)
)
)
}
#' Update Framework7 picker
#'
#' \code{updateF7Picker} changes the value of a picker input on the client.
#'
#' @param inputId The id of the input object.
#' @param value Picker initial value, if any.
#' @param choices New picker choices.
#' @param rotateEffect Enables 3D rotate effect. Default to TRUE.
#' @param openIn Can be auto, popover (to open picker in popover), sheet (to open in sheet modal).
#' In case of auto will open in sheet modal on small screens and in popover on large screens. Default
#' to auto.
#' @param scrollToInput Scroll viewport (page-content) to input when picker opened. Default
#' to FALSE.
#' @param closeByOutsideClick If enabled, picker will be closed by clicking outside of picker or related input element.
#' Default to TRUE.
#' @param toolbar Enables picker toolbar. Default to TRUE.
#' @param toolbarCloseText Text for Done/Close toolbar button.
#' @param sheetSwipeToClose Enables ability to close Picker sheet with swipe. Default to FALSE.
#' @param session The Shiny session object, usually the default value will suffice.
#'
#' @export
#' @rdname picker
#'
#' @examples
#' # Update picker input
#' if (interactive()) {
#' library(shiny)
#' library(shinyMobile)
#' shinyApp(
#' ui = f7Page(
#' title = "My app",
#' f7SingleLayout(
#' navbar = f7Navbar(title = "Update picker"),
#' f7Card(
#' f7Button(inputId = "update", label = "Update picker"),
#' f7Picker(
#' inputId = "mypicker",
#' placeholder = "Some text here!",
#' label = "Picker Input",
#' choices = c('a', 'b', 'c')
#' ),
#' verbatimTextOutput("pickerval"),
#' br(),
#' f7Button(inputId = "removeToolbar", label = "Remove picker toolbar", color = "red")
#' )
#' )
#' ),
#' server = function(input, output, session) {
#'
#' output$pickerval <- renderText(input$mypicker)
#'
#' observeEvent(input$update, {
#' updateF7Picker(
#' inputId = "mypicker",
#' value = "b",
#' choices = letters,
#' openIn = "sheet",
#' toolbarCloseText = "Prout",
#' sheetSwipeToClose = TRUE
#' )
#' })
#'
#' observeEvent(input$removeToolbar, {
#' updateF7Picker(
#' inputId = "mypicker",
#' value = "b",
#' choices = letters,
#' openIn = "sheet",
#' toolbar = FALSE
#' )
#' })
#'
#' }
#' )
#' }
updateF7Picker <- function(inputId, value = NULL, choices = NULL,
rotateEffect = NULL, openIn = NULL, scrollToInput = NULL,
closeByOutsideClick = NULL, toolbar = NULL, toolbarCloseText = NULL,
sheetSwipeToClose = NULL,
session = shiny::getDefaultReactiveDomain()) {
message <- dropNulls(list(
value = value,
choices = choices,
rotateEffect = rotateEffect,
openIn = openIn,
scrollToInput = scrollToInput,
closeByOutsideClick = closeByOutsideClick,
toolbar = toolbar,
toolbarCloseText = toolbarCloseText,
sheetSwipeToClose = sheetSwipeToClose
))
session$sendInputMessage(inputId, message)
}
f7ColorPickerPalettes <- list(
c('#FFEBEE', '#FFCDD2', '#EF9A9A',
'#E57373', '#EF5350', '#F44336',
'#E53935', '#D32F2F', '#C62828',
'#B71C1C'),
c('#F3E5F5', '#E1BEE7', '#CE93D8',
'#BA68C8', '#AB47BC', '#9C27B0',
'#8E24AA', '#7B1FA2', '#6A1B9A',
'#4A148C'),
c('#E8EAF6', '#C5CAE9', '#9FA8DA',
'#7986CB', '#5C6BC0', '#3F51B5',
'#3949AB', '#303F9F', '#283593',
'#1A237E'),
c('#E1F5FE', '#B3E5FC', '#81D4FA',
'#4FC3F7', '#29B6F6', '#03A9F4',
'#039BE5', '#0288D1', '#0277BD',
'#01579B'),
c('#E0F2F1', '#B2DFDB', '#80CBC4',
'#4DB6AC', '#26A69A', '#009688',
'#00897B', '#00796B', '#00695C',
'#004D40'),
c('#F1F8E9', '#DCEDC8', '#C5E1A5',
'#AED581', '#9CCC65', '#8BC34A',
'#7CB342', '#689F38', '#558B2F',
'#33691E'),
c('#FFFDE7', '#FFF9C4', '#FFF59D',
'#FFF176', '#FFEE58', '#FFEB3B',
'#FDD835', '#FBC02D', '#F9A825',
'#F57F17'),
c('#FFF3E0', '#FFE0B2', '#FFCC80',
'#FFB74D', '#FFA726', '#FF9800',
'#FB8C00', '#F57C00', '#EF6C00',
'#E65100')
)
f7ColorPickerModules <- c(
"wheel", "sb-spectrum",
"hue-slider", "hs-spectrum",
"brightness-slider", "rgb-slider",
"hsb-sliders", "alpha-slider",
"rgb-bars", "palette", "hex",
"current-color", "initial-current-colors"
)
globalVariables(c("f7ColorPickerPalettes", "f7ColorPickerModules"))
#' Create a Framework7 color picker input
#'
#' @param inputId Color picker input.
#' @param label Color picker label.
#' @param value Color picker value. hex, rgb, hsl, hsb, alpha, hue,
#' rgba, hsla are supported.
#' @param placeholder Color picker placeholder.
#' @param modules Picker color modules. Choose at least one.
#' @param palettes Picker color predefined palettes. Must be a list
#' of color vectors, each value specified as HEX string.
#' @param sliderValue When enabled, it will display sliders values.
#' @param sliderValueEditable When enabled, it will display sliders values as <input>
#' elements to edit directly.
#' @param sliderLabel When enabled, it will display sliders labels with text.
#' @param hexLabel When enabled, it will display HEX module label text, e.g. HEX.
#' @param hexValueEditable When enabled, it will display HEX module value as <input> element to edit directly.
#' @param groupedModules When enabled it will add more exposure
#' to sliders modules to make them look more separated.
#'
#' @export
#'
#' @examples
#' if (interactive()) {
#' library(shiny)
#' library(shinyMobile)
#'
#' shinyApp(
#' ui = f7Page(
#' title = "My app",
#' f7SingleLayout(
#' navbar = f7Navbar(title = "f7ColorPicker"),
#' f7ColorPicker(
#' inputId = "mycolorpicker",
#' placeholder = "Some text here!",
#' label = "Select a color"
#' ),
#' "The picker value is:",
#' textOutput("colorPickerVal")
#' )
#' ),
#' server = function(input, output) {
#' output$colorPickerVal <- renderText(input$mycolorpicker)
#' }
#' )
#' }
f7ColorPicker <- function(inputId, label, value = "#ff0000", placeholder = NULL,
modules = f7ColorPickerModules, palettes = f7ColorPickerPalettes,
sliderValue = TRUE, sliderValueEditable = TRUE,
sliderLabel = TRUE, hexLabel = TRUE,
hexValueEditable = TRUE, groupedModules = TRUE) {
# if the value is provided as a rgb, hsl, hsb, rgba or hsla
if (is.numeric(value) & length(value) > 1) value <- jsonlite::toJSON(value)
modules <- jsonlite::toJSON(modules)
palettes <- jsonlite::toJSON(palettes)
# We define a global variable that is
# reused in the pickerInputBinding.js
pickerProps <- shiny::tags$script(
paste0(
'var colorPickerModules = ', modules, ';
var colorPickerPalettes = ', palettes, ';
var colorPickerValue = "', value, '";
var colorPickerSliderValue = ', tolower(sliderValue), ';
var colorPickerSliderValueEditable = ', tolower(sliderValueEditable), ';
var colorPickerSliderLabel = ', tolower(sliderLabel), ';
var colorPickerHexLabel = ', tolower(hexLabel), ';
var colorPickerHexValueEditable = ', tolower(hexValueEditable), ';
var colorPickerGroupedModules = ', tolower(groupedModules), ';
'
)
)
inputTag <- shiny::tags$input(
type = "text",
placeholder = placeholder,
id = inputId,
class = "color-picker-input"
)
wrapperTag <- shiny::tags$div(
class = "list no-hairlines-md",
shiny::tags$ul(
shiny::tags$li(
shiny::tags$div(
class = "item-content item-input",
shiny::tags$div(
class = "item-media",
shiny::tags$i(
class = "icon demo-list-icon",
id = paste0(inputId, "-value")
)
),
shiny::tags$div(
class = "item-inner",
shiny::tags$div(
class = "item-input-wrap",
inputTag
)
)
)
)
)
)
labelTag <- shiny::tags$div(class = "block-title", label)
shiny::tagList(
shiny::singleton(pickerProps),
labelTag,
wrapperTag
)
}
#' Framework7 date picker
#'
#' \code{f7DatePicker} creates a Framework7 date picker input.
#'
#' @param inputId Date input id.
#' @param label Input label.
#' @param value Array with initial selected dates. Each array item represents selected date.
#' @param multiple If \code{TRUE} allow to select multiple dates.
#' @param direction Months layout direction, could be 'horizontal' or 'vertical'.
#' @param minDate Minimum allowed date.
#' @param maxDate Maximum allowed date.
#' @param dateFormat Date format: "yyyy-mm-dd", for instance.
#' @param openIn Can be auto, popover (to open calendar in popover), sheet
#' (to open in sheet modal) or customModal (to open in custom Calendar modal overlay).
#' In case of auto will open in sheet modal on small screens and in popover on large screens.
#' @param scrollToInput Scroll viewport (page-content) to input when calendar opened.
#' @param closeByOutsideClick If enabled, picker will be closed by clicking outside of picker or related input element.
#' @param toolbar Enables calendar toolbar.
#' @param toolbarCloseText Text for Done/Close toolbar button.
#' @param header Enables calendar header.
#' @param headerPlaceholder Default calendar header placeholder text.
#'
#' @importFrom jsonlite toJSON
#' @rdname datepicker
#'
#' @return a \code{Date} vector.
#'
#' @export
#' @examples
#' # Date picker
#' if (interactive()) {
#' library(shiny)
#' library(shinyMobile)
#'
#' shinyApp(
#' ui = f7Page(
#' title = "My app",
#' f7SingleLayout(
#' navbar = f7Navbar(title = "f7DatePicker"),
#' f7DatePicker(
#' inputId = "date",
#' label = "Choose a date",
#' value = "2019-08-24"
#' ),
#' "The selected date is",
#' verbatimTextOutput("selectDate"),
#' f7DatePicker(
#' inputId = "multipleDates",
#' label = "Choose multiple dates",
#' value = Sys.Date() + 0:3,
#' multiple = TRUE
#' ),
#' "The selected date is",
#' verbatimTextOutput("selectMultipleDates"),
#' f7DatePicker(
#' inputId = "default",
#' label = "Choose a date",
#' value = NULL
#' ),
#' "The selected date is",
#' verbatimTextOutput("selectDefault")
#' )
#' ),
#' server = function(input, output, session) {
#'
#' output$selectDate <- renderPrint(input$date)
#' output$selectMultipleDates <- renderPrint(input$multipleDates)
#' output$selectDefault <- renderPrint(input$default)
#'
#' }
#' )
#' }
f7DatePicker <- function(inputId, label, value = NULL, multiple = FALSE, direction = c("horizontal", "vertical"),
minDate = NULL, maxDate = NULL, dateFormat = "yyyy-mm-dd",
openIn = c("auto", "popover", "sheet", "customModal"),
scrollToInput = FALSE, closeByOutsideClick = TRUE,
toolbar = TRUE, toolbarCloseText = "Done", header = FALSE,
headerPlaceholder = "Select date") {
direction <- match.arg(direction)
openIn <- match.arg(openIn)
if (!is.null(value) && length(value) == 1) {
value <- list(value)
}
config <- dropNulls(list(
value = value,
multiple = multiple,
direction = direction,
minDate = minDate,
maxDate = maxDate,
dateFormat = dateFormat,
openIn = openIn,
scrollToInput = scrollToInput,
closeByClickOutside = closeByOutsideClick,
toolbar = toolbar,
toolbarCloseText = toolbarCloseText,
header = header,
headerPlaceholder = headerPlaceholder
))
# date picker props
datePickerTag <- shiny::tags$input(
type = "text",
class = "calendar-input",
id = inputId
)
# label
labelTag <- shiny::tags$div(class = "block-title", label)
shiny::tagList(
if (!is.null(label)) labelTag,
# input tag
shiny::tags$div(
class = "list no-hairlines-md",
shiny::tags$ul(
shiny::tags$li(
shiny::tags$div(
class = "item-content item-input",
shiny::tags$div(
class = "item-inner",
shiny::tags$div(
class = "item-input-wrap",
datePickerTag,
shiny::tags$script(
type = "application/json",
`data-for` = inputId,
jsonlite::toJSON(
x = config,
auto_unbox = TRUE,
json_verbatim = TRUE
)
)
)
)
)
)
)
)
)
}
#' Update Framework7 date picker
#'
#' \code{updateF7DatePicker} changes the value of a date picker input on the client.
#'
#' @param inputId The id of the input object.
#' @param value The new value for the input.
#' @param ... Parameters used to update the date picker,
#' use same arguments as in \code{\link{f7DatePicker}}.
#' @param session The Shiny session object, usually the default value will suffice.
#'
#' @export
#'
#' @rdname datepicker
#'
#' @examples
#' # Update date picker
#' if (interactive()) {
#' library(shiny)
#' library(shinyMobile)
#'
#' shinyApp(
#' ui = f7Page(
#' title = "My app",
#' f7SingleLayout(
#' navbar = f7Navbar(title = "Update date picker"),
#' f7Card(
#' f7Button(inputId = "selectToday", label = "Select today"),
#' f7Button(inputId = "rmToolbar", label = "Remove toolbar"),
#' f7Button(inputId = "addToolbar", label = "Add toolbar"),
#' f7DatePicker(
#' inputId = "mypicker",
#' label = "Choose a date",
#' value = Sys.Date() - 7,
#' openIn = "auto",
#' direction = "horizontal"
#' ),
#' verbatimTextOutput("pickerval")
#' )
#' )
#' ),
#' server = function(input, output, session) {
#'
#' output$pickerval <- renderPrint(input$mypicker)
#'
#' observeEvent(input$selectToday, {
#' updateF7DatePicker(
#' inputId = "mypicker",
#' value = Sys.Date()
#' )
#' })
#'
#' observeEvent(input$rmToolbar, {
#' updateF7DatePicker(
#' inputId = "mypicker",
#' toolbar = FALSE,
#' dateFormat = "yyyy-mm-dd" # preserve date format
#' )
#' })
#'
#' observeEvent(input$addToolbar, {
#' updateF7DatePicker(
#' inputId = "mypicker",
#' toolbar = TRUE,
#' dateFormat = "yyyy-mm-dd" # preserve date format
#' )
#' })
#'
#' }
#' )
#' }
updateF7DatePicker <- function(inputId, value = NULL, ...,
session = shiny::getDefaultReactiveDomain()) {
if (!is.null(value)) {
if (length(value) == 1) {
value <- list(as.character(value))
} else {
value <- as.character(value)
}
}
config <- dropNulls(list(...))
if (length(config) == 0)
config <- NULL
message <- dropNulls(list(
value = value,
config = config
))
session$sendInputMessage(inputId, message)
}
#' Framework7 checkbox
#'
#' \link{f7Checkbox} creates a checkbox input.
#'
#' @param inputId The input slot that will be used to access the value.
#' @param label Display label for the control, or NULL for no label.
#' @param value Initial value (TRUE or FALSE).
#'
#' @rdname checkbox
#' @examples
#' if(interactive()){
#' library(shiny)
#' library(shinyMobile)
#'
#' shinyApp(
#' ui = f7Page(
#' title = "My app",
#' f7SingleLayout(
#' navbar = f7Navbar(title = "f7Checkbox"),
#' f7Card(
#' f7Checkbox(
#' inputId = "check",
#' label = "Checkbox",
#' value = FALSE
#' ),
#' verbatimTextOutput("test")
#' )
#' )
#' ),
#' server = function(input, output) {
#' output$test <- renderPrint({input$check})
#' }
#' )
#' }
#
#' @export
f7Checkbox <- function(inputId, label, value = FALSE) {
value <- shiny::restoreInput(id = inputId, default = value)
inputTag <- shiny::tags$input(id = inputId, type = "checkbox")
if (!is.null(value) && value)
inputTag$attribs$checked <- "checked"
shiny::tags$label(
class = "item-checkbox item-content shiny-input-container",
inputTag,
shiny::tags$i(class = "icon icon-checkbox"),
shiny::tags$div(
class = "item-inner",
shiny::tags$div(class = "item-title", label)
)
)
}
#' Deprecated functions
#'
#' \code{f7checkBox} creates a checkbox input. Use \link{f7Checkbox} instead.
#'
#' @rdname f7-deprecated
#' @inheritParams f7checkBoxGroup
#' @keywords internal
#' @export
f7checkBox <- function(inputId, label, value = FALSE){
.Deprecated(
"f7Checkbox",
package = "shinyMobile",
"f7checkBox will be removed in future release. Please use
f7Checkbox instead.",
old = as.character(sys.call(sys.parent()))[1L])
f7Checkbox(inputId, label, value)
}
#' Update Framework7 checkbox
#'
#' \code{updateF7Checkbox} changes the value of a checkbox input on the client.
#'
#' @rdname checkbox
#' @param inputId The id of the input object.
#' @param label The label to set for the input object.
#' @param value The value to set for the input object.
#' @param session The Shiny session object.
#'
#' @export
#'
#' @examples
#' if (interactive()) {
#' library(shiny)
#' library(shinyMobile)
#'
#' ui <- f7Page(
#' f7SingleLayout(
#' navbar = f7Navbar(title = "updateF7CheckBox"),
#' f7Slider(
#' inputId = "controller",
#' label = "Number of observations",
#' max = 10,
#' min = 0,
#' value = 1,
#' step = 1,
#' scale = TRUE
#' ),
#' f7checkBox(
#' inputId = "check",
#' label = "Checkbox"
#' )
#' )
#' )
#'
#' server <- function(input, output, session) {
#' observe({
#' # TRUE if input$controller is odd, FALSE if even.
#' x_even <- input$controller %% 2 == 1
#'
#' if (x_even) {
#' showNotification(
#' id = "notif",
#' paste("The slider is ", input$controller, "and the checkbox is", input$check),
#' duration = NULL,
#' type = "warning"
#' )
#' } else {
#' removeNotification("notif")
#' }
#'
#' updateF7Checkbox("check", value = x_even)
#' })
#' }
#'
#' shinyApp(ui, server)
#' }