Accordion 手风琴

如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。

安装方法

  1. 在命令行中执行以下命令npm install @icedesign/base@latest -S

开发指南

何时使用

可以折叠/展开的内容区域。

API

手风琴

参数说明类型默认值
prefix样式前缀String'next-'
style组件接受行内样式Object-
dataSource使用数据模型构建Array-
singleShrink如果这个属性为true, 在single为true时, 组件可以收起全部子元素Boolean-
single是否只能展开1个Booleanfalse
onChange接收一个回调函数传递改变状态, 在使用dataSource时改回调需要产生改变组件才能生效签名:Function() => voidFunction-
className扩展classString-

Accordion.Panel

参数说明类型默认值
prefix样式类名的品牌前缀String-
style子组件接受行内样式Object-
expanded是否展开Booleanfalse
disabled是否禁止用户操作Boolean-
title标题ReactNode''
multiTitle标题是否多行显示Booleanfalse
className扩展classString-

代码示例

基本

Accordion接受子组件Panel展示信息。Panel可设置:title(标题)、disable(是否可操作)等属性,其中title、content值可为字符串或Component组件,expand、disable接受布尔值。

Accordion 手风琴 - 图1

查看源码在线预览

  1. import { Accordion } from "@icedesign/base";
  2. const { Panel } = Accordion;
  3. ReactDOM.render(
  4. <Accordion>
  5. <Panel
  6. multiTitle
  7. 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."
  8. >
  9. <ul>
  10. <li>Promotions are marketing campaigns ran by Marketplace</li>
  11. <li>
  12. Participate to sale your products during that promotion and make a
  13. profit
  14. </li>
  15. </ul>
  16. </Panel>
  17. <Panel title="What are Promotion Products?">
  18. <ul>
  19. <li>
  20. Promotion Products is a service that helps you to promote products you
  21. list on Marketplace during a certain time range
  22. </li>
  23. <li>
  24. You can choose which products should be available for the promotion
  25. </li>
  26. <li>
  27. Not all Products of you will be available, because Promotions will
  28. only attract certain Product areas
  29. </li>
  30. </ul>
  31. </Panel>
  32. <Panel title="Why can i not submit a higher price?">
  33. <ul>
  34. <li>
  35. The Promotion requires a certain price to make sure that our customers
  36. are attracted
  37. </li>
  38. </ul>
  39. </Panel>
  40. <Panel title="What is Promo Stock?">
  41. Promo Stock is the criteria needed to be followed to be able to join
  42. Promotion. With setting particular Promo Stock value you commit to have
  43. this amount of stock available while Promotion is active.
  44. </Panel>
  45. </Accordion>,
  46. mountNode
  47. );

外部综合操作

Accordion的onChange属性接受一个事件, 事件返回点击后的状态, 外部接收这个值传递给dataSource让Accordion发生改变dataSource接受一个数组, 数组元素为一个对象, 包含属性:title(标题)、content(内容)、disable(是否可操作)等属性,其中title、content值可为字符串或Component组件,expanded、disable接受布尔值。Accordion的dataSource属性接受一个状态数组与外部state对应。

Accordion 手风琴 - 图2

