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 beginsprefixOnly– if true, only items where the beginning matches the typed text will be offered for completionforceOpen– if true, list will pop-up when comp receives focusrequireSelection– if true, first match is always selected and filled into the TextInput if user presses TabdataProvider– allowed types are Array, ArrayCollection, XmlListCollection; can also be asigned dynamically, i.e. with the result of an asynchronous calllabelField, labelFunction– standard Flex labelField and labelFunctionreturnField– set this if you want the component to fill a value different from labelField or labelFuncitonsortFunction– function for sorting matching items, default is alphabetically ascendingtext– text of the input textselectedItem– selectedItem of list; read and write,set to null to reset selectionselectedIndex– 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 userenter– type:FlexEvent, enter event of the TextInputchange– 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;
}
}
}
Nice component, keep up good work.
Comment by pava — February 6, 2010, 1:44 pm
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
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
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
Bruce, you have two options:
sortFunctionpropertyComment by admin — February 20, 2010, 10:11 am
[...] 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