Recently I had instructions to create a component for “Faceted Search”. Yeah, I’m not really sure what that means, either. From my perspective, this is just a set of tags, with the ability to multiselect them. Sounds a lot like a list, doesn’t it? The only tricky part was the user interaction. The design also required selection and deselection without a modifier key. I call this a toggle behavior, as it works a lot like a radio button or a checkbox. Click the widget and it’s state changes.
First I pictured a custom item renderer, and a click handler, and some custom code around the state of each item. However, knowing that multiselect is already built into List, it seemed worth while to browse through the component’s source code and see how it works. This turned up a dead simple solution by extending List and overriding one function.
package com.tehfoo.components {
import spark.components.List;
public class ToggleList extends List {
override protected function calculateSelectedIndices(
index:int, shiftKey:Boolean, ctrlKey:Boolean):Vector.<int> {
return super.calculateSelectedIndices(index, shiftKey, true);
}
}
}
In case it’s hard to see, I’m just passing “true” in place of the ctrlKey variable. Just like that, the list now responds as if the control key (or command key for us Mac weenies) is always held down. This allows multiselect behavior without a modifier key.
Source for the entire component is above. Easy, peasy, toggle-berry pie!


