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

Make mv-options work with expressions, fixes #777 #972

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

DmitrySharabin
Copy link
Member

I thought it would be nice if such an awesome feature like mv-options would work with expressions correctly in the upcoming release. :)

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;
Copy link
Member Author

@DmitrySharabin DmitrySharabin Sep 26, 2023

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;

Copy link
Member

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…

Copy link
Member Author

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?

Copy link
Member

@LeaVerou LeaVerou left a 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;
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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?

Copy link
Member

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.

Copy link
Member Author

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. 😅

@DmitrySharabin DmitrySharabin marked this pull request as draft February 11, 2024 13:33
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.

2 participants