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

Add HintComponent and ErrorMessageComponent #98

Merged
merged 17 commits into from
Jan 27, 2022

Conversation

tmaier
Copy link
Contributor

@tmaier tmaier commented Jan 3, 2022

Closes #97

This is a Pull Request to add components for hints and error messages, as proposed in #36

I introduce here a class attribute called tag. tag_class was not really a good fit for this use case.
Please see, if this fits your idea for the gem.

Note: For some reason of my local setup, I was unable to test this at all. I hope the CI pipeline provides some insights :)

Open Topics

  • Ensure enough tests exist and the pass
  • Get alignment on .class class attribute
  • (idea) Generate ID attribute and show how to use it with aria-describedby . I think Rails 6.1 or 7.0 introduced a convenient helper for this. I am just unsure how it was called...

@tmaier
Copy link
Contributor Author

tmaier commented Jan 6, 2022

@Spone I updated that branch. I think it is ready for review

@nicolas-brousse nicolas-brousse requested review from nicolas-brousse and Spone and removed request for nicolas-brousse January 12, 2022 17:53
@tmaier
Copy link
Contributor Author

tmaier commented Jan 14, 2022

The field_id helper introduced with Rails 7 is very useful in this regard

<%= form_for @user, builder: ViewComponent::Form::Builder do |f| %>
  <%= f.label :password %>          
  <%= f.password_field :password, aria: { describedby: f.field_id(: password, :description) } %> 
  <div id="<%= f.field_id(:title, :description) %>">
    <%= f.hint :password, 'The password should be at least 8 characters long' %>
    <%= f.error_message :password %>
  </div>
<% end %>

Reference: https://github.com/rails/rails/blob/7-0-stable/actionview/CHANGELOG.md

Based on https://gist.github.com/tmaier/22966c6bddac86e3612c8eddc072b919

Notable differences:
- self.tag to override the tag used easily. I am not using tag_class, as this is not a classical form component
- ErrorMessageComponent#messages is public so that one can override it, if necessary
- Rename #component to #rendered_component
- Rename #subject to #component
@tmaier tmaier force-pushed the tmaier-97-hints-error-messages branch from 251072a to a83381e Compare January 14, 2022 16:03
@tmaier
Copy link
Contributor Author

tmaier commented Jan 14, 2022

All issues should be fixed. Please re-run the CI pipeline

@Spone
Copy link
Collaborator

Spone commented Jan 14, 2022

Thank you @tmaier, we've scheduled some time next week to review this PR.

Copy link
Contributor

@boardfish boardfish left a comment

Choose a reason for hiding this comment

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

Mostly spec improvements here. @Spone, I feel like there should be a pattern within specs of component referring to an instance of the ViewComponent to test, and rendered_component referring to the rendered HTML output.

subject should refer to the object that we're testing against, in any case. It's not always easy to whittle that down to just one thing, especially in cases where component should respond to methods in a certain way (render?, errors) and rendered_component should have also changed in response to that (showing the validation error or not), but helpers like have_attributes can help with that. I finished a blog post that might help with that today if you're interested.

README.md Outdated Show resolved Hide resolved
app/components/view_component/form/hint_component.rb Outdated Show resolved Hide resolved
spec/view_component/form/hint_component_spec.rb Outdated Show resolved Hide resolved
spec/view_component/form/error_message_component_spec.rb Outdated Show resolved Hide resolved
spec/view_component/form/error_message_component_spec.rb Outdated Show resolved Hide resolved
spec/view_component/form/error_message_component_spec.rb Outdated Show resolved Hide resolved
tmaier and others added 6 commits January 18, 2022 22:32
Co-authored-by: Simon Fish <si@mon.fish>
Co-authored-by: Simon Fish <si@mon.fish>
Co-authored-by: Simon Fish <si@mon.fish>
@tmaier
Copy link
Contributor Author

tmaier commented Jan 18, 2022

Thanks for the review. I accepted all your changes.

@tmaier
Copy link
Contributor Author

tmaier commented Jan 26, 2022

@Spone can you re-run the CI pipeline, please? And consider to merge it, if it passes...

@nicolas-brousse
Copy link
Member

Hi @tmaier sorry for the delay @Spone and I are really busy at work those days.

Also thank you @boardfish for your review ☺️

Thanks for your PR, all is looking good.
I've just one hesitation about naming for error_message, I've created almost the same helper on our existing application, and named it error_field to be close to existing helper. What do you think about @Spone, @boardfish and @tmaier?
I'm okay with error_message it's just a suggestion before we merge this :)

@Spone
Copy link
Collaborator

Spone commented Jan 26, 2022

Thanks for your PR, all is looking good. I've just one hesitation about naming for error_message, I've created almost the same helper on our existing application, and named it error_field to be close to existing helper.

I prefer error_message, since this is not really a "field" (you cannot input data in it).

"message" is also the word used in validation options, so the intent is clear if we reuse it.

@tmaier
Copy link
Contributor Author

tmaier commented Jan 26, 2022

I think error_message and hint are consistent with label.

error_field would only be consistent, if we rename hint_field and label_field...

@tmaier
Copy link
Contributor Author

tmaier commented Jan 26, 2022

Besides this: I fixed the specs

Copy link
Collaborator

@Spone Spone left a comment

Choose a reason for hiding this comment

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

Thanks a lot for adding this!

@Spone Spone merged commit b6ecfd4 into pantographe:main Jan 27, 2022
@tmaier tmaier deleted the tmaier-97-hints-error-messages branch January 27, 2022 20:51
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.

Primitives for hints & error messages
4 participants