computed.km 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. import window from '../ui/web/window';
  2. do { window::mount Root };
  3. function FloatInput:
  4. &(Reactive[Maybe[NormalFloat]]) => ui::Component
  5. &(value) =>
  6. \ buf := use { ui::State::[String] '' },
  7. \ use { ui::Effect
  8. \ content := connect buf,
  9. (value <- { parse-float content }) },
  10. { ui::Input buf };
  11. const Root: ui::Component :=
  12. let container := { ui::StyledContainer ['container'] },
  13. let row := { ui::StyledContainer ['row'] },
  14. \ price := use { ui::State { Some 0.0 } },
  15. \ quantity := use { ui::State { Some 0.0 } },
  16. let total :=
  17. \ (p?, q?) := Computed (price.{watch}, quantity.{watch}),
  18. \ (p, q) := Maybe (p?, q?),
  19. (p * q),
  20. { container [
  21. { row [ { ui::Label 'Price:' }, { FloatInput price } ] },
  22. { row [ { ui::Label 'Quantity:' }, { FloatInput quantity } ] },
  23. { ui::Label
  24. \ total? := map total,
  25. \ total := consume (total?, 'invalid input'),
  26. { "Total = ?" total.{String} }
  27. }
  28. ] };