Ability to pass LiveComponent as field #13
Merged
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.
This commit enables user to pass in custom LiveComponent for more
complex form field.
User do it like so:
This creates a stateful LiveComponent that can update your form input
values.
This works with a few assumption:
:id
phx-keyup
, becausephx-change
doesn'twork outside of
form
.<form>
, because HTML5 spec mandates thatyou cannot have nested form tags.
In your Suggestion component, you can show user the available
suggestions, then when they click, you should handle the event and then
let the FormComponent know. You do this by:
This will forward the request to the parent LiveView, which will then
broadcast it to the FormComponent. For example, say you are editing a
schema called Listing. It has a field called
newsletter_id
(belongs_to), and you want to have suggestions when user is typing so
they can search in-line. You would then send it as:
This would create a
hidden_input
on your<form>
, which would update:newsletter_id
to be4
.Fixes #2