Developer Programs

Learn

Docs

Button Group

Components > Editing Data > Button Group
Use this component to...
Prompt the user to select one value from a small list of options

Overview

A button group is a touch-friendly approach for selecting exactly one option from a short list of options.

Domestic International

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.
    • This is the use case covered in this document.
  • 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.
  • 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

rui-button-group
rui-button-group
Module: rui-buttons - Package: @jkhy/responsive-ui-wc

Provides a touch-friendly approach for selecting one or more options from a short list of options

NameTypeDefaultDescription
valueanyundefinedIf the button group supports single selection (the default), bind this to a property in your controller that specifies the initially selected item in the button group.
valueArrayArray[]If the button group supports multiple selection, bind this to an array property in your controller that specifies the initially selected items in the button group.
rui-button-group-value-changeevent

Event fired when the selected item in the button group changes:

  • If the button group supports single selection (the default), the new selected item is passed as the event detail.
  • If the button group supports multiple selection, an array listing all selected items is passed as the event detail.
isMultiplebooleanfalseSet this property to true to allow the user to select multiple options.
isVerticalbooleanfalseSet this property to true to display the button group vertically.
rui-button-group-option
rui-button-group-option
Module: rui-buttons - Package: @jkhy/responsive-ui-wc

Defines one option in a rui-button-group

NameTypeDefaultDescription
valueanynullThe value assigned to the button group when the user selects this option
iconTypestring''The name of the icon to display in an icon button. You can find a list of all enterprise icons here. The use of custom icons is also supported.
isDisabledbooleanfalseSpecifies whether this button group option is disabled.

Implementation

Begin by importing the rui-buttons module into your application.

Import the module
// import into app.module
import '@jkhy/responsive-ui-wc/components/rui-buttons/rui-buttons-imports';

Define a backing value in the controller that will contain the selected value for the button group. Initialize that property to one of the value options in the button group.

Define a backing value
public flavorValue: string = 'Cho';

Add an instance of rui-button-group to your markup. Nest one or more rui-button-group-option components within the button group. Bind value to the TypeScript property that stores the backing value for this button group. Update that backing property when the rui-button-group-value-change event fires.

Button group HTML
<rui-button-group [value]="flavorValue" (rui-button-group-value-change)="flavorValue = $event.detail">
    <rui-button-group-option value="Cho">Chocolate</rui-button-group-option>
    <rui-button-group-option value="Van">Vanilla</rui-button-group-option>
    <rui-button-group-option value="Str">Strawberry</rui-button-group-option>
</rui-button-group>

Using Templates in Button Groups

There may be scenarios where you need to display multiple data in button group options instead of just displaying a simple text value. It turns out this is straightforward since the button group option content is projected and not just a simple string property. Let’s take a look at an example illustrating this.

First, let’s create a new class to define the object that will hold the data.

Define a class
export class ShippingMethod {
    constructor(
        public value: string,
        public name: string,
        public price: string
    ) { }
}

Import the newly created class into your TypeScript file. Define an array of shipping methods and an array of shipping method button options.

Define the shipping methods
import { ShippingMethod } from '../classes/shipping-method';

export class ButtonGroupComponent implements OnInit {

    public shippingMethodList: ShippingMethod[] = [];

    ngOnInit() {
        // Create list of shipping methods
        this.shippingMethodList = new Array<ShippingMethod>();
        this.shippingMethodList.push(new ShippingMethod('USPS', 'US Postal Service', '4.95'));
        this.shippingMethodList.push(new ShippingMethod('UPS Ground', 'UPS Ground', '6.95'));
        this.shippingMethodList.push(new ShippingMethod('UPS Overnight', 'UPS Overnight', '18.90'));

        // Auto-select the first item in the list
        this.shippingMethod.setValue(this.shippingMethodList[0].value);
    }
}

We now add the rui-button-group element to the markup. This is a vertical button group in our example, so we bind isVertical to true. We nest the template within each button group option.

Vertical templated button group HTML
<rui-button-group value="{{shippingMethod.value}}" (rui-button-group-value-change)="updateShippingMethod($event)" [isVertical]="true">

    <rui-button-group-option *ngFor="let shippingMethod of shippingMethodList" value="{{shippingMethod.value}}">
        <div class="shipping-option-button">
            <span class="shipping-option-name">{{shippingMethod.name}}</span>
            <span class="shipping-option-price">&dollar;{{shippingMethod.price}}</span>
        </div>
    </rui-button-group-option>

</rui-button-group>

Selecting Multiple Options in a Button Group

Button groups support either a single selection or multiple selection.

For multiple selection, as we saw in the other examples, first define an array property in the TypeScript that will contain the selected values for the button group. Initialize that property to the value of one or more of the options in the button group.

Define the available pizza toppings
public selectedToppings = [ 'Pepperoni', 'BlackOlives' ];

