This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Add ButtonGroup component #33
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
87fc098
Add ButtonGroup component
a3f7079
Add `children` PropType validation and export ButtonGroup
c81f839
Make `GroupedButton` component fit on one line
0c2b0e6
Merge branch 'master' into add-buttongroup-component
323d8f5
style: Make GroupedButton on one line
a9a8478
test: Add missing ButtonGroup test
a85c38b
fix(buttongroup): Fix disabled prop being overridden
c707efd
fix(buttongroup): Remove borderRight from middle elements
a1a264a
test(buttongroup): Add test case for non-button children
1dc8fa0
fix(buttongroup): Fix bug preventing children from being disabled
9f8493c
style(buttongroup): Fix glamorous import
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,52 @@ | ||
import React from 'react'; | ||
import { arrayOf, bool, node } from 'prop-types'; | ||
import glamorous, { Div } from 'glamorous'; | ||
|
||
import Button from '../Button/Button'; | ||
import constants from '../../constants'; | ||
|
||
const { borderRadius } = constants; | ||
|
||
const ButtonGroupItem = glamorous(Button)({ | ||
borderRadius: 0, | ||
borderRightWidth: 0, | ||
|
||
'&:first-of-type': { | ||
borderTopLeftRadius: borderRadius, | ||
borderBottomLeftRadius: borderRadius, | ||
}, | ||
'&:last-of-type': { | ||
borderTopRightRadius: borderRadius, | ||
borderBottomRightRadius: borderRadius, | ||
borderRightWidth: 1, | ||
}, | ||
}); | ||
|
||
const ButtonGroup = ({ children, disabled, ...props }) => ( | ||
<Div display="inline-block" {...props}> | ||
{React.Children.map(children, child => { | ||
if (child.type !== Button) { | ||
return child; | ||
} | ||
return ( | ||
<ButtonGroupItem | ||
{...child.props} | ||
disabled={disabled || child.props.disabled} | ||
/> | ||
); | ||
})} | ||
</Div> | ||
); | ||
|
||
ButtonGroup.propTypes = { | ||
/** Buttons to be grouped together. */ | ||
children: arrayOf(node).isRequired, | ||
/** Indicates that the control is not available for interaction */ | ||
disabled: bool, | ||
}; | ||
|
||
ButtonGroup.defaultProps = { | ||
disabled: false, | ||
}; | ||
|
||
export default ButtonGroup; |
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,53 @@ | ||
import React from 'react'; | ||
import renderer from 'react-test-renderer'; | ||
import ButtonGroup from './ButtonGroup'; | ||
import Button from '../Button/Button'; | ||
|
||
test('renders without crashing', () => { | ||
const tree = renderer | ||
.create( | ||
<ButtonGroup> | ||
<Button>Button 1</Button> | ||
<Button>Button 2</Button> | ||
</ButtonGroup>, | ||
) | ||
.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
|
||
test('renders when disabled', () => { | ||
const tree = renderer | ||
.create( | ||
<ButtonGroup disabled> | ||
<Button>Button 1</Button> | ||
<Button>Button 2</Button> | ||
</ButtonGroup>, | ||
) | ||
.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably add a test for when |
||
|
||
test('renders with disabled children', () => { | ||
const tree = renderer | ||
.create( | ||
<ButtonGroup> | ||
<Button disabled>Button 1</Button> | ||
<Button>Button 2</Button> | ||
</ButtonGroup>, | ||
) | ||
.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
|
||
test('renders with non-button children', () => { | ||
const tree = renderer | ||
.create( | ||
<ButtonGroup> | ||
<Button>Button 1</Button> | ||
<Button>Button 2</Button> | ||
<span>Span</span> | ||
</ButtonGroup>, | ||
) | ||
.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); |
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,10 @@ | ||
### Example | ||
|
||
``` | ||
<ButtonGroup> | ||
<Button>File</Button> | ||
<Button>Edit</Button> | ||
<Button>View</Button> | ||
<Button>History</Button> | ||
</ButtonGroup> | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little bit confused why this is necessary. Is there really a good reason to ever wrap something that isn't a
Button
in aButtonGroup
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my use case I have a hidden input inside the ButtonGroup so there's one.