Skip to content
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

phoenix_html v4 compatibility #130

Merged
merged 19 commits into from
Jul 18, 2024

Conversation

angelikatyborska
Copy link
Contributor

@angelikatyborska angelikatyborska commented Jul 16, 2024

Resolves #129

Goal: make bitstyles_phoenix work in any combination of phoenix_live_view versions 18 to 20, and phoenix_html versions 3 to 4

Replacements

  • Phoenix.HTML.Tag.content_tag in a LV context can be replaced with dynamic_tag which exists in phoenix_live_view v18
  • Phoenix.HTML.Tag.content_tag in a non-LV context is used for generating the "showcase" macros, can be replaced with a copy-paste of the function. It relies on import Phoenix.HTML, only: [attributes_escape: 1, html_escape: 1] which both still exist in phoenix_html v4
  • Phoenix.HTML.Form.humanize - this function was previously used by Phoenix to generate default label text from a field name. It looks like there no longer is a default label text. We can copy-paste this function and keep using it.
  • Phoenix.HTML.Form.label - should be replaced with a new BitstylesPhoenix.Component.Form.label component that's a copy-paste from Phoenix's CoreComponents
  • Phoenix.HTML.Form.text_input, Phoenix.HTML.Form.email_input, and so on, and Phoenix.HTML.Form.textarea, and Phoenix.HTML.Form.select - should all be replaced with a new BitstylesPhoenix.Component.Form.input component that's a copy-paste from Phoenix's CoreComponents but modified to be just the input, no label and no errors. (yes, "textarea" and "select" are handled by an "input" component in CoreComponents 🤷)

CI changes

https://hexdocs.pm/elixir/library-guidelines.html#dependency-handling recommends:

The best practice of handling mix.lock file therefore would be to keep it in VCS, and run two different Continuous Integration (CI) workflows: the usual deterministic one, and another one, that starts with mix deps.unlock --all and always compiles your library and runs tests against latest versions of dependencies. The latter one might be even run nightly or otherwise recurrently to stay notified about any possible issue in regard to dependencies updates.

Running tests with unlocked deps would detect that bitstyles_phoenix does not work with the newest phoenix_html version because there was no version specification for this dependency in the mix file.

Other notes

  • Phoenix.Component.used_input? that's used by PLV 1.0.0-RC in core components does not exist in lower versions. It's used to decide whether input errors should be rendered.
  • Phoenix.HTML.FormField was added to phoenix_html in 3.3.0, so we need to require that version at minimum.
  • Phoenix.Component.to_form used in the tests was added to PLV in 18.12. Since we cannot provide different PLV version requirements for local development and for library usage, we need to bump the lowest supported version from 18.9 to 18.12.

@angelikatyborska angelikatyborska force-pushed the feature/129-phoenix_html-v4-compatibility branch from 53b6d0d to 73f7a85 Compare July 16, 2024 15:13
@angelikatyborska angelikatyborska force-pushed the feature/129-phoenix_html-v4-compatibility branch from 73f7a85 to 7918745 Compare July 16, 2024 15:17
@@ -100,7 +106,7 @@ defmodule BitstylesPhoenix.Component.Form do
*
</span>
</label>
<input id="user_name" maxlength="255" name="user[name]" required="required" type="text"/>
<input id="user_name" name="user[name]" type="text" maxlength="255" required="required"/>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In all the existing test cases in this module that have a git diff, the diff is only about reordering attributes.

@angelikatyborska
Copy link
Contributor Author

The regular unit test job runs phoenix_html v3, the one with unlocked deps runs v4. Both succeeding meaning that I achieved the task 🎉

@angelikatyborska angelikatyborska marked this pull request as ready for review July 17, 2024 13:58
<%= render_slot(@inner_block) %>
<% end %>
Copy link
Member

Choose a reason for hiding this comment

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

🥳

elixir-version: '1.14.5'
- run: mix deps.unlock --all
- run: mix deps.get
- run: mix test
Copy link
Member

Choose a reason for hiding this comment

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

Nice test

<%= render_slot(@inner_block) %>
</label>
"""
end
Copy link
Member

Choose a reason for hiding this comment

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

Awesome work ✨

'''
)

def input(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
Copy link
Member

Choose a reason for hiding this comment

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

Since you want this one to be public to test it we should actually have a less generic name for this one, since otherwise we get the .input component everywhere, which might even clash with existing helper components.

How about ui_raw_input ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, done: 5b61a76 (#130)

end
|> Safe.to_iodata()
|> IO.iodata_to_binary()
Copy link
Member

Choose a reason for hiding this comment

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

👏

CHANGELOG.md Outdated
@@ -2,6 +2,7 @@

## Unreleased

- Added support for phoenix_html `v4`. You can also continue using phoenix_html `v3`.
Copy link
Member

@andreasknoepfle andreasknoepfle Jul 18, 2024

Choose a reason for hiding this comment

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

Maybe we can also document the newly added components for raw inputs and labels (whatever name you choose)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this enough ca1ec04 (#130) ? I was struggling to find the right words to describe the components in the changelog and gave up on writing more, hoping people would just look for more details in the components' documentation

Copy link
Member

Choose a reason for hiding this comment

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

Perfect!

Copy link
Member

@andreasknoepfle andreasknoepfle left a comment

Choose a reason for hiding this comment

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

💚

@angelikatyborska angelikatyborska merged commit 4a7b7af into main Jul 18, 2024
3 checks passed
@angelikatyborska angelikatyborska deleted the feature/129-phoenix_html-v4-compatibility branch July 18, 2024 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

phoenix_html v4 compatibility
2 participants