Button 按钮
如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。
安装方法
- 在命令行中执行以下命令
npm install @icedesign/base@latest -S
按钮用于开始一个即时操作。
Guide
何时使用
标记了一个(或封装一组)操作命令,响应用户点击行为,触发相应的业务逻辑。
API
按钮
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefix | 组件样式的品牌前缀 | String | 'next-' |
type | 按钮的类型可选值:'primary', 'secondary', 'normal', 'dark', 'light' | Enum | 'normal' |
size | 按钮的尺寸可选值:'small', 'medium', 'large' | Enum | 'medium' |
shape | 按钮的形态可选值:'ghost', 'text', 'warning' | Enum | - |
htmlType | 设置 button 标签的原生 type 值 | String | 'button' |
component | 设置标签类型可选值:'button', 'span', 'a', 'div' | Enum | 'button' |
loading | 设置按钮的载入状态 | Boolean | false |
disabled | 是否禁用 | Boolean | - |
onClick | 点击按钮的回调签名:Function(e: Object) => void参数:e: {Object} Event Object | Function | () => {} |
className | 自定义样式 | String | - |
Button.Group
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
size | 统一设置 Button 组件的按钮大小 | String | 'medium' |
Button.Split
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefix | 样式品牌前缀 | String | 'next-' |
align | Dropdown 的对齐方式,参考 Dropdown | String | 'tr br' |
offset | Dropdown 的位置偏移,参考 Dropdown | Array | 0, 4 |
type | 类型,同 Button可选值:'primary', 'secondary', 'normal', 'dark', 'light' | Enum | 'normal' |
shape | 外观,同 Button可选值:'ghost', 'text', 'warning' | Enum | - |
size | 尺寸,同 Button可选值:'small', 'medium', 'large' | Enum | 'medium' |
trigger | Dropdown 触发方式,参考 Dropdown可选值:'click', 'hover' | Enum | 'click' |
menu | 弹出的内容,参考 Dropdown | ReactNode | - |
onClick | 点击按钮的回调签名:Function(e: Object) => void参数:e: {Object} Event Object | Function | () => {} |
代码示例
值得注意的是,对于幽灵按钮而言,通常用在有色背景下。针对浅色背景和深色背景的使用场景,幽灵按钮提供了两个特殊的类型,分别是 light
, dark
。正常情况下通过设置 type
为 primary
secondary
可分别创建主按钮、次按钮,若不设置 type
值则为普通按钮。不同的样式可以用来区别其重要程度。通过 shape
属性可以更改按钮的默认形态,包括幽灵按钮、文本按钮、警告按钮。按钮有三种视觉层次:主按钮、次按钮、普通按钮。
查看源码在线预览
import { Button } from "@icedesign/base";
ReactDOM.render(
<div>
<Button type="normal">普通按钮</Button>
<Button type="primary">主要按钮</Button>
<Button type="secondary">次要按钮</Button>
<br />
<br />
<Button type="normal" shape="text">
普通文本按钮
</Button>{" "}
<Button type="primary" shape="text">
主要文本按钮
</Button>{" "}
<Button type="secondary" shape="text">
次要文本按钮
</Button>
<br />
<br />
<Button type="normal" shape="warning">
普通警告按钮
</Button>{" "}
<Button type="primary" shape="warning">
主要警告按钮
</Button>{" "}
<Button type="secondary" shape="warning">
次要警告按钮
</Button>
<br />
<br />
<div style={{ clear: "both" }}>
<div className="ghost-light-background">
<Button type="light" shape="ghost">
幽灵按钮(浅色)
</Button>
</div>
<div className="ghost-dark-background">
<Button type="dark" shape="ghost">
幽灵按钮(深色)
</Button>
</div>
</div>
</div>,
mountNode
);
.ghost-light-background {
display: inline-block;
height: 100px;
line-height: 100px;
width: 50%;
background: #EBECF0;
margin-bottom: 20px;
padding-left:10px;
box-sizing: border-box;
}
.ghost-dark-background {
display: inline-block;
height: 100px;
line-height: 100px;
width: 50%;
background: #333;
margin-bottom: 20px;
padding-left:10px;
box-sizing: border-box;
}
可选值包括 button
, span
, a
, div
。默认情况下 Button 组件使用 <button>
标签来渲染按钮,通过 component
属性可以自定义 Button 的标签类型。
查看源码在线预览
import { Button } from "@icedesign/base";
ReactDOM.render(
<div>
<Button
type="primary"
component="a"
href="http://www.alibaba.com"
target="_blank"
>
<span>确定</span>
</Button>{" "}
<Button
type="secondary"
component="a"
href="http://www.alibaba.com"
target="_blank"
>
次要按钮
</Button>{" "}
<Button
type="normal"
component="a"
href="http://www.alibaba.com"
target="_blank"
>
普通按钮
</Button>
</div>,
mountNode
);
添加 disabled
属性即可让按钮处于不可用状态,同时按钮样式也会改变。
查看源码在线预览
import { Button } from "@icedesign/base";
ReactDOM.render(
<div>
<Button type="primary">主按钮</Button>
<Button type="primary" disabled>
主按钮(失效)
</Button>
<br />
<br />
<Button type="secondary">次按钮</Button>
<Button type="secondary" disabled>
次按钮(失效)
</Button>
<br />
<br />
<Button type="normal">普通按钮</Button>
<Button type="normal" disabled>
普通按钮(失效)
</Button>
<br />
<br />
<div>
<div className="ghost-light-background">
<Button type="light" shape="ghost" disabled>
幽灵按钮(浅色)
</Button>
</div>
<div className="ghost-dark-background">
<Button type="dark" shape="ghost" disabled>
幽灵按钮(深色)
</Button>
</div>
</div>
</div>,
mountNode
);
.ghost-light-background {
display: inline-block;
height: 100px;
line-height: 100px;
width: 50%;
background: #EBECF0;
margin-bottom: 20px;
padding-left:10px;
box-sizing: border-box;
}
.ghost-dark-background {
display: inline-block;
height: 100px;
line-height: 100px;
width: 50%;
background: #333;
margin-bottom: 20px;
padding-left:10px;
box-sizing: border-box;
}
可以将多个 Button
放入 Button.Group
的容器中形成一个按钮组。
查看源码在线预览
import { Button, Icon } from "@icedesign/base";
const ButtonGroup = Button.Group;
ReactDOM.render(
<div>
<h4>基本组合</h4>
<ButtonGroup>
<Button type="primary">确定</Button>
<Button type="primary">取消</Button>
</ButtonGroup>
<br />
<br />
<ButtonGroup>
<Button disabled>左</Button>
<Button disabled>中</Button>
<Button disabled>右</Button>
</ButtonGroup>
<br />
<br />
<ButtonGroup>
<Button type="secondary">左</Button>
<Button type="secondary">中</Button>
<Button type="secondary">右</Button>
</ButtonGroup>
<h4>带图标按钮组合</h4>
<ButtonGroup>
<Button type="primary">
<Icon type="arrow-left" />
<span>后退</span>
</Button>
<Button type="primary">
前进<Icon type="arrow-right" />
</Button>
</ButtonGroup>
<br />
<br />
<ButtonGroup>
<Button type="primary">
<Icon type="arrow-left" />
</Button>
<Button type="primary">
<Icon type="arrow-right" />
</Button>
</ButtonGroup>
<h4>多个组合</h4>
<ButtonGroup>
<Button>1</Button>
<Button>2</Button>
<Button>3</Button>
<Button>4</Button>
<Button>
<span>前进</span>
<Icon type="arrow-right" />
</Button>
</ButtonGroup>
</div>,
mountNode
);
Button 可以嵌入 Icon,此时无需自定义 Icon 的大小,Icon 会跟随 Button 组件的大小。
查看源码在线预览
import { Button, Icon } from "@icedesign/base";
ReactDOM.render(
<div>
<Button>
<Icon type="atm" />按钮
</Button>{" "}
<Button shape="text">
<Icon type="atm" />文字按钮
</Button>{" "}
<Button shape="warning">
<Icon type="atm" />文字按钮
</Button>
</div>,
mountNode
);
添加 loading
属性即可让按钮处于加载状态,最后两个按钮演示点击后进入加载状态。
查看源码在线预览
import { Button } from "@icedesign/base";
class Demo extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
loading: false,
iconLoading: false
};
}
enterLoading() {
this.setState({ loading: true });
}
enterIconLoading() {
this.setState({ iconLoading: true });
}
render() {
return (
<div>
<Button type="secondary" loading>
加载中
</Button>
<Button
type="primary"
loading={this.state.loading}
onClick={this.enterLoading.bind(this)}
>
点击变加载
</Button>
</div>
);
}
}
ReactDOM.render(<Demo />, mountNode);
对于按钮组而言,可以通过在 Button.Group
上统一设置整个组按钮的大小。通过设置 size
为 large
medium
small
分别把按钮设为大、中、小尺寸。若不设置 size
,则尺寸为中。按钮有大、中、小三种尺寸。
查看源码在线预览
import { Button, Icon } from "@icedesign/base";
const ButtonGroup = Button.Group;
ReactDOM.render(
<div>
<Button type="primary" size="large">
<Icon type="atm" />大号按钮
</Button>
<Button type="primary">
<Icon type="atm" />中号按钮(默认)
</Button>
<Button type="primary" size="small">
<Icon type="atm" />小号按钮
</Button>
<br />
<br />
<ButtonGroup size="large">
<Button>大</Button>
<Button>大</Button>
<Button>大</Button>
</ButtonGroup>
<ButtonGroup>
<Button>默认</Button>
<Button>默认</Button>
<Button>默认</Button>
</ButtonGroup>
<ButtonGroup size="small">
<Button>小</Button>
<Button>小</Button>
<Button>小</Button>
</ButtonGroup>
</div>,
mountNode
);
SplitButton
查看源码在线预览
import { Button, Menu } from "@icedesign/base";
const SplitButton = Button.Split;
const menu = (
<Menu>
<Menu.Item disabled>abc</Menu.Item>
<Menu.Item key="abc">abc</Menu.Item>
<Menu.Divider key="5" />
<Menu.Item key="1">abc</Menu.Item>
<Menu.Item key="2">abc</Menu.Item>
</Menu>
);
ReactDOM.render(
<div>
<h3>普通按钮</h3>
<SplitButton menu={menu} type="primary" onClick={e => console.log(e)}>
Upload
</SplitButton>
<SplitButton menu={menu} type="secondary">
Upload
</SplitButton>
<SplitButton menu={menu} type="normal" disabled>
Upload
</SplitButton>
<h3>文字按钮</h3>
<SplitButton menu={menu} type="primary" shape="text">
Upload
</SplitButton>
<SplitButton menu={menu} type="secondary" shape="text">
Upload
</SplitButton>
<SplitButton menu={menu} type="normal" shape="text" disabled>
Upload
</SplitButton>
<h3>幽灵按钮</h3>
<div>
<div className="ghost-light-background">
<SplitButton type="light" shape="ghost" menu={menu}>
Ghost Light
</SplitButton>
</div>
<div className="ghost-dark-background">
<SplitButton type="dark" shape="ghost" menu={menu}>
Ghost Dark
</SplitButton>
</div>
</div>
</div>,
mountNode
);
.ghost-light-background {
display: inline-block;
height: 100px;
line-height: 100px;
width: 50%;
background: #EBECF0;
margin-bottom: 20px;
padding-left:10px;
box-sizing: border-box;
}
.ghost-dark-background {
display: inline-block;
height: 100px;
line-height: 100px;
width: 50%;
background: #333;
margin-bottom: 20px;
padding-left:10px;
box-sizing: border-box;
}