package module
Version: v0.0.0-...-f43790c Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2020 License: Unlicense Imports: 3 Imported by: 1


GopherJS_ Input Suggest Menu

.. image::

.. image::

.. image::

.. image::

.. image::

.. image::

Development Environment:

  - `Ubuntu 20.04`_
  - `Go 1.12.17`_
  - GopherJS_


Install GopherJS_ and this package:

.. code-block:: bash

  $ go get -u
  $ go get -u


See demos first. The following is key point in the code.

- Demo_ (no CSS framework)
- Demo-Bulma_ (with Bulma 0.9.0)


.. code-block:: html

  <div style="position: relative;">
    <input id="word" type="text" autofocus="autofocus" tabindex="1">

The parent of input element is set to *position: relative;*. Also do not use
*text-align: center;* in parent/ancestor element of input element.


.. code-block:: go

  BindSuggest("word", func(w string) []string {
  	// suggestion function implemented by you
  	return frozenTrie.GetSuggestedWords(w, 30)

See `example <example>`_ directory for complete example.

Control Suggest Menu Behavior by Outside Widget

Two methods *UpdateSuggestion* and *HideSuggestion* are exported so that outside
widget such as virtual Pali keypad can control the behavior of the suggest menu.
The reason why Pali keypad need this is that when users click the button on the
Pali keypad, no keyboard events are fired so that suggest menu will not be
updated or hidden automatically. As a result, Pali keypad has to update or hide
the suggest menu manually by these two methods.


Released in public domain. See UNLICENSE_.


.. [1] `GopherJS - A compiler from Go to JavaScript <>`_
       (`GitHub <>`__,
       `GopherJS Playground <>`_,

.. [2] `Element.classList - Web APIs | MDN <>`_

.. [3] `javascript set left position <>`_

       `HTML DOM Style left Property - W3Schools <>`_

.. [4] `javascript min width <>`_

       `HTML DOM Style minWidth Property - W3Schools <>`_

.. [5] `GitHub - siongui/godom: Make DOM manipultation in Go as similar to JavaScript as possible. (via GopherJS) <>`_

.. [6] | `bulma input suggest dropdown - Google search <>`_
       | `Autocomplete | Buefy <>`_

.. _Ubuntu 20.04:
.. _Go 1.12.17:
.. _HTML:
.. _Go:
.. _GopherJS:
.. _Demo:
.. _Demo-Bulma:

.. |godoc| image::



Package suggest provides input suggestion menu feature for frontend. This is similar to Autocomplete feature of Buefy ( The code of this library is written in Go and must be compiled to JavaScript via GopherJS. The BindSuggest method is initialization function of the input suggest. For simple application, using this method once in your application initialization code should be enough and it will run automatically. If you need to control the behavior of the suggest menu during the runtime of your application, use UpdateSuggestion or HideSuggestion methods.



View Source
const (
	TAB    = 9
	RETURN = 13
	ESC    = 27
	LEFT   = 37
	UP     = 38
	RIGHT  = 39
	DOWN   = 40


This section is empty.


func HideSuggestion

func HideSuggestion()

HideSuggestion will hide the input suggest menu.

func IsShowSuggestMenu

func IsShowSuggestMenu() bool

IsShowSuggestMenu reports if the suggest menu is visible now.

func OnHideSuggestMenu

func OnHideSuggestMenu(handler func())

OnHideSuggestMenu registers an event handler when suggest menu is being hidden.

func OnHighlightSelectedWord

func OnHighlightSelectedWord(handler func(string))

OnHighlightSelectedWord registers an event handler of which the argument is the selected word.

func OnUpdateSuggestMenu

func OnUpdateSuggestMenu(handler func(string))

OnUpdateSuggestMenu registers an event handler of which the argument is the selected word.

func UpdateSuggestion

func UpdateSuggestion()

UpdateSuggestion will read the value of input element and update suggest menu accordingly.


type SuggestMenuStateMachine

type SuggestMenuStateMachine struct {
	Input                        *Object
	SuggestMenu                  *Object
	FuncSugguestWords            func(string) []string
	CurrentSelectedWordIndex     int
	IsShowSuggestMenu            bool
	SuggestedWordsDivs           []*Object
	OriginalWord                 string
	SuggestedWords               []string
	HighlightSelectedWordHandler []func(string)
	UpdateSuggestMenuHandler     []func(string)
	HideSuggestMenuHandler       []func()

func BindSuggest

func BindSuggest(id string, fnSugguestWords func(string) []string) *SuggestMenuStateMachine

BindSuggest is the initialization function for the input suggest feature. The first argument is the id of the input element. The second argument is a function implemented by you. Given a string, the function will return possible suggest strings for users to choose.

func NewSuggestMenuStateMachine

func NewSuggestMenuStateMachine(input, sm *Object, fnSugguestWords func(string) []string) *SuggestMenuStateMachine

func (*SuggestMenuStateMachine) GetWord

func (s *SuggestMenuStateMachine) GetWord() string

func (*SuggestMenuStateMachine) HandleArrowDown

func (s *SuggestMenuStateMachine) HandleArrowDown()

func (*SuggestMenuStateMachine) HandleArrowUp

func (s *SuggestMenuStateMachine) HandleArrowUp()

func (*SuggestMenuStateMachine) HandleESC

func (s *SuggestMenuStateMachine) HandleESC()

func (*SuggestMenuStateMachine) HideSuggestMenu

func (s *SuggestMenuStateMachine) HideSuggestMenu()

func (*SuggestMenuStateMachine) HighlightSelectedWord

func (s *SuggestMenuStateMachine) HighlightSelectedWord(index int)

func (*SuggestMenuStateMachine) SetWord

func (s *SuggestMenuStateMachine) SetWord(word string)

func (*SuggestMenuStateMachine) ShowSuggestMenu

func (s *SuggestMenuStateMachine) ShowSuggestMenu()

func (*SuggestMenuStateMachine) UnhighlightSelectedWord

func (s *SuggestMenuStateMachine) UnhighlightSelectedWord(index int)

func (*SuggestMenuStateMachine) UpdateSuggestMenu

func (s *SuggestMenuStateMachine) UpdateSuggestMenu(word string)


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL