Skip to content

Commit 82423dc

Browse files
committed
Allow passing in keys of values
1 parent 179d00d commit 82423dc

2 files changed

Lines changed: 72 additions & 7 deletions

File tree

lib/peek_app_sdk/ui/odyssey/product_picker.ex

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,22 @@ defmodule PeekAppSDK.UI.Odyssey.ProductPicker do
107107
id={"#{@id}_checkboxes"}
108108
>
109109
<div :for={product <- @products} class="flex items-center gap-3">
110+
<% product_id = product[@id_key] %>
110111
<input
111112
type="checkbox"
112-
id={"#{@id}_product_#{product.id}"}
113-
checked={product.id in @selected_ids}
114-
data-product-id={product.id}
113+
id={"#{@id}_product_#{product_id}"}
114+
checked={product_id in @selected_ids}
115+
data-product-id={product_id}
115116
disabled={@disabled}
116117
class="checkbox checkbox-sm product-picker-checkbox"
117118
/>
118119
<div
119120
class="w-3 h-3 rounded-sm flex-shrink-0"
120-
style={"background-color: #{product.color_hex}"}
121+
style={"background-color: #{product[@color_key] || "#888888"}"}
121122
>
122123
</div>
123-
<label for={"#{@id}_product_#{product.id}"} class="text-sm text-gray-700 cursor-pointer">
124-
{product.name}
124+
<label for={"#{@id}_product_#{product_id}"} class="text-sm text-gray-700 cursor-pointer">
125+
{product[@name_key]}
125126
</label>
126127
</div>
127128
</div>
@@ -174,10 +175,14 @@ defmodule PeekAppSDK.UI.Odyssey.ProductPicker do
174175
"""
175176
attr :field, :any, required: true, doc: "a Phoenix.HTML.FormField struct"
176177
attr :id, :string, doc: "component id, defaults to form_field_product_picker"
177-
attr :products, :list, required: true, doc: "list of %{id, name, color_hex} maps"
178+
179+
attr :products, :list, required: true, doc: "list of product maps"
178180

179181
attr :selected_ids, :list, doc: "list of pre-selected product IDs (auto-extracted from field value when omitted)"
180182

183+
attr :id_key, :atom, default: :id, doc: "key to read product ID from each product map"
184+
attr :name_key, :atom, default: :name, doc: "key to read product name from each product map"
185+
attr :color_key, :atom, default: :color_hex, doc: "key to read product color hex from each product map"
181186
attr :all_label, :string, default: "All Products", doc: "label for the 'all' toggle option"
182187
attr :specific_label, :string, default: "Specific Products", doc: "label for the 'specific' toggle option"
183188
attr :label, :string, required: false, doc: "label for the toggle button"

test/peek_app_sdk/ui/odyssey/product_picker_test.exs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,66 @@ defmodule PeekAppSDK.UI.Odyssey.ProductPickerTest do
247247
end
248248
end
249249

250+
describe "custom key attrs" do
251+
test "uses custom color_key" do
252+
form = to_form(%{"p" => nil}, as: :t)
253+
products = [%{id: "1", name: "A", hex: "#AA0000"}]
254+
255+
html =
256+
render_component(
257+
fn assigns ->
258+
~H"""
259+
<.odyssey_product_picker field={@form[:p]} products={@products} selected_ids={["1"]} color_key={:hex} />
260+
"""
261+
end,
262+
%{form: form, products: products}
263+
)
264+
265+
assert html =~ "background-color: #AA0000"
266+
end
267+
268+
test "uses custom id_key and name_key" do
269+
form = to_form(%{"p" => nil}, as: :t)
270+
products = [%{product_id: "x1", title: "Kayak", color_hex: "#000"}]
271+
272+
html =
273+
render_component(
274+
fn assigns ->
275+
~H"""
276+
<.odyssey_product_picker
277+
field={@form[:p]}
278+
products={@products}
279+
selected_ids={["x1"]}
280+
id_key={:product_id}
281+
name_key={:title}
282+
/>
283+
"""
284+
end,
285+
%{form: form, products: products}
286+
)
287+
288+
assert html =~ "Kayak"
289+
assert html =~ "data-product-id=\"x1\""
290+
end
291+
292+
test "falls back to #888888 when color key is missing" do
293+
form = to_form(%{"p" => nil}, as: :t)
294+
products = [%{id: "1", name: "A"}]
295+
296+
html =
297+
render_component(
298+
fn assigns ->
299+
~H"""
300+
<.odyssey_product_picker field={@form[:p]} products={@products} selected_ids={["1"]} />
301+
"""
302+
end,
303+
%{form: form, products: products}
304+
)
305+
306+
assert html =~ "background-color: #888888"
307+
end
308+
end
309+
250310
describe "extract_ids_from_field/1" do
251311
alias PeekAppSDK.UI.Odyssey.ProductPicker
252312

0 commit comments

Comments
 (0)