Skip to content
This repository was archived by the owner on Mar 1, 2023. It is now read-only.

Commit 096d0a4

Browse files
committed
Basic support for autoresizer
1 parent 42d3ef8 commit 096d0a4

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*.log
33
target/
44
.idea/
5+
react-virtualized
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package react
2+
package virtualized
3+
4+
import japgolly.scalajs.react._
5+
import japgolly.scalajs.react.vdom.VdomNode
6+
import japgolly.scalajs.react.component.Js.{RawMounted, UnmountedMapped}
7+
import japgolly.scalajs.react.internal.Effect.Id
8+
9+
import scala.scalajs.js
10+
import scala.scalajs.js.annotation.JSImport
11+
12+
object AutoSizer {
13+
@js.native
14+
@JSImport("react-virtualized", "AutoSizer")
15+
object RawComponent extends js.Object
16+
17+
type OnResize = js.Function1[Size, Unit]
18+
19+
@js.native
20+
trait Props extends js.Object {
21+
/** Function responsible for rendering children.*/
22+
// children: (warams: Size) => React.Element<*>, = js.native
23+
24+
/** Disable dynamic :height property */
25+
var disableHeight: js.UndefOr[Boolean] = js.native
26+
27+
/** Disable dynamic :width property */
28+
var disableWidth: js.UndefOr[Boolean] = js.native
29+
30+
/** Nonce of the inlined stylesheet for Content Security Policy */
31+
var nonce: js.UndefOr[String] = js.native
32+
33+
/** Callback to be invoked on-resize */
34+
var onResize: OnResize = js.native
35+
}
36+
37+
def props(
38+
disableHeight: js.UndefOr[Boolean] = js.undefined,
39+
disableWidth: js.UndefOr[Boolean] = js.undefined,
40+
nonce: js.UndefOr[String] = js.undefined,
41+
onResize: Size => Callback = _ => Callback.empty,
42+
): Props = {
43+
val p = (new js.Object).asInstanceOf[Props]
44+
p.disableHeight = disableHeight
45+
p.disableWidth = disableWidth
46+
p.nonce = nonce
47+
p.onResize = (s: Size) => onResize(s).runNow
48+
p
49+
}
50+
51+
private val component = JsComponent[Props, Children.Varargs, Null](RawComponent)
52+
53+
def apply(p: Props, children: VdomNode*): UnmountedMapped[Id, Props, Null, RawMounted, Props, Null] = component(p)(children: _*)
54+
}

facade/src/main/scala/react/virtualized/package.scala

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package virtualized {
1515
def toJsObject(style: Style): js.Object =
1616
style.styles.toJSDictionary.asInstanceOf[js.Object]
1717

18-
1918
def fromJsObject(o: js.Object): Style = {
2019
val xDict = o.asInstanceOf[js.Dictionary[String | Int]]
2120
val map = (for ((prop, value) <- xDict) yield
@@ -25,6 +24,20 @@ package virtualized {
2524

2625
}
2726

27+
@js.native
28+
trait Size extends js.Object {
29+
var height: Double = js.native
30+
var width: Double = js.native
31+
}
32+
object Size {
33+
def apply(height: Double, width: Double): Size = {
34+
val p = (new js.Object).asInstanceOf[Size]
35+
p.height = height
36+
p.width = width
37+
p
38+
}
39+
}
40+
2841
trait HeaderRendererParameter extends js.Object {
2942
var columnData: js.Any
3043
var dataKey: String
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package react
2+
package virtualized
3+
4+
import org.scalatest._
5+
import japgolly.scalajs.react._
6+
7+
class AutoSizerSpec extends FlatSpec with Matchers with NonImplicitAssertions with TestUtils {
8+
9+
"AutoSizer" should
10+
"require width and dataKey" in {
11+
Column(Column.props(200, "key")).props.width should be (200)
12+
Column(Column.props(200, "key")).props.dataKey should be ("key")
13+
}
14+
it should "support disableWidth" in {
15+
AutoSizer(AutoSizer.props()).props.disableWidth.toOption should contain(false)
16+
AutoSizer(AutoSizer.props(disableWidth = true)).props.disableWidth.toOption should contain(true)
17+
}
18+
it should "support disableHeight" in {
19+
AutoSizer(AutoSizer.props()).props.disableHeight.toOption should contain(false)
20+
AutoSizer(AutoSizer.props(disableHeight = true)).props.disableHeight.toOption should contain(true)
21+
}
22+
it should "support onResize" in {
23+
val size = Size(height = 10, width = 20)
24+
AutoSizer(AutoSizer.props(onResize = x => Callback.log(x))).props.onResize(size) should be(())
25+
}
26+
}

0 commit comments

Comments
 (0)