Code Monkey home page Code Monkey logo

simple-sri's Introduction

Simple Scalajs-React-Interface

Build Status Scala.js Maven Central

Lightweight scalajs react solution.

Ported react version: 16

Features

  • Simple and straightforward style to build react component.
  • Use diode instead of redux to manage state.
  • Write your DOM tests side by side with scala-jest.

Setup

Dependencies:

val sriVersion = "0.3.0"
libraryDependencies ++= Seq(
    "com.github.cuzfrog" %%% "simple-sri" % sriVersion,
    "com.github.cuzfrog" %%% "simple-sri-diode" % sriVersion, //Diode connector
    "com.github.cuzfrog" %%% "simple-sri-test-utils" % sriVersion % Test
)

Example

Define React component:

import sri.react._
import sri.web.vdom.tagsPrefix_<^._

class BasicInput extends Component[BasicInput.Props, BasicInput.State] {
  override def getInitialState = BasicInput.State("")

  override def render(): ReactRenders = {
    <.input(
      ^.value := state.value,
      ^.onChange := callback
    )
  }

  private val callback = (event: ReactEventI) => {
    val value = event.target.value
    setState(s => s.copy(value = value))
    println(value)
  }
}

object BasicInput {
  def apply(props: Props = Props()): ReactElement = CreateElement[BasicInput](props)

  case class State(value: String)
  case class Props()
}

You could use ComponentP, ComponentS to build component that only has Props or State.

Connect with diode circuit:

Define actions and models:

import diode.Action
case class FilterChange(v: String) extends Action
case class RootModel(filterModel: FilterModel)
case class FilterModel(filterValue: String, filteredElements: Seq[String])

Define diode circuit:

object AppCircuit extends Circuit[RootModel] with ReactConnector[RootModel] {...}

Connect to component:

class FilterInput extends ComponentP[FilterInput.Props] {...}

object FilterInput {
  case class Props(value: String, onChange: ReactEventI => Unit)

  def apply(): ReactElement = {
    AppCircuit.connect(_.filterModel) { proxy =>
      def value: String = proxy().filterValue
      val onChange: ReactEventI => Unit = (event: ReactEventI) => {
        val v = event.target.value
        println(s"filter input event! $v | $value")
        event.defaultPrevented
        proxy.dispatch(FilterChange(v))
      }
      CreateElement[FilterInput](Props(value, onChange))
    }
  }
}

You can find examples in tests sub-project.

Document

Component

A scala class wrapped in a PrototypeComponent, delegating functionalities to each other. Thus js native methods have been isolated from the client.

Component can be abstract and have parameters, cause they are common classes:

abstract class AbstractComponent[P,A](args:A) extends ComponentP[P]{...}

But concrete component that is to be instantiated to react element cannot have arguments.

Factory CreateElement provides ways to create react element. Clients do not need to manage instances.

Test

See examples in project tests.

Live example: run sbt tests/fastOptJS, then npm start, a FilterList could be accessed on localhost:8080

About

This project derives from scalajs-react-interface by @chandu0101.

Author: Cause Chung ([email protected])

License: Apache License Version 2.0

simple-sri's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.