Button按钮

按钮用于开始一个即时操作。

何时使用

标记了一个(或封装一组)操作命令,响应用户点击行为,触发相应的业务逻辑。

代码演示

Button按钮 - 图1

按钮展现模式

按钮的展现模式,有flat和raised两种,默认为raised。

  1. import { Button } from 'choerodon-ui/pro';
  2. class App extends React.PureComponent {
  3. render() {
  4. return (
  5. <div>
  6. <Button>默认raised按钮</Button>
  7. <Button funcType="flat">flat按钮</Button>
  8. <Button funcType="raised">raised按钮</Button>
  9. </div>
  10. );
  11. }
  12. }
  13. ReactDOM.render(
  14. <App />,
  15. mountNode);

Button按钮 - 图2

按钮不可选择

按钮能否选择,默认为false。

  1. import { Button } from 'choerodon-ui/pro';
  2. class App extends React.PureComponent {
  3. render() {
  4. return (
  5. <div>
  6. <Button>默认可选择按钮</Button>
  7. <Button disabled>不可选择按钮</Button>
  8. <Button color="primary" disabled>不可选择按钮</Button>
  9. <Button funcType="flat" color="primary" disabled>不可选择按钮</Button>
  10. <Button funcType="flat" icon="save" disabled />
  11. </div>
  12. );
  13. }
  14. }
  15. ReactDOM.render(
  16. <App />,
  17. mountNode);

Button按钮 - 图3

图标加载中

图标是否加载中(加载中无法点击)。

  1. import { DataSet, Button } from 'choerodon-ui/pro';
  2. import { runInAction } from 'mobx';
  3. class App extends React.Component {
  4. ds = new DataSet();
  5. state = {
  6. loading: true,
  7. };
  8. componentWillMount() {
  9. runInAction(() => {
  10. this.ds.status = 'submitting';
  11. });
  12. }
  13. handleClick = () => {
  14. this.setState({ loading: !this.state.loading });
  15. return new Promise(resolve => setTimeout(resolve, 1000));
  16. };
  17. render() {
  18. return (
  19. <div>
  20. <Button icon="save" loading={this.state.loading}>保存</Button>
  21. <Button funcType="flat" color="primary" icon="save" loading>保存</Button>
  22. <Button icon="save" loading />
  23. <Button funcType="flat" icon="save" color="primary" loading />
  24. <Button dataSet={this.ds} type="submit">数据源状态</Button>
  25. <Button onClick={this.handleClick}>wait promise</Button>
  26. </div>
  27. );
  28. }
  29. }
  30. ReactDOM.render(
  31. <App />,
  32. mountNode
  33. );

Button按钮 - 图4

按钮大小

按钮大小。

  1. import { Button } from 'choerodon-ui/pro';
  2. class App extends React.PureComponent {
  3. render() {
  4. return (
  5. <div>
  6. <div>
  7. <Button>默认raised按钮</Button>
  8. <Button size="large">大raised按钮</Button>
  9. <Button size="small">小raised按钮</Button>
  10. </div>
  11. <div>
  12. <Button funcType="flat">flat按钮</Button>
  13. <Button funcType="flat" size="large">大flat按钮</Button>
  14. <Button funcType="flat" size="small">小flat按钮</Button>
  15. </div>
  16. <div>
  17. <Button funcType="flat" icon="search" />
  18. <Button funcType="flat" icon="search" size="large" />
  19. <Button funcType="flat" icon="search" size="small" />
  20. </div>
  21. </div>
  22. );
  23. }
  24. }
  25. ReactDOM.render(
  26. <App />,
  27. mountNode
  28. );

Button按钮 - 图5

按钮颜色

