Typehead textbox using 'text input' block

1,040 views
Skip to first unread message

Metri Dev

unread,
Feb 6, 2017, 1:22:02 AM2/6/17
to Blockly
I am trying to create custom block for 'typeahead' support. Please find the attached screenshot.
Here is the  Custom Block. Can someone help me understand how to capture stroke events from within text input and return suggestion?.
Thanks!
typahead.JPG

Rachel Fenichel

unread,
Feb 10, 2017, 5:47:52 PM2/10/17
to Blockly
Sounds like you want some combination of FieldDropdown and FieldTextInput.  FieldTextInput's key stroke handling is here: https://github.com/google/blockly/blob/master/core/field_textinput.js#L173


Rachel

--
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.
For more options, visit https://groups.google.com/d/optout.

phil cleaver

unread,
Feb 27, 2017, 10:02:50 AM2/27/17
to Blockly
The way you want to do it is wih the datalist HTML5 element - it does all the hard work for you.


I used it in one of my custom fields and it works great.

You need to create your own new field and use something like this code for it's showEditor function.... 


Blockly.FieldHtml.prototype.showEditor_ = function(opt_quietInput) {
  this.workspace_ = this.sourceBlock_.workspace;
  var quietInput = opt_quietInput || false;
  if (!quietInput && (goog.userAgent.MOBILE || goog.userAgent.ANDROID ||
                      goog.userAgent.IPAD)) {
    // Mobile browsers have issues with in-line textareas (focus & keyboards).
    var newValue = window.prompt(Blockly.Msg.CHANGE_VALUE_TITLE, this.text_);
    if (this.sourceBlock_) {
      newValue = this.callValidator(newValue);
    }
    this.setValue(newValue);
    return;
  } 

  Blockly.WidgetDiv.show(this, this.sourceBlock_.RTL, this.widgetDispose_());
  var div = Blockly.WidgetDiv.DIV;
  // Create the input.
  var htmlInput = 
      goog.dom.createDom(goog.dom.TagName.INPUT, 'blocklyHtmlInput');
//   htmlInput.setAttribute('spellcheck', this.spellcheck_);
  var fontSize =
      (Blockly.FieldHtml.FONTSIZE * this.workspace_.scale) + 'pt';
  div.style.fontSize = fontSize;
  htmlInput.style.fontSize = fontSize;

  var datalist = goog.dom.createDom(goog.dom.TagName.DATALIST);
  for (var i=0; i < this.autovalues_.length; i++) {
    var option = goog.dom.createDom(goog.dom.TagName.OPTION);
    option.setAttribute('value', this.autovalues_[i])
    datalist.appendChild(option);
  }
  datalist.setAttribute('id', 'datalist');
  htmlInput.setAttribute('list', 'datalist')

  /** @type {!HTMLInputElement} */
  Blockly.FieldHtml.htmlInput_ = htmlInput;
  div.appendChild(datalist);
  div.appendChild(htmlInput);

  htmlInput.value = htmlInput.defaultValue = this.text_;
  htmlInput.oldValue_ = null;
  this.validate_();
  this.resizeEditor_();
  if (!quietInput) {
    htmlInput.focus();
     htmlInput.select();
  }

  // Bind to keydown -- trap Enter without IME and Esc to hide.
  htmlInput.onKeyDownWrapper_ =
      Blockly.bindEventWithChecks_(htmlInput, 'keydown', this,
      this.onHtmlInputKeyDown_);
  // Bind to keyup -- trap Enter; resize after every keystroke.
  htmlInput.onKeyUpWrapper_ =
      Blockly.bindEventWithChecks_(htmlInput, 'keyup', this,
      this.onHtmlInputChange_);
  // Bind to keyPress -- repeatedly resize when holding down a key.
  htmlInput.onKeyPressWrapper_ =
      Blockly.bindEventWithChecks_(htmlInput, 'keypress', this,
      this.onHtmlInputChange_);
  htmlInput.onWorkspaceChangeWrapper_ = this.resizeEditor_.bind(this);
  this.workspace_.addChangeListener(htmlInput.onWorkspaceChangeWrapper_);
};

Rachel Fenichel

unread,
Feb 27, 2017, 10:50:17 PM2/27/17
to Blockly
Note that datalist isn't supported on Safari or iOS Safari and has partial support otherwise: http://caniuse.com/#search=datalist

Partial support may be fine for what you're doing, but you will want to check out the bug lists to be sure.

--

Aneesh Kashalikar

unread,
Aug 29, 2017, 3:18:59 PM8/29/17
to Blockly
How did you incorporate new FieldHtml in defining the blocks? Not exactly sure about that

phil cleaver

unread,
Sep 2, 2017, 8:14:32 AM9/2/17
to blo...@googlegroups.com
right...

in block.js around ln 1139

case 'field_html':
field = new Blockly.FieldHtml(element['text']);

blockly.js - 42

goog.require('Blockly.FieldHtml');


if you want it in the factory

factoryutils.js 112
} else if (field instanceof Blockly.FieldHtml) {
code.push(makeVar('tag', name) + " = block.getFieldValue('" + name + "');");

factoryutils.js 476

case 'field_html':
// Result: new Blockly.FieldHtml('Hello'), 'GREET'
fields.push('new Blockly.FieldHtml(' + JSON.stringify(block.getFieldValue('TEXT')) + '), ' + JSON.stringify(block.getFieldValue('FIELDNAME')));
break;


hope that works!


Virus-free. www.avg.com

--
You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/XBS1RIc5EHA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+unsubscribe@googlegroups.com.

Aneesh Kashalikar

unread,
Jan 28, 2018, 1:47:10 PM1/28/18
to Blockly
Could you provide your project code showing how this works?  Not able to get the FieldHtml to work right. It throws some errors when I insert the code you have put.
To unsubscribe from this group and all its topics, send an email to blockly+u...@googlegroups.com.

Aneesh Kashalikar

unread,
Jan 31, 2018, 11:40:28 AM1/31/18
to Blockly
Thanks for the idea of using a Datalist. For future users of a datalist: Copy the code in the Field text input file and change the name. Add a variable for the options to be included in the list in the constructor. Add the code where the goog.dom creates a datalist element and the div appends the element. Do this and you will have a custom field datalist.


On Monday, February 27, 2017 at 10:02:50 AM UTC-5, phil cleaver wrote:
Message has been deleted

Arbaz Sheikh

unread,
Nov 24, 2020, 9:54:07 AM11/24/20
to Blockly
The custom typeahead block works fine by using Datalist but there is a bug. When you type in any other Input text field, it gets reflected in the typeahead block text field. Has anyone faced the same issue and is any there any solution to this?
Reply all
Reply to author
Forward
0 new messages