Skip to content

Commit 8f76539

Browse files
authored
feat: add default variant to Alert/Badge (react-bootstrap#5456)
This sets a default variant and bg of "primary" for Alert and Badge respectively, allowing them to be used without having to set a variant or bg. Bootstrap's docs on Alerts require the use of a contextual class (https://getbootstrap.com/docs/5.1/components/alerts/) in order to have proper styling. This aligns Alert and Badge variant functionality with that of Button and Navbar. This could be viewed as a breaking change (e.g. for someone using Alert without a variant [as it now adds one] or someone relying on Button's `.btn-null` [if the variant was null]). However, I'm inclined to view this as a feature as it is adding & declaring functionality in the public API where behaviour wasn't previously explicitly defined in examples or docs.
1 parent 73a559e commit 8f76539

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

src/Alert.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const propTypes = {
8787
};
8888

8989
const defaultProps = {
90+
variant: 'primary',
9091
show: true,
9192
transition: Fade,
9293
closeLabel: 'Close alert',

src/Badge.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const propTypes = {
4343
};
4444

4545
const defaultProps = {
46+
bg: 'primary',
4647
pill: false,
4748
};
4849

test/AlertSpec.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,21 @@ describe('<Alert>', () => {
3131
.simulate('click');
3232
});
3333

34-
it('Should have use variant class', () => {
34+
it('Should default to variant="primary"', () => {
35+
mount(<Alert>Message</Alert>).assertSingle(`.alert-primary`);
36+
});
37+
38+
it('Should use variant class', () => {
3539
mount(<Alert variant="danger">Message</Alert>).assertSingle(
3640
'.alert-danger',
3741
);
3842
});
3943

44+
it('Should not have variant class when variant=null', () => {
45+
const wrapper = mount(<Alert variant={null}>Message</Alert>);
46+
expect(wrapper.find('.alert-primary').length).to.equal(0);
47+
});
48+
4049
it('should forward refs to the alert', () => {
4150
const ref = React.createRef();
4251
mount(<Alert ref={ref}>Yo</Alert>);

test/BadgeSpec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,17 @@ describe('Badge', () => {
2222
</Badge>,
2323
).assertSingle('a[href="#"]');
2424
});
25+
26+
it('Should default to bg="primary"', () => {
27+
mount(<Badge>Message</Badge>).assertSingle(`.bg-primary`);
28+
});
29+
30+
it('Should use bg class', () => {
31+
mount(<Badge bg="danger">Message</Badge>).assertSingle('.bg-danger');
32+
});
33+
34+
it('Should not have bg class when bg=null', () => {
35+
const wrapper = mount(<Badge bg={null}>Message</Badge>);
36+
expect(wrapper.find('.bg-primary').length).to.equal(0);
37+
});
2538
});

0 commit comments

Comments
 (0)