按钮的颜色,当 funcType 为 flat 时,color 为字体颜色。

  1. import { Button } from 'choerodon-ui/pro';
  2. class App extends React.PureComponent {
  3. render() {
  4. return (
  5. <div>
  6. <div>
  7. <div style={{ margin: '20px 0' }}>Background Color(With raised funcType)</div>
  8. <Button color="gray">Gray</Button>
  9. <Button color="primary">Blue</Button>
  10. <Button color="green">Green</Button>
  11. <Button color="red">Red</Button>
  12. <Button color="yellow">Yellow</Button>
  13. <Button color="purple">Purple</Button>
  14. <Button color="dark">Dark</Button>
  15. </div>
  16. <div>
  17. <div style={{ margin: '20px 0' }}>Font Color(With flat funcType)</div>
  18. <Button funcType="flat" color="gray">
  19. Gray
  20. </Button>
  21. <Button funcType="flat" color="primary">
  22. Blue
  23. </Button>
  24. <Button funcType="flat" color="green">
  25. Green
  26. </Button>
  27. <Button funcType="flat" color="red">
  28. Red
  29. </Button>
  30. <Button funcType="flat" color="yellow">
  31. Yellow
  32. </Button>
  33. <Button funcType="flat" color="purple">
  34. Purple
  35. </Button>
  36. <Button funcType="flat" color="dark">
  37. Dark
  38. </Button>
  39. </div>
  40. </div>
  41. );
  42. }
  43. }
  44. ReactDOM.render(<App />, mountNode);

Button按钮 - 图6

图标按钮

为按钮增加图标。

  1. import { Button, Icon } from 'choerodon-ui/pro';
  2. class App extends React.PureComponent {
  3. render() {
  4. return (
  5. <div>
  6. <Button icon="save">{false}</Button>
  7. <Button><Icon type="save" /></Button>
  8. <Button funcType="flat" color="primary" icon="save">保存</Button>
  9. <Button icon="sync" />
  10. <Button funcType="flat" icon="sync" />
  11. <Button funcType="flat" icon="search" color="primary" />
  12. <Button funcType="flat" icon="close" style={{ color: '#e12330' }} disabled />
  13. </div>
  14. );
  15. }
  16. }
  17. ReactDOM.render(
  18. <App />,
  19. mountNode);

Button按钮 - 图7

超链接按钮

超链接按钮。

  1. import { Button } from 'choerodon-ui/pro';
  2. ReactDOM.render(
  3. <div>
  4. <Button href="https://choerodon.io" target="_blank">跳转按钮</Button>
  5. <Button funcType="flat" href="https://choerodon.io" target="_blank">跳转按钮</Button>
  6. <Button funcType="flat" href="https://choerodon.io" target="_blank" icon="link" />
  7. </div>,
  8. mountNode
  9. );

Button按钮 - 图8

按钮节流

按钮点击节流。该类型按钮事件无法冒泡。

  1. import { Button } from 'choerodon-ui/pro';
  2. class App extends React.Component {
  3. state = {
  4. num: 0,
  5. };
  6. handleClick = e => {
  7. e.stopPropagation();
  8. this.setState({
  9. num: this.state.num + 1,
  10. });
  11. };
  12. handleBubbleClick = () => {
  13. console.log('bubble click');
  14. };
  15. render() {
  16. return (
  17. <div onClick={this.handleBubbleClick}>
  18. <Button onClick={this.handleClick} wait={1000} waitType="throttle">
  19. 节流按钮{this.state.num}
  20. </Button>
  21. <Button onClick={this.handleClick} wait={1000} waitType="debounce">
  22. 去抖按钮{this.state.num}
  23. </Button>
  24. </div>
  25. );
  26. }
  27. }
  28. ReactDOM.render(<App />, mountNode);

API

按钮的属性说明如下:

属性说明类型默认值
type按钮类型,可选值为 button submit resetstringbutton
color按钮颜色风格,可选值为 default primary blue yellow red dark green purplestringdefault
funcType按钮展现模式,可选值为 flat raisedstringraised
loading按钮是否是加载状态booleanfalse
icon按钮图标string
href点击跳转的地址,指定此属性 button 的行为和 a 链接一致string
target相当于 a 链接的 target 属性,href 存在时生效string
wait点击间隔时间number
waitType点击间隔类型,可选值: throttle debouncestringthrottle
onClick单击回调, 当回调返回值为Promise,则会出现loading状态直到Promise的状态不为pending(e) => voidPromise

更多属性请参考 ViewComponent