Skip to content

Conversation

@atasoya
Copy link

@atasoya atasoya commented Apr 18, 2025

Added Backpex.Fields.CheckboxGroup that is 100% compatible with existing Backpex.Fields.MultiSelect. It also has a 'columns' parameter to render checkboxes in the specified number of columns. Also added to the demo app under user_live.ex for 'Permissions'

Resolves #983

Screenshot 2025-04-18 at 20 41 32

@pehbehbeh pehbehbeh self-requested a review April 29, 2025 08:09
@pehbehbeh pehbehbeh added the feature New feature label Apr 29, 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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this to a hook?

],
columns: [
doc: "Number of columns to display checkboxes (1-4). Defaults to 2.",
type: :integer,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe 1 is even the better default value?

@Flo0807 WDYT?

Suggested change
default: 2
default: 1

4 -> "grid-cols-4"
end
end
defp get_column_class(_), do: ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file seems to be unformatted.

end

@impl Backpex.Field
def render_index_form(assigns), do: render_form(assigns)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should allow that.

Bildschirmfoto 2025-05-05 um 16 51 32

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Flo0807 Flo0807 changed the title Feature/backpex fields checkbox group Add Backpex.Fields.CheckboxGroup Aug 8, 2025
@Flo0807
Copy link
Collaborator

Flo0807 commented Dec 5, 2025

closed in favor of #1691

@Flo0807 Flo0807 closed this Dec 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Backpex.Fields.CheckboxGroup

3 participants