index.tsx 1.25 KB
Newer Older
偏右's avatar
偏右 committed
1
import React, { PureComponent, createElement } from 'react';
愚道's avatar
愚道 committed
2
import PropTypes, { string } from 'prop-types';
afc163's avatar
afc163 committed
3
import { Button } from 'antd';
4 5
import styles from './index.less';

愚道's avatar
愚道 committed
6 7 8 9 10 11 12 13 14 15
export interface EditableLink {
  title: string;
  href: string;
}

interface EditableLinkGroupProps {
  onAdd: () => void;
  links: EditableLink[];
  linkElement: React.Component;
}
16

愚道's avatar
愚道 committed
17
class EditableLinkGroup extends PureComponent<EditableLinkGroupProps> {
偏右's avatar
偏右 committed
18 19 20 21 22
  static propTypes = {
    links: PropTypes.array,
    onAdd: PropTypes.func,
    linkElement: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
  };
23

jim's avatar
jim committed
24 25 26 27 28
  static defaultProps = {
    links: [],
    onAdd: () => {},
    linkElement: 'a',
  };
陈帅's avatar
陈帅 committed
29

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

export default EditableLinkGroup;