Skip to content

Commit

Permalink
Pl-134/bug/required marker (#135)
Browse files Browse the repository at this point in the history
* Read the required state to evaluate showing the required marker

* Update tests

* Update versions

* Fix doc typo

* Update version
  • Loading branch information
ArthurClemens authored Oct 7, 2023
1 parent 6e93363 commit 716d3fc
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 42 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.5.4

Fixed a bug where the required marker would always be displayed, regardless of the field's required state.

## 0.5.3

Downgraded `phoenix_live_view` version to `0.19`; both `0.19` and `0.20` should be compatible.
Expand Down
2 changes: 1 addition & 1 deletion assets/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "primer-live",
"version": "0.5.3",
"version": "0.5.4",
"description": "JavaScript and CSS for PrimerLive",
"license": "MIT",
"repository": {},
Expand Down
21 changes: 9 additions & 12 deletions lib/component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2906,7 +2906,8 @@ defmodule PrimerLive.Component do
form: form,
field: field,
validation_marker_class: validation_marker_class,
caption: caption
caption: caption,
required?: required?
} = AttributeHelpers.common_input_attrs(assigns)

classes = %{
Expand Down Expand Up @@ -2973,7 +2974,9 @@ defmodule PrimerLive.Component do
end

has_header_label = label && label !== "Nil"
has_required_marker = !is_nil(assigns.required_marker) && assigns.required_marker !== ""

show_required_marker =
required? && !is_nil(assigns.required_marker) && assigns.required_marker !== ""

control_attributes =
AttributeHelpers.append_attributes(rest, [
Expand All @@ -2985,7 +2988,7 @@ defmodule PrimerLive.Component do
|> assign(:classes, classes)
|> assign(:control_attributes, control_attributes)
|> assign(:has_header_label, has_header_label)
|> assign(:has_required_marker, has_required_marker)
|> assign(:show_required_marker, show_required_marker)
|> assign(:label, label)
|> assign(:caption, caption)

Expand All @@ -2994,7 +2997,7 @@ defmodule PrimerLive.Component do
<%= if @has_header_label do %>
<div class={@classes.header}>
<%= @label %>
<%= if @has_required_marker do %>
<%= if @show_required_marker do %>
<span aria-hidden="true"><%= @required_marker %></span>
<% end %>
</div>
Expand Down Expand Up @@ -7152,14 +7155,9 @@ defmodule PrimerLive.Component do
PromptDeclarationHelpers.prompt_options()
PromptDeclarationHelpers.phx_click_touch()
PromptDeclarationHelpers.toggle_slot("the select menu component")

attr(:is_aligned_end, :boolean,
default: false,
doc: "Aligns the menu to the end (at the right in left-to-right langages)."
)
DeclarationHelpers.is_aligned_end("the menu")

attr(:is_right_aligned, :boolean, doc: "Deprecated: use `is_aligned_end`. Since 0.5.1.")

attr(:is_borderless, :boolean, default: false, doc: "Removes the borders between list items.")

DeclarationHelpers.class()
Expand Down Expand Up @@ -7791,8 +7789,7 @@ defmodule PrimerLive.Component do
PromptDeclarationHelpers.prompt_options()
PromptDeclarationHelpers.phx_click_touch()
PromptDeclarationHelpers.toggle_slot("the menu component")

attr(:is_aligned_end, :boolean, default: false, doc: "Aligns the menu to the right.")
DeclarationHelpers.is_aligned_end("the menu")

attr(:is_right_aligned, :boolean, doc: "Deprecated: use `is_aligned_end`. Since 0.5.1.")

Expand Down
2 changes: 2 additions & 0 deletions lib/helpers/attribute_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ defmodule PrimerLive.Helpers.AttributeHelpers do
%{
message: message,
valid?: valid?,
required?: required?,
ignore_errors?: ignore_errors?,
caption: caption
} = field_state
Expand Down Expand Up @@ -736,6 +737,7 @@ defmodule PrimerLive.Helpers.AttributeHelpers do
%{
message: message,
valid?: valid?,
required?: required?,
ignore_errors?: ignore_errors?,
show_message?: show_message?,
validation_message_id: validation_message_id,
Expand Down
13 changes: 13 additions & 0 deletions lib/helpers/declaration_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ defmodule PrimerLive.Helpers.DeclarationHelpers do
end
end

defmacro is_aligned_end(the_element) do
quote do
attr(:is_aligned_end, :boolean,
default: false,
doc:
"""
Aligns {the_element} to the end (at the right in left-to-right languages).
"""
|> String.replace("{the_element}", unquote(the_element))
)
end
end

# link attr

defmacro href do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule PrimerLive.MixProject do
def project do
[
app: :primer_live,
version: "0.5.3",
version: "0.5.4",
homepage_url: "https://github.com/ArthurClemens/primer_live",
description: description(),
package: package(),
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "primer-live",
"version": "0.5.3",
"version": "0.5.4",
"description": "JavaScript and CSS for PrimerLive",
"license": "MIT",
"module": "./priv/static/primer-live.esm.js",
Expand Down
6 changes: 3 additions & 3 deletions test/components/checkbox_group_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ defmodule PrimerLive.TestComponents.CheckboxGroupTest do
"""
<form>
<div class="FormControl pl-FormControl--input-group">
<div class="form-group-header"><label class="FormControl-label">Role</label><span aria-hidden="true">*</span></div>
<div class="form-group-header"><label class="FormControl-label">Role</label></div>
<div class="FormControl-caption">Select one</div>
<div class="pl-FormControl--input-group__container"><span class="FormControl-checkbox-wrap"><input
name="role-caption[]" type="hidden" value="false" /><input class="FormControl-checkbox"
Expand Down Expand Up @@ -124,7 +124,7 @@ defmodule PrimerLive.TestComponents.CheckboxGroupTest do
"""
<form method="post">
<div class="FormControl pl-FormControl--input-group pl-invalid">
<div class="form-group-header"><label class="FormControl-label">Statuses</label><span aria-hidden="true">*</span>
<div class="form-group-header"><label class="FormControl-label">Statuses</label>
</div>
<div class="pl-FormControl--input-group__container"><span class="FormControl-checkbox-wrap pl-invalid"><input
name="todo[statuses][]" type="hidden" value="false" /><input class="FormControl-checkbox"
Expand Down Expand Up @@ -177,7 +177,7 @@ defmodule PrimerLive.TestComponents.CheckboxGroupTest do
"""
<form method="post">
<div class="FormControl pl-FormControl--input-group pl-invalid">
<div class="form-group-header"><label class="FormControl-label">Statuses</label><span aria-hidden="true">*</span>
<div class="form-group-header"><label class="FormControl-label">Statuses</label>
</div>
<div class="pl-FormControl--input-group__container"><span class="FormControl-checkbox-wrap pl-invalid"><input
name="todo[statuses][]" type="hidden" value="false" /><input class="FormControl-checkbox"
Expand Down
6 changes: 3 additions & 3 deletions test/components/form_control_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ defmodule PrimerLive.TestComponents.FormControlTest do
|> format_html() ==
"""
<div class="FormControl">
<div class="form-group-header"><label class="FormControl-label">First name</label><span aria-hidden="true">*</span></div>inputs
<div class="form-group-header"><label class="FormControl-label">First name</label></div>inputs
</div>
"""
|> format_html()
Expand All @@ -67,7 +67,7 @@ defmodule PrimerLive.TestComponents.FormControlTest do
|> format_html() ==
"""
<div class="FormControl">
<div class="form-group-header"><label class="FormControl-label">First name</label><span aria-hidden="true">*</span></div>inputs
<div class="form-group-header"><label class="FormControl-label">First name</label></div>inputs
</div>
"""
|> format_html()
Expand Down Expand Up @@ -167,7 +167,7 @@ defmodule PrimerLive.TestComponents.FormControlTest do
|> format_html() ==
"""
<div class="FormControl my-form-control group-x control-x pl-neutral">
<div class="form-group-header header-x"><label class="FormControl-label label-x">First name</label><span aria-hidden="true">*</span></div>inputs
<div class="form-group-header header-x"><label class="FormControl-label label-x">First name</label></div>inputs
</div>
"""
|> format_html()
Expand Down
6 changes: 3 additions & 3 deletions test/components/form_group_deprecated_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule PrimerLive.TestComponents.FormGroupDeprecatedTest do
|> format_html() ==
"""
<div class="FormControl form-group">
<div class="form-group-header"><label class="FormControl-label">First name</label><span aria-hidden="true">*</span></div>inputs
<div class="form-group-header"><label class="FormControl-label">First name</label></div>inputs
</div>
"""
|> format_html()
Expand All @@ -57,7 +57,7 @@ defmodule PrimerLive.TestComponents.FormGroupDeprecatedTest do
|> format_html() ==
"""
<div class="FormControl form-group">
<div class="form-group-header"><label class="FormControl-label">First name</label><span aria-hidden="true">*</span></div>inputs
<div class="form-group-header"><label class="FormControl-label">First name</label></div>inputs
</div>
"""
|> format_html()
Expand Down Expand Up @@ -127,7 +127,7 @@ defmodule PrimerLive.TestComponents.FormGroupDeprecatedTest do
|> format_html() ==
"""
<div class="FormControl form-group my-form-group group-x control-x pl-neutral">
<div class="form-group-header header-x"><label class="FormControl-label label-x">First name</label><span aria-hidden="true">*</span></div>inputs
<div class="form-group-header header-x"><label class="FormControl-label label-x">First name</label></div>inputs
</div>
"""
|> format_html()
Expand Down
6 changes: 3 additions & 3 deletions test/components/radio_group_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ defmodule PrimerLive.TestComponents.RadioGroupTest do
"""
<form>
<div class="FormControl pl-FormControl--input-group">
<div class="form-group-header"><label class="FormControl-label">Role</label><span aria-hidden="true">*</span></div>
<div class="form-group-header"><label class="FormControl-label">Role</label></div>
<div class="FormControl-caption">Select one</div>
<div class="pl-FormControl--input-group__container"><span class="FormControl-radio-wrap"><input
class="FormControl-radio" id="role-caption_admin" name="role-caption" type="radio" value="admin" /><span
Expand Down Expand Up @@ -123,7 +123,7 @@ defmodule PrimerLive.TestComponents.RadioGroupTest do
"""
<form method="post">
<div class="FormControl pl-FormControl--input-group pl-invalid">
<div class="form-group-header"><label class="FormControl-label">Statuses</label><span aria-hidden="true">*</span>
<div class="form-group-header"><label class="FormControl-label">Statuses</label>
</div>
<div class="pl-FormControl--input-group__container"><span class="FormControl-radio-wrap pl-invalid"><input
class="FormControl-radio" id="in-progress-derived-label" name="todo[statuses]" type="radio"
Expand Down Expand Up @@ -174,7 +174,7 @@ defmodule PrimerLive.TestComponents.RadioGroupTest do
"""
<form method="post">
<div class="FormControl pl-FormControl--input-group pl-invalid">
<div class="form-group-header"><label class="FormControl-label">Statuses</label><span aria-hidden="true">*</span>
<div class="form-group-header"><label class="FormControl-label">Statuses</label>
</div>
<div class="pl-FormControl--input-group__container"><span class="FormControl-radio-wrap pl-invalid"><input
class="FormControl-radio" id="in-progress-custom-label" name="todo[statuses]" type="radio"
Expand Down
2 changes: 1 addition & 1 deletion test/components/select_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ defmodule PrimerLive.TestComponents.SelectTest do
</select></div>
<div class="FormControl-caption">Caption</div>
<div class="FormControl">
<div class="form-group-header"><label class="FormControl-label" for="count">Count</label><span aria-hidden="true">*</span></div>
<div class="form-group-header"><label class="FormControl-label" for="count">Count</label></div>
<div class="FormControl-select-wrap"><select class="FormControl-select FormControl-medium" id="count" name="count">
<option value="One">One</option>
<option value="Two">Two</option>
Expand Down
18 changes: 9 additions & 9 deletions test/components/text_input_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ defmodule PrimerLive.TestComponents.TextInputTest do
<input class="FormControl-input FormControl-medium" type="text" />
<div class="FormControl-caption">Caption</div>
<div class="FormControl">
<div class="form-group-header"><label class="FormControl-label"></label><span aria-hidden="true">*</span></div><input
<div class="form-group-header"><label class="FormControl-label"></label></div><input
class="FormControl-input FormControl-medium" type="text" />
<div class="FormControl-caption">Caption</div>
</div>
Expand All @@ -237,7 +237,7 @@ defmodule PrimerLive.TestComponents.TextInputTest do
|> format_html() ==
"""
<div class="FormControl">
<div class="form-group-header"><label class="FormControl-label" for="user_first_name">First name</label><span aria-hidden="true">*</span></div><input
<div class="form-group-header"><label class="FormControl-label" for="user_first_name">First name</label></div><input
class="FormControl-input FormControl-medium" id="user_first_name" name="user[first_name]" type="text" />
</div>
"""
Expand All @@ -253,7 +253,7 @@ defmodule PrimerLive.TestComponents.TextInputTest do
|> format_html() ==
"""
<div class="FormControl form-group">
<div class="form-group-header"><label class="FormControl-label" for="user_first_name">First name</label><span aria-hidden="true">*</span></div><input
<div class="form-group-header"><label class="FormControl-label" for="user_first_name">First name</label></div><input
class="FormControl-input FormControl-medium" id="user_first_name" name="user[first_name]" type="text" />
</div>
"""
Expand All @@ -269,7 +269,7 @@ defmodule PrimerLive.TestComponents.TextInputTest do
|> format_html() ==
"""
<div class="FormControl form-group">
<div class="form-group-header"><label class="FormControl-label" for="xyz">First name</label><span aria-hidden="true">*</span></div><input
<div class="form-group-header"><label class="FormControl-label" for="xyz">First name</label></div><input
class="FormControl-input FormControl-medium" id="xyz" name="user[first_name]" type="text" />
</div>
"""
Expand All @@ -293,7 +293,7 @@ defmodule PrimerLive.TestComponents.TextInputTest do
|> format_html() ==
"""
<div class="FormControl">
<div class="form-group-header"><label class="FormControl-label" for="user_first_name">Some label</label><span aria-hidden="true">*</span></div><input
<div class="form-group-header"><label class="FormControl-label" for="user_first_name">Some label</label></div><input
class="FormControl-input FormControl-medium" id="user_first_name" name="user[first_name]" type="text" />
</div>
"""
Expand All @@ -317,7 +317,7 @@ defmodule PrimerLive.TestComponents.TextInputTest do
|> format_html() ==
"""
<div class="FormControl form-group">
<div class="form-group-header"><label class="FormControl-label" for="user_first_name">Some label</label><span aria-hidden="true">*</span></div><input
<div class="form-group-header"><label class="FormControl-label" for="user_first_name">Some label</label></div><input
class="FormControl-input FormControl-medium" id="user_first_name" name="user[first_name]" type="text" />
</div>
"""
Expand Down Expand Up @@ -426,7 +426,7 @@ defmodule PrimerLive.TestComponents.TextInputTest do
|> format_html() ==
"""
<div class="FormControl pl-FormControl-disabled">
<div class="form-group-header"><label class="FormControl-label" for="first_name">First name</label><span aria-hidden="true">*</span></div><input
<div class="form-group-header"><label class="FormControl-label" for="first_name">First name</label></div><input
class="FormControl-input FormControl-medium" disabled id="first_name" name="first_name" type="text" />
</div>
"""
Expand Down Expand Up @@ -553,14 +553,14 @@ defmodule PrimerLive.TestComponents.TextInputTest do
<div class="FormControl-caption caption-x">Caption</div>
</div>
<div class="FormControl group-x control-x">
<div class="form-group-header header-x"><label class="FormControl-label label-x"></label><span aria-hidden="true">*</span></div>
<div class="form-group-header header-x"><label class="FormControl-label label-x"></label></div>
<div class="FormControl-input-wrap FormControl-input-wrap--leadingVisual input_wrap-x"><span
class="FormControl-input-leadingVisualWrap"><svg class="octicon" xmlns="http://www.w3.org/2000/svg" width="16"
height="16" viewBox="0 0 16 16">STRIPPED_SVG_PATHS</svg></span><input
class="FormControl-input FormControl-medium input-x my-text-input" type="text" /></div>
</div>
<div class="FormControl group-x control-x">
<div class="form-group-header header-x"><label class="FormControl-label label-x"></label><span aria-hidden="true">*</span></div>
<div class="form-group-header header-x"><label class="FormControl-label label-x"></label></div>
<div class="FormControl-input-wrap FormControl-input-wrap--trailingAction input_wrap-x"><input
class="FormControl-input FormControl-medium input-x my-text-input" type="text" /><span
class="FormControl-input-trailingAction"><button aria-label="Clear" class="btn-octicon" type="button"><span class="pl-button__content"><svg
Expand Down
Loading

0 comments on commit 716d3fc

Please sign in to comment.