-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components): add
ToggleButtonGroup
(#1496)
- Loading branch information
Showing
6 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@launchpad-ui/components": patch | ||
--- | ||
|
||
Add `ToggleButtonGroup` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { render, screen } from '../../../test/utils'; | ||
import { ToggleButton, ToggleButtonGroup } from '../src'; | ||
|
||
describe('ToggleButtonGroup', () => { | ||
it('renders', () => { | ||
render( | ||
<ToggleButtonGroup> | ||
<ToggleButton id="first">First</ToggleButton> | ||
<ToggleButton id="second">Second</ToggleButton> | ||
<ToggleButton id="third">Third</ToggleButton> | ||
</ToggleButtonGroup>, | ||
); | ||
expect(screen.getByRole('radiogroup')).toBeVisible(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { ForwardedRef } from 'react'; | ||
import type { ToggleButtonGroupProps } from 'react-aria-components'; | ||
|
||
import { cva } from 'class-variance-authority'; | ||
import { forwardRef } from 'react'; | ||
import { | ||
ToggleButtonGroup as AriaToggleButtonGroup, | ||
composeRenderProps, | ||
} from 'react-aria-components'; | ||
|
||
import styles from './styles/ToggleButtonGroup.module.css'; | ||
|
||
const group = cva(styles.group); | ||
|
||
const _ToggleButtonGroup = (props: ToggleButtonGroupProps, ref: ForwardedRef<HTMLDivElement>) => { | ||
return ( | ||
<AriaToggleButtonGroup | ||
{...props} | ||
ref={ref} | ||
className={composeRenderProps(props.className, (className, renderProps) => | ||
group({ ...renderProps, className }), | ||
)} | ||
/> | ||
); | ||
}; | ||
|
||
/** | ||
* A toggle button group allows a user to toggle multiple options, with single or multiple selection. | ||
* | ||
* https://react-spectrum.adobe.com/react-aria/ToggleButtonGroup.html | ||
*/ | ||
const ToggleButtonGroup = forwardRef(_ToggleButtonGroup); | ||
|
||
export { ToggleButtonGroup }; | ||
export type { ToggleButtonGroupProps }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.group { | ||
composes: buttonGroup from './base.module.css'; | ||
display: flex; | ||
|
||
&[data-orientation='vertical'] { | ||
flex-direction: column; | ||
width: fit-content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { ToggleButton, ToggleButtonGroup, ToggleIconButton } from '../src'; | ||
|
||
const meta: Meta<typeof ToggleButtonGroup> = { | ||
component: ToggleButtonGroup, | ||
// @ts-ignore | ||
subcomponents: { ToggleButton }, | ||
title: 'Components/Buttons/ToggleButtonGroup', | ||
parameters: { | ||
status: { | ||
type: import.meta.env.STORYBOOK_PACKAGE_STATUS__COMPONENTS, | ||
}, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof ToggleButtonGroup>; | ||
|
||
export const Example: Story = { | ||
args: { | ||
children: ( | ||
<> | ||
<ToggleButton id="first">First</ToggleButton> | ||
<ToggleButton id="second">Second</ToggleButton> | ||
<ToggleButton id="third">Third</ToggleButton> | ||
</> | ||
), | ||
defaultSelectedKeys: ['first'], | ||
}, | ||
}; | ||
|
||
export const MultipleSelection: Story = { | ||
args: { | ||
children: ( | ||
<> | ||
<ToggleButton id="first">First</ToggleButton> | ||
<ToggleButton id="second">Second</ToggleButton> | ||
<ToggleButton id="third">Third</ToggleButton> | ||
</> | ||
), | ||
selectionMode: 'multiple', | ||
defaultSelectedKeys: ['first', 'second'], | ||
}, | ||
}; | ||
|
||
export const Orientation: Story = { | ||
args: { | ||
children: ( | ||
<> | ||
<ToggleButton id="first">First</ToggleButton> | ||
<ToggleButton id="second">Second</ToggleButton> | ||
<ToggleButton id="third">Third</ToggleButton> | ||
</> | ||
), | ||
orientation: 'vertical', | ||
}, | ||
}; | ||
|
||
export const Icons: Story = { | ||
args: { | ||
children: ( | ||
<> | ||
<ToggleIconButton id="first" icon="flask" aria-label="flask" /> | ||
<ToggleIconButton id="second" icon="flag" aria-label="flag" /> | ||
<ToggleIconButton id="third" icon="toggle-on" aria-label="toggle" /> | ||
</> | ||
), | ||
orientation: 'vertical', | ||
}, | ||
}; |