Skip to content

Commit 8d335a8

Browse files
authored
kb(radiobuttongroup): Update KB example and styles (#2283)
* kb(radiobuttongroup): Update KB * kb(radiogroup): Provide example for older versions too * rename CSS class
1 parent 29ccedc commit 8d335a8

File tree

1 file changed

+111
-21
lines changed

1 file changed

+111
-21
lines changed

knowledge-base/radiogroup-like-buttongroup.md

Lines changed: 111 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type: how-to
55
page_title: How to Style the RadioGroup to Look Like a ButtonGroup
66
slug: radiogroup-kb-like-buttongroup
77
position:
8-
tags: radiogroup, button, buttongroup
8+
tags: radiogroup, button, buttongroup, styles
99
ticketid: 1547117
1010
res_type: kb
1111
---
@@ -29,15 +29,20 @@ How to implement RadioGroup behavior, but style the label and radio input pairs
2929

3030
Use custom CSS to make the radio inputs invisible and style their labels to look like buttons.
3131

32-
The RadioGroup will look the same as a [ButtonGroup with single selection]({%slug buttongroup-selection%}#single-selection). The major difference is that the RadioGroup has a single value of `<T>`, while each button in the ButtonGroup is bound to a separate `boolean` value for its selected state.
32+
The RadioGroup will look similar to a [ButtonGroup with single selection]({%slug buttongroup-selection%}#single-selection). The major difference is that the RadioGroup has a single value of type `<T>`, while each button in the ButtonGroup is bound to a separate `boolean` value for its selected state.
3333

34-
>caption Style the RadioGroup Like a ButtonGroup
34+
>tip The following example is for versions 6.0 and above, which feature an [updated HTML rendering for the RadioGroup]({%slug changes-in-6-0-0%}#radiogroup). If you are using an older version, use the [alternative CSS code from the section below](#solution-up-to-version-511).
35+
36+
>caption Style the RadioGroup like a ButtonGroup
3537
3638
````CSHTML
37-
<h1>RadioGroup like a ButtonGroup</h1>
39+
<h1 style="font-size:1.5rem;">RadioGroup like a ButtonGroup</h1>
3840
39-
<br />
40-
<TelerikRadioGroup Class="labels-only"
41+
<p>Selected Value: <strong><code>@CurrentStatus</code></strong></p>
42+
43+
<h2 style="font-size: 1.2rem; margin: .6em 0">Workaround with RadioGroup and CSS</h2>
44+
45+
<TelerikRadioGroup Class="radio-buttons"
4146
Data="@Statuses"
4247
@bind-Value="@CurrentStatus"
4348
ValueField="@nameof(Status.Id)"
@@ -55,61 +60,82 @@ The RadioGroup will look the same as a [ButtonGroup with single selection]({%slu
5560
LabelPosition="RadioGroupLabelPosition.After">
5661
</TelerikRadioGroup>
5762
58-
<br />
59-
<p>Selected Value: <strong>@CurrentStatus.ToString()</strong></p>
63+
<h2 style="font-size: 1.2rem; margin: .6em 0">Built-in Approach with ButtonGroup</h2>
64+
65+
<TelerikButtonGroup>
66+
@foreach (var item in Statuses)
67+
{
68+
<ButtonGroupToggleButton Selected="@( item.Id == CurrentStatus )"
69+
SelectedChanged="@( (bool newSelected) => ToggleButtonSelectedChanged(newSelected, item.Id) )">
70+
@item.Text
71+
</ButtonGroupToggleButton>
72+
}
73+
</TelerikButtonGroup>
6074
6175
<style>
6276
/* remove the horizontal space between the RadioGroup items */
63-
.labels-only {
64-
display: flex;
77+
.k-radio-list.radio-buttons {
78+
gap: 0;
6579
}
6680
6781
/* reset styles and support absolute radio inputs */
68-
.labels-only .k-radio-item {
82+
.radio-buttons .k-radio-list-item {
6983
margin: 0;
7084
padding: 0;
7185
position: relative;
7286
}
7387
7488
/* hide the radio buttons */
75-
.labels-only .k-radio {
89+
.radio-buttons .k-radio-wrap {
7690
opacity: 0;
7791
position: absolute;
7892
}
79-
.labels-only .k-radio:checked::before {
93+
94+
.radio-buttons .k-radio-wrap::before {
8095
display: none;
8196
}
8297
8398
/* make the radio labels look like buttons */
84-
.labels-only .k-radio-label {
99+
.radio-buttons .k-radio-label {
85100
display: inline-block;
86101
margin: 0;
87-
padding: .4em .6em .3em;
102+
padding: .2em .6em;
88103
border: 1px solid #ff6358;
89104
border-left-width: 0;
90105
position: relative;
106+
font-size: var(--kendo-font-size, inherit);
91107
color: #ff6358;
92108
}
93109
94110
/* first and last button borders */
95-
.labels-only .k-radio-item:first-child .k-radio-label {
111+
.radio-buttons .k-radio-list-item:first-child .k-radio-label {
96112
border-left-width: 1px;
97113
border-radius: .2em 0 0 .2em;
98114
}
99-
.labels-only .k-radio-item:last-child .k-radio-label {
115+
116+
.radio-buttons .k-radio-list-item:last-child .k-radio-label {
100117
border-radius: 0 .2em .2em 0;
101118
}
102119
103120
/* button selected state */
104-
.labels-only .k-radio:checked + .k-radio-label {
121+
.radio-buttons .k-radio-wrap:has(.k-radio:checked) + .k-radio-label {
105122
background-color: #ff6358;
106123
color: #fff;
107124
}
108125
</style>
109126
110127
@code{
111-
List<Status> Statuses { get; set; } = new();
112-
int CurrentStatus { get; set; } = 3;
128+
private List<Status> Statuses { get; set; } = new();
129+
130+
private int CurrentStatus { get; set; } = 3;
131+
132+
private void ToggleButtonSelectedChanged(bool newSelected, int id)
133+
{
134+
if (newSelected)
135+
{
136+
CurrentStatus = id;
137+
}
138+
}
113139
114140
protected override Task OnInitializedAsync()
115141
{
@@ -128,7 +154,71 @@ The RadioGroup will look the same as a [ButtonGroup with single selection]({%slu
128154
public class Status
129155
{
130156
public int Id { get; set; }
131-
public string Text { get; set; }
157+
public string Text { get; set; } = string.Empty;
132158
}
133159
}
134160
````
161+
162+
### Solution up to version 5.1.1
163+
164+
The following CSS code targets different RadioGroup HTML markup up to version 5.1.1.
165+
166+
>caption CSS code to style the RadioGroup like a ButtonGroup up to version 5.1.1
167+
168+
<div class="skip-repl"></div>
169+
170+
````CSS
171+
/* remove the horizontal space between the RadioGroup items */
172+
.k-radio-list.radio-buttons {
173+
gap: 0;
174+
}
175+
176+
/* reset styles and support absolute radio inputs */
177+
.radio-buttons .k-radio-item {
178+
margin: 0;
179+
padding: 0;
180+
position: relative;
181+
}
182+
183+
/* hide the radio buttons */
184+
.radio-buttons .k-radio {
185+
opacity: 0;
186+
position: absolute;
187+
}
188+
189+
.radio-buttons .k-radio::before {
190+
display: none;
191+
}
192+
193+
/* make the radio labels look like buttons */
194+
.radio-buttons .k-radio-label {
195+
display: inline-block;
196+
margin: 0;
197+
padding: .2em .6em;
198+
border: 1px solid #ff6358;
199+
border-left-width: 0;
200+
position: relative;
201+
font-size: var(--kendo-font-size, inherit);
202+
color: #ff6358;
203+
}
204+
205+
/* first and last button borders */
206+
.radio-buttons .k-radio-item:first-child .k-radio-label {
207+
border-left-width: 1px;
208+
border-radius: .2em 0 0 .2em;
209+
}
210+
211+
.radio-buttons .k-radio-item:last-child .k-radio-label {
212+
border-radius: 0 .2em .2em 0;
213+
}
214+
215+
/* button selected state */
216+
.radio-buttons .k-radio:checked + .k-radio-label {
217+
background-color: #ff6358;
218+
color: #fff;
219+
}
220+
````
221+
222+
## See Also
223+
224+
* [How to override CSS theme styles]({%slug themes-override%})

0 commit comments

Comments
 (0)