Then add an instance of rui-button-group to your markup with a separate rui-button-group-option for each of the button group options. Bind valueArray to the TypeScript array property that stores the backing value for this button group. Handle the rui-button-group-value-change event, storing the event’s detail back into the backing value in the TypeScript. Bind isMultiple to true to allow the component to accept multiple options.

Multiple selection button group HTML
<rui-button-group [isMultiple]="true" [valueArray]="selectedToppings" (rui-button-group-value-change)="selectedToppings = $event.detail">
    <rui-button-group-option value="Pepperoni">Pepperoni</rui-button-group-option>
    <rui-button-group-option value="Mushroom">Mushroom</rui-button-group-option>
    <rui-button-group-option value="ExtraCheese">Extra cheese</rui-button-group-option>
    <rui-button-group-option value="Sausage">Sausage</rui-button-group-option>
    <rui-button-group-option value="Onion">Onion</rui-button-group-option>
    <rui-button-group-option value="BlackOlives">Black olives</rui-button-group-option>
    <rui-button-group-option value="GreenPepper">Green pepper</rui-button-group-option>
</rui-button-group>

Angular wrapper development

Wrapper reference

jha-button-group
jha-button-group
Module: JhaButtonsModule - Package: @jkhy/responsive-ui-angular

Provides a touch-friendly approach for selecting one or more options from a short list of options

NameTypeDefaultDescription
formControlNamestringundefined

If you’re using jha-button-group for a Reactive form field, set this property to the field name in the Reactive form.

If you’re not using jha-button-group for a Reactive form field, use the jhaValue property instead.

jhaValueanyundefined

If you’re not using jha-button-group for a Reactive form field, use two-way binding to bind this to a property in your TypeScript that tracks the value for the currently selected item in the button group.

This value is updated automatically when the user selects an option in the group. If you programmatically update the backing value in the TypeScript, the button group updates its selected item to match the new value.

You must use two-way binding because this value can be changed within the button group and within your TypeScript, so the two must be kept in sync.

If you’re using jha-button-group for a Reactive form field, use the formControlName property instead.

jhaOptionsArray<JhaButtonGroupOption>[]Bind this property to an array of JhaButtonGroupOption objects in your TypeScript. Each item in the array corresponds to one option in the button group.
jhaValueChangeeventEvent fired when the selected item in the button group changes. Use this event if you need to update anything in your TypeScript whenever this value changes.
jhaIsMultiplebooleanfalseSet this property to true to allow the user to select multiple options.
jhaIsVerticalbooleanfalseSet this property to true to display the button group vertically.
jha-button-group-option
jha-button-group-option
Module: JhaButtonsModule - Package: @jkhy/responsive-ui-angular

Class that defines one option in a jha-button-group. Each option is defined in TypeScript, not in the markup.

NameTypeDefaultDescription
namestringText displayed for the button group option
valueanyThe value assigned to the button group when the user selects this option
tooltipstring(Optional) Tooltip displayed when the user hovers the mouse over this option
iconTypestring''(Optional) The name of the icon to display in an icon button. You can find a list of all enterprise icons here. The use of custom icons is also supported.
disabledbooleanfalse(Optional) Specifies whether or not this button group option is disabled

Implementation

Begin by importing the JhaButtonsModule module into your application.

Import the module
// import into app.module
import { JhaButtonsModule } from '@jkhy/responsive-ui-angular/jha-buttons';

@NgModule({
    imports: [
        ...
        JhaButtonsModule,
        ...
    ]
})
export class AppModule(){}

Button Group in a Reactive Form

Define the options for the button group using an array of JhaButtonGroupOption objects in your TypeScript.

Define the button group options
import { JhaButtonGroupOption } from '@jkhy/responsive-ui-angular/jha-buttons';

public addressTypeOptions: JhaButtonGroupOption[] = [
    new JhaButtonGroupOption('Domestic', 'Domestic'),
    new JhaButtonGroupOption('International', 'International')
];

Then add an instance of jha-button-group to your markup. Specify the formControlName for this field in the Reactive form. Bind jhaOptions to the array of JhaButtonGroupOption objects defined in the TypeScript.

Button group HTML
<jha-button-group formControlName="addressType" [jhaOptions]="addressTypeOptions"></jha-button-group>

Button Group Not in a Reactive Form

Define a property in the TypeScript that will contain the selected value for the button group. Initialize that property to one of the options in the button group.

Define a backing value
public flavorValue: string = 'Cho';

Define the options for the button group using an array of JhaButtonGroupOption objects in your TypeScript.

Define the button group options
import { JhaButtonGroupOption } from '@jkhy/responsive-ui-angular/jha-buttons';

public flavorOptions: JhaButtonGroupOption[] = [
    new JhaButtonGroupOption('Chocolate', 'Cho'),
    new JhaButtonGroupOption('Vanilla', 'Van'),
    new JhaButtonGroupOption('Strawberry', 'Str')
];