查看源码在线预览

  1. import { Accordion, Button } from "@icedesign/base";
  2. const list = [
  3. {
  4. title: "Well, hello there",
  5. content:
  6. "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."
  7. },
  8. {
  9. title: "Gigantomaniac Monster Text, very long, much width, wow",
  10. content:
  11. "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."
  12. },
  13. {
  14. title: "Generic Title",
  15. content:
  16. "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."
  17. },
  18. {
  19. title: "Login Infomation",
  20. content:
  21. "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."
  22. }
  23. ];
  24. class Demo extends React.Component {
  25. constructor(props, context) {
  26. super(props, context);
  27. this.state = {
  28. list: list,
  29. single: false
  30. };
  31. }
  32. onChange(status, list) {
  33. console.log(status);
  34. //重新拼装list, 使改变生效
  35. this.setState({
  36. list
  37. });
  38. }
  39. showAll() {
  40. this.setState({
  41. list: this.state.list.map(e => {
  42. return Object.assign({}, e, { expanded: true });
  43. })
  44. });
  45. }
  46. closeAll() {
  47. this.setState({
  48. list: this.state.list.map(e => {
  49. return Object.assign({}, e, { expanded: false });
  50. })
  51. });
  52. }
  53. setSingle() {
  54. this.setState({
  55. single: true
  56. });
  57. }
  58. cancelSingle() {
  59. this.setState({
  60. single: false
  61. });
  62. }
  63. render() {
  64. return (
  65. <div className="demo">
  66. <p>
  67. 外部控制:
  68. <Button onClick={this.showAll.bind(this)}>展开全部</Button>
  69. <Button onClick={this.closeAll.bind(this)}>收起全部</Button>
  70. <Button onClick={this.setSingle.bind(this)}>单例模式</Button>
  71. <Button onClick={this.cancelSingle.bind(this)}>多例模式</Button>
  72. </p>
  73. <br />
  74. <Accordion
  75. single={this.state.single}
  76. onChange={this.onChange.bind(this)}
  77. dataSource={this.state.list}
  78. />
  79. </div>
  80. );
  81. }
  82. }
  83. ReactDOM.render(<Demo />, mountNode);
  1. .demo button {
  2. margin-right: 5px;
  3. }

disabled 禁止操作

若dataSource属性内对disable为true, 鼠标点击将无效。

Accordion 手风琴 - 图3

查看源码在线预览

  1. import { Accordion } from "@icedesign/base";
  2. const list = [
  3. {
  4. title: "Well, hello there",
  5. content:
  6. "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.",
  7. disabled: true
  8. },
  9. {
  10. title: "Gigantomaniac Monster Text, very long, much width, wow",
  11. content:
  12. "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.",
  13. disabled: true
  14. },
  15. {
  16. title: "Generic Title",
  17. content:
  18. "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.",
  19. disabled: true
  20. },
  21. {
  22. title: "Login Infomation",
  23. content:
  24. "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.",
  25. disabled: true
  26. }
  27. ];
  28. class Demo extends React.Component {
  29. constructor(props, context) {
  30. super(props, context);
  31. this.state = {
  32. list: list
  33. };
  34. }
  35. onChange(status) {
  36. //点击后的status : [true, false, false, false]
  37. //重新拼装list, 使改变生效
  38. this.setState({
  39. list: this.state.list.map((e, k) => {
  40. return Object.assign({}, e, { expanded: status[k] });
  41. })
  42. });
  43. }
  44. render() {
  45. return (
  46. <Accordion
  47. onChange={this.onChange.bind(this)}
  48. dataSource={this.state.list}
  49. />
  50. );
  51. }
  52. }
  53. ReactDOM.render(<Demo />, mountNode);

单例模式

Accordion的single属性接受一个布尔值, 若为true, 最多只能有一个组件展开。

Accordion 手风琴 - 图4

查看源码在线预览

  1. import { Accordion } from "@icedesign/base";
  2. const list = [
  3. {
  4. title: "Well, hello there",
  5. content:
  6. "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."
  7. },
  8. {
  9. title: "Gigantomaniac Monster Text, very long, much width, wow",
  10. content:
  11. "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."
  12. },
  13. {
  14. title: "Generic Title",
  15. content:
  16. "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."
  17. },
  18. {
  19. title: "Login Infomation",
  20. content:
  21. "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."
  22. }
  23. ];
  24. class Demo extends React.Component {
  25. constructor(props, context) {
  26. super(props, context);
  27. this.state = {
  28. list: list
  29. };
  30. }
  31. onChange(status) {
  32. //点击后的status : [true, false, false, false]
  33. //重新拼装list, 使改变生效
  34. this.setState({
  35. list: this.state.list.map((e, k) => {
  36. return Object.assign({}, e, { expanded: status[k] });
  37. })
  38. });
  39. }
  40. render() {
  41. return (
  42. <Accordion
  43. single
  44. onChange={this.onChange.bind(this)}
  45. dataSource={this.state.list}
  46. />
  47. );
  48. }
  49. }
  50. ReactDOM.render(<Demo />, mountNode);

相关区块

Accordion 手风琴 - 图5

暂无相关区块