-
Notifications
You must be signed in to change notification settings - Fork 178
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
Make mv-options work with expressions, fixes #777 #972
base: main
Are you sure you want to change the base?
Conversation
src/primitive.js
Outdated
@@ -1316,6 +1316,9 @@ Mavo.observe({id: "primitive"}, function({node, type, attribute, record, element | |||
else if (attribute === "mv-options") { | |||
node.updateOptions(); | |||
|
|||
let option = node.options.get(node.value) ?? node.options.keys().next().value; |
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.
Or should we take into account the default value? 🤔
let option = node.options.get(node.value) ?? node.default ?? node.options.keys().next().value;
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.
The default value will be taken into account inside the node anyway, so this is not the right place to deal with it, and it would be an undersirable coupling (now every time we touch default values, we also need to look in the code that deals with mv-options
updates).
In fact, I wonder if even this line might be duplicating logic that should be elsewhere. Isn't there already logic that sets the value appropriately based on node.options
? If so, we should be invoking that…
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.
@LeaVerou, I could manage to find a place where, from my perspective, this logic should be. Could you please give this PR another shot?
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.
See comment
if (!this.options.has(value)) { | ||
// There is no option corresponding to the value. Try the default one. | ||
// If there is no option corresponding to the default value either, use the first option | ||
value = this.options.has(this.default)? this.default : this.options.keys().next().value; |
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.
We should not change the value if it doesn't match any of the options, as that's destructive. The user may be experimenting with a different UI, or have forgotten to add all pertinent options. As a design principle, loading data into any Mavo and then saving it back should be a non-destructive operation.
For <select>
there is a whole code path that creates (and removes when no longer needed) .mv-volatile
options in the <select>
to deal with this. mv-options
should behave similarly.
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.
That makes perfect sense. Thanks. I assume I must think about it a bit more.
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.
Possibly not for v0.3.0 then?
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.
FWIW there is already a code path around a property's editor mutating, you should probably hook into that, since that's what a changing mv-options
is, conceptually.
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.
Possibly not for v0.3.0 then?
I was going to suggest this. I might have been a bit too optimistic about the issue. 😅
I thought it would be nice if such an awesome feature like
mv-options
would work with expressions correctly in the upcoming release. :)