Button Group
Overview
A button group is a touch-friendly approach for selecting exactly one option from a short list of options.
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
Provides a touch-friendly approach for selecting one or more options from a short list of options
Name | Type | Default | Description |
---|---|---|---|
value | any | undefined | If 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. |
valueArray | Array | [] | 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-change | event | Event fired when the selected item in the button group changes:
| |
isMultiple | boolean | false | Set this property to true to allow the user to select multiple options. |
isVertical | boolean | false | Set this property to true to display the button group vertically. |
rui-button-group-option
Defines one option in a rui-button-group
Name | Type | Default | Description |
---|---|---|---|
value | any | null | The value assigned to the button group when the user selects this option |
iconType | string | '' | 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. |
isDisabled | boolean | false | Specifies whether this button group option is disabled. |
Implementation
Begin by importing the rui-buttons
module into your application.
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.
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.
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.
Import the newly created class into your TypeScript file. Define an array of shipping methods and an array of shipping method button options.
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.
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.
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.
Angular wrapper development
Wrapper reference
jha-button-group
Provides a touch-friendly approach for selecting one or more options from a short list of options
Name | Type | Default | Description |
---|---|---|---|
formControlName | string | undefined | 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. |
jhaValue | any | undefined | 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. |
jhaOptions | Array<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. |
jhaValueChange | event | Event 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. | |
jhaIsMultiple | boolean | false | Set this property to true to allow the user to select multiple options. |
jhaIsVertical | boolean | false | Set this property to true to display the button group vertically. |
jha-button-group-option
Class that defines one option in a jha-button-group. Each option is defined in TypeScript, not in the markup.
Name | Type | Default | Description |
---|---|---|---|
name | string | Text displayed for the button group option | |
value | any | The value assigned to the button group when the user selects this option | |
tooltip | string | (Optional) Tooltip displayed when the user hovers the mouse over this option | |
iconType | string | '' | (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. |
disabled | boolean | false | (Optional) Specifies whether or not this button group option is disabled |
Implementation
Begin by importing the JhaButtonsModule
module into your application.
Button Group in a Reactive Form
Define the options for the button group using an array of JhaButtonGroupOption
objects in your TypeScript.
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 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 the options for the button group using an array of JhaButtonGroupOption objects in your TypeScript.
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.
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.
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.
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.
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 the options for the button group using an array of JhaButtonGroupOption
objects in your TypeScript.
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.
Component playground
Design
Figma design
Dev Component | Design Component Name |
---|---|
Button group button | RUI / Buttons / Button Group Available values for the Style property:
Available values for the Position property:
|
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
- Sample App - Buttons
Dev Component | Design Component Name |
---|---|
Button group - unselected | JHA / Buttons / Button Group ; choose the “Default State” state |
Button group - selected | JHA / Buttons / Button Group ; choose the “Selected” state |
Button group - disabled | JHA / 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.