Current section
48 Versions
Jump to
Current section
48 Versions
Compare versions
17
files changed
+316
additions
-95
deletions
| @@ -1,5 +1,16 @@ | |
| 1 1 | # Changelog |
| 2 2 | |
| 3 | + ## 0.2.3 |
| 4 | + |
| 5 | + - Clarified `layout` attributes to change the rendered order of slots. |
| 6 | + - Improve field name and id generation. |
| 7 | + - Use checkboxes and radio buttons in action lists. |
| 8 | + |
| 9 | + Updated component: |
| 10 | + - `checkbox`: |
| 11 | + - Added attr `is_multiple`: When creating a list of checkboxes. Appends `[]` to the input name so that a list of values is passed to the form events. |
| 12 | + - Added attr `is_omit_label`: Omits any label. |
| 13 | + |
| 3 14 | ## 0.2.2 |
| 4 15 | |
| 5 16 | Updated component: |
| @@ -27,7 +27,7 @@ | |
| 27 27 | {<<"name">>,<<"ecto_sql">>}, |
| 28 28 | {<<"optional">>,false}, |
| 29 29 | {<<"repository">>,<<"hexpm">>}, |
| 30 | - {<<"requirement">>,<<"~> 3.6">>}], |
| 30 | + {<<"requirement">>,<<"~> 3.9">>}], |
| 31 31 | [{<<"app">>,<<"phoenix_html">>}, |
| 32 32 | {<<"name">>,<<"phoenix_html">>}, |
| 33 33 | {<<"optional">>,false}, |
| @@ -37,10 +37,10 @@ | |
| 37 37 | {<<"name">>,<<"phoenix_live_view">>}, |
| 38 38 | {<<"optional">>,false}, |
| 39 39 | {<<"repository">>,<<"hexpm">>}, |
| 40 | - {<<"requirement">>,<<"~> 0.18.3">>}], |
| 40 | + {<<"requirement">>,<<">= 0.18.3">>}], |
| 41 41 | [{<<"app">>,<<"jason">>}, |
| 42 42 | {<<"name">>,<<"jason">>}, |
| 43 43 | {<<"optional">>,false}, |
| 44 44 | {<<"repository">>,<<"hexpm">>}, |
| 45 | - {<<"requirement">>,<<"~> 1.0">>}]]}. |
| 46 | - {<<"version">>,<<"0.2.2">>}. |
| 45 | + {<<"requirement">>,<<"~> 1.4">>}]]}. |
| 46 | + {<<"version">>,<<"0.2.3">>}. |
unknownlib/component.ex
File is too large to be displayed (100 KB limit).
| @@ -350,22 +350,170 @@ defmodule PrimerLive.Helpers.AttributeHelpers do | |
| 350 350 | String.match?(url, ~r/^#/) |
| 351 351 | end |
| 352 352 | |
| 353 | + @doc """ |
| 354 | + Generates the input name of a form field, using `Phoenix.HTML.Form.input_name/2` with support for multiple inputs (e.g. checkboxes). |
| 355 | + Param opts accepts |
| 356 | + - name: the field name |
| 357 | + - is_multiple |
| 358 | + |
| 359 | + ## Tests |
| 360 | + |
| 361 | + iex> PrimerLive.Helpers.AttributeHelpers.input_name(nil, nil) |
| 362 | + nil |
| 363 | + |
| 364 | + iex> PrimerLive.Helpers.AttributeHelpers.input_name("my-form", nil) |
| 365 | + "my-form" |
| 366 | + |
| 367 | + iex> PrimerLive.Helpers.AttributeHelpers.input_name("my-form", "field") |
| 368 | + "my-form[field]" |
| 369 | + |
| 370 | + iex> PrimerLive.Helpers.AttributeHelpers.input_name(:form, nil) |
| 371 | + "form" |
| 372 | + |
| 373 | + iex> PrimerLive.Helpers.AttributeHelpers.input_name(nil, "my-field") |
| 374 | + "[my-field]" |
| 375 | + |
| 376 | + iex> PrimerLive.Helpers.AttributeHelpers.input_name(nil, :first_name) |
| 377 | + "[first_name]" |
| 378 | + |
| 379 | + iex> PrimerLive.Helpers.AttributeHelpers.input_name(:profile, :first_name) |
| 380 | + "profile[first_name]" |
| 381 | + |
| 382 | + iex> PrimerLive.Helpers.AttributeHelpers.input_name(nil, nil, name: "my-first-name") |
| 383 | + "my-first-name" |
| 384 | + |
| 385 | + iex> PrimerLive.Helpers.AttributeHelpers.input_name(:profile, nil, name: "my-first-name") |
| 386 | + "profile[my-first-name]" |
| 387 | + |
| 388 | + iex> PrimerLive.Helpers.AttributeHelpers.input_name(:profile, :first_name, name: "my-first-name") |
| 389 | + "profile[my-first-name]" |
| 390 | + |
| 391 | + iex> PrimerLive.Helpers.AttributeHelpers.input_name(:profile, :first_name, is_multiple: true) |
| 392 | + "profile[first_name][]" |
| 393 | + """ |
| 394 | + |
| 395 | + def input_name(form, field, opts \\ []) do |
| 396 | + _input_name(form, field, opts[:name], !!opts[:is_multiple]) |
| 397 | + end |
| 398 | + |
| 399 | + defp _input_name(form, field, name, _is_multiple) when is_nil(form) and is_nil(field), |
| 400 | + do: name |
| 401 | + |
| 402 | + defp _input_name(form, field, name, is_multiple) do |
| 403 | + single_input_name = |
| 404 | + Phoenix.HTML.Form.input_name(form, name || field || "") |
| 405 | + |> String.replace(~r/(\[\])+$/, "") |
| 406 | + |
| 407 | + cond do |
| 408 | + is_multiple && single_input_name !== "" -> single_input_name <> "[]" |
| 409 | + single_input_name !== "" -> single_input_name |
| 410 | + true -> nil |
| 411 | + end |
| 412 | + end |
| 413 | + |
| 414 | + @doc """ |
| 415 | + Generates the input id of a form field. |
| 416 | + |
| 417 | + ## Tests |
| 418 | + |
| 419 | + iex> PrimerLive.Helpers.AttributeHelpers.input_id("my-input-id", nil, nil, "", nil) |
| 420 | + "my-input-id" |
| 421 | + |
| 422 | + iex> PrimerLive.Helpers.AttributeHelpers.input_id(nil, "my-id", nil, "", nil) |
| 423 | + "my-id" |
| 424 | + |
| 425 | + iex> PrimerLive.Helpers.AttributeHelpers.input_id(nil, nil, :select, "", nil) |
| 426 | + nil |
| 427 | + |
| 428 | + iex> PrimerLive.Helpers.AttributeHelpers.input_id(nil, nil, :select, "[my-input-name]", nil) |
| 429 | + "[my-input-name]" |
| 430 | + |
| 431 | + iex> PrimerLive.Helpers.AttributeHelpers.input_id(nil, nil, :select, "[my-input-name][]", nil) |
| 432 | + "[my-input-name]" |
| 433 | + |
| 434 | + iex> PrimerLive.Helpers.AttributeHelpers.input_id(nil, nil, :checkbox, "", nil) |
| 435 | + nil |
| 436 | + |
| 437 | + iex> PrimerLive.Helpers.AttributeHelpers.input_id(nil, nil, :checkbox, "my-input-name", nil) |
| 438 | + "my-input-name" |
| 439 | + |
| 440 | + iex> PrimerLive.Helpers.AttributeHelpers.input_id(nil, nil, :checkbox, "my-input-name", "my-value") |
| 441 | + "my-input-name[my-value]" |
| 442 | + |
| 443 | + iex> PrimerLive.Helpers.AttributeHelpers.input_id(nil, nil, :checkbox, "[my-input-name]", "my-value") |
| 444 | + "[my-input-name][my-value]" |
| 445 | + |
| 446 | + iex> PrimerLive.Helpers.AttributeHelpers.input_id(nil, nil, :checkbox, "[my-input-name][]", "my-value") |
| 447 | + "[my-input-name][my-value]" |
| 448 | + |
| 449 | + iex> PrimerLive.Helpers.AttributeHelpers.input_id(nil, nil, :checkbox, "[my-input-name][]", nil) |
| 450 | + "[my-input-name][]" |
| 451 | + |
| 452 | + iex> PrimerLive.Helpers.AttributeHelpers.input_id(nil, nil, :radio_button, "", nil) |
| 453 | + nil |
| 454 | + |
| 455 | + iex> PrimerLive.Helpers.AttributeHelpers.input_id(nil, nil, :radio_button, "my-input-name", nil) |
| 456 | + "my-input-name" |
| 457 | + |
| 458 | + iex> PrimerLive.Helpers.AttributeHelpers.input_id(nil, nil, :radio_button, "my-input-name", "my-value") |
| 459 | + "my-input-name[my-value]" |
| 460 | + |
| 461 | + """ |
| 462 | + def input_id(input_id, _id, _input_type, _input_name, _value_for_derived_label) |
| 463 | + when not is_nil(input_id), |
| 464 | + do: input_id |
| 465 | + |
| 466 | + def input_id(_input_id, id, _input_type, _input_name, _value_for_derived_label) |
| 467 | + when not is_nil(id), |
| 468 | + do: id |
| 469 | + |
| 470 | + def input_id(_input_id, _id, input_type, "", _value_for_derived_label) |
| 471 | + when input_type === :select, |
| 472 | + do: nil |
| 473 | + |
| 474 | + def input_id(_input_id, _id, input_type, input_name, _value_for_derived_label) |
| 475 | + when input_type === :select, |
| 476 | + do: input_name |> String.replace(~r/\[\]$/, "") |
| 477 | + |
| 478 | + def input_id(_input_id, _id, input_type, "", value_for_derived_label) |
| 479 | + when input_type === :checkbox or input_type === :radio_button, |
| 480 | + do: value_for_derived_label |
| 481 | + |
| 482 | + def input_id(_input_id, _id, input_type, input_name, value_for_derived_label) |
| 483 | + when (input_type === :checkbox or input_type === :radio_button) and is_binary(input_name) do |
| 484 | + cond do |
| 485 | + String.match?(input_name, ~r/\[\]$/) -> |
| 486 | + String.replace(input_name, ~r/(\[\]$)/, "[#{value_for_derived_label}]") |
| 487 | + |
| 488 | + !is_nil(value_for_derived_label) -> |
| 489 | + "#{input_name}[#{value_for_derived_label}]" |
| 490 | + |
| 491 | + true -> |
| 492 | + input_name |
| 493 | + end |
| 494 | + end |
| 495 | + |
| 496 | + def input_id(_input_id, _id, _input_type, "", _value_for_derived_label), do: nil |
| 497 | + def input_id(_input_id, _id, _input_type, input_name, _value_for_derived_label), do: input_name |
| 498 | + |
| 353 499 | @doc ~S""" |
| 354 500 | Extracts common attributes for form inputs. Shared for consistency. |
| 355 501 | """ |
| 356 502 | def common_input_attrs(assigns, input_type) do |
| 357 503 | rest = assigns[:rest] |
| 358 504 | name = rest[:name] |
| 505 | + id = rest[:id] |
| 359 506 | form = assigns[:form] |
| 360 507 | field = assigns[:field] |
| 361 | - field_or_name = field || name |
| 362 | - phx_feedback_for_id = Phoenix.HTML.Form.input_name(form, field) |
| 508 | + field_or_name = field || name || "" |
| 509 | + is_multiple = !!assigns[:is_multiple] |
| 363 510 | |
| 364 | - # ID |
| 511 | + input_name = input_name(form, field, name: name, is_multiple: is_multiple) |
| 512 | + |
| 513 | + phx_feedback_for_id = input_name |
| 365 514 | |
| 366 515 | # Get value from checkbox or radio button |
| 367 516 | |
| 368 | - id = rest[:id] |
| 369 517 | value = assigns[:value] || rest[:value] |
| 370 518 | checked_value = rest[:checked_value] |
| 371 519 | value_for_derived_label = checked_value || value |
| @@ -377,21 +525,7 @@ defmodule PrimerLive.Helpers.AttributeHelpers do | |
| 377 525 | _ -> nil |
| 378 526 | end |
| 379 527 | |
| 380 | - input_id = |
| 381 | - cond do |
| 382 | - assigns[:input_id] -> |
| 383 | - assigns[:input_id] |
| 384 | - |
| 385 | - id -> |
| 386 | - id |
| 387 | - |
| 388 | - input_type === :radio_button || input_type === :checkbox -> |
| 389 | - Phoenix.HTML.Form.input_id(form, field_or_name, value_for_derived_label) |
| 390 | - |> String.replace("__", "_") |
| 391 | - |
| 392 | - true -> |
| 393 | - Phoenix.HTML.Form.input_id(form, field_or_name) |
| 394 | - end |
| 528 | + input_id = input_id(assigns[:input_id], id, input_type, input_name, value_for_derived_label) |
| 395 529 | |
| 396 530 | # Form group |
| 397 531 | |
| @@ -446,6 +580,7 @@ defmodule PrimerLive.Helpers.AttributeHelpers do | |
| 446 580 | form: form, |
| 447 581 | field: field, |
| 448 582 | # ID and label |
| 583 | + input_name: input_name, |
| 449 584 | input_id: input_id, |
| 450 585 | phx_feedback_for_id: phx_feedback_for_id, |
| 451 586 | value: value, |
| @@ -133,7 +133,7 @@ defmodule PrimerLive.Helpers.FormHelpers do | |
| 133 133 | end |
| 134 134 | |
| 135 135 | @doc """ |
| 136 | - Returns all error for a given field from a changeset. |
| 136 | + Returns all errors for a given field from a changeset. |
| 137 137 | |
| 138 138 | iex> PrimerLive.Helpers.FormHelpers.get_field_errors( |
| 139 139 | ...> %{ |
Loading more files…