Lavash.Component.Dsl (Lavash v0.3.0-rc.2)

Copy Markdown View Source

The Spark DSL extension for Lavash Components.

Components have the same capabilities as LiveViews:

  • prop - passed from parent (read-only)
  • state - internal state (socket or ephemeral)
  • read - async load an Ash resource by ID
  • form - create an AshPhoenix.Form from a resource
  • calculate - reactive computed value via rx()
  • actions - state transformers (set, run, update, effect, submit)

All declared fields are automatically projected as assigns.

Example:

defmodule MyApp.ProductEditModal do
  use Lavash.Component

  alias MyApp.Catalog.Product

  prop :product_id, :integer

  state :submitting, :boolean, default: false

  read :product, Product do
    id prop(:product_id)
  end

  form :edit_form, Product do
    data result(:product)
  end

  actions do
    action :save do
      set :submitting, true
      submit :edit_form, on_success: :save_success, on_error: :save_failed
    end

    action :save_success do
      set :submitting, false
    end

    action :save_failed do
      set :submitting, false
    end
  end
end

Summary

Functions

actions(body)

(macro)