Then add an instance of jha-button-group to your markup. Two-way bind jhaValue to the TypeScript property that stores the backing value for this button group. Bind jhaOptions to the array of JhaButtonGroupOption objects defined in the TypeScript.

Button group HTML
<jha-button-group [(jhaValue)]="flavorValue" [jhaOptions]="flavorOptions"></jha-button-group>

Using Templates in Button Groups

There may be scenarios where you need to display multiple data in button group options instead of just displaying a simple text value. This can be done using ng-template.

First, let’s create a new class to define the object that will hold the data.

Define a class
export class ShippingMethod {
    constructor(
        public value: string,
        public name: string,
        public price: string
    ) { }
}

Import the newly created class and the JhaButtonGroupOption class into your TypeScript file. Define an array of shipping methods and an array of shipping method button options.

Build the array of shipping methods, then loop through that array to build a list of button options.

Define the shipping methods
import { ShippingMethod } from '../classes/shipping-method';
import { JhaButtonGroupOption } from '@jkhy/responsive-ui-angular/jha-buttons';

export class ButtonGroupComponent implements OnInit {

    public shippingMethodList: ShippingMethod[] = [];
    public shippingMethodOptions: JhaButtonGroupOption[] = [];

    ngOnInit() {
        this.shippingMethodList = new Array<ShippingMethod>();
        this.shippingMethodList.push(new ShippingMethod('USPS', 'US Postal Service', '4.95'));
        this.shippingMethodList.push(new ShippingMethod('UPS Ground', 'UPS Ground', '6.95'));
        this.shippingMethodList.push(new ShippingMethod('UPS Overnight', 'UPS Overnight', '18.90'));

        // Add these to the list of shipping options
        for (let i = 0; i < this.shippingMethodList.length; i++ ) {
            this.shippingMethodOptions.push(new JhaButtonGroupOption(this.shippingMethodList[i].name, this.shippingMethodList[i]));
        }
    }
}

Add the jha-button-group element to your markup. Set the formControlName and bind jhaOptions to the array of shippingMethodOptions. In our example, this is a vertical button group, so jhaIsVertical is bound to true.

Create an ng-template and build the markup to match your interface.

Vertical templated button group HTML
<jha-button-group formControlName="shippingMethod" [jhaOptions]="shippingMethodOptions" [jhaIsVertical]="true">
    <ng-template let-shippingOption>
        <div class="shipping-option-button">
            <span class="shipping-option-name">{{shippingOption.name}}</span>
            <span class="shipping-option-price">${{shippingOption.value.price}}</span>
        </div>
    </ng-template>
</jha-button-group>

Selecting Multiple Options in a Button Group

Button groups, whether inside of a reactive form or not, can be set to accept multiple options.

As in the other examples, define a property in the TypeScript that will contain the selected values for the button group. Initialize that property to the value of one or more of the options in the button group.

Define a backing value
public flavorValues: string[] = ['Cho'];

Define the options for the button group using an array of JhaButtonGroupOption objects in your TypeScript.

Define the available options
import { JhaButtonGroupOption } from '@jkhy/responsive-ui-angular/jha-buttons';

public flavorOptions: JhaButtonGroupOption[] = [
    new JhaButtonGroupOption('Chocolate', 'Cho'),
    new JhaButtonGroupOption('Vanilla', 'Van'),
    new JhaButtonGroupOption('Strawberry', 'Str')
];

Then add an instance of jha-button-group to your markup. Two-way bind jhaValue to the TypeScript property that stores the backing value for this button group. Bind jhaOptions to the array of JhaButtonGroupOption objects defined in the TypeScript. Finally, bind jhaIsMultiple to true to allow the component to accept multiple options.

Multiple selection button group HTML
<jha-button-group [(jhaValue)]="flavorValues" [jhaOptions]="flavorOptions" [jhaIsMultiple]="true"></jha-button-group>

Component playground


Design

Figma design

Figma design info
You can find this component in the Components - Buttons page in the Figma UI Kit.
Dev ComponentDesign Component Name
Button group button

RUI / Buttons / Button Group

Available values for the Style property:

  • Unselected
  • Selected
  • Disabled

Available values for the Position property:

  • Left
  • Middle
  • Right

Change the button text to an appropriate value. Resize the button to fit the text, but be careful that you’re resizing the outer component and not an inner layer within the component.


Adobe XD design

Adobe XD design info
You can find this component in these artboards in the Adobe XD design samples:
  • Sample App - Buttons
Dev ComponentDesign Component Name
Button group - unselectedJHA / Buttons / Button Group ; choose the “Default State” state
Button group - selectedJHA / Buttons / Button Group ; choose the “Selected” state
Button group - disabledJHA / Buttons / Button Group / choose the “Disabled” state

Change the button text to an appropriate value. Resize the button to fit the text, but be careful that you’re resizing the outer component and not an inner layer within the component.


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 Wed Sep 25 2024