-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #26
- Loading branch information
Showing
14 changed files
with
197 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,108 @@ | ||
# Examples | ||
# Basics | ||
|
||
All of those examples are tested by Selenium. You can find Selenium test cases under [test](./test) folder. | ||
## How to replace JSX in Scala? | ||
|
||
scalajs-reactjs solely relies on those test cases to test its behaviors. In other words, the library should have example for all the behaviors it provides, and they all should be tested. | ||
To create elements in Scala, import `VirtualDOM._`. `VirtualDOM` is an extended [Static Tags](https://github.com/shogowada/statictags). | ||
|
||
`VirtualDOM` is made of three parts: tag, attributes (a.k.a. props), and children. | ||
|
||
For example, this code | ||
|
||
```scala | ||
<.div(^.id := "hello-world")("Hello, World!") | ||
``` | ||
|
||
is equivalent of the following: | ||
|
||
```html | ||
<div id="hello-world">Hello, World!</div> | ||
``` | ||
|
||
You can use as many as attributes and children you want. | ||
|
||
## How to create React components? | ||
|
||
To create React components, extend `ReactClassSpec[WrappedProps, State]` or one of its sub classes. | ||
|
||
You have four options: | ||
|
||
- `ReactClassSpec[WrappedProps, State]` | ||
- This is the parent of them all. You can have both custom props and state. | ||
- `StatelessReactClassSpec[WrappedProps]` | ||
- You cannot have state. | ||
- `PropslessReactClassSpec[State]` | ||
- You cannot have custom props. | ||
- `StaticReactClassSpec` | ||
- You cannot have both custom props and state. | ||
|
||
To render React components, use `<(/* React component */)` to make it an element. You can pass attributes and children like regular elements. | ||
|
||
```scala | ||
import io.github.shogowada.scalajs.reactjs.VirtualDOM._ | ||
|
||
class MyComponent extends ReactClassSpec[WrappedProps, State] { | ||
// ... | ||
} | ||
|
||
ReactDOM.render( | ||
<(new MyComponent())(/* attributes (a.k.a. props) */)(/* children */), | ||
mountNode | ||
) | ||
``` | ||
|
||
### What's WrappedProps? | ||
|
||
While many want to use case classes as props, React requires props to be a plain JavaScript object. So, to use case classes, we need to wrap the case class in another property. In this facade, we wrap it in "wrapped" property. | ||
|
||
```scala | ||
case class WrappedProps(foo: String, bar: Int) | ||
|
||
class MyComponent extends StatelessReactClassSpec[WrappedProps] { | ||
override def render(): ReactElement = | ||
<.div()( | ||
s"foo: ${props.wrapped.foo}", | ||
<.br.empty, | ||
s"bar: ${props.wrapped.bar}" | ||
) | ||
} | ||
|
||
ReactDOM.render( | ||
<(new MyComponent())(^.wrapped := WrappedProps("foo", 123))(), | ||
mountNode | ||
) | ||
``` | ||
|
||
Props looks like the following: | ||
|
||
```scala | ||
case class Props[WrappedProps](native: js.Dynamic) { | ||
def wrapped: WrappedProps = // ... | ||
def children: ReactElement = // ... | ||
} | ||
``` | ||
|
||
### How about states? | ||
|
||
States are wrapped and unwrapped automatically, so you don't need to do `state.wrapped`. | ||
|
||
```scala | ||
case class State(text: String) | ||
|
||
class MyComponent extends PropslessReactClassSpec[State] { | ||
override def getInitialState(): State = State(text = "") | ||
|
||
override def render(): ReactElement = | ||
<.div()( | ||
<.input( | ||
^.placeholder := "Type something here", | ||
^.value := state.text, | ||
^.onChange := onChange | ||
)() | ||
) | ||
|
||
val onChange = (event: InputFormSyntheticEvent) => { | ||
val newText = event.target.value | ||
setState(_.copy(text = newText)) | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.