Open
Conversation
When using the <:option> slot, it is convenient to be able to
store other data in the options solely for customising the rendering of
an option. E.g.:
```elixir
def handle_event("live_select_change", %{"id" => select_id, "text" => text}, socket) do
students = fetch_student_suggestions(text)
options = Enum.map(suggestions, &%{label: &1.full_name, value: &1.id, student: &1})
# ...
end
def render(assigns) do
~H"""
<LiveSelect.live_select field={f[:student_id]} >
<:option :let={option}>
<.student student={option.student} />
</:option>
</LiveSelect.live_select>
"""
end
```
However, all fields in an option currently need to support encoding to
JSON since the list of selected options is sent to the client. Thus,
only custom data fields which support encoding to JSON can be included
in the options.
This commit changes that by cleansing the set of selected options before
sending them to the client: any fields other than
* `:label`
* `:value`
* `:disabled`
* `:tag_label`
are omitted, these first three seem to be the 'canonical set' which the
`normalize_option/1` function returns, `:tag_label` is a supported
optional field.
That way, whatever custom data is stored on options for customising the
rendering is not sent to the client - and does not require defining a
protocol implementation for e.g. Jason.Encoder either.
2829b80 to
b73c96d
Compare
Author
|
An alternative approach which is maybe easier to maintain would be to add recognition of a new dedicated field for options, like |
This file contains hidden or 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
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.
When using the
<:option>slot, it is convenient to be able to store other data in the options solely for customising the rendering of an option. E.g.:However, all fields in an option currently need to support encoding to JSON since the list of selected options is sent to the client. Thus, only custom data fields which support encoding to JSON can be included in the options.
This commit changes that by cleansing the set of selected options before sending them to the client: any fields other than
:label:value:disabled:tag_labelare omitted, the first three seem to be the 'canonical set' which the
normalize_option/1function returns,:tag_labelis a supported optional field.That way, whatever custom data is stored on options for customising the rendering is not sent to the client - and does not require defining a protocol implementation for e.g.
Jason.Encodereither.