Skip to content

Commit

Permalink
feat: Add sealed classes example and update sample component
Browse files Browse the repository at this point in the history
  • Loading branch information
cheleb committed Aug 29, 2024
1 parent 320abad commit 637f85a
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 247 deletions.
36 changes: 19 additions & 17 deletions examples/client/src/main/scala/samples/EitherSample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@ import com.raquo.laminar.api.L.*

import com.raquo.airstream.state.Var

val either = Sample(
"Either", {
val either = {

case class EitherSample(
either: Either[Cat, Dog],
optionalInt: Option[Int]
)
case class EitherSample(
either: Either[Cat, Dog],
optionalInt: Option[Int]
)

val eitherVar = Var(EitherSample(Left(Cat("Scala le chat", 6)), Some(1)))
val eitherVar = Var(EitherSample(Left(Cat("Scala le chat", 6)), Some(1)))
Sample(
"Either", {

div(
child <-- eitherVar.signal.map { item =>
div(
s"$item"
)
},
eitherVar.asForm
)
}
)
div(
child <-- eitherVar.signal.map { item =>
div(
s"$item"
)
},
eitherVar.asForm
)
}
)
}
65 changes: 33 additions & 32 deletions examples/client/src/main/scala/samples/EnumSample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,36 @@ import com.raquo.airstream.state.Var

import com.raquo.laminar.api.L

val enums = Sample(
"Enums", {

enum Color(val code: String):
case Black extends Color("000")
case White extends Color("FFF")
case Isabelle extends Color("???")

case class Basket(color: Color, cat: Cat)

given colorForm: Form[Color] = enumForm(Color.values, Color.fromOrdinal)

case class Cat(
name: String,
age: Int,
color: Color
)

val eitherVar = Var(
Basket(Color.Black, Cat("Scala", 10, Color.White))
)

div(
child <-- eitherVar.signal.map { item =>
div(
s"$item"
)
},
eitherVar.asForm
)
}
)
val enums = {
enum Color(val code: String):
case Black extends Color("000")
case White extends Color("FFF")
case Isabelle extends Color("???")

case class Basket(color: Color, cat: Cat)

given colorForm: Form[Color] = enumForm(Color.values, Color.fromOrdinal)

case class Cat(
name: String,
age: Int,
color: Color
)

val eitherVar = Var(
Basket(Color.Black, Cat("Scala", 10, Color.White))
)
Sample(
"Enums", {

div(
child <-- eitherVar.signal.map { item =>
div(
s"$item"
)
},
eitherVar.asForm
)
}
)
}
46 changes: 20 additions & 26 deletions examples/client/src/main/scala/samples/ListElement.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,27 @@ import dev.cheleb.scalamigen.{*, given}

import com.raquo.laminar.api.L.*

case class Person2(id: Int, name: String, age: Int)
val list = {
case class Person2(id: Int, name: String, age: Int)

case class ListElement(
ints: List[Person2]
)

val listPersonVar = Var(
ListElement(List(1, 2, 3).map(id => Person2(id, "Vlad", 20)))
)
val listIntVar = Var(List(1, 2, 3))
case class ListElement(
ints: List[Person2]
)

given (Person2 => Int) = _.id
val listPersonVar = Var(
ListElement(List(1, 2, 3).map(id => Person2(id, "Vlad", 20)))
)

val list = Sample(
"List",
div(
child <-- listPersonVar.signal.map { item =>
div(
s"$item"
)
},
listPersonVar.asForm,
child <-- listIntVar.signal.map { item =>
div(
s"$item"
)
}
// Form.renderVar(listIntVar)
given (Person2 => Int) = _.id
Sample(
"List",
div(
child <-- listPersonVar.signal.map { item =>
div(
s"$item"
)
},
listPersonVar.asForm
)
)
)
}
95 changes: 48 additions & 47 deletions examples/client/src/main/scala/samples/Persons.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,59 @@ import magnolia1.*

import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.all.*
import samples.model.Password
import java.time.LocalDate

// Define some models
@NoPanel
case class Person(
@FieldName("First Name")
name: String,
password: Password,
birthDate: LocalDate,
fav: Pet,
pet: Option[Pet],
email: Option[String],
age: BigInt,
size: Double
)
case class Pet(
name: String,
age: BigInt,
House: House,
size: Double :| Positive
)
val person = {
// Define some models

case class House(capacity: Int)
@NoPanel
case class Person(
@FieldName("First Name")
name: String,
password: Password,
birthDate: LocalDate,
fav: Pet,
pet: Option[Pet],
email: Option[String],
age: BigInt,
size: Double
)
case class Pet(
name: String,
age: BigInt,
House: House,
size: Double :| Positive
)

// Provide default for optional
given Defaultable[Pet] with
def default = Pet("No pet", 0, House(0), 1)
case class House(capacity: Int)

// Instance your model
val vlad =
Person(
"",
Password("not a password"),
LocalDate.of(1431, 11, 8),
Pet("Batman", 666, House(2), 169),
Some(Pet("Wolfy", 12, House(1), 42)),
Some("[email protected]"),
48,
1.85
)
// Provide default for optional
given Defaultable[Pet] with
def default = Pet("No pet", 0, House(0), 1)

val personVar = Var(vlad)
// Instance your model
val vlad =
Person(
"",
Password("not a password"),
LocalDate.of(1431, 11, 8),
Pet("Batman", 666, House(2), 169),
Some(Pet("Wolfy", 12, House(1), 42)),
Some("[email protected]"),
48,
1.85
)

val person = Sample(
"Person",
div(
child <-- personVar.signal.map { item =>
div(
s"$item"
)
},
personVar.asForm
val personVar = Var(vlad)
Sample(
"Person",
div(
child <-- personVar.signal.map { item =>
div(
s"$item"
)
},
personVar.asForm
)
)
)
}
44 changes: 23 additions & 21 deletions examples/client/src/main/scala/samples/Sealed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ import com.raquo.laminar.api.L.*

import com.raquo.airstream.state.Var

sealed trait Animal
case class Horse(name: String, age: Int) extends Animal
case class Lama(name: String, age: Int, splitDistance: Int) extends Animal

case class Owner(name: String, pet: Animal)

val sealedClasses = Sample(
"Sealed", {

val eitherVar = Var(Owner("Agnes", Horse("Niram <3", 6)))

div(
child <-- eitherVar.signal.map { item =>
div(
s"$item"
)
},
eitherVar.asForm
)
}
)
val sealedClasses = {
sealed trait Animal
case class Horse(name: String, age: Int) extends Animal
case class Lama(name: String, age: Int, splitDistance: Int) extends Animal

case class Owner(name: String, pet: Animal)

Sample(
"Sealed", {

val eitherVar = Var(Owner("Agnes", Horse("Niram <3", 6)))

div(
child <-- eitherVar.signal.map { item =>
div(
s"$item"
)
},
eitherVar.asForm
)
}
)
}
29 changes: 15 additions & 14 deletions examples/client/src/main/scala/samples/SimpleSample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import com.raquo.laminar.api.L.*

import com.raquo.airstream.state.Var

val simple = Sample(
"Simple", {
val simple = {
val eitherVar = Var(Cat("Scala le chat", 6))
Sample(
"Simple", {

val eitherVar = Var(Cat("Scala le chat", 6))

div(
child <-- eitherVar.signal.map { item =>
div(
s"$item"
)
},
eitherVar.asForm
)
}
)
div(
child <-- eitherVar.signal.map { item =>
div(
s"$item"
)
},
eitherVar.asForm
)
}
)
}
Loading

0 comments on commit 637f85a

Please sign in to comment.