-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[Admin] Add form component #5360
Merged
waiting-for-dev
merged 1 commit into
solidusio:nebulab/admin
from
nebulab:waiting-for-dev/admin/form_component
Aug 31, 2023
Merged
[Admin] Add form component #5360
waiting-for-dev
merged 1 commit into
solidusio:nebulab/admin
from
nebulab:waiting-for-dev/admin/form_component
Aug 31, 2023
Conversation
This file contains 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
waiting-for-dev
force-pushed
the
waiting-for-dev/admin/form_component
branch
3 times, most recently
from
August 29, 2023 12:13
a0b26bd
to
10b9e5b
Compare
Introducing a form component brings a two-fold benefit: 1. It wraps all the form component dependencies in a single place so that we don't need to redeclare them in every other form (e.g., the product creation form). 2. It allows defining a form from the outside, which is a first step towards making the admin forms configurable. Internally, it uses Rails' `form_with` helper [1], and all the given arguments are passed to it. The only exception is the `elements:` key, which is used to define the form elements. The form elements are simple builders of renderable things that happen within a form context. That's to say, to be rendered, both the form component instance and the Rails' form builder are given to a `#call` method. We ship with out-of-the-box form elements for fields, fieldsets and arbitrary components or HTML. Notice that there's a circular dependency between form and form elements. While the form calls the elements to get the renderable content, the elements can use the form to get the configured dependencies. For instance, when rendering a text field element, it'll use the configured text_field dependency in the form. This complexity is an acceptable trade-off for a double benefit: the ability to change all the form dependencies at once (e.g., using a different text field component across a form), and the removal of boilerplate from the form element definitions. Nonetheless, the default behavior can be overridden by passing a different component to the form element. Having access to the form component is also used by the fieldset element to render the nested elements. This commit also makes some tangential changes that were necessary: - We rename the `form:` parameter to `builder:` in the components within the form namespace. As we have now a form component, that makes it more explicit that it corresponds to the Rails' form builder. - We add `.rb` files within the preview directories to Tailwind watchable files. - We modify the `mock_component` test helper to accept not providing a block. [1] - https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_with Ref. solidusio#5329
waiting-for-dev
force-pushed
the
waiting-for-dev/admin/form_component
branch
from
August 30, 2023 14:30
10b9e5b
to
e390f7b
Compare
rainerdema
approved these changes
Aug 31, 2023
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.
This is amazing, thanks @waiting-for-dev!
admin/spec/components/previews/solidus_admin/ui/forms/text_field/component_preview.rb
Show resolved
Hide resolved
waiting-for-dev
added a commit
to nebulab/solidus
that referenced
this pull request
Sep 1, 2023
solidusio#5363 was merged just before solidusio#5360, so it didn't pick up the rename.
This was referenced Sep 1, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Introducing a form component brings a two-fold benefit:
Internally, it uses Rails'
form_with
helper, and all the given arguments are passed to it. The only exception is theelements:
key, which is used to define the form elements.The form elements are simple builders of renderable things that happen within a form context. That's to say, to be rendered, both the form component instance and the Rails' form builder are given to a
#call
method.We ship with out-of-the-box form elements for fields, fieldsets and arbitrary components or HTML.
Notice that there's a circular dependency between form and form elements. While the form calls the elements to get the renderable content, the elements can use the form to get the configured dependencies. For instance, when rendering a text field element, it'll use the configured text_field dependency in the form. This complexity is an acceptable trade-off for a double benefit: the ability to change all the form dependencies at once (e.g., using a different text field component across a form), and the removal of boilerplate from the form element definitions. Nonetheless, the default behavior can be overridden by passing a different component to the form element. by passing a different component to the form element. Having access to
the form component is also used by the fieldset element to render the nested elements.
This commit also makes some tangential changes that were necessary:
form:
parameter tobuilder:
in the components within the form namespace. As we have now a form component, that makes it more explicit that it corresponds to the Rails' form builder..rb
files within the preview directories to Tailwind watchable files.mock_component
test helper to accept not providing a block.Ref. #5329
Checklist
Check out our PR guidelines for more details.
The following are mandatory for all PRs:
The following are not always needed: