Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow exporting external components to ReactComponentClass instances #377

Merged
merged 1 commit into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

## vNEXT
### Highlights :tada:
+ Updated scalajs-dom to 1.0.0 [PR #362](https://github.com/shadaj/slinky/pull/362)
+ Updated `scalajs-dom` to 1.0.0 [PR #362](https://github.com/shadaj/slinky/pull/362)
+ Add facades for the `React.Profiler` component [PR #372](https://github.com/shadaj/slinky/pull/372)
+ Add facades for the `act` funcion in `react-test-renderer` [PR #376](https://github.com/shadaj/slinky/pull/376)

### Bug Fixes :bug:
+ Allow exporting external component definitions as instances of `ReactComponentClass` [PR #377](https://github.com/shadaj/slinky/pull/377)

### Breaking Changes :warning:
+ Due to the update of scalajs-dom to 1.0.0 a support for `dd` and `dt` tags has been dropped.

Expand Down
11 changes: 11 additions & 0 deletions core/src/main/scala/slinky/core/ReactComponentClass.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ object ReactComponentClass {
.componentConstructor(propsReader, wrapper.hot_stateWriter, wrapper.hot_stateReader, ctag)
.asInstanceOf[ReactComponentClass[wrapper.Props]]

implicit def externalToClass(
external: ExternalComponentWithAttributesWithRefType[_, _]
): ReactComponentClass[external.Props] =
external.component
.asInstanceOf[ReactComponentClass[external.Props]]

implicit def externalNoPropsToClass(
external: ExternalComponentNoPropsWithAttributesWithRefType[_, _]
): ReactComponentClass[Unit] =
external.component.asInstanceOf[ReactComponentClass[Unit]]

implicit def functionalComponentToClass[P](
component: FunctionalComponent[P]
)(implicit propsReader: Reader[P]): ReactComponentClass[P] =
Expand Down
17 changes: 17 additions & 0 deletions tests/src/test/scala/slinky/core/ExportedComponentTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ object TestExportedComponentStateless extends StatelessComponentWrapper {
}
}

object TestExportedExternalComponent extends ExternalComponentNoProps {
case class Props(children: Seq[ReactElement])
val component = "div"
}

class ExportedComponentTest extends AnyFunSuite {
test("Can construct an instance of an exported component with JS-provided props") {
val container = document.createElement("div")
Expand Down Expand Up @@ -72,4 +77,16 @@ class ExportedComponentTest extends AnyFunSuite {

assert(container.innerHTML == "lol")
}

test("Can construct an instance of an exported external component with JS-provided props") {
val container = document.createElement("div")
ReactDOM.render(React.createElement(
TestExportedExternalComponent: ReactComponentClass[_],
js.Dictionary(
"children" -> js.Array("hello")
)
), container)

assert(container.innerHTML == "<div>hello</div>")
}
}