Accordion 手风琴
如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。
安装方法
- 在命令行中执行以下命令
npm install @icedesign/base@latest -S
开发指南
何时使用
可以折叠/展开的内容区域。
API
手风琴
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefix | 样式前缀 | String | 'next-' |
style | 组件接受行内样式 | Object | - |
dataSource | 使用数据模型构建 | Array | - |
singleShrink | 如果这个属性为true, 在single为true时, 组件可以收起全部子元素 | Boolean | - |
single | 是否只能展开1个 | Boolean | false |
onChange | 接收一个回调函数传递改变状态, 在使用dataSource时改回调需要产生改变组件才能生效签名:Function() => void | Function | - |
className | 扩展class | String | - |
Accordion.Panel
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
prefix | 样式类名的品牌前缀 | String | - |
style | 子组件接受行内样式 | Object | - |
expanded | 是否展开 | Boolean | false |
disabled | 是否禁止用户操作 | Boolean | - |
title | 标题 | ReactNode | '' |
multiTitle | 标题是否多行显示 | Boolean | false |
className | 扩展class | String | - |
代码示例
Accordion接受子组件Panel展示信息。Panel可设置:title(标题)、disable(是否可操作)等属性,其中title、content值可为字符串或Component组件,expand、disable接受布尔值。
查看源码在线预览
import { Accordion } from "@icedesign/base";
const { Panel } = Accordion;
ReactDOM.render(
<Accordion>
<Panel
multiTitle
title="There is a long title, you can set the multiTitle to multi line display, the associated configuration properties and a single height is not the same, the specific configuration platform configuration can be configured."
>
<ul>
<li>Promotions are marketing campaigns ran by Marketplace</li>
<li>
Participate to sale your products during that promotion and make a
profit
</li>
</ul>
</Panel>
<Panel title="What are Promotion Products?">
<ul>
<li>
Promotion Products is a service that helps you to promote products you
list on Marketplace during a certain time range
</li>
<li>
You can choose which products should be available for the promotion
</li>
<li>
Not all Products of you will be available, because Promotions will
only attract certain Product areas
</li>
</ul>
</Panel>
<Panel title="Why can i not submit a higher price?">
<ul>
<li>
The Promotion requires a certain price to make sure that our customers
are attracted
</li>
</ul>
</Panel>
<Panel title="What is Promo Stock?">
Promo Stock is the criteria needed to be followed to be able to join
Promotion. With setting particular Promo Stock value you commit to have
this amount of stock available while Promotion is active.
</Panel>
</Accordion>,
mountNode
);
Accordion的onChange属性接受一个事件, 事件返回点击后的状态, 外部接收这个值传递给dataSource让Accordion发生改变dataSource接受一个数组, 数组元素为一个对象, 包含属性:title(标题)、content(内容)、disable(是否可操作)等属性,其中title、content值可为字符串或Component组件,expanded、disable接受布尔值。Accordion的dataSource属性接受一个状态数组与外部state对应。
查看源码在线预览
import { Accordion, Button } from "@icedesign/base";
const list = [
{
title: "Well, hello there",
content:
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
},
{
title: "Gigantomaniac Monster Text, very long, much width, wow",
content:
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
},
{
title: "Generic Title",
content:
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
},
{
title: "Login Infomation",
content:
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
}
];
class Demo extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
list: list,
single: false
};
}
onChange(status, list) {
console.log(status);
//重新拼装list, 使改变生效
this.setState({
list
});
}
showAll() {
this.setState({
list: this.state.list.map(e => {
return Object.assign({}, e, { expanded: true });
})
});
}
closeAll() {
this.setState({
list: this.state.list.map(e => {
return Object.assign({}, e, { expanded: false });
})
});
}
setSingle() {
this.setState({
single: true
});
}
cancelSingle() {
this.setState({
single: false
});
}
render() {
return (
<div className="demo">
<p>
外部控制:
<Button onClick={this.showAll.bind(this)}>展开全部</Button>
<Button onClick={this.closeAll.bind(this)}>收起全部</Button>
<Button onClick={this.setSingle.bind(this)}>单例模式</Button>
<Button onClick={this.cancelSingle.bind(this)}>多例模式</Button>
</p>
<br />
<Accordion
single={this.state.single}
onChange={this.onChange.bind(this)}
dataSource={this.state.list}
/>
</div>
);
}
}
ReactDOM.render(<Demo />, mountNode);
.demo button {
margin-right: 5px;
}
若dataSource属性内对disable为true, 鼠标点击将无效。
查看源码在线预览
import { Accordion } from "@icedesign/base";
const list = [
{
title: "Well, hello there",
content:
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.",
disabled: true
},
{
title: "Gigantomaniac Monster Text, very long, much width, wow",
content:
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.",
disabled: true
},
{
title: "Generic Title",
content:
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.",
disabled: true
},
{
title: "Login Infomation",
content:
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.",
disabled: true
}
];
class Demo extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
list: list
};
}
onChange(status) {
//点击后的status : [true, false, false, false]
//重新拼装list, 使改变生效
this.setState({
list: this.state.list.map((e, k) => {
return Object.assign({}, e, { expanded: status[k] });
})
});
}
render() {
return (
<Accordion
onChange={this.onChange.bind(this)}
dataSource={this.state.list}
/>
);
}
}
ReactDOM.render(<Demo />, mountNode);
Accordion的single属性接受一个布尔值, 若为true, 最多只能有一个组件展开。
查看源码在线预览
import { Accordion } from "@icedesign/base";
const list = [
{
title: "Well, hello there",
content:
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
},
{
title: "Gigantomaniac Monster Text, very long, much width, wow",
content:
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
},
{
title: "Generic Title",
content:
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
},
{
title: "Login Infomation",
content:
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
}
];
class Demo extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
list: list
};
}
onChange(status) {
//点击后的status : [true, false, false, false]
//重新拼装list, 使改变生效
this.setState({
list: this.state.list.map((e, k) => {
return Object.assign({}, e, { expanded: status[k] });
})
});
}
render() {
return (
<Accordion
single
onChange={this.onChange.bind(this)}
dataSource={this.state.list}
/>
);
}
}
ReactDOM.render(<Demo />, mountNode);