-
Notifications
You must be signed in to change notification settings - Fork 73
Add Backpex.Fields.Checkgroup
#1691
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
Open
Flo0807
wants to merge
11
commits into
develop
Choose a base branch
from
feature/add-checkgroup-field
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
95c2d20
Add checkgroup field
Flo0807 bded7fe
Merge branch 'develop' into feature/add-checkgroup-field
Flo0807 ac76ebe
Remove redundant Backpex.HTML alias in Checkgroup
Flo0807 e494a60
Fix type coercion in Checkgroup checked= comparison
Flo0807 7ed8c65
Use semantic fieldset/legend for Checkgroup and fix group ARIA
Flo0807 245c588
Wire aria-invalid and aria-describedby for Checkgroup errors
Flo0807 13404ee
Support readonly on Checkgroup field
Flo0807 5eec3b9
Document default: [] requirement in Checkgroup moduledoc
Flo0807 c6bf8d2
Add Checkgroup to fields guide
Flo0807 91b73ed
Tighten vertical rhythm between Checkgroup options
Flo0807 9c8cedf
Scope Checkgroup touch-target min-height to mobile
Flo0807 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
15 changes: 15 additions & 0 deletions
15
demo/priv/repo/migrations/20251205230743_set_default_for_permissions.exs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| defmodule Demo.Repo.Migrations.SetDefaultForPermissions do | ||
| use Ecto.Migration | ||
|
|
||
| def up do | ||
| # Set default for new records | ||
| execute "ALTER TABLE users ALTER COLUMN permissions SET DEFAULT '{}'::text[]" | ||
|
|
||
| # Update existing NULL values to empty array | ||
| execute "UPDATE users SET permissions = '{}' WHERE permissions IS NULL" | ||
| end | ||
|
|
||
| def down do | ||
| execute "ALTER TABLE users ALTER COLUMN permissions DROP DEFAULT" | ||
| end | ||
| end | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| defmodule Backpex.Fields.Checkgroup do | ||
| @config_schema [ | ||
| options: [ | ||
| doc: "List of options or function that receives the assigns.", | ||
| type: {:or, [{:list, :any}, {:fun, 1}]}, | ||
| required: true | ||
| ], | ||
| readonly: [ | ||
| doc: "Sets the field to readonly. Also see the [panels](/guides/fields/readonly.md) guide.", | ||
| type: {:or, [:boolean, {:fun, 1}]} | ||
| ] | ||
| ] | ||
|
|
||
| @moduledoc """ | ||
| A field for handling multiple checkboxes with predefined options. | ||
|
|
||
| This field stores selected values as an array. | ||
|
|
||
| > #### Schema and migration defaults {: .warning} | ||
| > | ||
| > You **must** declare `default: []` on the `{:array, _}` schema field *and* set a matching | ||
| > SQL default (`DEFAULT '{}'::text[]` in PostgreSQL). Otherwise unchecking every box will | ||
| > persist `NULL` instead of an empty array. The reason is Ecto's `filter_empty_values/3`: | ||
| > the hidden sentinel input submits the scalar `""`, which Ecto treats as empty and replaces | ||
| > with the schema default. | ||
| > | ||
| > # Ecto schema | ||
| > field :roles, {:array, :string}, default: [] | ||
| > | ||
| > # Migration | ||
| > alter table(:users) do | ||
| > modify :roles, {:array, :string}, default: [] | ||
| > end | ||
|
|
||
| ## Field-specific options | ||
|
|
||
| See `Backpex.Field` for general field options. | ||
|
|
||
| #{NimbleOptions.docs(@config_schema)} | ||
|
|
||
| ## Example | ||
|
|
||
| @impl Backpex.LiveResource | ||
| def fields do | ||
| [ | ||
| roles: %{ | ||
| module: Backpex.Fields.Checkgroup, | ||
| label: "Roles", | ||
| options: [{"Admin", "admin"}, {"User", "user"}, {"Editor", "editor"}] | ||
| } | ||
| ] | ||
| end | ||
| """ | ||
| use Backpex.Field, config_schema: @config_schema | ||
|
|
||
| @impl Backpex.Field | ||
| def render_value(assigns) do | ||
| options = get_options(assigns) | ||
| labels = get_labels(assigns.value, options) | ||
|
|
||
| assigns = assign(assigns, :labels, labels) | ||
|
|
||
| ~H""" | ||
| <p class={@live_action in [:index, :resource_action] && "truncate"}> | ||
| {if @labels == [], do: raw("—"), else: @labels |> Enum.map(&HTML.pretty_value/1) |> Enum.join(", ")} | ||
| </p> | ||
| """ | ||
| end | ||
|
|
||
| @impl Backpex.Field | ||
| def render_form(assigns) do | ||
| options = get_options(assigns) | ||
|
|
||
| assigns = assign(assigns, :options, options) | ||
|
|
||
| ~H""" | ||
| <div> | ||
| <Layout.field_container> | ||
| <:label :if={not @hide_label} align={Backpex.Field.align_label(@field_options, assigns)}> | ||
| <Layout.input_label as="span" text={@field_options[:label]} /> | ||
| </:label> | ||
| <BackpexForm.input | ||
| type="checkgroup" | ||
| field={@form[@name]} | ||
| options={@options} | ||
| translate_error_fun={Backpex.Field.translate_error_fun(@field_options, assigns)} | ||
| help_text={Backpex.Field.help_text(@field_options, assigns)} | ||
| readonly={@readonly} | ||
| /> | ||
| </Layout.field_container> | ||
| </div> | ||
| """ | ||
| end | ||
|
|
||
| defp get_labels(value, options) do | ||
| values = List.wrap(value) |> Enum.map(&to_string/1) | ||
|
|
||
| options | ||
| |> Enum.filter(fn {_label, option_value} -> to_string(option_value) in values end) | ||
| |> Enum.map(fn {label, _value} -> label end) | ||
| end | ||
|
|
||
| defp get_options(assigns) do | ||
| case Map.get(assigns.field_options, :options) do | ||
| options when is_function(options) -> options.(assigns) | ||
| options -> options | ||
| end | ||
| end | ||
| end |
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
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.
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.
Are we using that?