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
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,12 @@ var _ = Mavo.Primitive = class Primitive extends Mavo.Node {
let presentational;

if (this.options) {
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. 😅

}

presentational = this.options.get(value);
}

Expand Down Expand Up @@ -1316,6 +1322,8 @@ Mavo.observe({id: "primitive"}, function({node, type, attribute, record, element
else if (attribute === "mv-options") {
node.updateOptions();

node.setValue(node.value, {force: true, silent: true});

if (node.editor) {
node.generateDefaultEditor();
}
Expand Down