Overview
Of the various controls being used on any data capturing form, Dropdown is one of the most commonly used. It allows users to choose a value from a predefined list. By default, dropdowns appeal to users with a list of options to read, analyze and select. It would be great if we could help users with an icon representing the selection option. This will help users to visualize and understand the options better.
In this article, we will explore the capabilities provided by Office UI Fabric dropdown control and how we can extend it to facelift our dropdowns to make them more appealing.
Office Fabric Dropdown Control
The Office UI Fabric offers numerous dropdown variations including basic, multi-select, controlled single-select, controlled multi-select, and customized. The customized dropdown option allows us to add options to dropdown with Office UI Fabric icons.
Most Ul Fabric icons come with inverse, fill, reversed, or solid styling. For example, a variation of the Circle icon offers CircleRing and CircleShapeSolid.
Bus icons offer Bus and BusSolid variations as below,
We can effectively use these variations of inverse, fill, reversed, or solid styling to facelift our dropdown controls.
Facelift Dropdowns
Add the below imports,
- import { Dropdown, IDropdownOption, IDropdownProps } from 'office-ui-fabric-react/lib/Dropdown';
- import { Icon } from 'office-ui-fabric-react/lib/Icon';
Define an array to hold a custom choice of colors.
- private dropdownOptions: { key: string, text: string, data: any }[] = [];
Inside render method, add a few colors to our array.
- public constructor(props) {
- super(props);
- this.dropdownOptions.push({
- key: "Red",
- text: "Red",
- data: { icon: 'CircleShapeSolid', colorName: "#ff0000" }
- });
- this.dropdownOptions.push({
- key: "Green",
- text: "Green",
- data: { icon: 'CircleShapeSolid', colorName: "#00ff00" }
- });
- this.dropdownOptions.push({
- key: "Blue",
- text: "Blue",
- data: { icon: 'CircleShapeSolid', colorName: "#0000ff" }
- });
- this.dropdownOptions.push({
- key: "Purple",
- text: "Purple",
- data: { icon: 'CircleShapeSolid', colorName: "#800080" }
- });
- this.dropdownOptions.push({
- key: "Orange",
- text: "Orange",
- data: { icon: 'CircleShapeSolid', colorName: "#ffa500" }
- });
- }
- <Dropdown
- label="Color"
- onChange={this.onSelectionChanged}
- onRenderTitle={this.onRenderTitle}
- onRenderOption={this.onRenderOption}
- onRenderCaretDown={this.onRenderCaretDown}
- options={this.dropdownOptions}
- />
We will make use of the below dropdown events.
|
Event
|
Description
|
|
onChange
|
Callback issued when the selected option changes.
|
|
onRenderTitle
|
Optional custom renderer for selected option displayed in the input.
|
|
onRenderOption
|
Optional custom renderer for option in the dropdown.
|
|
onRenderCaretDown
|
Optional custom renderer for chevron icon.
|
Implement onRenderOption event to uplift our dropdown by showing colored icon next to dropdown option.
- private onRenderOption(option: IDropdownOption): JSX.Element {
- return (
- <div>
- {option.data && option.data.icon && (
- <Icon style={{ marginRight: '8px', color: option.data.colorName }} iconName={option.data.icon} aria-hidden="true" title={option.data.icon} />
- )}
- <span>{option.text}</span>
- </div>
- );
- }
Implement onRenderTitle event to show the selected option with colored icon.
- private onRenderTitle(options: IDropdownOption[]): JSX.Element {
- const option = options[0];
- return (
- <div>
- {option.data && option.data.icon && (
- <Icon style={{ marginRight: '8px', color: option.data.colorName }} iconName={option.data.icon} aria-hidden="true" title={option.data.icon} />
- )}
- <span>{option.text}</span>
- </div>
- );
- }

Bhargava ChPosted Mar 12, 2020, 3:30 AM
Thanks a lot Nandeep, but some clients are very strict about browser so in that case do we have any kind approaches to achieve this?
Bhargava ChPosted Mar 10, 2020, 3:47 AM
Nice article and onChange is not working in IE7 to 9.