Step 步骤
如果项目中使用的是 0.x 版本的基础组件(@icedesign/base, @ali/ice, @alife/next),请在左侧导航顶部切换组件版本。
安装方法
- 在命令行中执行以下命令
npm install @alifd/next@latest -S
API
Step
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
current | 当前步骤 | Number | 0 |
shape | 类型可选值:'circle', 'arrow', 'dot' | Enum | 'circle' |
direction | 展示方向可选值:'hoz', 'ver' | Enum | 'hoz' |
labelPlacement | 横向布局时的内容排列可选值:'hoz', 'ver' | Enum | 'ver' |
readOnly | 是否只读模式 | Boolean | - |
animation | 是否开启动效 | Boolean | true |
itemRender | StepItem 的自定义渲染签名:Function(index: Number, status: String) => Node参数:index: {Number} 节点索引status: {String} 节点状态返回值:{Node} 节点的渲染结果 | Function | null |
Step.Item
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
status | 步骤的状态,如不传,会根据外层的 Step 的 current 属性生成,可选值为 wait , process , finish 可选值:'wait', 'process', 'finish' | Enum | - |
title | 标题 | ReactNode | - |
icon | 图标 | String | - |
content | 内容,用于垂直状态下的内容填充 | ReactNode | - |
itemRender | StepItem 的自定义渲染, 会覆盖父节点设置的itemRender签名:Function(index: Number, status: String) => Node参数:index: {Number} 节点索引status: {String} 节点状态返回值:{Node} 节点的渲染结果 | Function | - |
percent | 百分比 | Number | - |
disabled | 是否禁用 | Boolean | - |
onClick | 点击步骤时的回调签名:Function(index: Number) => void参数:index: {Number} 节点索引 | Function | () => {} |
代码示例
在最简单的情况下,Step 有三种类型,可以通过 shape
属性进行切换。
circle
类型可通过labelPlacement
设置文本排列方向。
查看源码在线预览
import { Step } from '@alifd/next';
const steps = [['Step 1', 'Open the refrigerator door'], ['Step 2', 'Put the elephant in the refrigerator'], ['Step 3', 'Close the refrigerator door']].map((item, index) => <Step.Item aria-current={index === 1 ? 'step' : null} key={index} title={item[0]} content={item[1]}/>);
ReactDOM.render(<div>
<h3>Arrow</h3>
<Step current={1} shape="arrow">
{steps}
</Step>
<h3>Circle</h3>
<Step current={1} shape="circle">
{steps}
</Step>
<h3>Circle(Horizontal content)</h3>
<Step current={1} shape="circle" labelPlacement="hoz">
{steps}
</Step>
<h3>Dot</h3>
<Step current={1} shape="dot">
{steps}
</Step>
</div>, mountNode);
对于点型和圆圈型的 Step 组件而言,可以通过设置 direction
属性设置其展示方向为垂直。箭头形不支持垂直模式。
查看源码在线预览
import { Step } from '@alifd/next';
ReactDOM.render(<div>
<Step current={1} direction="ver" animation={false}>
<Step.Item title="Step 1" content="Open the refrigerator door" />
<Step.Item title="Step 2" content="Put the elephant in the refrigerator" />
<Step.Item title="Step 3" content="Close the refrigerator door" />
</Step>
<br /><br />
<Step current={1} direction="ver" shape="dot" animation={false}>
<Step.Item title="Step 1" content="Open the refrigerator door" />
<Step.Item title="Step 2" content="Put the elephant in the refrigerator" />
<Step.Item title="Step 3" content="Close the refrigerator door" />
</Step>
</div>, mountNode);
可以在点型和圆形步骤条中使用图标,圆形步骤条还支持使用百分比。
查看源码在线预览
import { Step } from '@alifd/next';
ReactDOM.render(<div>
<Step current={1} animation={false} shape="dot">
<Step.Item title="Step 1" content="Open the refrigerator door" icon="calendar" />
<Step.Item title="Step 2" content="Put the elephant in the refrigerator" percent={40}/>
<Step.Item title="Step 3" content="Close the refrigerator door" icon="smile" />
</Step>
<br />
<br />
<Step current={1} animation={false}>
<Step.Item title="Step 1" content="Open the refrigerator door" icon="calendar" />
<Step.Item title="Step 2" content="Put the elephant in the refrigerator" percent={40}/>
<Step.Item title="Step 3" content="Close the refrigerator door" icon="smile" />
</Step>
</div>, mountNode);
可以通过在 Step.Item
上设置 disabled
属性来禁用某个步骤。
查看源码在线预览
import { Step } from '@alifd/next';
ReactDOM.render(<div>
<Step current={1} shape="arrow">
<Step.Item title="Step 1" />
<Step.Item title="Step 2" />
<Step.Item title="Step 3" disabled />
<Step.Item title="Step 4" />
</Step>
<br />
<br />
<Step current={1} shape="dot">
<Step.Item title="Step 1" />
<Step.Item title="Step 2" />
<Step.Item title="Step 3" disabled />
<Step.Item title="Step 4" />
</Step>
<br />
<br />
<Step current={1} shape="circle">
<Step.Item title="Step 1" />
<Step.Item title="Step 2" />
<Step.Item title="Step 3" disabled />
<Step.Item title="Step 4" />
</Step>
</div>, mountNode);
Step.Item
默认有三种状态,分别是 wait
, process
, finish
。用户可以通过传递 itemRender
属性进行自定义的渲染。
查看源码在线预览
import { Step, Icon } from '@alifd/next';
const steps = ['one', 'two', 'three', 'four'];
function itemRender(index) {
return <div className="custom-node1"><span>{index + 1}</span></div>;
}
function itemRender2(index, status) {
return <div className="custom-node2">{status === 'finish' ? <Icon type="success" /> : <span>{index + 1}</span>} </div>;
}
ReactDOM.render(<div className="fusion-demo">
<div className="fusion-demo-item">
<Step current={2} animation={false} itemRender={itemRender}>
{
steps.map(item => <Step.Item key={item} title={item} />)
}
</Step>
</div>
<div className="fusion-demo-item">
<Step current={2} animation={false} itemRender={itemRender2}>
{
steps.map(item => <Step.Item key={item} title={item} />)
}
</Step>
</div>
</div>, mountNode);
.fusion-demo-item {
margin: 15px 0;
}
.custom-node1 {
height: 100%;
width: 100%;
border-radius: 20%;
border: 1px dashed #3E71F1;
text-align: center;
}
.custom-node1 span {
font-size: 12px;
position: absolute;
top: 50%;
text-align: center;
width: 100%;
left: 0;
transform: translateY(-50%);
}
.custom-node2 {
height: 100%;
width: 100%;
border-radius: 20%;
border: 1px dashed #3E71F1;
text-align: center;
background: #3E71F1;
color: #fff;
position: relative;
}
.custom-node2 span, .custom-node2 i {
position: absolute;
top: 50%;
transform: translateY(-50%);
left: 0;
width: 100%;
text-align: center;
}
只读模式,不可触发回调。
查看源码在线预览
import { Step, Button } from '@alifd/next';
const StepItem = Step.Item, ButtonGroup = Button.Group;
class Component extends React.Component {
constructor(props) {
super(props);
this.state = {
currentStep: 3
};
}
next() {
const s = this.state.currentStep + 1;
this.setState({
currentStep: s > 6 ? 6 : s
});
}
prev() {
const s = this.state.currentStep - 1;
this.setState({
currentStep: s < 0 ? 0 : s
});
}
onClick(currentStep) {
console.log(currentStep);
this.setState({
currentStep: currentStep
});
}
render() {
const {currentStep} = this.state;
return (
<div>
<Step current={currentStep} readOnly>
<StepItem title="Step 1" onClick={this.onClick.bind(this)} />
<StepItem title="Step 2" onClick={this.onClick.bind(this)} />
<StepItem title="Step 3" onClick={this.onClick.bind(this)} />
<StepItem title="Step 4" onClick={this.onClick.bind(this)} />
<StepItem title="Step 5" onClick={this.onClick.bind(this)} />
<StepItem title="Step 6" onClick={this.onClick.bind(this)} />
</Step>
<br />
<br />
<ButtonGroup>
<Button onClick={this.prev.bind(this)} type="primary" disabled={currentStep === 0}>Backward</Button>
<Button onClick={this.next.bind(this)} type="primary" disabled={currentStep === 6}>Forward</Button>
</ButtonGroup>
</div>
);
}
}
ReactDOM.render(<Component />, mountNode);
默认情况下,Step 定义为展示型组件,上层组件可以通过修改传入的 current 属性值来修改当前的步骤,同时可以设置每个节点的 click 事件,来自定义回调。
查看源码在线预览
import { Step, Button, Select } from '@alifd/next';
const StepItem = Step.Item, ButtonGroup = Button.Group;
class Component extends React.Component {
constructor(props) {
super(props);
this.state = {
currentStep: 3,
stepType: 'circle',
stepAnimation: true,
labelPlacement: 'ver'
};
this.onClick = this.onClick.bind(this);
}
next() {
const s = this.state.currentStep + 1;
this.setState({
currentStep: s > 6 ? 6 : s
});
}
prev() {
const s = this.state.currentStep - 1;
this.setState({
currentStep: s < 0 ? 0 : s
});
}
onClick(currentStep) {
console.log(currentStep);
this.setState({
currentStep: currentStep
});
}
onStepTypeChange(value) {
this.setState({ stepType: value });
}
onStepAnimation(value) {
this.setState({ stepAnimation: value });
}
onLabelPlacementChange(value) {
this.setState({ labelPlacement: value });
}
render() {
const {currentStep} = this.state;
return (
<div>
<div className="custom-step-option">
<Select placeholder="Choose the dispaly type" onChange={this.onStepTypeChange.bind(this)} className="custom-select" defaultValue="circle">
{
['circle', 'arrow', 'dot'].map(item => <Select.Option value={item} key={item}>{item}</Select.Option>)
}
</Select>
<Select placeholder="Label placement" onChange={this.onLabelPlacementChange.bind(this)} className="custom-select" defaultValue="ver">
{
['hoz', 'ver'].map(item => <Select.Option value={item} key={item}>{item}</Select.Option>)
}
</Select>
<Select placeholder="Enable animation" onChange={this.onStepAnimation.bind(this)} className="custom-select" defaultValue>
{
[true, false].map((item, index) => <Select.Option value={item} key={index}>{item ? 'animation on' : 'animation off'}</Select.Option>)
}
</Select>
</div>
<Step current={currentStep} shape={this.state.stepType} animation={this.state.stepAnimation} labelPlacement={this.state.labelPlacement}>
<StepItem title="Step 1" onClick={this.onClick} content="Description" />
<StepItem title="Step 2" onClick={this.onClick} content="Description" />
<StepItem title="Step 3" onClick={this.onClick} content="Description" />
<StepItem title="Step 4" onClick={this.onClick} content="Description" />
<StepItem title="Step 5" onClick={this.onClick} content="Description" />
<StepItem title="Step 6" onClick={this.onClick} content="Description" />
</Step>
<br />
<br />
<ButtonGroup>
<Button onClick={this.prev.bind(this)} type="primary" disabled={currentStep === 0}>Backward</Button>
<Button onClick={this.next.bind(this)} type="primary" disabled={currentStep === 6}>Forward</Button>
</ButtonGroup>
</div>
);
}
}
ReactDOM.render(<Component />, mountNode);
.custom-step-option {
margin-bottom: 20px;
}
.custom-select {
margin-right: 20px;
}