# ui/uidropdown (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/ui/uidropdown/

Dropdown. (combo box)

## UIDropdown extends WorldNode

Dropdown. (combo box)
- The closed header button shows the current value, and pressing it expands the option list downward.
- Usage:
    const dropdown = new UIDropdown({ width: 180, height: 30 });
    dropdown.setOptionList(["Dark", "Light", "System"]);
    dropdown.setChangedEvent((self) => { self.getSelectedText(); });
    panelNode.addChild(dropdown);

Example: Dropdown for picking graphics quality

```js
import { Engine, EngineConfiguration, UIScene, UIDropdown, Color, Vector2 } from "./build/vanilla.js";

class DropdownScene extends UIScene {
	create() {
		super.create();
		const dropdown = new UIDropdown({ width: 200, height: 32, fontSize: 14, highlightColor: new Color(0.3, 0.8, 1, 0.3) });
		dropdown.setOptionList(["Low", "Medium", "High", "Ultra"]);
		dropdown.setSelectedIndex(2);
		dropdown.setChangedEvent((self) => {
			const selectedText = self.getSelectedText();
			console.log(selectedText);
		});
		dropdown.setLocalPosition(Vector2.create(220, 80));
		const root = this.getRoot();
		root.addChild(dropdown);
		dropdown.setOpen(true);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new DropdownScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
```

- `constructor(options = {})` (constructor, since 0.3.0): Constructor.
  - `options` object: { width = 180, height = 30, optionHeight = 28, fontSize = 12,
                            baseColor?, popupColor?, highlightColor?, textColor? }
- `setOptionList(optionList)` (method, since 0.3.0): Sets the option list. (rebuilds the expanded list rows)
  - `optionList` string[]: Option text list. (copied and kept, the selected index is reduced to the last one if it falls outside the list)
- `selectIndex(optionIndex)` (method, since 0.3.0): Handles selection.
  - `optionIndex` number: Index of the option to pick. (does nothing if outside the list, picking folds the list and calls changedEvent only if it changed)
- `setOpen(isOpen)` (method, since 0.3.0): Expand / collapse.
  - When expanding, moves the list to the tree root so it is not hidden by sibling nodes drawn later. (restored when collapsed)
  - `isOpen` boolean: If true, it unfolds.
- `setChangedEvent(changedEvent)` (method, since 0.3.0): Sets the handler for when the selection changes. (only when a different option is picked with selectIndex, not called by setSelectedIndex)
  - `changedEvent` Function: (self)

  Example: Receiving the picked option

  ```js
  import { UIDropdown } from "./build/vanilla.js";
  
  const dropdown = new UIDropdown({ width: 160 });
  dropdown.setOptionList(["Low", "Medium", "High"]);
  dropdown.setChangedEvent((self) => {
  	const selectedText = self.getSelectedText();
  	console.log(selectedText);
  });
  dropdown.setSelectedIndex(1);
  dropdown.selectIndex(2);
  ```

- `setSelectedIndex(selectedIndex)` (method, since 0.3.0): Sets the selected index. (changes only the header text and the highlight without calling the handler)
  - `selectedIndex` number: Selected index. (clamped to 0 ~ option count - 1)
- `getSelectedIndex()` (method, since 0.3.0): Returns the selected index.
  - returns number: Selected index. (0 even if there are no options)
- `getSelectedText()` (method, since 0.3.0): Returns the text of the picked option.
  - returns string: Text of the picked option. (empty string if there are no options)
- `isOpen()` (method, since 0.3.0): Returns whether it is unfolded.
  - returns boolean: True if unfolded.
- `getOptionList()` (method, since 0.4.0): Returns the option list.
  - returns string[]: List of option texts. (In the order given to setOptionList, do not modify)
- `getPopupNode()` (method, since 0.4.0): Returns the popup list node. (A child of the tree root while open)
  - returns WorldNode: Popup list node.
- `getOptionNodes()` (method, since 0.4.0): Returns the option row nodes. (In option order, rebuilt on every setOptionList)
  - returns WorldNode[]: Row nodes. (a new array)

  Example: Reading the option row nodes and selecting the second row as if it were pressed

  ```js
  import { UIDropdown } from "./build/vanilla.js";
  
  const dropdown = new UIDropdown({ width: 160 });
  dropdown.setOptionList(["Low", "Medium", "High"]);
  const optionNodes = dropdown.getOptionNodes();
  console.log(optionNodes.length, optionNodes[1].getName(), dropdown.getOptionList()[1]);
  dropdown.selectIndex(1);
  console.log(dropdown.getSelectedText());
  ```

