index.tsx 1.07 KB
Newer Older
偏右's avatar
偏右 committed
1
import React, { PureComponent, createElement } from 'react';
afc163's avatar
afc163 committed
2
import { Button } from 'antd';
3 4
import styles from './index.less';

愚道's avatar
愚道 committed
5 6 7
export interface EditableLink {
  title: string;
  href: string;
陈帅's avatar
陈帅 committed
8
  id?: string;
愚道's avatar
愚道 committed
9 10 11 12 13
}

interface EditableLinkGroupProps {
  onAdd: () => void;
  links: EditableLink[];
陈帅's avatar
陈帅 committed
14
  linkElement: React.ComponentClass<any>;
愚道's avatar
愚道 committed
15
}
16

愚道's avatar
愚道 committed
17
class EditableLinkGroup extends PureComponent<EditableLinkGroupProps> {
jim's avatar
jim committed
18 19 20 21 22
  static defaultProps = {
    links: [],
    onAdd: () => {},
    linkElement: 'a',
  };
陈帅's avatar
陈帅 committed
23

24
  render() {
偏右's avatar
偏右 committed
25
    const { links, linkElement, onAdd } = this.props;
26 27
    return (
      <div className={styles.linkGroup}>
jim's avatar
jim committed
28 29 30 31
        {links.map(link =>
          createElement(
            linkElement,
            {
偏右's avatar
偏右 committed
32 33 34
              key: `linkGroup-item-${link.id || link.title}`,
              to: link.href,
              href: link.href,
jim's avatar
jim committed
35 36 37 38
            },
            link.title
          )
        )}
39
        {
afc163's avatar
afc163 committed
40 41
          <Button size="small" type="primary" ghost onClick={onAdd} icon="plus">
            添加
42 43 44 45 46 47 48 49
          </Button>
        }
      </div>
    );
  }
}

export default EditableLinkGroup;