Skip to content

Commit dd8314c

Browse files
authored
Merge pull request #62 from Appsilon/improve-resize-sensor
`ResizeSensor.shinyInput` component added
2 parents a65ee3a + df0acae commit dd8314c

File tree

8 files changed

+110
-27
lines changed

8 files changed

+110
-27
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export(RadioGroup.shinyInput)
6767
export(RangeSlider)
6868
export(RangeSlider.shinyInput)
6969
export(ResizeSensor)
70+
export(ResizeSensor.shinyInput)
7071
export(Select)
7172
export(Select.shinyInput)
7273
export(Slider)

R/components.R

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@ properties <- function(name) {
2626
}
2727
}
2828

29-
button <- function(name) {
30-
function(inputId, ...) {
29+
input <- function(name, defaultValue) {
30+
function(inputId, ..., value = defaultValue) {
3131
shiny.react::reactElement(
3232
module = "@/shiny.blueprint",
3333
name = name,
34-
props = shiny.react::asProps(inputId = inputId, ...),
34+
props = shiny.react::asProps(inputId = inputId, ..., value = value),
3535
deps = blueprintDependency()
3636
)
3737
}
3838
}
3939

40-
input <- function(name, defaultValue) {
41-
function(inputId, ..., value = defaultValue) {
40+
inputWithoutDefault <- function(name) {
41+
function(inputId, ...) {
4242
shiny.react::reactElement(
4343
module = "@/shiny.blueprint",
4444
name = name,
45-
props = shiny.react::asProps(inputId = inputId, ..., value = value),
45+
props = shiny.react::asProps(inputId = inputId, ...),
4646
deps = blueprintDependency()
4747
)
4848
}
@@ -166,15 +166,15 @@ Button <- component("Button")
166166

167167
#' @rdname Button
168168
#' @export
169-
Button.shinyInput <- button("Button") # nolint
169+
Button.shinyInput <- inputWithoutDefault("Button") # nolint
170170

171171
#' @rdname Button
172172
#' @export
173173
AnchorButton <- component("AnchorButton")
174174

175175
#' @rdname Button
176176
#' @export
177-
AnchorButton.shinyInput <- button("AnchorButton") # nolint
177+
AnchorButton.shinyInput <- inputWithoutDefault("AnchorButton") # nolint
178178

179179
#' Button group
180180
#'
@@ -395,6 +395,10 @@ ProgressBar <- component("ProgressBar")
395395
#' @export
396396
ResizeSensor <- component("ResizeSensor")
397397

398+
#' @rdname ResizeSensor
399+
#' @export
400+
ResizeSensor.shinyInput <- inputWithoutDefault("ResizeSensor") # nolint
401+
398402
#' Spinner
399403
#'
400404
#' Documentation: <https://blueprintjs.com/docs/#core/components/spinner>
@@ -444,11 +448,12 @@ Text <- component("Text")
444448
#' Documentation: <https://blueprintjs.com/docs/#core/components/tree>
445449
#'
446450
#' @example inst/examples/Tree.R
447-
#' @param inputId The `input` slot that will be used to access the value.
448-
#' @param data A list of nodes data. Params:
449-
#' required: `label`.
450-
#' optional: `childNodes`, `icon`, `hasCaret`, `isExpanded`, `disabled`, `secondaryLabel`
451-
#' @param ... Component props and children. See the official Blueprint docs for details.
451+
#' @inherit template params
452+
#' @param data A list of nodes parameters:
453+
#' \itemize{
454+
#' \item required: `label`
455+
#' \item optional: `childNodes`, `icon`, `hasCaret`, `isExpanded`, `disabled`, `secondaryLabel`
456+
#' }
452457
#' @export
453458
Tree <- component("Tree")
454459

inst/examples/ResizeSensor.R

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ setInput <- function(inputId, accessor = NULL) {
77
))
88
}
99

10+
printSize <- function(content) {
11+
paste0(content$width, "x", content$height)
12+
}
13+
1014
ui <- function(id) {
1115
ns <- NS(id)
1216
tagList(
@@ -25,6 +29,16 @@ ui <- function(id) {
2529
class = "resizable",
2630
textOutput(ns("size"))
2731
)
32+
),
33+
ResizeSensor.shinyInput(
34+
inputId = ns("resizeSensor"),
35+
content = div(
36+
textOutput(ns("resizeSensorInput")),
37+
style = "
38+
border: 1px solid black;
39+
width: 100px;
40+
"
41+
)
2842
)
2943
)
3044
}
@@ -33,7 +47,11 @@ server <- function(id) {
3347
moduleServer(id, function(input, output, session) {
3448
output$size <- renderText({
3549
content <- req(input$resize)
36-
paste0(content$width, "x", content$height)
50+
printSize(content)
51+
})
52+
output$resizeSensorInput <- renderText({
53+
content <- req(input$resizeSensor)
54+
printSize(content)
3755
})
3856
})
3957
}

inst/www/blueprint.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/src/index.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
import '@blueprintjs/core/lib/css/blueprint.css';
2-
import '@blueprintjs/popover2/lib/css/blueprint-popover2.css';
3-
import '@blueprintjs/select/lib/css/blueprint-select.css';
1+
import "@blueprintjs/core/lib/css/blueprint.css";
2+
import "@blueprintjs/popover2/lib/css/blueprint-popover2.css";
3+
import "@blueprintjs/select/lib/css/blueprint-select.css";
44

5-
import * as BlueprintSelect from '@blueprintjs/select';
6-
import * as Blueprint from '@blueprintjs/core';
5+
import * as BlueprintSelect from "@blueprintjs/select";
6+
import * as Blueprint from "@blueprintjs/core";
77

88
import * as Inputs from './inputs';
99
import Suggest from './suggest';
1010
import Select from './select';
1111
import MultiSelect from './multiselect';
1212
import MultiSlider from './multiSlider';
1313
import Tree from './tree';
14+
import ResizeSensor from "./resizeSensor.js";
1415

1516
import './toaster';
1617

1718
window.jsmodule = {
1819
...window.jsmodule,
19-
'@blueprintjs/core': Blueprint,
20-
'@blueprintjs/select': BlueprintSelect,
21-
'@/shiny.blueprint': {
20+
"@blueprintjs/core": Blueprint,
21+
"@blueprintjs/select": BlueprintSelect,
22+
"@/shiny.blueprint": {
2223
...Inputs,
2324
Suggest,
2425
Select,
2526
MultiSelect,
2627
MultiSlider,
2728
Tree,
29+
ResizeSensor,
2830
},
2931
};

js/src/resizeSensor.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React, { useCallback } from "react";
2+
import PropTypes from "prop-types";
3+
import { ResizeSensor as BlueprintResizeSensor } from "@blueprintjs/core";
4+
5+
const propTypes = {
6+
inputId: PropTypes.string,
7+
content: PropTypes.node,
8+
propsRest: PropTypes.object,
9+
};
10+
11+
const ResizeSensor = ({ inputId, content, ...propsRest }) => {
12+
content.props.style = {
13+
...content.props.style,
14+
overflow: "auto",
15+
resize: "both",
16+
};
17+
const handleResize = useCallback(([{ contentRect }]) => {
18+
Shiny.setInputValue(inputId, contentRect);
19+
});
20+
21+
return React.createElement(
22+
BlueprintResizeSensor,
23+
{
24+
onResize: handleResize,
25+
...propsRest,
26+
},
27+
content
28+
);
29+
};
30+
31+
ResizeSensor.propTypes = propTypes;
32+
export default ResizeSensor;

man/ResizeSensor.Rd

Lines changed: 24 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/Tree.Rd

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)