DropdownField: specify a default value?

1,085 views
Skip to first unread message

Loren Norman

unread,
Jan 17, 2024, 5:34:42 AM1/17/24
to Blockly
Hello, and thanks in advance for taking a look at this!

I've got an "hourly range" selector block that looks like this:

Screenshot 2024-01-16 at 10.57.22 AM.png
I'm concerned with the third dropdown, which has options like this:
[
  [ "0", "0" ],
  [ "1", "1" ],
  ...
  [ "23", "23" ]
]

It should always default to "23" (the last option in the list) unless specifically given a value otherwise.

My question: Can a default dropdown value be accomplished via the block's JSON specification? I don't see anything to this effect in the docs, but it seems like an obvious omission. For example, TextField's "text" property is very similar in functionality. Shouldn't a Dropdown have a "selected" property or something like this?

I understand I can set it with an extension, and also whenever it is included in another block or toolbox, and in shadow declarations. However, this is exactly the point: that's a lot of work and boilerplate and code locations to keep in sync for something so straightforward and obvious. What am I missing? Any and all tips welcome.

Thanks again!

Christopher Allen

unread,
Jan 18, 2024, 7:51:34 AM1/18/24
to blo...@googlegroups.com
Hi Loren,

My question: Can a default dropdown value be accomplished via the block's JSON specification? I don't see anything to this effect in the docs, but it seems like an obvious omission. For example, TextField's "text" property is very similar in functionality. Shouldn't a Dropdown have a "selected" property or something like this?

I believe you are correct that there is no way to specify the default value for a field_dropdown in a JSON block definition: FieldDropdown defaults to selecting the first (well, zeroth) option, and you'll need an JS block definition, extension, or toolbox definition to set it to a non-default value.

You could open a feature request for this.  We (the core team) briefly discussed the matter yesterday and though we're unlikely to have time to implement it ourselves we think it is something an external contributor (such as yourself, perhaps) might be interested in doing and could probably do without too much difficulty.
 

Christopher

Mark Friedman

unread,
Jan 19, 2024, 2:27:39 PM1/19/24
to blo...@googlegroups.com
I understand I can set it with an extension, and also whenever it is included in another block or toolbox, and in shadow declarations. However, this is exactly the point: that's a lot of work and boilerplate and code locations to keep in sync for something so straightforward and obvious.

I'll just add to Christoper's reply to point out that I don't think it's as complicated as you say.  You would just add a call to, say, myDropdown.setValue("23") in the init() method for your "hourly range" selector block.  That would then set the initial value to 23 in all uses of the custom block.  Also note that if you already have the JSON for the block definition you can reuse that within your init() method by calling this.jsonInit() and then follow that by the call to setValue() mentioned above.

-Mark

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/CAN0w5eY97krGVwP-b9eBpafVqh6KKEtiAvvqsH%2BFfss7dAHbAw%40mail.gmail.com.

Loren Norman

unread,
Feb 13, 2024, 2:13:55 PM2/13/24
to Blockly
Ok great, thanks to both of you! (Apologies for late reply, just found the responses in my spam folder, nice work Google!)

I appreciate the approach the project takes. Exposing simple ways to embellish default behaviors must be far less work for you all to maintain while leaving it open for many possible behaviors, than building an explosion of all possible features and settings anyone might want.

Great notes on a feature request and the potential of contributing to the source myself. Short answer is "not right now" but it's something I'm generally interested in. We're just getting started integrated Blockly into our app, so the fullness of time will bring a lot of energy and attention one way or another.

Thanks again!
Reply all
Reply to author
Forward
0 new messages