# single-choice-dropdown

## Overview

{% hint style="info" %}
**Version compatibility**

* `single-choice-dropdown` is supported in all versions of the JourneyApps Container and Runtime.
* It received several functional updates in version **4.84.0** of the JourneyApps Runtime.
  {% endhint %}

A `single-choice-dropdown` component allows users to make a single selection from a pre-defined set of options in a dropdown component.

### Basic Example

{% code title="main.view\.xml" %}

```xml
<var name="selected_country" type="single-choice">
    <option key="us">USA</option>
    <option key="uk">UK</option>
    <option key="de">Germany</option>
    ...
</var>

<single-choice-dropdown label="Country of residence" bind="selected_country" required="false" />
```

{% endcode %}

![](https://2865107717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9TCHLR67eLHBOjPvHhud%2Fuploads%2Fgit-blob-1a3216174c2d5167c10b76f7ea1f787f3f80608b%2Fsingle-choice-dropdown-example-selected.png?alt=media)

![](https://2865107717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9TCHLR67eLHBOjPvHhud%2Fuploads%2Fgit-blob-59358e3fd6dc7cc9391415df3d27f64cc9dc4572%2Fsingle-choice-dropdown-example-options.png?alt=media)

## Standard Attributes

### `bind`

{% content-ref url="../xml-fields/bind" %}
[bind](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/bind)
{% endcontent-ref %}

### `label`

{% content-ref url="../xml-fields/label" %}
[label](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/label)
{% endcontent-ref %}

### `required`

{% content-ref url="../xml-fields/required" %}
[required](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/required)
{% endcontent-ref %}

## Advanced Attributes

### `align-content` <a href="#standard-attributes" id="standard-attributes"></a>

{% hint style="info" %}
**Version compatibility**

`align-content` was introduced in version **4.84.0** of the JourneyApps Runtime.
{% endhint %}

{% content-ref url="../xml-fields/align-content" %}
[align-content](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/align-content)
{% endcontent-ref %}

### `align-dialog-content`

**Optional**

**Type**: `center` | `left` | `right`

**Default**: `center`

{% hint style="info" %}
**Version compatibility**

`align-dialog-content` was introduced in version **4.84.0** of the JourneyApps Runtime.
{% endhint %}

Specifies how the content of the list of options dialog should be aligned. This includes the display value of each option, as well as the header text of the dialog.

```xml
<single-choice-dropdown label="Country of residence" align-dialog-content="left" bind="selected_country" />
```

### `align-label`

{% content-ref url="../xml-fields/align-label" %}
[align-label](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/align-label)
{% endcontent-ref %}

### `clear-button-visibility`

{% content-ref url="../xml-fields/clear-button-visibility" %}
[clear-button-visibility](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/clear-button-visibility)
{% endcontent-ref %}

### `dialog-title`

**Optional**

**Type**: `string` (static text, a [format string](https://docs.journeyapps.com/reference/app-features/xml-format-strings) or the return value of a [JS/TS function](https://docs.journeyapps.com/reference/app-features/calling-js-functions-from-xml))

**Default**: "Choose an option"

{% hint style="info" %}
**Version compatibility**

`dialog-title` was introduced in version **4.84.0** of the JourneyApps Runtime.
{% endhint %}

Header text of the dialog that displays the list of options of the `single-choice-dropdown`.

```xml
<single-choice-dropdown label="Country of residence" dialog-title="Choose the country of residence" bind="selected_country" />
```

### `disabled`

{% content-ref url="../xml-fields/disabled" %}
[disabled](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/disabled)
{% endcontent-ref %}

### `empty-message`

{% hint style="info" %}
**Version compatibility**

`empty-message` was introduced in version **4.84.0** of the JourneyApps Runtime.
{% endhint %}

**Optional**

**Type**: `string` (static text, a [format string](https://docs.journeyapps.com/reference/app-features/xml-format-strings) or the return value of a [JS/TS function](https://docs.journeyapps.com/reference/app-features/calling-js-functions-from-xml))

**Default**: unset

Text that is displayed if no options are available to list once the user opens the `single-choice-dropdown`.

```xml
<single-choice-dropdown label="Country of residence" bind="selected_country" empty-message="No countries are available. Please contact your administrator." />
```

### `icon-position`

{% hint style="info" %}
**Version compatibility**

`icon-position` was introduced in version **4.86.1** of the JourneyApps Runtime.
{% endhint %}

{% content-ref url="../xml-fields/icon-position" %}
[icon-position](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/icon-position)
{% endcontent-ref %}

### `id`

{% content-ref url="../xml-fields/id" %}
[id](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/id)
{% endcontent-ref %}

### `label-case`

{% hint style="info" %}
**Version compatibility**

`label-case` was introduced in version **4.84.0** of the JourneyApps Runtime.
{% endhint %}

{% content-ref url="../xml-fields/label-case" %}
[label-case](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/label-case)
{% endcontent-ref %}

### `label-color`

{% content-ref url="../xml-fields/label-color" %}
[label-color](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/label-color)
{% endcontent-ref %}

### `modifier-text`

{% hint style="info" %}
**Version compatibility**

`modifier-text` was introduced in version **4.84.0** of the JourneyApps Run
{% endhint %}

{% content-ref url="../xml-fields/modifier-text" %}
[modifier-text](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/modifier-text)
{% endcontent-ref %}

### `on-change`

{% content-ref url="../xml-fields/on-change" %}
[on-change](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/on-change)
{% endcontent-ref %}

### `placeholder`

{% content-ref url="../xml-fields/placeholder" %}
[placeholder](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/placeholder)
{% endcontent-ref %}

### `search-controls`

**Optional**

**Type**: `auto` | `none` | `show`

**Default**: `auto`

{% hint style="info" %}
**Version compatibility**

`search-controls` was introduced in version **4.84.0** of the JourneyApps Runtime`.`
{% endhint %}

Set the visibility of the search box of the `single-choice-dropdown` component. `auto` shows the search box when the list of options contains 12 options or more. `none` never shows the search box, and `show` always shows the search box at the top of the list of options.

```xml
<single-choice-dropdown label="Country of residence" bind="selected_country" search-controls="show" />
```

### `show-if` and `hide-if`

{% content-ref url="../xml-fields/show-if" %}
[show-if](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/show-if)
{% endcontent-ref %}

{% content-ref url="../xml-fields/hide-if" %}
[hide-if](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/hide-if)
{% endcontent-ref %}

## Component Methods

The following component methods are available when an [`id`](https://docs.journeyapps.com/reference/build/ui-components/xml-fields/id) is assigned to the component and `component.singleChoiceDropdown({id:'my-id'})` is called from JS/TS:

### `clear`

Programmatically clear the selected value bound to the `single-choice-dropdown`.

### `clearSearch`

Programmatically clear a value entered into the search box.

### `openDropdown`

Programmatically open the list of items.

### `closeDropdown`

Programmatically close the list of items.

### `scrollDown`

Programmatically scroll down the list of items when the `single-choice-dropdown` is opened.

### `scrollIntoView`

Programmatically scroll until the `single-choice-dropdown` is visible in the view.

### `scrollUp`

Programmatically scroll up the list of items when the `single-choice-dropdown` is opened.

### `selectItem`

Programmatically select an item from the list by its label.

{% code title="main.view\.xml" %}

```xml
<var name="favorite_color" type="single-choice">
    <option key="green">Green</option>
    <option key="red">Red</option>
    <option key="yellow">Yellow</option>
</var>

<single-choice-dropdown id="my-dropdown" bind="favorite_color" label="Favorite Color" />
```

{% endcode %}

{% code title="main.js" %}

```javascript
function select() {
    component.singleChoiceDropdown({id: 'my-dropdown'}).openDropdown(); // Need to open the dropdown first
    component.singleChoiceDropdown({id: 'my-dropdown'}).selectItem('Yellow');
}
```

{% endcode %}

### `selectItemByIndex`

Programmatically select an item from the list by its index. **Note**: Indexes begin at 1.

{% code title="main.view\.xml" %}

```xml
<var name="favorite_color" type="single-choice">
    <option key="green">Green</option>
    <option key="red">Red</option>
    <option key="yellow">Yellow</option>
</var>

<single-choice-dropdown id="my-dropdown" bind="favorite_color" label="Favorite Color" />
```

{% endcode %}

{% code title="main.js" %}

```javascript
function select() {
    component.singleChoiceDropdown({id: 'my-dropdown'}).openDropdown(); // Need to open the dropdown first
    component.singleChoiceDropdown({id: 'my-dropdown'}).selectItemByIndex(2);
    // Selects "Red"
}
```

{% endcode %}

### `setSearch`

Programmatically enter a search value and triggers a search of the `single-choice-dropdown`.
