react-querybuilder/react-querybuilder

ability to disable `getFirstOption` if no options are selected initially

oleg-andreyev opened this issue · 5 comments

Using React QueryBuilder with https://github.com/jakezatecky/react-dual-listbox (multiselect) and noticed that as soon as widget is initialed some value (first) is selected.

While it's not a bug (because everything is working) it bugs customer that something is selected out-of-the-box.

It would be great to document this behaviour or allow to disable it.

If you're talking about the fieldSelector or operatorSelector dropdowns, those can be set to a default value of "~" by passing autoSelectField={false} autoSelectOperator={false} in your <QueryBuilder> props.

For the valueEditor, I'd recommend adding a "dummy" option to the values array of the field in question (which is what autoSelectField={false} and autoSelectOperator={false} do). You can also set a defaultValue property on the field that doesn't match any of the actual option values.

This codesandbox implements all the options I've described for the "Instrument" field.

The reason RQB picks a value (either the first value or the configured defaultValue), is that native HTML <select> elements display the first value's label even when a value is not actually selected. This can mislead the user so RQB tries to avoid that situation.


Side note: I actually started to answer this question by recommending using the skipHook prop on the default ValueEditor, but that doesn't work like I thought it would. Implementation would have looked something like this:

const CustomValueEditor = (props: ValueEditorProps) => (
  <ValueEditor {...props} skipHook />
);

I'll try to see if I can get that to work before v7 is released.

@oleg-andreyev I just realized this might be fixed by #631/#632 as long as valueEditorType === "multiselect". Can you test with v6.5.5?

@oleg-andreyev #631/#632 didn't resolve the issue after all, so I created #645 to work the way I think it should. It doesn't have a "disable getFirstOption" config, but valueEditorType: "multiple" should do what you're asking.

Can you try the new code in this sandbox?

If this doesn't meet your needs let me know.

going to try this week, a bit busy, thanks

going to try this week, a bit busy, thanks

I went ahead and merged the #645. I'm going to push out another v7 alpha in a bit so (no rush!) you can test the changes in a proper release. I'm open to adjusting this before v7 is final, which could be some time.