maxmarcon / live_select

Dynamic (multi)selection field for LiveView
https://hex.pm/packages/live_select
Apache License 2.0
173 stars 32 forks source link

Add `update_selection/1` to dynamically update (append, filter etc) the selection #68

Open shamanime opened 3 months ago

shamanime commented 3 months ago
          Thanks for the explanation. I'm starting to see the value in this approach.

Instead of using 2 new options, one to pass a filter and the other to append to the selection, my suggestion would be to generalize the approach by passing an update_selection/1 function that receives the whole selection and returns the new one:

append case:

values_to_append = [1, 2, 3]

send_update(LiveSelect.Component, id: live_select_id, update_selection: fn selection -> selection ++ values_to_append end)

filter case:

send_update(LiveSelect.Component, id: live_select_id, update_selection: fn selection -> Enum.filter(selection, & &1 > 2)
end)

2 birds with a stone. And this general approach will hopefully accommodate future use cases.

What do you think?

EDIT: this would also make sense in single mode

_Originally posted by @maxmarcon in https://github.com/maxmarcon/live_select/issues/64#issuecomment-2156743839_

shamanime commented 3 months ago

@maxmarcon would you please assign me on this