-
Notifications
You must be signed in to change notification settings - Fork 59
Add update_selection/1 to dynamically change the selection #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4ee02c9
555bc2b
2d97b8e
f2f483d
f95c9b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -191,6 +191,25 @@ defmodule LiveSelect.Component do | |
| input_event: true, | ||
| current_text: new_current_text_after_selection(socket) | ||
| }) | ||
| |> scroll_to_active_option() | ||
| else | ||
| socket | ||
| end | ||
|
|
||
| socket = | ||
| if Map.has_key?(assigns, :update_selection) do | ||
| update(socket, :selection, fn | ||
| selection, | ||
| %{update_selection: update_fn, options: options, mode: mode, value_mapper: value_mapper} -> | ||
| update_selection(update_fn, selection, options, mode, value_mapper) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My suggestion is to replace this line with: Where We won't have dupe detection but I think it's overkill. We don't have it anyway when you pass the selection explicitly with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ran into a problem with this and still need to think of a way to figure it out. The
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry that was not clear, my function fixes the problem by splitting it and selectively running
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for clarifying. Ok, but I don't think the problem is actually fixed by your function. Take this example: current_selection = [%{value: 1, label: "one"}, %{value: 2, label: "two"}]
value_mapper = fn n -> %{value: n, label: to_string(n)} end
update_fn = fn current_selection -> current_selection ++ [2, 3] endWith this arguments, your I can't think of an elegant solution for this. Can you? My impulse would be to say: it's the responsibility of the writer of
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok I thought about it and I think another, perhaps better option would to not call So something like: |
||
| end) | ||
| |> then(fn socket -> | ||
| client_select(socket, %{ | ||
| input_event: true, | ||
| current_text: new_current_text_after_selection(socket) | ||
| }) | ||
| end) | ||
| |> scroll_to_active_option() | ||
| else | ||
| socket | ||
| end | ||
|
|
@@ -396,6 +415,7 @@ defmodule LiveSelect.Component do | |
| :clear_button, | ||
| :hide_dropdown, | ||
| :value_mapper, | ||
| :update_selection, | ||
| # for backwards compatibility | ||
| :form | ||
| ] | ||
|
|
@@ -565,6 +585,26 @@ defmodule LiveSelect.Component do | |
|
|
||
| defp update_selection(nil, _current_selection, _options, _mode, _value_mapper), do: [] | ||
|
|
||
| defp update_selection(update_fn, current_selection, options, _mode, value_mapper) | ||
| when is_function(update_fn, 1) do | ||
| new_selection = update_fn.(current_selection) | ||
|
|
||
| {existing, new} = Enum.split_with(new_selection, &(&1 in current_selection)) | ||
|
|
||
| new = | ||
| Enum.map(new, &normalize_selection_value(&1, options, value_mapper)) | ||
| |> Enum.reject(&is_nil/1) | ||
|
|
||
| Enum.uniq(existing ++ new) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This forces uniqueness. Otherwise appending would allow duplicate values and I couldn't think of a reason to allow it.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove this new function, see comment above
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trying the approach on the comment made the duplicate entry test fail, so far this works. |
||
| end | ||
|
|
||
| defp update_selection(update_fn, _current_selection, _options, _mode, _value_mapper) | ||
| when is_function(update_fn) do | ||
| raise """ | ||
| Option for `:update_selection` must be a function with arity 1 | ||
| """ | ||
| end | ||
|
|
||
| defp update_selection(value, current_selection, options, :single, value_mapper) do | ||
| List.wrap(normalize_selection_value(value, options ++ current_selection, value_mapper)) | ||
| end | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.