-
Notifications
You must be signed in to change notification settings - Fork 64
Add Backpex.Fields.CheckboxGroup
#1089
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
Add Backpex.Fields.CheckboxGroup
#1089
Conversation
| <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> |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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:
| type: :integer, | |
| type: {:in, 1..4}, |
Not tested.
| required: true | ||
| ], | ||
| columns: [ | ||
| doc: "Number of columns to display checkboxes (1-4). Defaults to 2.", |
There was a problem hiding this comment.
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)
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 4 -> "grid-cols-4" | ||
| end | ||
| end | ||
| defp get_column_class(_), do: "" |
There was a problem hiding this comment.
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.
| defp get_column_class(_), do: "" |
| end | ||
| """ | ||
| use Backpex.Field, config_schema: @config_schema | ||
| alias Backpex.HTML.Form |
There was a problem hiding this comment.
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
| assigns = assigns | ||
| |> Phoenix.Component.assign(:column_class, column_class) | ||
| |> Phoenix.Component.assign(:selected_values, selected_values) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| @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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defaults can be removed.
| @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 |
There was a problem hiding this comment.
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.
Backpex.Fields.CheckboxGroup
|
closed in favor of #1691 |

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