Hi all
I’m trying to implement a config options to pass to the table in live_table
I need to pass table_options
config to the table component & helpers.
The table_options are derived from the @default_options, merged with any options defined in the parent liveview, which uses LiveResource.
I want the user to be able to define the table_options in his liveview,
which need to be merged with the default options, and this forms the table_options.
These table_options need to be passed to the helpers and the TableComponent.
TableComponent-
defmodule LiveTable.TableComponent do
use Phoenix.Component
def live_table(assigns) do
~H"""
<div class="flex flex-col" id=" live-table" phx-hook="Download"
# Heex template
</div>
"""
end
end
LiveResource-
defmodule LiveTable.LiveResource do
defmacro __using__(opts) do
quote do
use Helpers,
resource: unquote(opts[:resource]),
sorting: get_in(table_options, [:sorting, :enabled]),
pagination: get_in(table_options, [:pagination, :enabled]),
default_sort: get_in(table_options, [:sorting, :default_sort])
import TableComponent
def table_options(), do: %{}
end
end
end
If the user doesnt define a table_options function in his liveview, I want to take the default params - merged with the empty map.
Default options, and merging with given options-
@default_options %{
pagination: %{
enabled: true,
page_size: 10,
sizes: [10, 25, 50]
},
sorting: %{
enabled: true,
default_sort: [asc: :id]
},
exports: %{
enabled: true,
formats: [:csv, :pdf]
},
search: %{
debounce: 300
}
}
def deep_merge(left, right) do
Map.merge(left, right, fn
_, %{} = left, %{} = right -> deep_merge(left, right)
_, _left, right -> right
end)
end
def get_table_options(table_options) do
app_defaults = Application.get_env(:live_table, :defaults, %{})
@default_options
|> deep_merge(app_defaults)
|> deep_merge(table_options)
end
How to do this? The table_options passed to the TableComponent need to be interpolated into the heex template, with conditional rendering.