Flex 4 AutoComplete

As the by-product of an enterprise project I have built an AutoComplete component in Flex 4.
Using the new Spark architecture component logic and appearance can be separated in a clean way.

Here is a demo app showing two ways of usage:

Click here to view source code and download project. You will need Flash Builder Beta 2 to compile the project.

You can use the following properties to customize the component:

  • minChars – the number of caharcters typed by the user after which completion begins
  • prefixOnly – if true, only items where the beginning matches the typed text will be offered for completion
  • forceOpen – if true, list will pop-up when comp receives focus
  • requireSelection – if true, first match is always selected and filled into the TextInput if user presses Tab
  • dataProvider – allowed types are Array, ArrayCollection, XmlListCollection; can also be asigned dynamically, i.e. with the result of an asynchronous call
  • labelField, labelFunction – standard Flex labelField and labelFunction
  • returnField – set this if you want the component to fill a value different from labelField or labelFunciton
  • sortFunction – function for sorting matching items, default is alphabetically ascending
  • text – text of the input text
  • selectedItem – selectedItem of list; read and write,set to null to reset selection
  • selectedIndex – selectedIndex of list, read only

The component dispatches 3 custom events:

  • select – type:CustomEvent; dispatched when user selects a list item by mouse or pressing Enter; listen to this to detect selection made by user
  • enter – type:FlexEvent, enter event of the TextInput
  • change – type:TextOperationEvent, change event of the TextInput

The visual appearance is defined by the skin file AutoCompleteSkin.as. The skin consist of a TextInput and a List component placed in a PopUpAnchor.

    <s:PopUpAnchor id="popUp"  displayPopUp="false"
        top="0" bottom="0" left="0" right="0"
        popUpWidthMatchesAnchorWidth="true"
        popUpPosition="below" >

        <s:Group id="dropDown" minHeight="22" width="100%">

            <s:List id="list" width="100%" minWidth="22" />

        </s:Group>

    </s:PopUpAnchor>

    <s:TextInput id="inputTxt" left="0" right="0" top="0" bottom="0" />

The logic in AutoComplete works like this: when the user types in text that matches with any items in the list we pop up the list showing these items.
There is a little trick here – all keyboard events are dispatched by the TextInput but we need to be able to select items in the list with the cursor keys. What we do is to re-dispatch some keyboard events on the list:

    private function onKeyDown(event: KeyboardEvent) : void{

        if (popUp.displayPopUp){
            switch (event.keyCode){
                case Keyboard.UP:
                case Keyboard.DOWN:
                case Keyboard.END:
                case Keyboard.HOME:
                case Keyboard.PAGE_UP:
                case Keyboard.PAGE_DOWN:
                    inputTxt.selectRange(text.length, text.length)
                    list.dispatchEvent(event)
                    break;
                case Keyboard.ENTER:
                    acceptCompletion();
                    break;
                case Keyboard.TAB:
                    if (requireSelection)
                        acceptCompletion();
                    else
                        popUp.displayPopUp = false
                    break;
                case Keyboard.ESCAPE:
                    popUp.displayPopUp = false
                    break;
            }
        }
    }
Filed under:Flex—Tags: , , ,
  1. Nice component, keep up good work.

    Comment by pava — February 6, 2010, 1:44 pm

  2. Am I doing something wrong? I can not seem to use a .setFocus() on the autocomplete.

    Comment by bruce — February 11, 2010, 9:37 pm

  3. To set focus programatically try autoComplete.inputTxt.setFocus(). I’ll correct the bug some time, thanks for telling.

    Comment by admin — February 12, 2010, 9:14 am

  4. and to turn OFF sorting? To have the items displayed in the order they are in the dataprovider? (and Ditto, please keep up the good work, very clean, very usable. you rock! We need more components for those of us who are NOT component builders but system builders!)

    Comment by bruce — February 19, 2010, 10:15 pm

  5. Bruce, you have two options:

    • 1. define your own sort function that sorts according to item order in dataprovider (i.e. it does nothing), and assign it to the sortFunction property
    • 2. simply comment out the sorting of the data inside the comp, around line 165.

    Comment by admin — February 20, 2010, 10:11 am

  6. [...] component is based on the revised codebase of AutoComplete 4 component originally created by Tenger Ivan from FlashCommander.org [...]

    Pingback by JabbyPanda’s travel to RIA world » AutoComplete component for Flex 4 that supports entered text highlighting — March 8, 2010, 2:33 pm

Leave a comment: