Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions packages/core/src/RadioButton/RadioButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export const RadioContainer = styled.div<{

type BaseElement = HTMLInputElement
type BaseProps = InputHTMLAttributes<BaseElement>
type ColumnDirection = 'column' | 'row'

export type RadioButtonChangeHandler = ChangeEventHandler<BaseElement>
export type RadioButtonValueChangeHandler<V extends string = string> = (
Expand Down Expand Up @@ -373,9 +374,16 @@ export function RadioButton<V extends string = string>({
* Radio Group
*/
/* stylelint-disable no-descending-specificity */
const RadioButtonGroupContainer = styled.div<{ readonly compact: boolean }>`
display: grid;
grid-row-gap: ${({ compact }) => (!compact ? spacing.large : spacing.medium)};

const RadioButtonGroupContainer = styled.div<{
readonly compact: boolean
readonly direction: ColumnDirection
}>`
display: flex;
flex-direction: ${({ direction }) => (!direction ? 'column' : direction)};
justify-content: ${({ direction }) =>
direction === 'column' ? '' : 'space-between'};
row-gap: ${({ compact }) => (!compact ? spacing.large : spacing.medium)};
margin: ${({ compact }) =>
!compact ? `${spacing.medium} 0` : `${spacing.small} 0`};
`
Expand Down Expand Up @@ -407,6 +415,11 @@ export interface RadioButtonGroupProps<V extends string = string>

*/
readonly compact?: boolean
/**
* Aligns the menu item as a column or row.
* Default: `column`
*/
readonly direction?: ColumnDirection
}

export function RadioButtonGroup<V extends string = string>({
Expand All @@ -417,13 +430,18 @@ export function RadioButtonGroup<V extends string = string>({
onValueChange,
error,
compact: compactFromProps,
direction = 'column',
...rest
}: RadioButtonGroupProps<V>): JSX.Element {
const { compact: compactFromTheme } = useTheme()
const compact = compactFromProps ?? compactFromTheme

return (
<RadioButtonGroupContainer compact={compact} {...rest}>
<RadioButtonGroupContainer
compact={compact}
direction={direction}
{...rest}
>
{options.map((option, index) => (
<RadioButton<V>
key={index}
Expand Down
22 changes: 18 additions & 4 deletions packages/core/src/RadioButton/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1372,8 +1372,14 @@ exports[`RadioButtons RadioButtonGroup 1`] = `
}

.c2 {
display: grid;
grid-row-gap: 16px;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
row-gap: 16px;
margin: 8px 0;
}

Expand Down Expand Up @@ -1411,6 +1417,7 @@ exports[`RadioButtons RadioButtonGroup 1`] = `
>
<div
className="c2"
direction="column"
>
<div
checked={true}
Expand Down Expand Up @@ -1839,8 +1846,14 @@ exports[`RadioButtons RadioButtonGroup 2`] = `
}

.c2 {
display: grid;
grid-row-gap: 16px;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
row-gap: 16px;
margin: 8px 0;
}

Expand Down Expand Up @@ -1878,6 +1891,7 @@ exports[`RadioButtons RadioButtonGroup 2`] = `
>
<div
className="c2"
direction="column"
>
<div
checked={false}
Expand Down
18 changes: 18 additions & 0 deletions packages/docs/src/mdx/coreComponents/RadioButton.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ export const DemoComponent = () => {
/>
```

#### Group with direction set to row

```typescript type="live"
<RadioButtonGroupField
name="weather-group-row-test"
label="Grouped buttons with direction set to row"
options={[
{ value: 'sunny', label: 'Sunny' },
{ value: 'cloudy', label: 'Cloudy' },
{ value: 'rainy', label: 'Rainy' },
{ value: 'windy', label: 'Windy', disabled: true },
]}
value="sunny"
onValueChange={() => {}}
direction="row"
/>
```

## RadioIconButton

A RadioIconButton is a clickable element which will execute a command when clicked.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ A formik component that wraps a RadioButtonGroup component.
</FormikDemo>
```

```typescript type="demo"
<FormikDemo initialValues={{ day: 'mon' }}>
<FormikRadioButtonGroupField
label="Row direction set to row"
name="dayrow"
options={[
{ label: 'Monday', value: 'mon' },
{ label: 'Tuesday', value: 'tue' },
{ label: 'Wednesday', value: 'wed' },
{ label: 'Thursday', value: 'thu' },
{ label: 'Friday', value: 'fri' },
]}
direction="row"
/>
</FormikDemo>
```

## Usage in code

```typescript
Expand Down