Add Backpex.Fields.CheckboxGroup#1089
Closed
atasoya wants to merge 2 commits intonaymspace:developfrom
Closed
Conversation
pehbehbeh
requested changes
May 5, 2025
Comment on lines
+202
to
+218
| <script> | ||
| window.addEventListener('phx:update', () => { | ||
| if (!window.CheckboxGroupHook) { | ||
| window.CheckboxGroupHook = { | ||
| mounted() { | ||
| this.handleEvent(`checklist:${this.el.dataset.field}:changed`, ({values}) => { | ||
| // Update the actual checkboxes | ||
| const checkboxes = this.el.querySelectorAll('input[type="checkbox"]'); | ||
| checkboxes.forEach(checkbox => { | ||
| checkbox.checked = values.includes(checkbox.value); | ||
| }); | ||
| }); | ||
| } | ||
| }; | ||
| } | ||
| }); | ||
| </script> |
| ], | ||
| columns: [ | ||
| doc: "Number of columns to display checkboxes (1-4). Defaults to 2.", | ||
| type: :integer, |
Member
There was a problem hiding this comment.
This might be a bit clearer:
Suggested change
| type: :integer, | |
| type: {:in, 1..4}, |
Not tested.
| required: true | ||
| ], | ||
| columns: [ | ||
| doc: "Number of columns to display checkboxes (1-4). Defaults to 2.", |
Member
There was a problem hiding this comment.
- default is automatically documented
- in case we use a range below, it should also be documented already (not tested)
Suggested change
| doc: "Number of columns to display checkboxes (1-4). Defaults to 2.", | |
| doc: "Number of columns to display checkboxes.", |
| columns: [ | ||
| doc: "Number of columns to display checkboxes (1-4). Defaults to 2.", | ||
| type: :integer, | ||
| default: 2 |
Member
There was a problem hiding this comment.
| 4 -> "grid-cols-4" | ||
| end | ||
| end | ||
| defp get_column_class(_), do: "" |
Member
There was a problem hiding this comment.
I would let it fail here because it is not supported anyway.
Suggested change
| defp get_column_class(_), do: "" |
| end | ||
| """ | ||
| use Backpex.Field, config_schema: @config_schema | ||
| alias Backpex.HTML.Form |
Member
There was a problem hiding this comment.
app-1 | warning: unused alias Form
app-1 | │
app-1 | 54 │ alias Backpex.HTML.Form
app-1 | │ ~
app-1 | │
app-1 | └─ lib/backpex/fields/checkbox_group.ex:54:3
Comment on lines
+167
to
+169
| assigns = assigns | ||
| |> Phoenix.Component.assign(:column_class, column_class) | ||
| |> Phoenix.Component.assign(:selected_values, selected_values) |
Member
There was a problem hiding this comment.
This file seems to be unformatted.
| end | ||
|
|
||
| @impl Backpex.Field | ||
| def render_index_form(assigns), do: render_form(assigns) |
Member
Comment on lines
+228
to
+254
| @impl Backpex.Field | ||
| def render_form_readonly(assigns), do: render_value(assigns) | ||
|
|
||
| @impl Backpex.Field | ||
| def display_field({name, _field_options}), do: name | ||
|
|
||
| @impl Backpex.Field | ||
| def schema(_field, schema), do: schema | ||
|
|
||
| @impl Backpex.Field | ||
| def association?(_field), do: false | ||
|
|
||
| @impl Backpex.Field | ||
| def assign_uploads(_field, socket), do: socket | ||
|
|
||
| @impl Backpex.Field | ||
| def before_changeset(changeset, _attrs, _metadata, _repo, _field, _assigns), do: changeset | ||
|
|
||
| @impl Backpex.Field | ||
| def search_condition(schema_name, field_name, search_string) do | ||
| import Ecto.Query | ||
|
|
||
| dynamic( | ||
| [{^schema_name, schema_name}], | ||
| ilike(fragment("CAST(? AS TEXT)", field(schema_name, ^field_name)), ^search_string) | ||
| ) | ||
| end |
Member
There was a problem hiding this comment.
Defaults can be removed.
Suggested change
| @impl Backpex.Field | |
| def render_form_readonly(assigns), do: render_value(assigns) | |
| @impl Backpex.Field | |
| def display_field({name, _field_options}), do: name | |
| @impl Backpex.Field | |
| def schema(_field, schema), do: schema | |
| @impl Backpex.Field | |
| def association?(_field), do: false | |
| @impl Backpex.Field | |
| def assign_uploads(_field, socket), do: socket | |
| @impl Backpex.Field | |
| def before_changeset(changeset, _attrs, _metadata, _repo, _field, _assigns), do: changeset | |
| @impl Backpex.Field | |
| def search_condition(schema_name, field_name, search_string) do | |
| import Ecto.Query | |
| dynamic( | |
| [{^schema_name, schema_name}], | |
| ilike(fragment("CAST(? AS TEXT)", field(schema_name, ^field_name)), ^search_string) | |
| ) | |
| end |
| |> push_event("checklist:#{name}:changed", %{values: new_selected_values}) | ||
| |> noreply() | ||
| end | ||
| end |
Member
There was a problem hiding this comment.
I dont't really like how much code is redundant to Backpex.Fields.MultiSelect. I think we should implement a way to reuse code in both modules.
Backpex.Fields.CheckboxGroup
Collaborator
|
closed in favor of #1691 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Added
Backpex.Fields.CheckboxGroupthat is 100% compatible with existingBackpex.Fields.MultiSelect. It also has a 'columns' parameter to render checkboxes in the specified number of columns. Also added to the demo app underuser_live.exfor 'Permissions'Resolves #983