Button按钮

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

    按钮类型

    按钮有五种类型:主按钮、次按钮、虚线按钮、危险按钮和链接按钮。主按钮在同一个操作区域最多出现一次。

    Button按钮 - 图2

    按钮有大、中、小三种尺寸。

    通过设置 为 large small 分别把按钮设为大、小尺寸。若不设置 size,则尺寸为中。

    1. import { Button, Radio, Icon } from 'antd';
    2. class ButtonSize extends React.Component {
    3. state = {
    4. size: 'large',
    5. };
    6. handleSizeChange = e => {
    7. this.setState({ size: e.target.value });
    8. };
    9. render() {
    10. const { size } = this.state;
    11. return (
    12. <div>
    13. <Radio.Group value={size} onChange={this.handleSizeChange}>
    14. <Radio.Button value="large">Large</Radio.Button>
    15. <Radio.Button value="default">Default</Radio.Button>
    16. <Radio.Button value="small">Small</Radio.Button>
    17. </Radio.Group>
    18. <br />
    19. <br />
    20. <Button type="primary" size={size}>
    21. Primary
    22. </Button>
    23. <Button size={size}>Normal</Button>
    24. <Button type="dashed" size={size}>
    25. Dashed
    26. </Button>
    27. <Button type="danger" size={size}>
    28. Danger
    29. </Button>
    30. <Button type="link" size={size}>
    31. Link
    32. </Button>
    33. <br />
    34. <Button type="primary" shape="circle" icon="download" size={size} />
    35. <Button type="primary" shape="round" icon="download" size={size}>
    36. Download
    37. </Button>
    38. <Button type="primary" icon="download" size={size}>
    39. Download
    40. </Button>
    41. <br />
    42. <Button.Group size={size}>
    43. <Button type="primary">
    44. <Icon type="left" />
    45. Backward
    46. </Button>
    47. <Button type="primary">
    48. Forward
    49. <Icon type="right" />
    50. </Button>
    51. </Button.Group>
    52. </div>
    53. );
    54. }
    55. }
    56. ReactDOM.render(<ButtonSize />, mountNode);

    加载中状态

    添加 loading 属性即可让按钮处于加载状态,最后两个按钮演示点击后进入加载状态。

    1. class App extends React.Component {
    2. state = {
    3. loading: false,
    4. iconLoading: false,
    5. };
    6. enterLoading = () => {
    7. this.setState({ loading: true });
    8. };
    9. enterIconLoading = () => {
    10. };
    11. render() {
    12. return (
    13. <div>
    14. <Button type="primary" loading>
    15. Loading
    16. </Button>
    17. <Button type="primary" size="small" loading>
    18. Loading
    19. </Button>
    20. <br />
    21. <Button type="primary" loading={this.state.loading} onClick={this.enterLoading}>
    22. Click me!
    23. </Button>
    24. <Button
    25. type="primary"
    26. icon="poweroff"
    27. loading={this.state.iconLoading}
    28. onClick={this.enterIconLoading}
    29. >
    30. Click me!
    31. </Button>
    32. <br />
    33. <Button shape="circle" loading />
    34. <Button type="primary" shape="circle" loading />
    35. </div>
    36. );
    37. }
    38. }
    39. ReactDOM.render(<App />, mountNode);

    可以将多个 Button 放入 Button.Group 的容器中。

    通过设置 sizelarge small 分别把按钮组合设为大、小尺寸。若不设置 size,则尺寸为中。

    Button按钮 - 图5

    block 按钮

    block属性将使按钮适合其父宽度。

    1. import { Button } from 'antd';
    2. ReactDOM.render(
    3. <div>
    4. <Button type="primary" block>
    5. Primary
    6. </Button>
    7. <Button block>Default</Button>
    8. <Button type="dashed" block>
    9. Dashed
    10. </Button>
    11. <Button type="danger" block>
    12. Danger
    13. </Button>
    14. <Button type="link" block>
    15. Link
    16. </Button>
    17. </div>,
    18. mountNode,
    19. );

    当需要在 Button 内嵌入 Icon 时,可以设置 icon 属性,或者直接在 Button 内使用 Icon 组件。

    如果想控制 Icon 具体的位置,只能直接使用 Icon 组件,而非 icon 属性。

    1. ReactDOM.render(
    2. <div>
    3. <Button type="primary" shape="circle" icon="search" />
    4. <Button type="primary" icon="search">
    5. Search
    6. </Button>
    7. <Button shape="circle" icon="search" />
    8. <Button icon="search">Search</Button>
    9. <Button shape="circle" icon="search" />
    10. <Button icon="search">Search</Button>
    11. <Button type="dashed" shape="circle" icon="search" />
    12. <Button type="dashed" icon="search">
    13. Search
    14. </Button>
    15. </div>,
    16. mountNode,
    17. );

    Button按钮 - 图7

    添加 disabled 属性即可让按钮处于不可用状态,同时按钮样式也会改变。

    多个按钮组合

    按钮组合使用时,推荐使用 1 个主操作 + n 个次操作,3 个以上操作时把更多操作放到 Dropdown.Button 中组合使用。

    1. import { Button, Menu, Dropdown, Icon } from 'antd';
    2. function handleMenuClick(e) {
    3. console.log('click', e);
    4. }
    5. const menu = (
    6. <Menu onClick={handleMenuClick}>
    7. <Menu.Item key="1">1st item</Menu.Item>
    8. <Menu.Item key="2">2nd item</Menu.Item>
    9. <Menu.Item key="3">3rd item</Menu.Item>
    10. </Menu>
    11. );
    12. ReactDOM.render(
    13. <div>
    14. <Button type="primary">primary</Button>
    15. <Button>secondary</Button>
    16. <Dropdown overlay={menu}>
    17. <Button>
    18. Actions <Icon type="down" />
    19. </Button>
    20. </Dropdown>
    21. </div>,
    22. mountNode,
    23. );

    Button按钮 - 图9

    幽灵按钮将按钮的内容反色,背景变为透明,常用在有色背景上。

    1. import { Button } from 'antd';
    2. ReactDOM.render(
    3. <div style={{ background: 'rgb(190, 200, 200)', padding: '26px 16px 16px' }}>
    4. <Button type="primary" ghost>
    5. Primary
    6. </Button>
    7. <Button ghost>Default</Button>
    8. <Button type="dashed" ghost>
    9. Dashed
    10. </Button>
    11. <Button type="danger" ghost>
    12. danger
    13. </Button>
    14. <Button type="link" ghost>
    15. link
    16. </Button>
    17. </div>,
    18. mountNode,
    19. );

    通过设置 Button 的属性来产生不同的按钮样式,推荐顺序为:type -> shape -> size -> loading -> disabled

    按钮的属性说明如下:

    支持原生 button 的其他所有属性。

    根据 Ant Design 设计规范要求,我们会在按钮内只有两个汉字时自动添加空格,如果你不需要这个特性,可以设置 ConfigProvider 的 为 false