Buttons
import React from 'react';
import { Button } from 'reactstrap';
export default class Example extends React.Component {
render() {
return (
<div>
<Button color="primary">primary</Button>{' '}
<Button color="secondary">secondary</Button>{' '}
<Button color="success">success</Button>{' '}
<Button color="info">info</Button>{' '}
<Button color="warning">warning</Button>{' '}
<Button color="danger">danger</Button>{' '}
<Button color="link">link</Button>
</div>
);
}
}
Properties
Button.propTypes = {
active: PropTypes.bool,
'aria-label': PropTypes.string,
block: PropTypes.bool,
color: PropTypes.string, // default: 'secondary'
disabled: PropTypes.bool,
outline: PropTypes.bool,
// Pass in a Component to override default button element
// example: react-router Link
// default: 'button'
tag: PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
PropTypes.shape({ $$typeof: PropTypes.symbol, render: PropTypes.func }),
PropTypes.arrayOf(PropTypes.oneOfType([
PropTypes.func,
PropTypes.string,
PropTypes.shape({ $$typeof: PropTypes.symbol, render: PropTypes.func }),
]))
]),
// ref will only get you a reference to the Button component, use innerRef to get a reference to the DOM element (for things like focus management).
innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func, PropTypes.string]),
onClick: PropTypes.func,
size: PropTypes.string,
children: PropTypes.node,
className: PropTypes.string,
cssModule: PropTypes.object,
// use close prop for BS4 close icon utility
close: PropTypes.bool,
}
Button.defaultProps = {
color: 'secondary',
tag: 'button',
}
Outline Buttons
import React from 'react';
import { Button } from 'reactstrap';
export default class Example extends React.Component {
render() {
return (
<div>
<Button outline color="primary">primary</Button>{' '}
<Button outline color="secondary">secondary</Button>{' '}
<Button outline color="success">success</Button>{' '}
<Button outline color="info">info</Button>{' '}
<Button outline color="warning">warning</Button>{' '}
<Button outline color="danger">danger</Button>
</div>
);
}
}
Sizes
<Button color="primary" size="lg">Large Button</Button>{' '}
<Button color="secondary" size="lg">Large Button</Button>
<Button color="primary" size="sm">Small Button</Button>{' '}
<Button color="secondary" size="sm">Small Button</Button>
<Button color="primary" size="lg" block>Block level button</Button>
<Button color="secondary" size="lg" block>Block level button</Button>
Active State
<Button color="primary" size="lg" active>Primary link</Button>{' '}
<Button color="secondary" size="lg" active>Link</Button>
Disabled State
<Button color="primary" size="lg" disabled>Primary button</Button>{' '}
<Button color="secondary" size="lg" disabled>Button</Button>
Checkbox and Radio Buttons (Stateful Buttons)
In order to have checkbox and radio buttons, your component needs to manage the state of which button(s) are active/select. It is not in the opinion of this library to manage state within it's components so it is left up to you. Below is a simple example showcasing how this could be done using the components which already exist in this library.
import React, { Component } from 'react';
import { Button, ButtonGroup } from 'reactstrap';
class Example extends Component {
constructor (props) {
super(props);
this.state = { cSelected: [] };
this.onRadioBtnClick = this.onRadioBtnClick.bind(this);
this.onCheckboxBtnClick = this.onCheckboxBtnClick.bind(this);
}
onRadioBtnClick(rSelected) {
this.setState({ rSelected });
}
onCheckboxBtnClick(selected) {
const index = this.state.cSelected.indexOf(selected);
if (index < 0) {
this.state.cSelected.push(selected);
} else {
this.state.cSelected.splice(index, 1);
}
this.setState({ cSelected: [...this.state.cSelected] });
}
render() {
return (
<div>
<h5>Radio Buttons</h5>
<ButtonGroup>
<Button color="primary" onClick={() => this.onRadioBtnClick(1)} active={this.state.rSelected === 1}>One</Button>
<Button color="primary" onClick={() => this.onRadioBtnClick(2)} active={this.state.rSelected === 2}>Two</Button>
<Button color="primary" onClick={() => this.onRadioBtnClick(3)} active={this.state.rSelected === 3}>Three</Button>
</ButtonGroup>
<p>Selected: {this.state.rSelected}</p>
<h5>Checkbox Buttons</h5>
<ButtonGroup>
<Button color="primary" onClick={() => this.onCheckboxBtnClick(1)} active={this.state.cSelected.includes(1)}>One</Button>
<Button color="primary" onClick={() => this.onCheckboxBtnClick(2)} active={this.state.cSelected.includes(2)}>Two</Button>
<Button color="primary" onClick={() => this.onCheckboxBtnClick(3)} active={this.state.cSelected.includes(3)}>Three</Button>
</ButtonGroup>
<p>Selected: {JSON.stringify(this.state.cSelected)}</p>
</div>
);
}
}
export default Example;
Close icon
Use a generic close icon to dismiss content. Use <Button close />
for the default icon. Otherwise, custom content for the button may be defined. (e.g. JSX: <Button close><span aria-hidden="true">–</span></Button>
) The default aria-label is "Close".
import React, { Component } from 'react';
import { Button, Card, CardBody, CardText, CardGroup, CardTitle } from 'reactstrap';
const Example = () => (
<div>
<CardGroup>
<Card>
<CardBody>
<CardTitle>
<Button close />
</CardTitle>
<CardText>Default close icon</CardText>
</CardBody>
</Card>
<Card>
<CardBody>
<CardTitle>
<Button close aria-label="Cancel">
<span aria-hidden>–</span>
</Button>
</CardTitle>
<CardText>
Custom content and aria-label
</CardText>
</CardBody>
</Card>
</CardGroup>
</div>
);
export default Example;
当前内容版权归 reactstrap 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 reactstrap .