Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit eee6d5f

Browse files
Dan LundgrenTigge
authored andcommitted
feat(radiobutton): updated radiobutton with direction
Added new property for setting columndirection
1 parent abd8f10 commit eee6d5f

File tree

4 files changed

+75
-8
lines changed

4 files changed

+75
-8
lines changed

packages/core/src/RadioButton/RadioButton.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ export const RadioContainer = styled.div<{
233233

234234
type BaseElement = HTMLInputElement
235235
type BaseProps = InputHTMLAttributes<BaseElement>
236+
type ColumnDirection = 'column' | 'row'
236237

237238
export type RadioButtonChangeHandler = ChangeEventHandler<BaseElement>
238239
export type RadioButtonValueChangeHandler<V extends string = string> = (
@@ -373,9 +374,16 @@ export function RadioButton<V extends string = string>({
373374
* Radio Group
374375
*/
375376
/* stylelint-disable no-descending-specificity */
376-
const RadioButtonGroupContainer = styled.div<{ readonly compact: boolean }>`
377-
display: grid;
378-
grid-row-gap: ${({ compact }) => (!compact ? spacing.large : spacing.medium)};
377+
378+
const RadioButtonGroupContainer = styled.div<{
379+
readonly compact: boolean
380+
readonly direction: ColumnDirection
381+
}>`
382+
display: flex;
383+
flex-direction: ${({ direction }) => (!direction ? 'column' : direction)};
384+
justify-content: ${({ direction }) =>
385+
direction === 'column' ? '' : 'space-between'};
386+
row-gap: ${({ compact }) => (!compact ? spacing.large : spacing.medium)};
379387
margin: ${({ compact }) =>
380388
!compact ? `${spacing.medium} 0` : `${spacing.small} 0`};
381389
`
@@ -407,6 +415,11 @@ export interface RadioButtonGroupProps<V extends string = string>
407415
408416
*/
409417
readonly compact?: boolean
418+
/**
419+
* Aligns the menu item as a column or row.
420+
* Default: `column`
421+
*/
422+
readonly direction?: ColumnDirection
410423
}
411424

412425
export function RadioButtonGroup<V extends string = string>({
@@ -417,13 +430,18 @@ export function RadioButtonGroup<V extends string = string>({
417430
onValueChange,
418431
error,
419432
compact: compactFromProps,
433+
direction = 'column',
420434
...rest
421435
}: RadioButtonGroupProps<V>): JSX.Element {
422436
const { compact: compactFromTheme } = useTheme()
423437
const compact = compactFromProps ?? compactFromTheme
424438

425439
return (
426-
<RadioButtonGroupContainer compact={compact} {...rest}>
440+
<RadioButtonGroupContainer
441+
compact={compact}
442+
direction={direction}
443+
{...rest}
444+
>
427445
{options.map((option, index) => (
428446
<RadioButton<V>
429447
key={index}

packages/core/src/RadioButton/__snapshots__/index.test.tsx.snap

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,8 +1372,14 @@ exports[`RadioButtons RadioButtonGroup 1`] = `
13721372
}
13731373
13741374
.c2 {
1375-
display: grid;
1376-
grid-row-gap: 16px;
1375+
display: -webkit-box;
1376+
display: -webkit-flex;
1377+
display: -ms-flexbox;
1378+
display: flex;
1379+
-webkit-flex-direction: column;
1380+
-ms-flex-direction: column;
1381+
flex-direction: column;
1382+
row-gap: 16px;
13771383
margin: 8px 0;
13781384
}
13791385
@@ -1411,6 +1417,7 @@ exports[`RadioButtons RadioButtonGroup 1`] = `
14111417
>
14121418
<div
14131419
className="c2"
1420+
direction="column"
14141421
>
14151422
<div
14161423
checked={true}
@@ -1839,8 +1846,14 @@ exports[`RadioButtons RadioButtonGroup 2`] = `
18391846
}
18401847
18411848
.c2 {
1842-
display: grid;
1843-
grid-row-gap: 16px;
1849+
display: -webkit-box;
1850+
display: -webkit-flex;
1851+
display: -ms-flexbox;
1852+
display: flex;
1853+
-webkit-flex-direction: column;
1854+
-ms-flex-direction: column;
1855+
flex-direction: column;
1856+
row-gap: 16px;
18441857
margin: 8px 0;
18451858
}
18461859
@@ -1878,6 +1891,7 @@ exports[`RadioButtons RadioButtonGroup 2`] = `
18781891
>
18791892
<div
18801893
className="c2"
1894+
direction="column"
18811895
>
18821896
<div
18831897
checked={false}

packages/docs/src/mdx/coreComponents/RadioButton.mdx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ export const DemoComponent = () => {
118118
/>
119119
```
120120

121+
#### Group with direction set to row
122+
123+
```typescript type="live"
124+
<RadioButtonGroupField
125+
name="weather-group-row-test"
126+
label="Grouped buttons with direction set to row"
127+
options={[
128+
{ value: 'sunny', label: 'Sunny' },
129+
{ value: 'cloudy', label: 'Cloudy' },
130+
{ value: 'rainy', label: 'Rainy' },
131+
{ value: 'windy', label: 'Windy', disabled: true },
132+
]}
133+
value="sunny"
134+
onValueChange={() => {}}
135+
direction="row"
136+
/>
137+
```
138+
121139
## RadioIconButton
122140

123141
A RadioIconButton is a clickable element which will execute a command when clicked.

packages/docs/src/mdx/formikComponents/FormikRadioButtonGroup.mdx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ A formik component that wraps a RadioButtonGroup component.
2626
</FormikDemo>
2727
```
2828

29+
```typescript type="demo"
30+
<FormikDemo initialValues={{ day: 'mon' }}>
31+
<FormikRadioButtonGroupField
32+
label="Row direction set to row"
33+
name="dayrow"
34+
options={[
35+
{ label: 'Monday', value: 'mon' },
36+
{ label: 'Tuesday', value: 'tue' },
37+
{ label: 'Wednesday', value: 'wed' },
38+
{ label: 'Thursday', value: 'thu' },
39+
{ label: 'Friday', value: 'fri' },
40+
]}
41+
direction="row"
42+
/>
43+
</FormikDemo>
44+
```
45+
2946
## Usage in code
3047

3148
```typescript

0 commit comments

Comments
 (0)