Developer Programs

Learn

Docs

Dropdown Input / Select

Components > Editing Data > Dropdown Input / Select
Use this component to...
Prompt the user to select exactly one item from a medium-sized dropdown list of options

Overview

Use the dropdown input component to prompt the user to select exactly one item from a medium-sized dropdown list of options.

Responsive UI offers two different components for this use case:

  • The native HTML select component provides a user experience that is unique to each different type of device.
    • The select component displays a simple text string for each item in the list.
  • The PrimeNG p-dropdown component provides the same themed user experience across all types of devices.
    • The p-dropdown component can display either a text string or multiple data for each dropdown item, using a template.
Here is the native HTML select element, with its dropdown menu hidden
Here is the native HTML select element, with its dropdown menu **hidden**
Here is the same native HTML select element, with its dropdown menu shown on macOS
Here is the same **native HTML select element**, with its dropdown menu **shown**
Here is the p-dropdown component, displaying templated items and a themed appearance
Here is the p-dropdown component, displaying templated items and a themed appearance

Implementation approaches for selecting one item from a list

If your use case is prompting the user to select exactly one item from a list, the best implementation approach to use is based on the number of list items the user can select from:

  • If the number of list items is small (say 5 items or less) and the list items can all be displayed horizontally in a row, then display them as a button group.
  • If the number of list items is relatively small and all list items need to remain visible on the screen at once, then display them as a vertical list of radio buttons.
  • If the number of list items is medium (say larger than 5 but smaller than around 50 items), then display the list in a dropdown input that contains all possible values.
    • This is the use case covered in this document.
  • If the number of list items is large (say larger than 50 items but smaller than around 1000 items), then use a regular text input with autocomplete functionality, where all list items that are a subset of the current input display in a dropdown. Since the user’s input is freeform, you’ll still need to validate it.
  • If the number of list items is very large (say larger than 1000 items), then just prompt for freeform text and validate that.

Development

Web component development

Component reference

p-dropdown
p-dropdown
Module: DropdownModule - Package: primeng
⚠️ This component requires the use of Angular and will not work in non-Angular applications.

Allows user to edit date/time values in a non-mobile environment

View the p-dropdown reference doc

If you're adding PrimeNG to your solution for the first time, reference their setup documentation. It's important to note that the inclusion of the nova-light theme css is not necessary to work with Responsive UI. Styling for the PrimeNG components is provided by Responsive UI's theming.

Native HTML select component implementation

Display the list using a select element, using one option element for each item in the list.

Native HTML select implementation
<select id="State" formControlName="state" class="rui-form-control" required>
    <option *ngFor="let state of stateList" value="{{state.value}}">
        {{state.name}}
    </option>
</select>

p-dropdown implementation

The select component described above displays a simple text value for each option in the list. If you need to display a template for each option that includes more than one value per option, use the PrimeNg p-dropdown component and use ng-template to specify the template for the list items.

In the TypeScript, import SelectItem and define the data for the list of options.

Defining the list items
import { SelectItem } from 'primeng/api';

...

public recentAccounts: SelectItem[] =
[
    { label: 'John Doe', value: 'A000007' },
    { label: 'Kevin Peterson', value: '141' },
    { label: 'Frank Kristopoulos', value: 'A000013' },
    { label: 'Fidel Martin', value: '80103' }
];

In the HTML, specify one ng-template for the selected option when the component is in its rest state (pTemplate="selectedItem") and another ng-template for options in the dropdown (pTemplate="item"). The example here uses flex layout to display the customer name (the item’s label property) and the customer account number (the item’s value property).

p-dropdown implementation
<p-dropdown [options]="recentAccounts" formControlName="recentAccounts" styleClass="rui-form-control">
    <ng-template let-item pTemplate="selectedItem"> 
        <div style="display:flex">
            <div class="rui-regular-text-label" style="flex: 1 0 0%;">{{item.label}}</div>
            <div class="rui-bright-text-label" style="flex: 0 0 auto">{{item.value}}</div>
        </div>
    </ng-template>
    <ng-template let-item pTemplate="item">
        <div style="display:flex">
            <div class="rui-regular-text-label" style="flex: 1 0 0%">{{item.label}}</div>
            <div class="rui-bright-text-label" style="flex: 0 0 auto">{{item.value}}</div>
        </div>
    </ng-template>
</p-dropdown>

Angular component development

Component reference

p-dropdown
p-dropdown
Module: DropdownModule - Package: primeng

Allows user to edit date/time values in a non-mobile environment

View the p-dropdown reference doc

If you're adding PrimeNG to your solution for the first time, reference their setup documentation. It's important to note that the inclusion of the nova-light theme css is not necessary to work with Responsive UI. Styling for the PrimeNG components is provided by Responsive UI's theming.

Native HTML select component implementation

Display the list using a select element, using one option element for each item in the list.

Native HTML select implementation
<select id="State" formControlName="state" class="form-control" required>
    <option *ngFor="let state of stateList" value="{{state.value}}">
        {{state.name}}
    </option>
</select>

p-dropdown implementation

The select component described above displays a simple text value for each option in the list. If you need to display a template for each option that includes more than one value per option, use the PrimeNg p-dropdown component and use ng-template to specify the template for the list items.

In the TypeScript, import SelectItem and define the data for the list of options.

Defining the list items
import { SelectItem } from 'primeng/api';

...

public recentAccounts: SelectItem[] =
[
    { label: 'John Doe', value: 'A000007' },
    { label: 'Kevin Peterson', value: '141' },
    { label: 'Frank Kristopoulos', value: 'A000013' },
    { label: 'Fidel Martin', value: '80103' }
];

In the HTML, specify one ng-template for the selected option when the component is in its rest state (pTemplate="selectedItem") and another ng-template for options in the dropdown (pTemplate="item"). The example here uses flex layout to display the customer name (the item’s label property) and the customer account number (the item’s value property).

p-dropdown implementation
<p-dropdown [options]="recentAccounts" formControlName="recentAccounts" styleClass="form-control">
    <ng-template let-item pTemplate="selectedItem"> 
        <div style="display:flex">
            <div class="jha-regular-text-label" style="flex: 1 0 0%;">{{item.label}}</div>
            <div class="jha-bright-text-label" style="flex: 0 0 auto">{{item.value}}</div>
        </div>
    </ng-template>
    <ng-template let-item pTemplate="item">
        <div style="display:flex">
            <div class="jha-regular-text-label" style="flex: 1 0 0%">{{item.label}}</div>
            <div class="jha-bright-text-label" style="flex: 0 0 auto">{{item.value}}</div>
        </div>
    </ng-template>
</p-dropdown>

Design

Figma design

Figma design info
You can find this component in the Components - Forms page in the Figma UI Kit.
Dev ComponentDesign Component Name
Dropdown inputRUI / Forms / Dropdown Input

Adobe XD design

Adobe XD design info
You can find this component in these artboards in the Adobe XD design samples:
  • Sample App - Editing Form Data
Dev ComponentDesign Component Name
Dropdown inputJHA / Forms / Dropdown Input

Support options
Have questions on this topic?
Join the Responsive UI team in Microsoft Teams to connect with the community.
See something in this page that needs to change?
Send us feedback on this page.
Last updated Thu Jun 27 2024